Commit c005d5bc authored by andersca@apple.com's avatar andersca@apple.com

Move user style sheet handling to UserContentController

https://bugs.webkit.org/show_bug.cgi?id=127322
<rdar://problem/15861296>

Reviewed by Andreas Kling.

* dom/DocumentStyleSheetCollection.cpp:
(WebCore::DocumentStyleSheetCollection::updateInjectedStyleSheetCache):
* page/PageGroup.cpp:
(WebCore::PageGroup::addUserStyleSheetToWorld):
(WebCore::PageGroup::removeUserStyleSheetFromWorld):
(WebCore::PageGroup::removeUserStyleSheetsFromWorld):
(WebCore::PageGroup::removeAllUserContent):
* page/PageGroup.h:
* page/UserContentController.cpp:
(WebCore::UserContentController::addUserStyleSheet):
(WebCore::UserContentController::removeUserStyleSheet):
(WebCore::UserContentController::removeUserStyleSheets):
(WebCore::UserContentController::removeAllUserContent):
(WebCore::UserContentController::invalidateInjectedStyleSheetCacheInAllFrames):
* page/UserContentController.h:
(WebCore::UserContentController::userStyleSheets):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@162395 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 64c95337
2014-01-20 Anders Carlsson <andersca@apple.com>
Move user style sheet handling to UserContentController
https://bugs.webkit.org/show_bug.cgi?id=127322
<rdar://problem/15861296>
Reviewed by Andreas Kling.
* dom/DocumentStyleSheetCollection.cpp:
(WebCore::DocumentStyleSheetCollection::updateInjectedStyleSheetCache):
* page/PageGroup.cpp:
(WebCore::PageGroup::addUserStyleSheetToWorld):
(WebCore::PageGroup::removeUserStyleSheetFromWorld):
(WebCore::PageGroup::removeUserStyleSheetsFromWorld):
(WebCore::PageGroup::removeAllUserContent):
* page/PageGroup.h:
* page/UserContentController.cpp:
(WebCore::UserContentController::addUserStyleSheet):
(WebCore::UserContentController::removeUserStyleSheet):
(WebCore::UserContentController::removeUserStyleSheets):
(WebCore::UserContentController::removeAllUserContent):
(WebCore::UserContentController::invalidateInjectedStyleSheetCacheInAllFrames):
* page/UserContentController.h:
(WebCore::UserContentController::userStyleSheets):
2014-01-20 Benjamin Poulain <benjamin@webkit.org>
Add a nicer way to iterate over all the attributes of an element
......@@ -43,6 +43,7 @@
#include "StyleResolver.h"
#include "StyleSheetContents.h"
#include "StyleSheetList.h"
#include "UserContentController.h"
#include "UserContentURLPattern.h"
namespace WebCore {
......@@ -140,27 +141,30 @@ void DocumentStyleSheetCollection::updateInjectedStyleSheetCache() const
Page* owningPage = m_document.page();
if (!owningPage)
return;
const PageGroup& pageGroup = owningPage->group();
const UserStyleSheetMap* sheetsMap = pageGroup.userStyleSheets();
if (!sheetsMap)
const auto* userContentController = owningPage->userContentController();
if (!userContentController)
return;
const UserStyleSheetMap* userStyleSheets = userContentController->userStyleSheets();
if (!userStyleSheets)
return;
UserStyleSheetMap::const_iterator end = sheetsMap->end();
for (UserStyleSheetMap::const_iterator it = sheetsMap->begin(); it != end; ++it) {
const UserStyleSheetVector* sheets = it->value.get();
for (unsigned i = 0; i < sheets->size(); ++i) {
const UserStyleSheet* sheet = sheets->at(i).get();
for (auto& styleSheets : userStyleSheets->values()) {
for (const auto& sheet : *styleSheets) {
if (sheet->injectedFrames() == InjectInTopFrameOnly && m_document.ownerElement())
continue;
if (!UserContentURLPattern::matchesPatterns(m_document.url(), sheet->whitelist(), sheet->blacklist()))
continue;
RefPtr<CSSStyleSheet> groupSheet = CSSStyleSheet::createInline(const_cast<Document&>(m_document), sheet->url());
bool isUserStyleSheet = sheet->level() == UserStyleUserLevel;
if (isUserStyleSheet)
m_injectedUserStyleSheets.append(groupSheet);
else
m_injectedAuthorStyleSheets.append(groupSheet);
groupSheet->contents().setIsUserStyleSheet(isUserStyleSheet);
groupSheet->contents().parseString(sheet->source());
}
......
......@@ -704,7 +704,7 @@ void Frame::injectUserScripts(UserScriptInjectionTime injectionTime)
if (loader().stateMachine()->creatingInitialEmptyDocument() && !settings().shouldInjectUserScriptsInInitialEmptyDocument())
return;
UserContentController* userContentController = m_page->userContentController();
const auto* userContentController = m_page->userContentController();
if (!userContentController)
return;
......
......@@ -286,15 +286,8 @@ void PageGroup::addUserScriptToWorld(DOMWrapperWorld& world, const String& sourc
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)
{
auto userStyleSheet = std::make_unique<UserStyleSheet>(source, url, whitelist, blacklist, injectedFrames, level);
if (!m_userStyleSheets)
m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
std::unique_ptr<UserStyleSheetVector>& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
if (!styleSheetsInWorld)
styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
styleSheetsInWorld->append(std::move(userStyleSheet));
m_userContentController->addUserStyleSheet(world, std::move(userStyleSheet), injectionTime);
if (injectionTime == InjectInExistingDocuments)
invalidateInjectedStyleSheetCacheInAllFrames();
}
void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url)
......@@ -304,29 +297,7 @@ void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url
void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
{
if (!m_userStyleSheets)
return;
auto it = m_userStyleSheets->find(&world);
bool sheetsChanged = false;
if (it == m_userStyleSheets->end())
return;
auto stylesheets = it->value.get();
for (int i = stylesheets->size() - 1; i >= 0; --i) {
if (stylesheets->at(i)->url() == url) {
stylesheets->remove(i);
sheetsChanged = true;
}
}
if (!sheetsChanged)
return;
if (stylesheets->isEmpty())
m_userStyleSheets->remove(it);
invalidateInjectedStyleSheetCacheInAllFrames();
m_userContentController->removeUserStyleSheet(world, url);
}
void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
......@@ -336,34 +307,12 @@ void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
{
if (!m_userStyleSheets)
return;
if (!m_userStyleSheets->remove(&world))
return;
invalidateInjectedStyleSheetCacheInAllFrames();
m_userContentController->removeUserStyleSheets(world);
}
void PageGroup::removeAllUserContent()
{
m_userContentController->removeAllUserContent();
if (m_userStyleSheets) {
m_userStyleSheets = nullptr;
invalidateInjectedStyleSheetCacheInAllFrames();
}
}
void PageGroup::invalidateInjectedStyleSheetCacheInAllFrames()
{
// Clear our cached sheets and have them just reparse.
for (auto it = m_pages.begin(), end = m_pages.end(); it != end; ++it) {
for (Frame* frame = &(*it)->mainFrame(); frame; frame = frame->tree().traverseNext()) {
frame->document()->styleSheetCollection().invalidateInjectedStyleSheetCache();
frame->document()->styleResolverChanged(DeferRecalcStyle);
}
}
}
#if ENABLE(VIDEO_TRACK)
......
......@@ -100,8 +100,6 @@ namespace WebCore {
void removeUserStyleSheetsFromWorld(DOMWrapperWorld&);
void removeAllUserContent();
const UserStyleSheetMap* userStyleSheets() const { return m_userStyleSheets.get(); }
GroupSettings& groupSettings() const { return *m_groupSettings; }
#if ENABLE(VIDEO_TRACK)
......@@ -111,7 +109,6 @@ namespace WebCore {
private:
void addVisitedLink(LinkHash);
void invalidateInjectedStyleSheetCacheInAllFrames();
String m_name;
HashSet<Page*> m_pages;
......@@ -124,7 +121,6 @@ namespace WebCore {
HashMap<RefPtr<SecurityOrigin>, RefPtr<StorageNamespace>> m_transientLocalStorageMap;
RefPtr<UserContentController> m_userContentController;
std::unique_ptr<UserStyleSheetMap> m_userStyleSheets;
const std::unique_ptr<GroupSettings> m_groupSettings;
......
......@@ -27,7 +27,11 @@
#include "UserContentController.h"
#include "DOMWrapperWorld.h"
#include "Document.h"
#include "MainFrame.h"
#include "Page.h"
#include "UserScript.h"
#include "UserStyleSheet.h"
namespace WebCore {
......@@ -94,9 +98,77 @@ void UserContentController::removeUserScripts(DOMWrapperWorld& world)
m_userScripts->remove(&world);
}
void UserContentController::addUserStyleSheet(DOMWrapperWorld& world, std::unique_ptr<UserStyleSheet> userStyleSheet, UserStyleInjectionTime injectionTime)
{
if (!m_userStyleSheets)
m_userStyleSheets = std::make_unique<UserStyleSheetMap>();
auto& styleSheetsInWorld = m_userStyleSheets->add(&world, nullptr).iterator->value;
if (!styleSheetsInWorld)
styleSheetsInWorld = std::make_unique<UserStyleSheetVector>();
styleSheetsInWorld->append(std::move(userStyleSheet));
if (injectionTime == InjectInExistingDocuments)
invalidateInjectedStyleSheetCacheInAllFrames();
}
void UserContentController::removeUserStyleSheet(DOMWrapperWorld& world, const URL& url)
{
if (!m_userStyleSheets)
return;
auto it = m_userStyleSheets->find(&world);
if (it == m_userStyleSheets->end())
return;
auto& stylesheets = *it->value;
bool sheetsChanged = false;
for (int i = stylesheets.size() - 1; i >= 0; --i) {
if (stylesheets[i]->url() == url) {
stylesheets.remove(i);
sheetsChanged = true;
}
}
if (!sheetsChanged)
return;
if (stylesheets.isEmpty())
m_userStyleSheets->remove(it);
invalidateInjectedStyleSheetCacheInAllFrames();
}
void UserContentController::removeUserStyleSheets(DOMWrapperWorld& world)
{
if (!m_userStyleSheets)
return;
if (!m_userStyleSheets->remove(&world))
return;
invalidateInjectedStyleSheetCacheInAllFrames();
}
void UserContentController::removeAllUserContent()
{
m_userScripts = nullptr;
if (m_userStyleSheets) {
m_userStyleSheets = nullptr;
invalidateInjectedStyleSheetCacheInAllFrames();
}
}
void UserContentController::invalidateInjectedStyleSheetCacheInAllFrames()
{
for (auto& page : m_pages) {
for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext()) {
frame->document()->styleSheetCollection().invalidateInjectedStyleSheetCache();
frame->document()->styleResolverChanged(DeferRecalcStyle);
}
}
}
} // namespace WebCore
......@@ -27,6 +27,7 @@
#define UserContentController_h
#include "UserScriptTypes.h"
#include "UserStyleSheetTypes.h"
#include <wtf/HashSet.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
......@@ -37,6 +38,7 @@ class DOMWrapperWorld;
class Page;
class URL;
class UserScript;
class UserStyleSheet;
class UserContentController : public RefCounted<UserContentController> {
public:
......@@ -52,14 +54,23 @@ public:
void removeUserScript(DOMWrapperWorld&, const URL&);
void removeUserScripts(DOMWrapperWorld&);
const UserStyleSheetMap* userStyleSheets() const { return m_userStyleSheets.get(); }
void addUserStyleSheet(DOMWrapperWorld&, std::unique_ptr<UserStyleSheet>, UserStyleInjectionTime);
void removeUserStyleSheet(DOMWrapperWorld&, const URL&);
void removeUserStyleSheets(DOMWrapperWorld&);
void removeAllUserContent();
private:
UserContentController();
void invalidateInjectedStyleSheetCacheInAllFrames();
HashSet<Page*> m_pages;
std::unique_ptr<UserScriptMap> m_userScripts;
std::unique_ptr<UserStyleSheetMap> m_userStyleSheets;
};
} // namespace WebCore
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment