PageGroup.cpp 11.5 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 <wtf/StdLibExtras.h>
darin@apple.com's avatar
darin@apple.com committed
42

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

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

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

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

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

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

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

79 80 81 82 83
PageGroup::~PageGroup()
{
    removeAllUserContent();
}

84 85 86 87 88 89 90 91 92 93
typedef HashMap<String, PageGroup*> PageGroupMap;
static PageGroupMap* pageGroups = 0;

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

94
    PageGroupMap::AddResult result = pageGroups->add(groupName, nullptr);
95

96
    if (result.isNewEntry) {
97 98
        ASSERT(!result.iterator->value);
        result.iterator->value = new PageGroup(groupName);
99 100
    }

101 102
    ASSERT(result.iterator->value);
    return result.iterator->value;
103 104
}

beidson@apple.com's avatar
beidson@apple.com committed
105 106 107 108 109
void PageGroup::closeLocalStorage()
{
    if (!pageGroups)
        return;

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

116 117 118 119 120
void PageGroup::clearLocalStorageForAllOrigins()
{
    if (!pageGroups)
        return;

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

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

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

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

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

149 150 151 152 153
void PageGroup::syncLocalStorage()
{
    if (!pageGroups)
        return;

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

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

    return pageGroups->size();
}

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

174
void PageGroup::removePage(Page& page)
darin@apple.com's avatar
darin@apple.com committed
175
{
176 177
    ASSERT(m_pages.contains(&page));
    m_pages.remove(&page);
darin@apple.com's avatar
darin@apple.com committed
178 179
}

brettw@chromium.org's avatar
brettw@chromium.org committed
180
bool PageGroup::isLinkVisited(LinkHash visitedLinkHash)
darin@apple.com's avatar
darin@apple.com committed
181 182 183 184
{
    if (!m_visitedLinksPopulated) {
        m_visitedLinksPopulated = true;
        ASSERT(!m_pages.isEmpty());
185
        (*m_pages.begin())->chrome().client().populateVisitedLinks();
darin@apple.com's avatar
darin@apple.com committed
186
    }
darin@apple.com's avatar
darin@apple.com committed
187 188
    return m_visitedLinkHashes.contains(visitedLinkHash);
}
darin@apple.com's avatar
darin@apple.com committed
189

190 191
void PageGroup::addVisitedLinkHash(LinkHash hash)
{
192 193
    if (shouldTrackVisitedLinks)
        addVisitedLink(hash);
194 195
}

brettw@chromium.org's avatar
brettw@chromium.org committed
196
inline void PageGroup::addVisitedLink(LinkHash hash)
darin@apple.com's avatar
darin@apple.com committed
197 198
{
    ASSERT(shouldTrackVisitedLinks);
199
    if (!m_visitedLinkHashes.add(hash).isNewEntry)
darin@apple.com's avatar
darin@apple.com committed
200
        return;
brettw@chromium.org's avatar
brettw@chromium.org committed
201
    Page::visitedStateChanged(this, hash);
202
    pageCache()->markPagesForVistedLinkStyleRecalc();
darin@apple.com's avatar
darin@apple.com committed
203 204
}

darin@apple.com's avatar
darin@apple.com committed
205
void PageGroup::addVisitedLink(const URL& url)
darin@apple.com's avatar
darin@apple.com committed
206
{
darin@apple.com's avatar
darin@apple.com committed
207 208
    if (!shouldTrackVisitedLinks)
        return;
darin@apple.com's avatar
darin@apple.com committed
209
    ASSERT(!url.isEmpty());
210
    addVisitedLink(visitedLinkHash(url.string()));
darin@apple.com's avatar
darin@apple.com committed
211 212 213 214
}

void PageGroup::addVisitedLink(const UChar* characters, size_t length)
{
darin@apple.com's avatar
darin@apple.com committed
215 216
    if (!shouldTrackVisitedLinks)
        return;
brettw@chromium.org's avatar
brettw@chromium.org committed
217
    addVisitedLink(visitedLinkHash(characters, length));
darin@apple.com's avatar
darin@apple.com committed
218 219
}

220 221 222 223 224 225 226 227 228 229
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
230 231
void PageGroup::removeVisitedLinks()
{
darin@apple.com's avatar
darin@apple.com committed
232
    m_visitedLinksPopulated = false;
darin@apple.com's avatar
darin@apple.com committed
233 234 235 236
    if (m_visitedLinkHashes.isEmpty())
        return;
    m_visitedLinkHashes.clear();
    Page::allVisitedStateChanged(this);
237
    pageCache()->markPagesForVistedLinkStyleRecalc();
darin@apple.com's avatar
darin@apple.com committed
238 239 240 241 242
}

void PageGroup::removeAllVisitedLinks()
{
    Page::removeAllVisitedLinks();
243
    pageCache()->markPagesForVistedLinkStyleRecalc();
darin@apple.com's avatar
darin@apple.com committed
244 245
}

darin@apple.com's avatar
darin@apple.com committed
246 247 248 249 250 251 252 253 254
void PageGroup::setShouldTrackVisitedLinks(bool shouldTrack)
{
    if (shouldTrackVisitedLinks == shouldTrack)
        return;
    shouldTrackVisitedLinks = shouldTrack;
    if (!shouldTrackVisitedLinks)
        removeAllVisitedLinks();
}

255
StorageNamespace* PageGroup::localStorage()
256
{
257 258
    if (!m_localStorage)
        m_localStorage = StorageNamespace::localStorageNamespace(this);
259

260
    return m_localStorage.get();
261 262
}

263
StorageNamespace* PageGroup::transientLocalStorage(SecurityOrigin* topOrigin)
264
{
265
    auto result = m_transientLocalStorageMap.add(topOrigin, nullptr);
266

267 268 269 270
    if (result.isNewEntry)
        result.iterator->value = StorageNamespace::transientLocalStorageNamespace(this, topOrigin);

    return result.iterator->value.get();
271
}
272

273
void PageGroup::addUserScriptToWorld(DOMWrapperWorld& world, const String& source, const URL& url, const Vector<String>& whitelist, const Vector<String>& blacklist, UserScriptInjectionTime injectionTime, UserContentInjectedFrames injectedFrames)
274
{
275
    auto userScript = std::make_unique<UserScript>(source, url, whitelist, blacklist, injectionTime, injectedFrames);
276
    if (!m_userScripts)
277
        m_userScripts = std::make_unique<UserScriptMap>();
278
    std::unique_ptr<UserScriptVector>& scriptsInWorld = m_userScripts->add(&world, nullptr).iterator->value;
279
    if (!scriptsInWorld)
280 281
        scriptsInWorld = std::make_unique<UserScriptVector>();
    scriptsInWorld->append(std::move(userScript));
282 283
}

284
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)
285
{
286
    auto userStyleSheet = std::make_unique<UserStyleSheet>(source, url, whitelist, blacklist, injectedFrames, level);
287
    if (!m_userStyleSheets)
288
        m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
289
    std::unique_ptr<UserStyleSheetVector>& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
290
    if (!styleSheetsInWorld)
291 292
        styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
    styleSheetsInWorld->append(std::move(userStyleSheet));
293

294
    if (injectionTime == InjectInExistingDocuments)
295
        invalidateInjectedStyleSheetCacheInAllFrames();
296
}
297

298
void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url)
299
{
300 301 302
    if (!m_userScripts)
        return;

303
    auto it = m_userScripts->find(&world);
304 305 306
    if (it == m_userScripts->end())
        return;
    
307
    auto scripts = it->value.get();
308 309 310
    for (int i = scripts->size() - 1; i >= 0; --i) {
        if (scripts->at(i)->url() == url)
            scripts->remove(i);
311 312
    }
    
313 314
    if (scripts->isEmpty())
        m_userScripts->remove(it);
315 316
}

