diff options
author | Szabolcs David <davidsz@inf.u-szeged.hu> | 2016-06-03 02:41:21 -0700 |
---|---|---|
committer | Szabolcs David <davidsz@inf.u-szeged.hu> | 2016-07-07 08:41:35 +0000 |
commit | 9bb9076c50048913075f11692f784ebb959d63cf (patch) | |
tree | 4a27e9eb3c7bd767963505f14ac0c703c811338c /src/core/renderer | |
parent | 800365f6faad962a4dd2e71173527d285a3f62b5 (diff) |
Parse metadata block in user scripts
This allows WebEngine to build up user script objects from the
metadata section of their source code. It also implements @include,
@exclude and @match rules for restricting script injection to given
URL patterns.
Change-Id: Ic7b3023c0143643bfbf71adbfa25a8022b223fcf
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/renderer')
-rw-r--r-- | src/core/renderer/user_resource_controller.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/core/renderer/user_resource_controller.cpp b/src/core/renderer/user_resource_controller.cpp index 68b5f3d40..8c603b805 100644 --- a/src/core/renderer/user_resource_controller.cpp +++ b/src/core/renderer/user_resource_controller.cpp @@ -39,9 +39,12 @@ #include "user_resource_controller.h" +#include "base/strings/pattern.h" #include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_view.h" #include "content/public/renderer/render_view_observer.h" +#include "extensions/common/url_pattern.h" +#include "third_party/WebKit/public/web/WebDocument.h" #include "third_party/WebKit/public/web/WebLocalFrame.h" #include "third_party/WebKit/public/web/WebScriptSource.h" #include "third_party/WebKit/public/web/WebView.h" @@ -49,6 +52,8 @@ #include "common/qt_messages.h" #include "common/user_script_data.h" +#include "type_conversion.h" +#include "user_script.h" Q_GLOBAL_STATIC(UserResourceController, qt_webengine_userResourceController) @@ -57,6 +62,40 @@ static content::RenderView * const globalScriptsIndex = 0; // Scripts meant to run after the load event will be run 500ms after DOMContentLoaded if the load event doesn't come within that delay. static const int afterLoadTimeout = 500; +static bool scriptMatchesURL(const UserScriptData &scriptData, const GURL &url) { + // Logic taken from Chromium (extensions/common/user_script.cc) + bool matchFound; + if (!scriptData.urlPatterns.empty()) { + matchFound = false; + for (auto it = scriptData.urlPatterns.begin(), end = scriptData.urlPatterns.end(); it != end; ++it) { + URLPattern urlPattern(QtWebEngineCore::UserScript::validUserScriptSchemes(), *it); + if (urlPattern.MatchesURL(url)) + matchFound = true; + } + if (!matchFound) + return false; + } + + if (!scriptData.globs.empty()) { + matchFound = false; + for (auto it = scriptData.globs.begin(), end = scriptData.globs.end(); it != end; ++it) { + if (base::MatchPattern(url.spec(), *it)) + matchFound = true; + } + if (!matchFound) + return false; + } + + if (!scriptData.excludeGlobs.empty()) { + for (auto it = scriptData.excludeGlobs.begin(), end = scriptData.excludeGlobs.end(); it != end; ++it) { + if (base::MatchPattern(url.spec(), *it)) + return false; + } + } + + return true; +} + class UserResourceController::RenderViewObserverHelper : public content::RenderViewObserver { public: @@ -99,6 +138,8 @@ void UserResourceController::runScripts(UserScriptData::InjectionPoint p, blink: if (script.injectionPoint != p || (!script.injectForSubframes && !isMainFrame)) continue; + if (!scriptMatchesURL(script, frame->document().url())) + continue; blink::WebScriptSource source(blink::WebString::fromUTF8(script.source), script.url); if (script.worldId) frame->executeScriptInIsolatedWorld(script.worldId, &source, /*numSources = */1, /*contentScriptExtentsionGroup = */ 0); |