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_view.h" | 11 #include "content/public/renderer/render_view.h" |
12 #include "extensions/common/extension.h" | 12 #include "extensions/common/extension.h" |
13 #include "extensions/common/guest_view/guest_view_messages.h" | |
13 #include "extensions/common/permissions/permissions_data.h" | 14 #include "extensions/common/permissions/permissions_data.h" |
14 #include "extensions/renderer/injection_host.h" | 15 #include "extensions/renderer/injection_host.h" |
15 #include "extensions/renderer/script_context.h" | 16 #include "extensions/renderer/script_context.h" |
16 #include "extensions/renderer/scripts_run_info.h" | 17 #include "extensions/renderer/scripts_run_info.h" |
17 #include "grit/extensions_renderer_resources.h" | 18 #include "grit/extensions_renderer_resources.h" |
18 #include "third_party/WebKit/public/web/WebDocument.h" | 19 #include "third_party/WebKit/public/web/WebDocument.h" |
19 #include "third_party/WebKit/public/web/WebFrame.h" | 20 #include "third_party/WebKit/public/web/WebFrame.h" |
20 #include "third_party/WebKit/public/web/WebScriptSource.h" | 21 #include "third_party/WebKit/public/web/WebScriptSource.h" |
21 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
23 | 24 |
24 namespace extensions { | 25 namespace extensions { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
29 struct RoutingInfoKey { | |
30 int routing_id; | |
31 int script_id; | |
32 | |
33 bool operator<(const RoutingInfoKey& other) const { | |
34 if (routing_id != other.routing_id) | |
35 return routing_id < other.routing_id; | |
36 | |
37 if (script_id != other.script_id) | |
38 return script_id < other.script_id; | |
39 return false; // keys are equal. | |
40 } | |
41 }; | |
42 | |
43 using RoutingInfoMap = std::map<RoutingInfoKey, bool>; | |
44 | |
28 // These two strings are injected before and after the Greasemonkey API and | 45 // These two strings are injected before and after the Greasemonkey API and |
29 // user script to wrap it in an anonymous scope. | 46 // user script to wrap it in an anonymous scope. |
30 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; | 47 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; |
31 const char kUserScriptTail[] = "\n})(window);"; | 48 const char kUserScriptTail[] = "\n})(window);"; |
32 | 49 |
50 base::LazyInstance<RoutingInfoMap> g_routing_info_map = | |
51 LAZY_INSTANCE_INITIALIZER; | |
52 | |
33 // Greasemonkey API source that is injected with the scripts. | 53 // Greasemonkey API source that is injected with the scripts. |
34 struct GreasemonkeyApiJsString { | 54 struct GreasemonkeyApiJsString { |
35 GreasemonkeyApiJsString(); | 55 GreasemonkeyApiJsString(); |
36 blink::WebScriptSource GetSource() const; | 56 blink::WebScriptSource GetSource() const; |
37 | 57 |
38 private: | 58 private: |
39 std::string source_; | 59 std::string source_; |
40 }; | 60 }; |
41 | 61 |
42 // The below constructor, monstrous as it is, just makes a WebScriptSource from | 62 // The below constructor, monstrous as it is, just makes a WebScriptSource from |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 | 144 |
125 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( | 145 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( |
126 const InjectionHost* injection_host, | 146 const InjectionHost* injection_host, |
127 blink::WebFrame* web_frame, | 147 blink::WebFrame* web_frame, |
128 int tab_id, | 148 int tab_id, |
129 const GURL& top_url) const { | 149 const GURL& top_url) const { |
130 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 150 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
131 web_frame, web_frame->document().url(), script_->match_about_blank()); | 151 web_frame, web_frame->document().url(), script_->match_about_blank()); |
132 PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame( | 152 PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame( |
133 effective_document_url, top_url, tab_id, is_declarative_); | 153 effective_document_url, top_url, tab_id, is_declarative_); |
134 | |
135 if (script_->consumer_instance_type() != | 154 if (script_->consumer_instance_type() != |
136 UserScript::ConsumerInstanceType::WEBVIEW || | 155 UserScript::ConsumerInstanceType::WEBVIEW || |
137 can_execute == PermissionsData::ACCESS_DENIED) | 156 can_execute == PermissionsData::ACCESS_DENIED) |
138 return can_execute; | 157 return can_execute; |
139 | 158 |
140 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) | 159 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) |
141 ->GetRoutingID(); | 160 ->GetRoutingID(); |
142 return script_->routing_info().render_view_id == routing_id | 161 |
143 ? PermissionsData::ACCESS_ALLOWED | 162 RoutingInfoKey key; |
Fady Samuel
2015/03/27 20:25:36
Give it a constructor:
RoutingInfoKey key(routing
Xi Han
2015/03/30 17:50:49
Done.
| |
144 : PermissionsData::ACCESS_DENIED; | 163 key.routing_id = routing_id; |
164 key.script_id = script_->id(); | |
165 | |
166 RoutingInfoMap& map = g_routing_info_map.Get(); | |
167 RoutingInfoMap::iterator iter = map.find(key); | |
Fady Samuel
2015/03/27 20:25:36
nit: auto
Xi Han
2015/03/30 17:50:49
Done.
| |
168 | |
169 bool allowed = false; | |
170 if (iter != map.end()) { | |
171 allowed = iter->second; | |
172 } else { | |
173 auto rdv = content::RenderView::FromWebView(web_frame->top()->view()); | |
174 rdv->Send(new GuestViewHostMsg_CanExecuteContentScript( | |
Fady Samuel
2015/03/27 20:25:36
I don't think this is necessary:
content::RenderT
Xi Han
2015/03/30 17:50:49
Done.
| |
175 routing_id, script_->id(), &allowed)); | |
176 map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); | |
177 } | |
178 | |
179 return allowed ? PermissionsData::ACCESS_ALLOWED | |
180 : PermissionsData::ACCESS_DENIED; | |
145 } | 181 } |
146 | 182 |
147 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 183 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
148 UserScript::RunLocation run_location) const { | 184 UserScript::RunLocation run_location) const { |
149 DCHECK_EQ(script_->run_location(), run_location); | 185 DCHECK_EQ(script_->run_location(), run_location); |
150 | 186 |
151 std::vector<blink::WebScriptSource> sources; | 187 std::vector<blink::WebScriptSource> sources; |
152 const UserScript::FileList& js_scripts = script_->js_scripts(); | 188 const UserScript::FileList& js_scripts = script_->js_scripts(); |
153 bool is_standalone_or_emulate_greasemonkey = | 189 bool is_standalone_or_emulate_greasemonkey = |
154 script_->is_standalone() || script_->emulate_greasemonkey(); | 190 script_->is_standalone() || script_->emulate_greasemonkey(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 248 |
213 void UserScriptInjector::OnInjectionComplete( | 249 void UserScriptInjector::OnInjectionComplete( |
214 scoped_ptr<base::ListValue> execution_results, | 250 scoped_ptr<base::ListValue> execution_results, |
215 UserScript::RunLocation run_location) { | 251 UserScript::RunLocation run_location) { |
216 } | 252 } |
217 | 253 |
218 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { | 254 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { |
219 } | 255 } |
220 | 256 |
221 } // namespace extensions | 257 } // namespace extensions |
OLD | NEW |