OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/user_script_injector.h" | 5 #include "extensions/renderer/user_script_injector.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "content/public/common/url_constants.h" | 10 #include "content/public/common/url_constants.h" |
| 11 #include "content/public/renderer/render_thread.h" |
11 #include "content/public/renderer/render_view.h" | 12 #include "content/public/renderer/render_view.h" |
12 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
| 14 #include "extensions/common/guest_view/guest_view_messages.h" |
13 #include "extensions/common/permissions/permissions_data.h" | 15 #include "extensions/common/permissions/permissions_data.h" |
14 #include "extensions/renderer/injection_host.h" | 16 #include "extensions/renderer/injection_host.h" |
15 #include "extensions/renderer/script_context.h" | 17 #include "extensions/renderer/script_context.h" |
16 #include "extensions/renderer/scripts_run_info.h" | 18 #include "extensions/renderer/scripts_run_info.h" |
17 #include "grit/extensions_renderer_resources.h" | 19 #include "grit/extensions_renderer_resources.h" |
18 #include "third_party/WebKit/public/web/WebDocument.h" | 20 #include "third_party/WebKit/public/web/WebDocument.h" |
19 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
20 #include "third_party/WebKit/public/web/WebScriptSource.h" | 22 #include "third_party/WebKit/public/web/WebScriptSource.h" |
21 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
23 | 25 |
24 namespace extensions { | 26 namespace extensions { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
| 30 struct RoutingInfoKey { |
| 31 int routing_id; |
| 32 int script_id; |
| 33 |
| 34 RoutingInfoKey(int routing_id, int script_id) { |
| 35 this->routing_id = routing_id; |
| 36 this->script_id = script_id; |
| 37 } |
| 38 |
| 39 bool operator<(const RoutingInfoKey& other) const { |
| 40 if (routing_id != other.routing_id) |
| 41 return routing_id < other.routing_id; |
| 42 |
| 43 if (script_id != other.script_id) |
| 44 return script_id < other.script_id; |
| 45 return false; // keys are equal. |
| 46 } |
| 47 }; |
| 48 |
| 49 using RoutingInfoMap = std::map<RoutingInfoKey, bool>; |
| 50 |
28 // These two strings are injected before and after the Greasemonkey API and | 51 // These two strings are injected before and after the Greasemonkey API and |
29 // user script to wrap it in an anonymous scope. | 52 // user script to wrap it in an anonymous scope. |
30 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 53 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
31 const char kUserScriptTail[] = "\n})(window);"; | 54 const char kUserScriptTail[] = "\n})(window);"; |
32 | 55 |
| 56 base::LazyInstance<RoutingInfoMap> g_routing_info_map = |
| 57 LAZY_INSTANCE_INITIALIZER; |
| 58 |
33 // Greasemonkey API source that is injected with the scripts. | 59 // Greasemonkey API source that is injected with the scripts. |
34 struct GreasemonkeyApiJsString { | 60 struct GreasemonkeyApiJsString { |
35 GreasemonkeyApiJsString(); | 61 GreasemonkeyApiJsString(); |
36 blink::WebScriptSource GetSource() const; | 62 blink::WebScriptSource GetSource() const; |
37 | 63 |
38 private: | 64 private: |
39 std::string source_; | 65 std::string source_; |
40 }; | 66 }; |
41 | 67 |
42 // The below constructor, monstrous as it is, just makes a WebScriptSource from | 68 // The below constructor, monstrous as it is, just makes a WebScriptSource from |
43 // the GreasemonkeyApiJs resource. | 69 // the GreasemonkeyApiJs resource. |
44 GreasemonkeyApiJsString::GreasemonkeyApiJsString() | 70 GreasemonkeyApiJsString::GreasemonkeyApiJsString() |
45 : source_(ResourceBundle::GetSharedInstance() | 71 : source_(ResourceBundle::GetSharedInstance() |
46 .GetRawDataResource(IDR_GREASEMONKEY_API_JS) | 72 .GetRawDataResource(IDR_GREASEMONKEY_API_JS) |
47 .as_string()) { | 73 .as_string()) { |
48 } | 74 } |
49 | 75 |
50 blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { | 76 blink::WebScriptSource GreasemonkeyApiJsString::GetSource() const { |
51 return blink::WebScriptSource(blink::WebString::fromUTF8(source_)); | 77 return blink::WebScriptSource(blink::WebString::fromUTF8(source_)); |
52 } | 78 } |
53 | 79 |
54 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = | 80 base::LazyInstance<GreasemonkeyApiJsString> g_greasemonkey_api = |
55 LAZY_INSTANCE_INITIALIZER; | 81 LAZY_INSTANCE_INITIALIZER; |
56 | 82 |
57 } // namespace | 83 } // namespace |
58 | 84 |
59 UserScriptInjector::UserScriptInjector(const UserScript* script, | 85 UserScriptInjector::UserScriptInjector(const UserScript* script, |
60 UserScriptSet* script_list, | 86 UserScriptSet* script_list, |
61 bool is_declarative) | 87 bool is_declarative, |
| 88 bool has_guest_content_script) |
62 : script_(script), | 89 : script_(script), |
63 script_id_(script_->id()), | 90 script_id_(script_->id()), |
64 host_id_(script_->host_id()), | 91 host_id_(script_->host_id()), |
65 is_declarative_(is_declarative), | 92 is_declarative_(is_declarative), |
| 93 has_guest_content_script_(has_guest_content_script), |
66 user_script_set_observer_(this) { | 94 user_script_set_observer_(this) { |
67 user_script_set_observer_.Add(script_list); | 95 user_script_set_observer_.Add(script_list); |
68 } | 96 } |
69 | 97 |
70 UserScriptInjector::~UserScriptInjector() { | 98 UserScriptInjector::~UserScriptInjector() { |
71 } | 99 } |
72 | 100 |
73 void UserScriptInjector::OnUserScriptsUpdated( | 101 void UserScriptInjector::OnUserScriptsUpdated( |
74 const std::set<HostID>& changed_hosts, | 102 const std::set<HostID>& changed_hosts, |
75 const std::vector<UserScript*>& scripts) { | 103 const std::vector<UserScript*>& scripts) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 152 |
125 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( | 153 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( |
126 const InjectionHost* injection_host, | 154 const InjectionHost* injection_host, |
127 blink::WebFrame* web_frame, | 155 blink::WebFrame* web_frame, |
128 int tab_id, | 156 int tab_id, |
129 const GURL& top_url) const { | 157 const GURL& top_url) const { |
130 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 158 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
131 web_frame, web_frame->document().url(), script_->match_about_blank()); | 159 web_frame, web_frame->document().url(), script_->match_about_blank()); |
132 PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame( | 160 PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame( |
133 effective_document_url, top_url, tab_id, is_declarative_); | 161 effective_document_url, top_url, tab_id, is_declarative_); |
134 | |
135 if (script_->consumer_instance_type() != | 162 if (script_->consumer_instance_type() != |
136 UserScript::ConsumerInstanceType::WEBVIEW || | 163 UserScript::ConsumerInstanceType::WEBVIEW || |
137 can_execute == PermissionsData::ACCESS_DENIED) | 164 can_execute == PermissionsData::ACCESS_DENIED || |
| 165 !has_guest_content_script_) |
138 return can_execute; | 166 return can_execute; |
139 | 167 |
140 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) | 168 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) |
141 ->GetRoutingID(); | 169 ->GetRoutingID(); |
142 return script_->routing_info().render_view_id == routing_id | 170 |
143 ? PermissionsData::ACCESS_ALLOWED | 171 RoutingInfoKey key(routing_id, script_->id()); |
144 : PermissionsData::ACCESS_DENIED; | 172 |
| 173 RoutingInfoMap& map = g_routing_info_map.Get(); |
| 174 auto iter = map.find(key); |
| 175 |
| 176 bool allowed = false; |
| 177 if (iter != map.end()) { |
| 178 allowed = iter->second; |
| 179 } else { |
| 180 content::RenderThread::Get()->Send( |
| 181 new GuestViewHostMsg_CanExecuteContentScript(routing_id, script_->id(), |
| 182 &allowed)); |
| 183 map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); |
| 184 } |
| 185 |
| 186 return allowed ? PermissionsData::ACCESS_ALLOWED |
| 187 : PermissionsData::ACCESS_DENIED; |
145 } | 188 } |
146 | 189 |
147 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 190 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
148 UserScript::RunLocation run_location) const { | 191 UserScript::RunLocation run_location) const { |
149 DCHECK_EQ(script_->run_location(), run_location); | 192 DCHECK_EQ(script_->run_location(), run_location); |
150 | 193 |
151 std::vector<blink::WebScriptSource> sources; | 194 std::vector<blink::WebScriptSource> sources; |
152 const UserScript::FileList& js_scripts = script_->js_scripts(); | 195 const UserScript::FileList& js_scripts = script_->js_scripts(); |
153 bool is_standalone_or_emulate_greasemonkey = | 196 bool is_standalone_or_emulate_greasemonkey = |
154 script_->is_standalone() || script_->emulate_greasemonkey(); | 197 script_->is_standalone() || script_->emulate_greasemonkey(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 255 |
213 void UserScriptInjector::OnInjectionComplete( | 256 void UserScriptInjector::OnInjectionComplete( |
214 scoped_ptr<base::ListValue> execution_results, | 257 scoped_ptr<base::ListValue> execution_results, |
215 UserScript::RunLocation run_location) { | 258 UserScript::RunLocation run_location) { |
216 } | 259 } |
217 | 260 |
218 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { | 261 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { |
219 } | 262 } |
220 | 263 |
221 } // namespace extensions | 264 } // namespace extensions |
OLD | NEW |