Chromium Code Reviews| 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..bdfd0e13ff8d4b37e437afac21507e1190cda9f9 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; |
|
Devlin
2015/04/08 17:50:56
Is there a way we can clear this map? Probably no
Xi Han
2015/04/08 22:10:37
I have the same thoughts, but it is difficult to p
|
| + |
| // Greasemonkey API source that is injected with the scripts. |
| struct GreasemonkeyApiJsString { |
| GreasemonkeyApiJsString(); |
| @@ -131,17 +157,31 @@ 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 || |
| + UserScript::ConsumerInstanceType::WEBVIEW || |
| can_execute == PermissionsData::ACCESS_DENIED) |
| 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(), |
|
Devlin
2015/04/08 17:50:56
This still makes me a little sad, and it'd be bett
Xi Han
2015/04/08 22:10:37
Done.
|
| + &allowed)); |
| + map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); |
| + } |
| + |
| + return allowed ? PermissionsData::ACCESS_ALLOWED |
| + : PermissionsData::ACCESS_DENIED; |
| } |
| std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |