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

UserContentController should keep track of user scripts

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

Reviewed by Andreas Kling.

Move handling of user scripts from PageGroup to UserContentController.

* page/Frame.cpp:
(WebCore::Frame::injectUserScripts):
* page/PageGroup.cpp:
(WebCore::PageGroup::addUserScriptToWorld):
(WebCore::PageGroup::removeUserScriptFromWorld):
(WebCore::PageGroup::removeUserScriptsFromWorld):
(WebCore::PageGroup::removeAllUserContent):
* page/PageGroup.h:
* page/UserContentController.cpp:
(WebCore::UserContentController::addUserScript):
(WebCore::UserContentController::removeUserScript):
(WebCore::UserContentController::removeUserScripts):
(WebCore::UserContentController::removeAllUserContent):
* page/UserContentController.h:
(WebCore::UserContentController::userScripts):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@162384 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 070c2097
2014-01-20 Anders Carlsson <andersca@apple.com>
UserContentController should keep track of user scripts
https://bugs.webkit.org/show_bug.cgi?id=127317
<rdar://problem/15861296>
Reviewed by Andreas Kling.
Move handling of user scripts from PageGroup to UserContentController.
* page/Frame.cpp:
(WebCore::Frame::injectUserScripts):
* page/PageGroup.cpp:
(WebCore::PageGroup::addUserScriptToWorld):
(WebCore::PageGroup::removeUserScriptFromWorld):
(WebCore::PageGroup::removeUserScriptsFromWorld):
(WebCore::PageGroup::removeAllUserContent):
* page/PageGroup.h:
* page/UserContentController.cpp:
(WebCore::UserContentController::addUserScript):
(WebCore::UserContentController::removeUserScript):
(WebCore::UserContentController::removeUserScripts):
(WebCore::UserContentController::removeAllUserContent):
* page/UserContentController.h:
(WebCore::UserContentController::userScripts):
2014-01-20 Anders Carlsson <andersca@apple.com>
Give each page a UserContentController
https://bugs.webkit.org/show_bug.cgi?id=127315
<rdar://problem/15861296>
Reviewed by Andreas Kling.
......@@ -92,6 +92,7 @@
#include "TextIterator.h"
#include "TextNodeTraversal.h"
#include "TextResourceDecoder.h"
#include "UserContentController.h"
#include "UserContentURLPattern.h"
#include "UserTypingGestureIndicator.h"
#include "VisibleUnits.h"
......@@ -703,13 +704,17 @@ void Frame::injectUserScripts(UserScriptInjectionTime injectionTime)
if (loader().stateMachine()->creatingInitialEmptyDocument() && !settings().shouldInjectUserScriptsInInitialEmptyDocument())
return;
UserContentController* userContentController = m_page->userContentController();
if (!userContentController)
return;
// Walk the hashtable. Inject by world.
const UserScriptMap* userScripts = m_page->group().userScripts();
const UserScriptMap* userScripts = userContentController->userScripts();
if (!userScripts)
return;
for (auto it = userScripts->begin(), end = userScripts->end(); it != end; ++it)
injectUserScriptsForWorld(*it->key.get(), *it->value, injectionTime);
for (const auto& worldAndUserScript : *userScripts)
injectUserScriptsForWorld(*worldAndUserScript.key, *worldAndUserScript.value, injectionTime);
}
void Frame::injectUserScriptsForWorld(DOMWrapperWorld& world, const UserScriptVector& userScripts, UserScriptInjectionTime injectionTime)
......
......@@ -73,6 +73,7 @@ PageGroup::PageGroup(const String& name)
PageGroup::PageGroup(Page& page)
: m_visitedLinksPopulated(false)
, m_identifier(getUniqueIdentifier())
, m_userContentController(UserContentController::create())
, m_groupSettings(std::make_unique<GroupSettings>())
{
addPage(page);
......@@ -279,12 +280,7 @@ StorageNamespace* PageGroup::transientLocalStorage(SecurityOrigin* topOrigin)
void PageGroup::addUserScriptToWorld(DOMWrapperWorld& world, const String& source, const URL& url, const Vector<String>& whitelist, const Vector<String>& blacklist, UserScriptInjectionTime injectionTime, UserContentInjectedFrames injectedFrames)
{
auto userScript = std::make_unique<UserScript>(source, url, whitelist, blacklist, injectionTime, injectedFrames);
if (!m_userScripts)
m_userScripts = std::make_unique<UserScriptMap>();
std::unique_ptr<UserScriptVector>& scriptsInWorld = m_userScripts->add(&world, nullptr).iterator->value;
if (!scriptsInWorld)
scriptsInWorld = std::make_unique<UserScriptVector>();
scriptsInWorld->append(std::move(userScript));
m_userContentController->addUserScript(world, std::move(userScript));
}
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)
......@@ -303,21 +299,7 @@ void PageGroup::addUserStyleSheetToWorld(DOMWrapperWorld& world, const String& s
void PageGroup::removeUserScriptFromWorld(DOMWrapperWorld& world, const URL& url)
{
if (!m_userScripts)
return;
auto it = m_userScripts->find(&world);
if (it == m_userScripts->end())
return;
auto scripts = it->value.get();
for (int i = scripts->size() - 1; i >= 0; --i) {
if (scripts->at(i)->url() == url)
scripts->remove(i);
}
if (scripts->isEmpty())
m_userScripts->remove(it);
m_userContentController->removeUserScript(world, url);
}
void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL& url)
......@@ -349,10 +331,7 @@ void PageGroup::removeUserStyleSheetFromWorld(DOMWrapperWorld& world, const URL&
void PageGroup::removeUserScriptsFromWorld(DOMWrapperWorld& world)
{
if (!m_userScripts)
return;
m_userScripts->remove(&world);
m_userContentController->removeUserScripts(world);
}
void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
......@@ -368,7 +347,7 @@ void PageGroup::removeUserStyleSheetsFromWorld(DOMWrapperWorld& world)
void PageGroup::removeAllUserContent()
{
m_userScripts = nullptr;
m_userContentController->removeAllUserContent();
if (m_userStyleSheets) {
m_userStyleSheets = nullptr;
......
......@@ -94,13 +94,12 @@ namespace WebCore {
void addUserScriptToWorld(DOMWrapperWorld&, const String& source, const URL&, const Vector<String>& whitelist, const Vector<String>& blacklist, UserScriptInjectionTime, UserContentInjectedFrames);
void addUserStyleSheetToWorld(DOMWrapperWorld&, const String& source, const URL&, const Vector<String>& whitelist, const Vector<String>& blacklist, UserContentInjectedFrames, UserStyleLevel = UserStyleUserLevel, UserStyleInjectionTime = InjectInExistingDocuments);
void removeUserScriptFromWorld(DOMWrapperWorld&, const URL&);
void removeUserStyleSheetFromWorld(DOMWrapperWorld&, const URL&);
void removeUserScriptFromWorld(DOMWrapperWorld&, const URL&);
void removeUserScriptsFromWorld(DOMWrapperWorld&);
void removeUserStyleSheetsFromWorld(DOMWrapperWorld&);
void removeAllUserContent();
const UserScriptMap* userScripts() const { return m_userScripts.get(); }
const UserStyleSheetMap* userStyleSheets() const { return m_userStyleSheets.get(); }
GroupSettings& groupSettings() const { return *m_groupSettings; }
......@@ -125,7 +124,6 @@ namespace WebCore {
HashMap<RefPtr<SecurityOrigin>, RefPtr<StorageNamespace>> m_transientLocalStorageMap;
RefPtr<UserContentController> m_userContentController;
std::unique_ptr<UserScriptMap> m_userScripts;
std::unique_ptr<UserStyleSheetMap> m_userStyleSheets;
const std::unique_ptr<GroupSettings> m_groupSettings;
......
......@@ -56,4 +56,47 @@ void UserContentController::removePage(Page& page)
m_pages.remove(&page);
}
void UserContentController::addUserScript(DOMWrapperWorld& world, std::unique_ptr<UserScript> userScript)
{
if (!m_userScripts)
m_userScripts = std::make_unique<UserScriptMap>();
auto& scriptsInWorld = m_userScripts->add(&world, nullptr).iterator->value;
if (!scriptsInWorld)
scriptsInWorld = std::make_unique<UserScriptVector>();
scriptsInWorld->append(std::move(userScript));
}
void UserContentController::removeUserScript(DOMWrapperWorld& world, const URL& url)
{
if (!m_userScripts)
return;
auto it = m_userScripts->find(&world);
if (it == m_userScripts->end())
return;
auto scripts = it->value.get();
for (int i = scripts->size() - 1; i >= 0; --i) {
if (scripts->at(i)->url() == url)
scripts->remove(i);
}
if (scripts->isEmpty())
m_userScripts->remove(it);
}
void UserContentController::removeUserScripts(DOMWrapperWorld& world)
{
if (!m_userScripts)
return;
m_userScripts->remove(&world);
}
void UserContentController::removeAllUserContent()
{
m_userScripts = nullptr;
}
} // namespace WebCore
......@@ -26,11 +26,10 @@
#ifndef UserContentController_h
#define UserContentController_h
#include <wtf/HashMap.h>
#include "UserScriptTypes.h"
#include <wtf/HashSet.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
namespace WebCore {
......@@ -47,10 +46,20 @@ public:
void addPage(Page&);
void removePage(Page&);
const UserScriptMap* userScripts() const { return m_userScripts.get(); }
void addUserScript(DOMWrapperWorld&, std::unique_ptr<UserScript>);
void removeUserScript(DOMWrapperWorld&, const URL&);
void removeUserScripts(DOMWrapperWorld&);
void removeAllUserContent();
private:
UserContentController();
HashSet<Page*> m_pages;
std::unique_ptr<UserScriptMap> m_userScripts;
};
} // 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