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()); | |
73 if (!extension) | |
74 continue; | |
75 scoped_ptr<ScriptInjection> injection = GetInjectionForScript( | 76 scoped_ptr<ScriptInjection> injection = GetInjectionForScript( |
76 *iter, | 77 *iter, |
77 web_frame, | 78 web_frame, |
78 tab_id, | 79 tab_id, |
79 run_location, | 80 run_location, |
80 document_url, | 81 document_url, |
81 extension, | |
82 false /* is_declarative */); | 82 false /* is_declarative */); |
83 if (injection.get()) | 83 if (injection.get()) |
84 injections->push_back(injection.release()); | 84 injections->push_back(injection.release()); |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 bool UserScriptSet::UpdateUserScripts( | 88 bool UserScriptSet::UpdateUserScripts( |
89 base::SharedMemoryHandle shared_memory, | 89 base::SharedMemoryHandle shared_memory, |
90 const std::set<std::string>& changed_extensions) { | 90 const std::set<std::string>& changed_extensions) { |
91 bool only_inject_incognito = | 91 bool only_inject_incognito = |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 observers_, | 148 observers_, |
149 OnUserScriptsUpdated(changed_extensions, scripts_.get())); | 149 OnUserScriptsUpdated(changed_extensions, scripts_.get())); |
150 return true; | 150 return true; |
151 } | 151 } |
152 | 152 |
153 scoped_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( | 153 scoped_ptr<ScriptInjection> UserScriptSet::GetDeclarativeScriptInjection( |
154 int script_id, | 154 int script_id, |
155 blink::WebFrame* web_frame, | 155 blink::WebFrame* web_frame, |
156 int tab_id, | 156 int tab_id, |
157 UserScript::RunLocation run_location, | 157 UserScript::RunLocation run_location, |
158 const GURL& document_url, | 158 const GURL& document_url) { |
159 const Extension* extension) { | |
160 for (ScopedVector<UserScript>::const_iterator it = scripts_.begin(); | 159 for (ScopedVector<UserScript>::const_iterator it = scripts_.begin(); |
161 it != scripts_.end(); | 160 it != scripts_.end(); |
162 ++it) { | 161 ++it) { |
163 if ((*it)->id() == script_id) { | 162 if ((*it)->id() == script_id) { |
164 return GetInjectionForScript(*it, | 163 return GetInjectionForScript(*it, |
165 web_frame, | 164 web_frame, |
166 tab_id, | 165 tab_id, |
167 run_location, | 166 run_location, |
168 document_url, | 167 document_url, |
169 extension, | |
170 true /* is_declarative */); | 168 true /* is_declarative */); |
171 } | 169 } |
172 } | 170 } |
173 return scoped_ptr<ScriptInjection>(); | 171 return scoped_ptr<ScriptInjection>(); |
174 } | 172 } |
175 | 173 |
176 // TODO(dcheng): Scripts can't be injected on a remote frame, so this function | 174 // TODO(dcheng): Scripts can't be injected on a remote frame, so this function |
177 // signature needs to be updated. | 175 // signature needs to be updated. |
178 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( | 176 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( |
179 UserScript* script, | 177 UserScript* script, |
180 blink::WebFrame* web_frame, | 178 blink::WebFrame* web_frame, |
181 int tab_id, | 179 int tab_id, |
182 UserScript::RunLocation run_location, | 180 UserScript::RunLocation run_location, |
183 const GURL& document_url, | 181 const GURL& document_url, |
184 const Extension* extension, | |
185 bool is_declarative) { | 182 bool is_declarative) { |
186 scoped_ptr<ScriptInjection> injection; | 183 scoped_ptr<ScriptInjection> injection; |
| 184 scoped_ptr<InjectionHost> injection_host; |
| 185 const HostID& host_id = script->host_id(); |
| 186 if (host_id.type() == HostID::EXTENSIONS) { |
| 187 const Extension* extension = extensions_->GetByID(host_id.id()); |
| 188 if (!extension) |
| 189 return injection.Pass(); |
| 190 injection_host.reset(new ExtensionInjectionHost(extension)); |
| 191 } else if (host_id.type() == HostID::WEBUI) { |
| 192 injection_host.reset(new WebUIInjectionHost(host_id)); |
| 193 } |
| 194 |
187 if (web_frame->parent() && !script->match_all_frames()) | 195 if (web_frame->parent() && !script->match_all_frames()) |
188 return injection.Pass(); // Only match subframes if the script declared it. | 196 return injection.Pass(); // Only match subframes if the script declared it. |
189 | 197 |
190 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( | 198 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( |
191 web_frame, document_url, script->match_about_blank()); | 199 web_frame, document_url, script->match_about_blank()); |
192 | 200 |
193 if (!script->MatchesURL(effective_document_url)) | 201 if (!script->MatchesURL(effective_document_url)) |
194 return injection.Pass(); | 202 return injection.Pass(); |
195 | 203 |
196 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, | 204 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, |
197 this, | 205 this, |
198 is_declarative)); | 206 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( | 207 if (injector->CanExecuteOnFrame( |
203 &extension_injection_host, | 208 injection_host.get(), |
204 web_frame, | 209 web_frame, |
205 -1, // Content scripts are not tab-specific. | 210 -1, // Content scripts are not tab-specific. |
206 web_frame->top()->document().url()) == | 211 web_frame->top()->document().url()) == |
207 PermissionsData::ACCESS_DENIED) { | 212 PermissionsData::ACCESS_DENIED) { |
208 return injection.Pass(); | 213 return injection.Pass(); |
209 } | 214 } |
210 | 215 |
211 bool inject_css = !script->css_scripts().empty() && | 216 bool inject_css = !script->css_scripts().empty() && |
212 run_location == UserScript::DOCUMENT_START; | 217 run_location == UserScript::DOCUMENT_START; |
213 bool inject_js = | 218 bool inject_js = |
214 !script->js_scripts().empty() && script->run_location() == run_location; | 219 !script->js_scripts().empty() && script->run_location() == run_location; |
215 if (inject_css || inject_js) { | 220 if (inject_css || inject_js) { |
216 injection.reset(new ScriptInjection( | 221 injection.reset(new ScriptInjection( |
217 injector.Pass(), | 222 injector.Pass(), |
218 web_frame->toWebLocalFrame(), | 223 web_frame->toWebLocalFrame(), |
219 host_id, | 224 injection_host.Pass(), |
220 script->consumer_instance_type(), | 225 script->consumer_instance_type(), |
221 run_location, | 226 run_location, |
222 tab_id)); | 227 tab_id)); |
223 } | 228 } |
224 return injection.Pass(); | 229 return injection.Pass(); |
225 } | 230 } |
226 | 231 |
227 } // namespace extensions | 232 } // namespace extensions |
OLD | NEW |