317
void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
318 319 320 321
{
    if (!m_userStyleSheets)
        return;

322
    auto it = m_userStyleSheets->find(&world);
323 324 325 326
    bool sheetsChanged = false;
    if (it == m_userStyleSheets->end())
        return;
    
327
    auto stylesheets = it->value.get();
328 329 330 331
    for (int i = stylesheets->size() - 1; i >= 0; --i) {
        if (stylesheets->at(i)->url() == url) {
            stylesheets->remove(i);
            sheetsChanged = true;
332
        }
333
    }
334
        
335 336 337
    if (!sheetsChanged)
        return;

338
    if (stylesheets->isEmpty())
339
        m_userStyleSheets->remove(it);
340

341
    invalidateInjectedStyleSheetCacheInAllFrames();
342 343
}

344
void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
345
{
346 347 348
    if (!m_userScripts)
        return;

349
    m_userScripts->remove(&world);
350 351
}

352
void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
353 354 355
{
    if (!m_userStyleSheets)
        return;
356

357
    if (!m_userStyleSheets->remove(&world))
358 359
        return;

360
    invalidateInjectedStyleSheetCacheInAllFrames();
361 362 363 364
}

void PageGroup::removeAllUserContent()
{
365
    m_userScripts = nullptr;
366

367
    if (m_userStyleSheets) {
368
        m_userStyleSheets = nullptr;
369
        invalidateInjectedStyleSheetCacheInAllFrames();
370
    }
371 372
}

373
void PageGroup::invalidateInjectedStyleSheetCacheInAllFrames()
374 375
{
    // Clear our cached sheets and have them just reparse.
376
    for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it) {
377
        for (Frame* frame = &(*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
378
            frame->document()->styleSheetCollection().invalidateInjectedStyleSheetCache();
379 380
            frame->document()->styleResolverChanged(DeferRecalcStyle);
        }
381 382 383
    }
}

384
#if ENABLE(VIDEO_TRACK)
385 386
void PageGroup::captionPreferencesChanged()
{
387 388
    for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it)
        (*it)->captionPreferencesChanged();
389 390 391
    pageCache()->markPagesForCaptionPreferencesChanged();
}

392 393
CaptionUserPreferences* PageGroup::captionPreferences()
{
394
    if (!m_captionPreferences) {
395
#if (PLATFORM(MAC) && !PLATFORM(IOS)) || HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
396
        m_captionPreferences = std::make_unique<CaptionUserPreferencesMediaAF>(*this);
397
#else
398
        m_captionPreferences = std::make_unique<CaptionUserPreferences>(*this);
399
#endif
400
    }
401 402 403 404 405

    return m_captionPreferences.get();
}
#endif

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