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; | |
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
| |
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 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 | 150 |
125 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( | 151 PermissionsData::AccessType UserScriptInjector::CanExecuteOnFrame( |
126 const InjectionHost* injection_host, | 152 const InjectionHost* injection_host, |
127 blink::WebFrame* web_frame, | 153 blink::WebFrame* web_frame, |
128 int tab_id, | 154 int tab_id, |
129 const GURL& top_url) const { | 155 const GURL& top_url) const { |
130 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 156 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
131 web_frame, web_frame->document().url(), script_->match_about_blank()); | 157 web_frame, web_frame->document().url(), script_->match_about_blank()); |
132 PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame( | 158 PermissionsData::AccessType can_execute = injection_host->CanExecuteOnFrame( |
133 effective_document_url, top_url, tab_id, is_declarative_); | 159 effective_document_url, top_url, tab_id, is_declarative_); |
134 | |
135 if (script_->consumer_instance_type() != | 160 if (script_->consumer_instance_type() != |
136 UserScript::ConsumerInstanceType::WEBVIEW || | 161 UserScript::ConsumerInstanceType::WEBVIEW || |
137 can_execute == PermissionsData::ACCESS_DENIED) | 162 can_execute == PermissionsData::ACCESS_DENIED) |
138 return can_execute; | 163 return can_execute; |
139 | 164 |
140 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) | 165 int routing_id = content::RenderView::FromWebView(web_frame->top()->view()) |
141 ->GetRoutingID(); | 166 ->GetRoutingID(); |
142 return script_->routing_info().render_view_id == routing_id | 167 |
143 ? PermissionsData::ACCESS_ALLOWED | 168 RoutingInfoKey key(routing_id, script_->id()); |
144 : PermissionsData::ACCESS_DENIED; | 169 |
170 RoutingInfoMap& map = g_routing_info_map.Get(); | |
171 auto iter = map.find(key); | |
172 | |
173 bool allowed = false; | |
174 if (iter != map.end()) { | |
175 allowed = iter->second; | |
176 } else { | |
177 content::RenderThread::Get()->Send( | |
178 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.
| |
179 &allowed)); | |
180 map.insert(std::pair<RoutingInfoKey, bool>(key, allowed)); | |
181 } | |
182 | |
183 return allowed ? PermissionsData::ACCESS_ALLOWED | |
184 : PermissionsData::ACCESS_DENIED; | |
145 } | 185 } |
146 | 186 |
147 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( | 187 std::vector<blink::WebScriptSource> UserScriptInjector::GetJsSources( |
148 UserScript::RunLocation run_location) const { | 188 UserScript::RunLocation run_location) const { |
149 DCHECK_EQ(script_->run_location(), run_location); | 189 DCHECK_EQ(script_->run_location(), run_location); |
150 | 190 |
151 std::vector<blink::WebScriptSource> sources; | 191 std::vector<blink::WebScriptSource> sources; |
152 const UserScript::FileList& js_scripts = script_->js_scripts(); | 192 const UserScript::FileList& js_scripts = script_->js_scripts(); |
153 bool is_standalone_or_emulate_greasemonkey = | 193 bool is_standalone_or_emulate_greasemonkey = |
154 script_->is_standalone() || script_->emulate_greasemonkey(); | 194 script_->is_standalone() || script_->emulate_greasemonkey(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 252 |
213 void UserScriptInjector::OnInjectionComplete( | 253 void UserScriptInjector::OnInjectionComplete( |
214 scoped_ptr<base::ListValue> execution_results, | 254 scoped_ptr<base::ListValue> execution_results, |
215 UserScript::RunLocation run_location) { | 255 UserScript::RunLocation run_location) { |
216 } | 256 } |
217 | 257 |
218 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { | 258 void UserScriptInjector::OnWillNotInject(InjectFailureReason reason) { |
219 } | 259 } |
220 | 260 |
221 } // namespace extensions | 261 } // namespace extensions |
OLD | NEW |