PageGroup.cpp 9.52 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
void PageGroup::addPage(Page& page)
darin@apple.com's avatar
darin@apple.com committed
164
{
165 166
    ASSERT(!m_pages.contains(&page));
    m_pages.add(&page);
167 168

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

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

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

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

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

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

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

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

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

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

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

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

259
    return m_localStorage.get();
260 261
}

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

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

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

272
void PageGroup::addUserScriptToWorld(DOMWrapperWorld& world, const String& source, const URL& url, const Vector<String>& whitelist, const Vector<String>& blacklist, UserScriptInjectionTime injectionTime, UserContentInjectedFrames injectedFrames)
273
{
274
    auto userScript = std::make_unique<UserScript>(source, url, whitelist, blacklist, injectionTime, injectedFrames);
275
    m_userContentController->addUserScript(world, std::move(userScript));
276 277
}

278
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)
279
{
280
    auto userStyleSheet = std::make_unique<UserStyleSheet>(source, url, whitelist, blacklist, injectedFrames, level);
281
    m_userContentController->addUserStyleSheet(world, std::move(userStyleSheet), injectionTime);
282

283
}
284

285
void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url)
286
{
287
    m_userContentController->removeUserScript(world, url);
288 289
}

290
void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
291
{
292
    m_userContentController->removeUserStyleSheet(world, url);
293 294
}

295
void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
296
{
297
    m_userContentController->removeUserScripts(world);
298 299
}

300
void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
301
{
302
    m_userContentController->removeUserStyleSheets(world);
303 304 305 306
}

void PageGroup::removeAllUserContent()
{
307
    m_userContentController->removeAllUserContent();
308 309
}

310
#if ENABLE(VIDEO_TRACK)
311 312
void PageGroup::captionPreferencesChanged()
{
313 314
    for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it)
        (*it)->captionPreferencesChanged();
315 316 317
    pageCache()->markPagesForCaptionPreferencesChanged();
}

318 319
CaptionUserPreferences* PageGroup::captionPreferences()
{
320
    if (!m_captionPreferences) {
321
#if (PLATFORM(MAC) && !PLATFORM(IOS)) || HAVE(MEDIA_ACCESSIBILITY_FRAMEWORK)
322
        m_captionPreferences = std::make_unique<CaptionUserPreferencesMediaAF>(*this);
323
#else
324
        m_captionPreferences = std::make_unique<CaptionUserPreferences>(*this);
325
#endif
326
    }
327 328 329 330 331

    return m_captionPreferences.get();
}
#endif

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