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

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

Issue 959413003: Implement <webview>.addContentScript/removeContentScript API [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments. Created 5 years, 8 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"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 continue; 61 continue;
62 DCHECK(!(*iter)->extension_id().empty()); 62 DCHECK(!(*iter)->extension_id().empty());
63 ids->insert((*iter)->extension_id()); 63 ids->insert((*iter)->extension_id());
64 } 64 }
65 } 65 }
66 66
67 void UserScriptSet::GetInjections( 67 void UserScriptSet::GetInjections(
68 ScopedVector<ScriptInjection>* injections, 68 ScopedVector<ScriptInjection>* injections,
69 blink::WebFrame* web_frame, 69 blink::WebFrame* web_frame,
70 int tab_id, 70 int tab_id,
71 UserScript::RunLocation run_location) { 71 UserScript::RunLocation run_location,
72 bool has_guest_content_script) {
72 GURL document_url = GetDocumentUrlForFrame(web_frame); 73 GURL document_url = GetDocumentUrlForFrame(web_frame);
73 for (ScopedVector<UserScript>::const_iterator iter = scripts_.begin(); 74 for (ScopedVector<UserScript>::const_iterator iter = scripts_.begin();
74 iter != scripts_.end(); 75 iter != scripts_.end();
75 ++iter) { 76 ++iter) {
76 scoped_ptr<ScriptInjection> injection = GetInjectionForScript( 77 scoped_ptr<ScriptInjection> injection = GetInjectionForScript(
77 *iter, 78 *iter,
78 web_frame, 79 web_frame,
79 tab_id, 80 tab_id,
80 run_location, 81 run_location,
81 document_url, 82 document_url,
82 false /* is_declarative */); 83 false /* is_declarative */,
84 has_guest_content_script);
83 if (injection.get()) 85 if (injection.get())
84 injections->push_back(injection.release()); 86 injections->push_back(injection.release());
85 } 87 }
86 } 88 }
87 89
88 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory, 90 bool UserScriptSet::UpdateUserScripts(base::SharedMemoryHandle shared_memory,
89 const std::set<HostID>& changed_hosts) { 91 const std::set<HostID>& changed_hosts) {
90 bool only_inject_incognito = 92 bool only_inject_incognito =
91 ExtensionsRendererClient::Get()->IsIncognitoProcess(); 93 ExtensionsRendererClient::Get()->IsIncognitoProcess();
92 94
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 const GURL& document_url) { 159 const GURL& document_url) {
158 for (ScopedVector<UserScript>::const_iterator it = scripts_.begin(); 160 for (ScopedVector<UserScript>::const_iterator it = scripts_.begin();
159 it != scripts_.end(); 161 it != scripts_.end();
160 ++it) { 162 ++it) {
161 if ((*it)->id() == script_id) { 163 if ((*it)->id() == script_id) {
162 return GetInjectionForScript(*it, 164 return GetInjectionForScript(*it,
163 web_frame, 165 web_frame,
164 tab_id, 166 tab_id,
165 run_location, 167 run_location,
166 document_url, 168 document_url,
167 true /* is_declarative */); 169 true /* is_declarative */,
170 false /*has_guest_content_script */);
168 } 171 }
169 } 172 }
170 return scoped_ptr<ScriptInjection>(); 173 return scoped_ptr<ScriptInjection>();
171 } 174 }
172 175
173 // TODO(dcheng): Scripts can't be injected on a remote frame, so this function 176 // TODO(dcheng): Scripts can't be injected on a remote frame, so this function
174 // signature needs to be updated. 177 // signature needs to be updated.
175 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript( 178 scoped_ptr<ScriptInjection> UserScriptSet::GetInjectionForScript(
176 UserScript* script, 179 UserScript* script,
177 blink::WebFrame* web_frame, 180 blink::WebFrame* web_frame,
178 int tab_id, 181 int tab_id,
179 UserScript::RunLocation run_location, 182 UserScript::RunLocation run_location,
180 const GURL& document_url, 183 const GURL& document_url,
181 bool is_declarative) { 184 bool is_declarative,
185 bool has_guest_content_script) {
182 scoped_ptr<ScriptInjection> injection; 186 scoped_ptr<ScriptInjection> injection;
183 scoped_ptr<const InjectionHost> injection_host; 187 scoped_ptr<const InjectionHost> injection_host;
184 188
185 const HostID& host_id = script->host_id(); 189 const HostID& host_id = script->host_id();
186 if (host_id.type() == HostID::EXTENSIONS) { 190 if (host_id.type() == HostID::EXTENSIONS) {
187 injection_host = ExtensionInjectionHost::Create(host_id.id(), extensions_); 191 injection_host = ExtensionInjectionHost::Create(host_id.id(), extensions_);
188 if (!injection_host) 192 if (!injection_host)
189 return injection.Pass(); 193 return injection.Pass();
190 } else { 194 } else {
191 DCHECK_EQ(host_id.type(), HostID::WEBUI); 195 DCHECK_EQ(host_id.type(), HostID::WEBUI);
192 injection_host.reset(new WebUIInjectionHost(host_id)); 196 injection_host.reset(new WebUIInjectionHost(host_id));
193 } 197 }
194 198
195 if (web_frame->parent() && !script->match_all_frames()) 199 if (web_frame->parent() && !script->match_all_frames())
196 return injection.Pass(); // Only match subframes if the script declared it. 200 return injection.Pass(); // Only match subframes if the script declared it.
197 201
198 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( 202 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL(
199 web_frame, document_url, script->match_about_blank()); 203 web_frame, document_url, script->match_about_blank());
200 204
201 if (!script->MatchesURL(effective_document_url)) 205 if (!script->MatchesURL(effective_document_url))
202 return injection.Pass(); 206 return injection.Pass();
203 207
204 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(script, 208 scoped_ptr<ScriptInjector> injector(new UserScriptInjector(
205 this, 209 script,
206 is_declarative)); 210 this,
211 is_declarative,
212 has_guest_content_script));
207 213
208 blink::WebFrame* top_frame = web_frame->top(); 214 blink::WebFrame* top_frame = web_frame->top();
209 // It doesn't make sense to do script injection for remote frames, since they 215 // It doesn't make sense to do script injection for remote frames, since they
210 // cannot host any documents or content. 216 // cannot host any documents or content.
211 // TODO(kalman): Fix this properly by moving all security checks into the 217 // TODO(kalman): Fix this properly by moving all security checks into the
212 // browser. See http://crbug.com/466373 for ongoing work here. 218 // browser. See http://crbug.com/466373 for ongoing work here.
213 if (top_frame->isWebRemoteFrame()) 219 if (top_frame->isWebRemoteFrame())
214 return injection.Pass(); 220 return injection.Pass();
215 221
216 if (injector->CanExecuteOnFrame(injection_host.get(), web_frame, 222 if (injector->CanExecuteOnFrame(injection_host.get(), web_frame,
(...skipping 12 matching lines...) Expand all
229 injector.Pass(), 235 injector.Pass(),
230 web_frame->toWebLocalFrame(), 236 web_frame->toWebLocalFrame(),
231 injection_host.Pass(), 237 injection_host.Pass(),
232 run_location, 238 run_location,
233 tab_id)); 239 tab_id));
234 } 240 }
235 return injection.Pass(); 241 return injection.Pass();
236 } 242 }
237 243
238 } // namespace extensions 244 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698