Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: extensions/renderer/user_script_set.cc

Issue 934763003: Refactoring: de-couple Extensions from "script injection System" [render side]:3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@decouple_brower_isolated_world_routingid_user_script_1
Patch Set: Rebase. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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<const InjectionHost> injection_host;
185
186 const HostID& host_id = script->host_id();
187 if (host_id.type() == HostID::EXTENSIONS) {
188 injection_host = ExtensionInjectionHost::Create(host_id.id(), extensions_);
189 if (!injection_host)
190 return injection.Pass();
191 } else {
192 DCHECK_EQ(host_id.type(), HostID::WEBUI);
193 injection_host.reset(new WebUIInjectionHost(host_id));
194 }
195
187 if (web_frame->parent() && !script->match_all_frames()) 196 if (web_frame->parent() && !script->match_all_frames())
188 return injection.Pass(); // Only match subframes if the script declared it. 197 return injection.Pass(); // Only match subframes if the script declared it.
189 198
190 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( 199 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL(
191 web_frame, document_url, script->match_about_blank()); 200 web_frame, document_url, script->match_about_blank());
192 201
193 if (!script->MatchesURL(effective_document_url)) 202 if (!script->MatchesURL(effective_document_url))
194 return injection.Pass(); 203 return injection.Pass();
195 204
196 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, 205 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script,
197 this, 206 this,
198 is_declarative)); 207 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( 208 if (injector->CanExecuteOnFrame(
203 &extension_injection_host, 209 injection_host.get(),
204 web_frame, 210 web_frame,
205 -1, // Content scripts are not tab-specific. 211 -1, // Content scripts are not tab-specific.
206 web_frame->top()->document().url()) == 212 web_frame->top()->document().url()) ==
207 PermissionsData::ACCESS_DENIED) { 213 PermissionsData::ACCESS_DENIED) {
208 return injection.Pass(); 214 return injection.Pass();
209 } 215 }
210 216
211 bool inject_css = !script->css_scripts().empty() && 217 bool inject_css = !script->css_scripts().empty() &&
212 run_location == UserScript::DOCUMENT_START; 218 run_location == UserScript::DOCUMENT_START;
213 bool inject_js = 219 bool inject_js =
214 !script->js_scripts().empty() && script->run_location() == run_location; 220 !script->js_scripts().empty() && script->run_location() == run_location;
215 if (inject_css || inject_js) { 221 if (inject_css || inject_js) {
216 injection.reset(new ScriptInjection( 222 injection.reset(new ScriptInjection(
217 injector.Pass(), 223 injector.Pass(),
218 web_frame->toWebLocalFrame(), 224 web_frame->toWebLocalFrame(),
219 host_id, 225 injection_host.Pass(),
220 run_location, 226 run_location,
221 tab_id)); 227 tab_id));
222 } 228 }
223 return injection.Pass(); 229 return injection.Pass();
224 } 230 }
225 231
226 } // namespace extensions 232 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698