PageGroup.cpp 11.2 KB
Newer Older
darin@apple.com's avatar
darin@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "PageGroup.h"

29
#include "Chrome.h"
darin@apple.com's avatar
darin@apple.com committed
30
#include "ChromeClient.h"
31
#include "DOMWrapperWorld.h"
darin@apple.com's avatar
darin@apple.com committed
32
#include "Document.h"
33
#include "DocumentStyleSheetCollection.h"
34
#include "GroupSettings.h"
darin@apple.com's avatar
darin@apple.com committed
35
#include "MainFrame.h"
darin@apple.com's avatar
darin@apple.com committed
36
#include "Page.h"
37
#include "PageCache.h"
38
#include "SecurityOrigin.h"
39
#include "Settings.h"
40
#include "StorageNamespace.h"
41
#include "UserContentController.h"
42
#include <wtf/StdLibExtras.h>
darin@apple.com's avatar
darin@apple.com committed
43

44
#if ENABLE(VIDEO_TRACK)
45
#if (PLATFORM(MAC) && !PLATFORM(IOS)) || HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
46
#include "CaptionUserPreferencesMediaAF.h"
47 48 49 50 51
#else
#include "CaptionUserPreferences.h"
#endif
#endif

darin@apple.com's avatar
darin@apple.com committed
52 53
namespace WebCore {

kmccullough@apple.com's avatar
kmccullough@apple.com committed
54 55 56 57 58 59
static unsigned getUniqueIdentifier()
{
    static unsigned currentIdentifier = 0;
    return ++currentIdentifier;
}

darin@apple.com's avatar
darin@apple.com committed
60 61
// --------

62
static bool shouldTrackVisitedLinks = false;
darin@apple.com's avatar
darin@apple.com committed
63

64 65 66 67
PageGroup::PageGroup(const String& name)
    : m_name(name)
    , m_visitedLinksPopulated(false)
    , m_identifier(getUniqueIdentifier())
68
    , m_userContentController(UserContentController::create())
69
    , m_groupSettings(std::make_unique<GroupSettings>())
70 71 72
{
}

73
PageGroup::PageGroup(Page& page)
darin@apple.com's avatar
darin@apple.com committed
74
    : m_visitedLinksPopulated(false)
kmccullough@apple.com's avatar
kmccullough@apple.com committed
75
    , m_identifier(getUniqueIdentifier())
76
    , m_userContentController(UserContentController::create())
77
    , m_groupSettings(std::make_unique<GroupSettings>())
darin@apple.com's avatar
darin@apple.com committed
78
{
79
    addPage(page);
darin@apple.com's avatar
darin@apple.com committed
80 81
}

82 83 84 85 86
PageGroup::~PageGroup()
{
    removeAllUserContent();
}

87 88 89 90 91 92 93 94 95 96
typedef HashMap<String, PageGroup*> PageGroupMap;
static PageGroupMap* pageGroups = 0;

PageGroup* PageGroup::pageGroup(const String& groupName)
{
    ASSERT(!groupName.isEmpty());
    
    if (!pageGroups)
        pageGroups = new PageGroupMap;

97
    PageGroupMap::AddResult result = pageGroups->add(groupName, nullptr);
98

99
    if (result.isNewEntry) {
100 101
        ASSERT(!result.iterator->value);
        result.iterator->value = new PageGroup(groupName);
102 103
    }

104 105
    ASSERT(result.iterator->value);
    return result.iterator->value;
106 107
}

beidson@apple.com's avatar
beidson@apple.com committed
108 109 110 111 112
void PageGroup::closeLocalStorage()
{
    if (!pageGroups)
        return;

113
    for (auto it = pageGroups->begin(), end = pageGroups->end(); it != end; ++it) {
114 115
        if (it->value->hasLocalStorage())
            it->value->localStorage()->close();
beidson@apple.com's avatar
beidson@apple.com committed
116 117 118
    }
}

119 120 121 122 123
void PageGroup::clearLocalStorageForAllOrigins()
{
    if (!pageGroups)
        return;

124
    for (auto it = pageGroups->begin(), end = pageGroups->end(); it != end; ++it) {
125 126
        if (it->value->hasLocalStorage())
            it->value->localStorage()->clearAllOriginsForDeletion();
127 128 129 130 131 132 133 134
    }
}

void PageGroup::clearLocalStorageForOrigin(SecurityOrigin* origin)
{
    if (!pageGroups)
        return;

135
    for (auto it = pageGroups->begin(), end = pageGroups->end(); it != end; ++it) {
136 137
        if (it->value->hasLocalStorage())
            it->value->localStorage()->clearOriginForDeletion(origin);
138
    }
139
}
140 141 142 143 144 145

void PageGroup::closeIdleLocalStorageDatabases()
{
    if (!pageGroups)
        return;

146
    for (auto it = pageGroups->begin(), end = pageGroups->end(); it != end; ++it) {
147 148 149 150 151
        if (it->value->hasLocalStorage())
            it->value->localStorage()->closeIdleLocalStorageDatabases();
    }
}

152 153 154 155 156
void PageGroup::syncLocalStorage()
{
    if (!pageGroups)
        return;

157
    for (auto it = pageGroups->begin(), end = pageGroups->end(); it != end; ++it) {
158 159
        if (it->value->hasLocalStorage())
            it->value->localStorage()->sync();
160 161 162 163 164 165 166 167 168 169 170
    }
}

unsigned PageGroup::numberOfPageGroups()
{
    if (!pageGroups)
        return 0;

    return pageGroups->size();
}

171
void PageGroup::addPage(Page& page)
darin@apple.com's avatar
darin@apple.com committed
172
{
173 174
    ASSERT(!m_pages.contains(&page));
    m_pages.add(&page);
175 176

    page.setUserContentController(m_userContentController.get());
darin@apple.com's avatar
darin@apple.com committed
177 178
}

179
void PageGroup::removePage(Page& page)
darin@apple.com's avatar
darin@apple.com committed
180
{
181 182
    ASSERT(m_pages.contains(&page));
    m_pages.remove(&page);
183 184

    page.setUserContentController(nullptr);
darin@apple.com's avatar
darin@apple.com committed
185 186
}

brettw@chromium.org's avatar
brettw@chromium.org committed
187
bool PageGroup::isLinkVisited(LinkHash visitedLinkHash)
darin@apple.com's avatar
darin@apple.com committed
188 189 190 191
{
    if (!m_visitedLinksPopulated) {
        m_visitedLinksPopulated = true;
        ASSERT(!m_pages.isEmpty());
192
        (*m_pages.begin())->chrome().client().populateVisitedLinks();
darin@apple.com's avatar
darin@apple.com committed
193
    }
darin@apple.com's avatar
darin@apple.com committed
194 195
    return m_visitedLinkHashes.contains(visitedLinkHash);
}
darin@apple.com's avatar
darin@apple.com committed
196

197 198
void PageGroup::addVisitedLinkHash(LinkHash hash)
{
199 200
    if (shouldTrackVisitedLinks)
        addVisitedLink(hash);
201 202
}

brettw@chromium.org's avatar
brettw@chromium.org committed
203
inline void PageGroup::addVisitedLink(LinkHash hash)
darin@apple.com's avatar
darin@apple.com committed
204 205
{
    ASSERT(shouldTrackVisitedLinks);
206
    if (!m_visitedLinkHashes.add(hash).isNewEntry)
darin@apple.com's avatar
darin@apple.com committed
207
        return;
brettw@chromium.org's avatar
brettw@chromium.org committed
208
    Page::visitedStateChanged(this, hash);
209
    pageCache()->markPagesForVistedLinkStyleRecalc();
darin@apple.com's avatar
darin@apple.com committed
210 211
}

darin@apple.com's avatar
darin@apple.com committed
212
void PageGroup::addVisitedLink(const URL& url)
darin@apple.com's avatar
darin@apple.com committed
213
{
darin@apple.com's avatar
darin@apple.com committed
214 215
    if (!shouldTrackVisitedLinks)
        return;
darin@apple.com's avatar
darin@apple.com committed
216
    ASSERT(!url.isEmpty());
217
    addVisitedLink(visitedLinkHash(url.string()));
darin@apple.com's avatar
darin@apple.com committed
218 219 220 221
}

void PageGroup::addVisitedLink(const UChar* characters, size_t length)
{
darin@apple.com's avatar
darin@apple.com committed
222 223
    if (!shouldTrackVisitedLinks)
        return;
brettw@chromium.org's avatar
brettw@chromium.org committed
224
    addVisitedLink(visitedLinkHash(characters, length));
darin@apple.com's avatar
darin@apple.com committed
225 226
}

227 228 229 230 231 232 233 234 235 236
void PageGroup::removeVisitedLink(const URL& url)
{
    LinkHash hash = visitedLinkHash(url.string());
    ASSERT(m_visitedLinkHashes.contains(hash));
    m_visitedLinkHashes.remove(hash);

    Page::allVisitedStateChanged(this);
    pageCache()->markPagesForVistedLinkStyleRecalc();
}

darin@apple.com's avatar
darin@apple.com committed
237 238
void PageGroup::removeVisitedLinks()
{
darin@apple.com's avatar
darin@apple.com committed
239
    m_visitedLinksPopulated = false;
darin@apple.com's avatar
darin@apple.com committed
240 241 242 243
    if (m_visitedLinkHashes.isEmpty())
        return;
    m_visitedLinkHashes.clear();
    Page::allVisitedStateChanged(this);
244
    pageCache()->markPagesForVistedLinkStyleRecalc();
darin@apple.com's avatar
darin@apple.com committed
245 246 247 248 249
}

void PageGroup::removeAllVisitedLinks()
{
    Page::removeAllVisitedLinks();
250
    pageCache()->markPagesForVistedLinkStyleRecalc();
darin@apple.com's avatar
darin@apple.com committed
251 252
}

darin@apple.com's avatar
darin@apple.com committed
253 254 255 256 257 258 259 260 261
void PageGroup::setShouldTrackVisitedLinks(bool shouldTrack)
{
    if (shouldTrackVisitedLinks == shouldTrack)
        return;
    shouldTrackVisitedLinks = shouldTrack;
    if (!shouldTrackVisitedLinks)
        removeAllVisitedLinks();
}

262
StorageNamespace* PageGroup::localStorage()
263
{
264 265
    if (!m_localStorage)
        m_localStorage = StorageNamespace::localStorageNamespace(this);
266

267
    return m_localStorage.get();
268 269
}

270
StorageNamespace* PageGroup::transientLocalStorage(SecurityOrigin* topOrigin)
271
{
272
    auto result = m_transientLocalStorageMap.add(topOrigin, nullptr);
273

274 275 276 277
    if (result.isNewEntry)
        result.iterator->value = StorageNamespace::transientLocalStorageNamespace(this, topOrigin);

    return result.iterator->value.get();
278
}
279

280
void PageGroup::addUserScriptToWorld(DOMWrapperWorld& world, const String& source, const URL& url, const Vector<String>& whitelist, const Vector<String>& blacklist, UserScriptInjectionTime injectionTime, UserContentInjectedFrames injectedFrames)
281
{
282
    auto userScript = std::make_unique<UserScript>(source, url, whitelist, blacklist, injectionTime, injectedFrames);
283
    m_userContentController->addUserScript(world, std::move(userScript));
284 285
}

286
void PageGroup::addUserStyleSheetToWorld(DOMWrapperWorld& world, const String& source, const URL& url, const Vector<String>& whitelist, const Vector<String>& blacklist, UserContentInjectedFrames injectedFrames, UserStyleLevel level, UserStyleInjectionTime injectionTime)
287
{
288
    auto userStyleSheet = std::make_unique<UserStyleSheet>(source, url, whitelist, blacklist, injectedFrames, level);
289
    if (!m_userStyleSheets)
290
        m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
291
    std::unique_ptr<UserStyleSheetVector>& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
292
    if (!styleSheetsInWorld)
293 294
        styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
    styleSheetsInWorld->append(std::move(userStyleSheet));
295

296
    if (injectionTime == InjectInExistingDocuments)
297
        invalidateInjectedStyleSheetCacheInAllFrames();
298
}
299

300
void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url)
301
{
302
    m_userContentController->removeUserScript(world, url);
303 304
}

305
void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
306 307 308 309
{
    if (!m_userStyleSheets)
        return;

310
    auto it = m_userStyleSheets->find(&world);
311 312 313 314
    bool sheetsChanged = false;
    if (it == m_userStyleSheets->end())
        return;
    
315
    auto stylesheets = it->value.get();
316 317 318 319
    for (int i = stylesheets->size() - 1; i >= 0; --i) {
        if (stylesheets->at(i)->url() == url) {
            stylesheets->remove(i);
            sheetsChanged = true;
320
        }
321
    }
322
        
323 324 325
    if (!sheetsChanged)
        return;

326
    if (stylesheets->isEmpty())
327
        m_userStyleSheets->remove(it);
328

329
    invalidateInjectedStyleSheetCacheInAllFrames();
330 331
}

332
void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
333
{
334
    m_userContentController->removeUserScripts(world);
335 336
}

337
void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
338 339 340
{
    if (!m_userStyleSheets)
        return;
341

342
    if (!m_userStyleSheets->remove(&world))
343 344
        return;

345
    invalidateInjectedStyleSheetCacheInAllFrames();
346 347 348 349
}

void PageGroup::removeAllUserContent()
{
350
    m_userContentController->removeAllUserContent();
351

352
    if (m_userStyleSheets) {
353
        m_userStyleSheets = nullptr;
354
        invalidateInjectedStyleSheetCacheInAllFrames();
355
    }
356 357
}

358
void PageGroup::invalidateInjectedStyleSheetCacheInAllFrames()
359 360
{
    // Clear our cached sheets and have them just reparse.
361
    for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it) {
362
        for (Frame* frame = &(*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
363
            frame->document()->styleSheetCollection().invalidateInjectedStyleSheetCache();
364 365
            frame->document()->styleResolverChanged(DeferRecalcStyle);
        }
366 367 368
    }
}

369
#if ENABLE(VIDEO_TRACK)
370 371
void PageGroup::captionPreferencesChanged()
{
372 373
    for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it)
        (*it)->captionPreferencesChanged();
374 375 376
    pageCache()->markPagesForCaptionPreferencesChanged();
}

377 378
CaptionUserPreferences* PageGroup::captionPreferences()
{
379
    if (!m_captionPreferences) {
380
#if (PLATFORM(MAC) && !PLATFORM(IOS)) || HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
381
        m_captionPreferences = std::make_unique<CaptionUserPreferencesMediaAF>(*this);
382
#else
383
        m_captionPreferences = std::make_unique<CaptionUserPreferences>(*this);
384
#endif
385
    }
386 387 388 389 390

    return m_captionPreferences.get();
}
#endif

darin@apple.com's avatar
darin@apple.com committed
391
} // namespace WebCore