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_set.h" | 5 #include "extensions/renderer/user_script_set.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "content/public/common/url_constants.h" | 8 #include "content/public/common/url_constants.h" |
9 #include "content/public/renderer/render_thread.h" | 9 #include "content/public/renderer/render_thread.h" |
10 #include "extensions/common/extension.h" | 10 #include "extensions/common/extension.h" |
11 #include "extensions/common/extension_set.h" | 11 #include "extensions/common/extension_set.h" |
12 #include "extensions/common/permissions/permissions_data.h" | 12 #include "extensions/common/permissions/permissions_data.h" |
13 #include "extensions/renderer/extension_injection_host.h" | 13 #include "extensions/renderer/extension_injection_host.h" |
14 #include "extensions/renderer/extensions_renderer_client.h" | 14 #include "extensions/renderer/extensions_renderer_client.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/script_injection.h" | 17 #include "extensions/renderer/script_injection.h" |
17 #include "extensions/renderer/user_script_injector.h" | 18 #include "extensions/renderer/user_script_injector.h" |
19 #include "extensions/renderer/web_ui_injection_host.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 "url/gurl.h" | 22 #include "url/gurl.h" |
21 | 23 |
22 namespace extensions { | 24 namespace extensions { |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 GURL GetDocumentUrlForFrame(blink::WebFrame* frame) { | 28 GURL GetDocumentUrlForFrame(blink::WebFrame* frame) { |
27 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); | 29 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); |
(...skipping 20 matching lines...) Expand all Loading... | |
48 | 50 |
49 void UserScriptSet::RemoveObserver(Observer* observer) { | 51 void UserScriptSet::RemoveObserver(Observer* observer) { |
50 observers_.RemoveObserver(observer); | 52 observers_.RemoveObserver(observer); |
51 } | 53 } |
52 | 54 |
53 void UserScriptSet::GetActiveExtensionIds( | 55 void UserScriptSet::GetActiveExtensionIds( |
54 std::set<std::string>* ids) const { | 56 std::set<std::string>* ids) const { |
55 for (ScopedVector<UserScript>::const_iterator iter = scripts_.begin(); | 57 for (ScopedVector<UserScript>::const_iterator iter = scripts_.begin(); |
56 iter != scripts_.end(); | 58 iter != scripts_.end(); |
57 ++iter) { | 59 ++iter) { |
60 if ((*iter)->host_id().type() != HostID::EXTENSIONS) | |
61 continue; | |
58 DCHECK(!(*iter)->extension_id().empty()); | 62 DCHECK(!(*iter)->extension_id().empty()); |
59 ids->insert((*iter)->extension_id()); | 63 ids->insert((*iter)->extension_id()); |
60 } | 64 } |
61 } | 65 } |
62 | 66 |
63 void UserScriptSet::GetInjections( | 67 void UserScriptSet::GetInjections( |
64 ScopedVector<ScriptInjection>* injections, | 68 ScopedVector<ScriptInjection>* injections, |
65 blink::WebFrame* web_frame, | 69 blink::WebFrame* web_frame, |
66 int tab_id, | 70 int tab_id, |
67 UserScript::RunLocation run_location) { | 71 UserScript::RunLocation run_location) { |
68 GURL document_url = GetDocumentUrlForFrame(web_frame); | 72 GURL document_url = GetDocumentUrlForFrame(web_frame); |
69 for (ScopedVector<UserScript>::const_iterator iter = scripts_.begin(); | 73 for (ScopedVector<UserScript>::const_iterator iter = scripts_.begin(); |
70 iter != scripts_.end(); | 74 iter != scripts_.end(); |
71 ++iter) { | 75 ++iter) { |
72 const Extension* extension = extensions_->GetByID((*iter)->extension_id()); | 76 scoped_ptr<InjectionHost> injection_host; |
73 if (!extension) | 77 const HostID& host_id = (*iter)->host_id(); |
74 continue; | 78 |
79 if (host_id.type() == HostID::EXTENSIONS) { | |
80 const Extension* extension = extensions_->GetByID(host_id.id()); | |
81 if (!extension) | |
82 continue; | |
83 injection_host.reset(new ExtensionInjectionHost(extension)); | |
84 } else if (host_id.type() == HostID::WEBUI) { | |
85 injection_host.reset(new WebUIInjectionHost(host_id)); | |
86 } | |
87 | |
75 scoped_ptr<ScriptInjection> injection = GetInjectionForScript( | 88 scoped_ptr<ScriptInjection> injection = GetInjectionForScript( |
76 *iter, | 89 *iter, |
77 web_frame, | 90 web_frame, |
78 tab_id, | 91 tab_id, |
79 run_location, | 92 run_location, |
80 document_url, | 93 document_url, |
81 extension, | 94 injection_host.get(), |
82 false /* is_declarative */); | 95 false /* is_declarative */); |
83 if (injection.get()) | 96 if (injection.get()) |
84 injections->push_back(injection.release()); | 97 injections->push_back(injection.release()); |
85 } | 98 } |
86 } | 99 } |
87 | 100 |
88 bool UserScriptSet::UpdateUserScripts( | 101 bool UserScriptSet::UpdateUserScripts( |
89 base::SharedMemoryHandle shared_memory, | 102 base::SharedMemoryHandle shared_memory, |
90 const std::set<std::string>& changed_extensions) { | 103 const std::set<std::string>& changed_extensions) { |
91 bool only_inject_incognito = | 104 bool only_inject_incognito = |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 OnUserScriptsUpdated(changed_extensions, scripts_.get())); | 162 OnUserScriptsUpdated(changed_extensions, scripts_.get())); |
150 return true; | 163 return true; |
151 } | 164 } |
152 | 165 |
153 scoped_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( | 166 scoped_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( |
154 int script_id, | 167 int script_id, |
155 blink::WebFrame* web_frame, | 168 blink::WebFrame* web_frame, |
156 int tab_id, | 169 int tab_id, |
157 UserScript::RunLocation run_location, | 170 UserScript::RunLocation run_location, |
158 const GURL& document_url, | 171 const GURL& document_url, |
159 const Extension* extension) { | 172 const InjectionHost* injection_host) { |
160 for (ScopedVector<UserScript>::const_iterator it = scripts_.begin(); | 173 for (ScopedVector<UserScript>::const_iterator it = scripts_.begin(); |
161 it != scripts_.end(); | 174 it != scripts_.end(); |
162 ++it) { | 175 ++it) { |
163 if ((*it)->id() == script_id) { | 176 if ((*it)->id() == script_id) { |
164 return GetInjectionForScript(*it, | 177 return GetInjectionForScript(*it, |
165 web_frame, | 178 web_frame, |
166 tab_id, | 179 tab_id, |
167 run_location, | 180 run_location, |
168 document_url, | 181 document_url, |
169 extension, | 182 injection_host, |
170 true /* is_declarative */); | 183 true /* is_declarative */); |
171 } | 184 } |
172 } | 185 } |
173 return scoped_ptr<ScriptInjection>(); | 186 return scoped_ptr<ScriptInjection>(); |
174 } | 187 } |
175 | 188 |
176 // TODO(dcheng): Scripts can't be injected on a remote frame, so this function | 189 // TODO(dcheng): Scripts can't be injected on a remote frame, so this function |
177 // signature needs to be updated. | 190 // signature needs to be updated. |
178 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( | 191 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( |
179 UserScript* script, | 192 UserScript* script, |
180 blink::WebFrame* web_frame, | 193 blink::WebFrame* web_frame, |
181 int tab_id, | 194 int tab_id, |
182 UserScript::RunLocation run_location, | 195 UserScript::RunLocation run_location, |
183 const GURL& document_url, | 196 const GURL& document_url, |
184 const Extension* extension, | 197 const InjectionHost* injection_host, |
Devlin
2015/02/17 18:56:52
We should probably just create the injection host
Xi Han
2015/02/18 21:11:46
Done.
| |
185 bool is_declarative) { | 198 bool is_declarative) { |
186 scoped_ptr<ScriptInjection> injection; | 199 scoped_ptr<ScriptInjection> injection; |
187 if (web_frame->parent() && !script->match_all_frames()) | 200 if (web_frame->parent() && !script->match_all_frames()) |
188 return injection.Pass(); // Only match subframes if the script declared it. | 201 return injection.Pass(); // Only match subframes if the script declared it. |
189 | 202 |
190 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 203 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
191 web_frame, document_url, script->match_about_blank()); | 204 web_frame, document_url, script->match_about_blank()); |
192 | 205 |
193 if (!script->MatchesURL(effective_document_url)) | 206 if (!script->MatchesURL(effective_document_url)) |
194 return injection.Pass(); | 207 return injection.Pass(); |
195 | 208 |
196 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, | 209 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, |
197 this, | 210 this, |
198 is_declarative)); | 211 is_declarative)); |
199 HostID host_id(HostID::EXTENSIONS, extension->id()); | |
200 ExtensionInjectionHost extension_injection_host( | |
201 make_scoped_refptr<const Extension>(extension)); | |
202 if (injector->CanExecuteOnFrame( | 212 if (injector->CanExecuteOnFrame( |
203 &extension_injection_host, | 213 injection_host, |
204 web_frame, | 214 web_frame, |
205 -1, // Content scripts are not tab-specific. | 215 -1, // Content scripts are not tab-specific. |
206 web_frame->top()->document().url()) == | 216 web_frame->top()->document().url()) == |
207 PermissionsData::ACCESS_DENIED) { | 217 PermissionsData::ACCESS_DENIED) { |
208 return injection.Pass(); | 218 return injection.Pass(); |
209 } | 219 } |
210 | 220 |
211 bool inject_css = !script->css_scripts().empty() && | 221 bool inject_css = !script->css_scripts().empty() && |
212 run_location == UserScript::DOCUMENT_START; | 222 run_location == UserScript::DOCUMENT_START; |
213 bool inject_js = | 223 bool inject_js = |
214 !script->js_scripts().empty() && script->run_location() == run_location; | 224 !script->js_scripts().empty() && script->run_location() == run_location; |
215 if (inject_css || inject_js) { | 225 if (inject_css || inject_js) { |
216 injection.reset(new ScriptInjection( | 226 injection.reset(new ScriptInjection( |
217 injector.Pass(), | 227 injector.Pass(), |
218 web_frame->toWebLocalFrame(), | 228 web_frame->toWebLocalFrame(), |
219 host_id, | 229 make_scoped_ptr(injection_host), |
220 script->consumer_instance_type(), | 230 script->consumer_instance_type(), |
221 run_location, | 231 run_location, |
222 tab_id)); | 232 tab_id)); |
223 } | 233 } |
224 return injection.Pass(); | 234 return injection.Pass(); |
225 } | 235 } |
226 | 236 |
227 } // namespace extensions | 237 } // namespace extensions |
OLD | NEW |