PageGroup.cpp 11.7 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_groupSettings(std::make_unique<GroupSettings>())
darin@apple.com's avatar
darin@apple.com committed
77
{
78
    addPage(page);
darin@apple.com's avatar
darin@apple.com committed
79 80
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    return pageGroups->size();
}

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

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

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

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

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

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

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

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

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

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

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

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

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

266
    return m_localStorage.get();
267 268
}

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

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

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

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

290
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)
291
{
292
    auto userStyleSheet = std::make_unique<UserStyleSheet>(source, url, whitelist, blacklist, injectedFrames, level);
293
    if (!m_userStyleSheets)
294
        m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
295
    std::unique_ptr<UserStyleSheetVector>& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
296
    if (!styleSheetsInWorld)
297 298
        styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
    styleSheetsInWorld->append(std::move(userStyleSheet));
299

300
    if (injectionTime == InjectInExistingDocuments)
301
        invalidateInjectedStyleSheetCacheInAllFrames();
302
}
303

304
void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url)
305
{
306 307 308
    if (!m_userScripts)
        return;

309
    auto it = m_userScripts->find(&world);
310 311 312
    if (it == m_userScripts->end())
        return;
    
313
    auto scripts = it->value.get();
314 315 316
    for (int i = scripts->size() - 1; i >= 0; --i) {
        if (scripts->at(i)->url() == url)
            scripts->remove(i);
317 318
    }
    
319 320
    if (scripts->isEmpty())
        m_userScripts->remove(it);
321 322
}

323
void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
324 325 326 327
{
    if (!m_userStyleSheets)
        return;

328
    auto it = m_userStyleSheets->find(&world);
329 330 331 332
    bool sheetsChanged = false;
    if (it == m_userStyleSheets->end())
        return;
    
333
    auto stylesheets = it->value.get();
334 335 336 337
    for (int i = stylesheets->size() - 1; i >= 0; --i) {
        if (stylesheets->at(i)->url() == url) {
            stylesheets->remove(i);
            sheetsChanged = true;
338
        }
339
    }
340
        
341 342 343
    if (!sheetsChanged)
        return;

344
    if (stylesheets->isEmpty())
345
        m_userStyleSheets->remove(it);
346

347
    invalidateInjectedStyleSheetCacheInAllFrames();
348 349
}

350
void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
351
{
352 353 354
    if (!m_userScripts)
        return;

355
    m_userScripts->remove(&world);
356 357
}

358
void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
359 360 361
{
    if (!m_userStyleSheets)
        return;
362

363
    if (!m_userStyleSheets->remove(&world))
364 365
        return;

366
    invalidateInjectedStyleSheetCacheInAllFrames();
367 368 369 370
}

void PageGroup::removeAllUserContent()
{
371
    m_userScripts = nullptr;
372

373
    if (m_userStyleSheets) {
374
        m_userStyleSheets = nullptr;
375
        invalidateInjectedStyleSheetCacheInAllFrames();
376
    }
377 378
}

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

390
#if ENABLE(VIDEO_TRACK)
391 392
void PageGroup::captionPreferencesChanged()
{
393 394
    for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it)
        (*it)->captionPreferencesChanged();
395 396 397
    pageCache()->markPagesForCaptionPreferencesChanged();
}

398 399
CaptionUserPreferences* PageGroup::captionPreferences()
{
400
    if (!m_captionPreferences) {
401
#if (PLATFORM(MAC) && !PLATFORM(IOS)) || HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
402
        m_captionPreferences = std::make_unique<CaptionUserPreferencesMediaAF>(*this);
403
#else
404
        m_captionPreferences = std::make_unique<CaptionUserPreferences>(*this);
405
#endif
406
    }
407 408 409 410 411

    return m_captionPreferences.get();
}
#endif

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