Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Unified Diff: extensions/renderer/user_script_injector.cc

Issue 959413003: Implement <webview>.addContentScript/removeContentScript API [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/renderer/user_script_injector.cc
diff --git a/extensions/renderer/user_script_injector.cc b/extensions/renderer/user_script_injector.cc
index 5a42ffa71b03f1a26c0d1fda54ff0427c341471c..603c20f95bbe787e7dc243a8b7f19da4a62ded82 100644
--- a/extensions/renderer/user_script_injector.cc
+++ b/extensions/renderer/user_script_injector.cc
@@ -8,8 +8,10 @@
#include "base/lazy_instance.h"
#include "content/public/common/url_constants.h"
+#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/render_view.h"
#include "extensions/common/extension.h"
+#include "extensions/common/guest_view/guest_view_messages.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/renderer/injection_host.h"
#include "extensions/renderer/script_context.h"
@@ -25,11 +27,35 @@ namespace extensions {
namespace {
+struct RoutingInfoKey {
+ int routing_id;
+ int script_id;
+
+ RoutingInfoKey(int routing_id, int script_id) {
+ this->routing_id = routing_id;
+ this->script_id = script_id;
+ }
+
+ bool operator<(const RoutingInfoKey& other) const {
+ if (routing_id != other.routing_id)
+ return routing_id < other.routing_id;
+
+ if (script_id != other.script_id)
+ return script_id < other.script_id;
+ return false; // keys are equal.
+ }
+};
+
+using RoutingInfoMap = std::map<RoutingInfoKey, bool>;
+
// These two strings are injected before and after the Greasemonkey API and
// user script to wrap it in an anonymous scope.
const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
const char kUserScriptTail[] = "\n})(window);";
+base::LazyInstance<RoutingInfoMap> g_routing_info_map =
+ LAZY_INSTANCE_INITIALIZER;
+
// Greasemonkey API source that is injected with the scripts.
struct GreasemonkeyApiJsString {
GreasemonkeyApiJsString();
@@ -58,11 +84,13 @@ base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api =
UserScriptInjector::UserScriptInjector(const UserScript* script,
UserScriptSet* script_list,
- bool is_declarative)
+ bool is_declarative,
+ bool has_guest_content_script)
: script_(script),
script_id_(script_->id()),
host_id_(script_->host_id()),
is_declarative_(is_declarative),
+ has_guest_content_script_(has_guest_content_script),
user_script_set_observer_(this) {
user_script_set_observer_.Add(script_list);
}
@@ -131,17 +159,32 @@ PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame(
web_frame, web_frame->document().url(), script_->match_about_blank());
PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame(
effective_document_url, top_url, tab_id, is_declarative_);
-
if (script_->consumer_instance_type() !=
- UserScript::ConsumerInstanceType::WEBVIEW ||
- can_execute == PermissionsData::ACCESS_DENIED)
+ UserScript::ConsumerInstanceType::WEBVIEW ||
+ can_execute == PermissionsData::ACCESS_DENIED ||
+ !has_guest_content_script_)
return can_execute;
int routing_id = content::RenderView::FromWebView(web_frame->top()->view())
->GetRoutingID();
- return script_->routing_info().render_view_id == routing_id
- ? PermissionsData::ACCESS_ALLOWED
- : PermissionsData::ACCESS_DENIED;
+
+ RoutingInfoKey key(routing_id, script_->id());
+
+ RoutingInfoMap& map = g_routing_info_map.Get();
+ auto iter = map.find(key);
+
+ bool allowed = false;
+ if (iter != map.end()) {
+ allowed = iter->second;
+ } else {
+ content::RenderThread::Get()->Send(
+ new GuestViewHostMsg_CanExecuteContentScript(routing_id, script_->id(),
+ &allowed));
+ map.insert(std::pair<RoutingInfoKey, bool>(key, allowed));
+ }
+
+ return allowed ? PermissionsData::ACCESS_ALLOWED
+ : PermissionsData::ACCESS_DENIED;
}
std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources(

Powered by Google App Engine
This is Rietveld 408576698