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

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

Issue 348313003: Create withheld permissions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test fix Created 6 years, 5 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 | Annotate | Revision Log
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/script_injection.h" 5 #include "extensions/renderer/script_injection.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 22 matching lines...) Expand all
33 33
34 typedef std::map<std::string, int> IsolatedWorldMap; 34 typedef std::map<std::string, int> IsolatedWorldMap;
35 base::LazyInstance<IsolatedWorldMap> g_isolated_worlds = 35 base::LazyInstance<IsolatedWorldMap> g_isolated_worlds =
36 LAZY_INSTANCE_INITIALIZER; 36 LAZY_INSTANCE_INITIALIZER;
37 37
38 const int kInvalidRequestId = -1; 38 const int kInvalidRequestId = -1;
39 39
40 // The id of the next pending injection. 40 // The id of the next pending injection.
41 int64 g_next_pending_id = 0; 41 int64 g_next_pending_id = 0;
42 42
43 bool ShouldDelayForPermission() { 43 bool NotifyBrowserOfInjections() {
not at google - send to devlin 2014/06/27 23:24:34 this should start with Should otherwise it sounds
Devlin 2014/06/30 17:06:11 Done.
44 return FeatureSwitch::scripts_require_action()->IsEnabled(); 44 return !FeatureSwitch::scripts_require_action()->IsEnabled();
45 } 45 }
46 46
47 // Append all the child frames of |parent_frame| to |frames_vector|. 47 // Append all the child frames of |parent_frame| to |frames_vector|.
48 void AppendAllChildFrames(blink::WebFrame* parent_frame, 48 void AppendAllChildFrames(blink::WebFrame* parent_frame,
49 std::vector<blink::WebFrame*>* frames_vector) { 49 std::vector<blink::WebFrame*>* frames_vector) {
50 DCHECK(parent_frame); 50 DCHECK(parent_frame);
51 for (blink::WebFrame* child_frame = parent_frame->firstChild(); child_frame; 51 for (blink::WebFrame* child_frame = parent_frame->firstChild(); child_frame;
52 child_frame = child_frame->nextSibling()) { 52 child_frame = child_frame->nextSibling()) {
53 frames_vector->push_back(child_frame); 53 frames_vector->push_back(child_frame);
54 AppendAllChildFrames(child_frame, frames_vector); 54 AppendAllChildFrames(child_frame, frames_vector);
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 if (request_id_ != -1) 138 if (request_id_ != -1)
139 return false; // We're waiting for permission right now, try again later. 139 return false; // We're waiting for permission right now, try again later.
140 140
141 if (!extension) { 141 if (!extension) {
142 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); 142 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED);
143 return true; // We're done. 143 return true; // We're done.
144 } 144 }
145 145
146 switch (injector_->CanExecuteOnFrame( 146 switch (injector_->CanExecuteOnFrame(
147 extension, web_frame_, tab_id_, web_frame_->top()->document().url())) { 147 extension, web_frame_, tab_id_, web_frame_->top()->document().url())) {
148 case ScriptInjector::DENY_ACCESS: 148 case PermissionsData::DENY_ACCESS:
149 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED); 149 NotifyWillNotInject(ScriptInjector::NOT_ALLOWED);
150 return true; // We're done. 150 return true; // We're done.
151 case ScriptInjector::REQUEST_ACCESS: 151 case PermissionsData::REQUEST_ACCESS:
152 RequestPermission(); 152 RequestPermission();
153 if (ShouldDelayForPermission()) 153 return false; // Wait around for permission.
154 return false; // Wait around for permission. 154 case PermissionsData::ALLOW_ACCESS:
not at google - send to devlin 2014/06/27 23:24:34 I think it's fine if this class just interprets wh
Devlin 2014/06/30 17:06:11 Done.
155 // else fall through
156 case ScriptInjector::ALLOW_ACCESS:
157 Inject(extension, scripts_run_info); 155 Inject(extension, scripts_run_info);
158 return true; // We're done! 156 return true; // We're done!
159 } 157 }
160 158
161 // Some compilers don't realize that we always return from the switch() above. 159 // Some compilers don't realize that we always return from the switch() above.
162 // Make them happy. 160 // Make them happy.
163 return false; 161 return false;
164 } 162 }
165 163
166 bool ScriptInjection::OnPermissionGranted(const Extension* extension, 164 bool ScriptInjection::OnPermissionGranted(const Extension* extension,
167 ScriptsRunInfo* scripts_run_info) { 165 ScriptsRunInfo* scripts_run_info) {
168 if (!extension) { 166 if (!extension) {
169 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED); 167 NotifyWillNotInject(ScriptInjector::EXTENSION_REMOVED);
170 return false; 168 return false;
171 } 169 }
172 170
173 Inject(extension, scripts_run_info); 171 Inject(extension, scripts_run_info);
174 return true; 172 return true;
175 } 173 }
176 174
177 void ScriptInjection::RequestPermission() { 175 void ScriptInjection::RequestPermission() {
178 content::RenderView* render_view = 176 content::RenderView* render_view =
179 content::RenderView::FromWebView(web_frame()->top()->view()); 177 content::RenderView::FromWebView(web_frame()->top()->view());
180 178
181 // If the feature to delay for permission isn't enabled, then just send an 179 // If we are just notifying the browser of the injection, then send an
182 // invalid request (which is treated like a notification). 180 // invalid request (which is treated like a notification).
183 request_id_ = 181 request_id_ = NotifyBrowserOfInjections() ? kInvalidRequestId
184 ShouldDelayForPermission() ? g_next_pending_id++ : kInvalidRequestId; 182 : g_next_pending_id++;
185 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission( 183 render_view->Send(new ExtensionHostMsg_RequestScriptInjectionPermission(
186 render_view->GetRoutingID(), 184 render_view->GetRoutingID(),
187 extension_id_, 185 extension_id_,
186 injector_->script_type(),
not at google - send to devlin 2014/06/27 23:24:34 I think that exposing the "script type" isn't quit
Devlin 2014/06/30 17:06:11 My thought was that it's not unlikely (and perhaps
not at google - send to devlin 2014/07/01 00:28:35 maybe. but we don't need it now, and like I said i
not at google - send to devlin 2014/07/01 02:56:50 I should explain more. My "wrong idiom" comment i
Devlin 2014/07/01 16:27:05 Yeah, constants.h is kind of a "I give up" locatio
not at google - send to devlin 2014/07/01 17:02:11 ok, let's throw it in UserScript. then maybe pull
Devlin 2014/07/01 18:34:08 Done.
188 render_view->GetPageId(), 187 render_view->GetPageId(),
189 request_id_)); 188 request_id_));
190 } 189 }
191 190
192 void ScriptInjection::NotifyWillNotInject( 191 void ScriptInjection::NotifyWillNotInject(
193 ScriptInjector::InjectFailureReason reason) { 192 ScriptInjector::InjectFailureReason reason) {
194 complete_ = true; 193 complete_ = true;
195 injector_->OnWillNotInject(reason); 194 injector_->OnWillNotInject(reason);
196 } 195 }
197 196
198 void ScriptInjection::Inject(const Extension* extension, 197 void ScriptInjection::Inject(const Extension* extension,
199 ScriptsRunInfo* scripts_run_info) { 198 ScriptsRunInfo* scripts_run_info) {
200 DCHECK(extension); 199 DCHECK(extension);
201 DCHECK(scripts_run_info); 200 DCHECK(scripts_run_info);
202 DCHECK(!complete_); 201 DCHECK(!complete_);
203 202
203 if (NotifyBrowserOfInjections())
204 RequestPermission();
205
204 std::vector<blink::WebFrame*> frame_vector; 206 std::vector<blink::WebFrame*> frame_vector;
205 frame_vector.push_back(web_frame_); 207 frame_vector.push_back(web_frame_);
206 if (injector_->ShouldExecuteInChildFrames()) 208 if (injector_->ShouldExecuteInChildFrames())
207 AppendAllChildFrames(web_frame_, &frame_vector); 209 AppendAllChildFrames(web_frame_, &frame_vector);
208 210
209 scoped_ptr<blink::WebScopedUserGesture> gesture; 211 scoped_ptr<blink::WebScopedUserGesture> gesture;
210 if (injector_->IsUserGesture()) 212 if (injector_->IsUserGesture())
211 gesture.reset(new blink::WebScopedUserGesture()); 213 gesture.reset(new blink::WebScopedUserGesture());
212 214
213 bool inject_js = injector_->ShouldInjectJs(run_location_); 215 bool inject_js = injector_->ShouldInjectJs(run_location_);
214 bool inject_css = injector_->ShouldInjectCss(run_location_); 216 bool inject_css = injector_->ShouldInjectCss(run_location_);
215 DCHECK(inject_js || inject_css); 217 DCHECK(inject_js || inject_css);
216 218
217 scoped_ptr<base::ListValue> execution_results(new base::ListValue()); 219 scoped_ptr<base::ListValue> execution_results(new base::ListValue());
218 GURL top_url = web_frame_->top()->document().url(); 220 GURL top_url = web_frame_->top()->document().url();
219 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin(); 221 for (std::vector<blink::WebFrame*>::iterator iter = frame_vector.begin();
220 iter != frame_vector.end(); 222 iter != frame_vector.end();
221 ++iter) { 223 ++iter) {
222 blink::WebFrame* frame = *iter; 224 blink::WebFrame* frame = *iter;
223 225
224 // We recheck access here in the renderer for extra safety against races 226 // We recheck access here in the renderer for extra safety against races
225 // with navigation, but different frames can have different URLs, and the 227 // with navigation, but different frames can have different URLs, and the
226 // extension might only have access to a subset of them. 228 // extension might only have access to a subset of them.
227 // For child frames, we just skip ones the extension doesn't have access 229 // For child frames, we just skip ones the extension doesn't have access
228 // to and carry on. 230 // to and carry on.
229 // Note: we don't consider REQUEST_ACCESS because there is nowhere to 231 // Note: we don't consider REQUEST_ACCESS because there is nowhere to
230 // surface a request for a child frame. 232 // surface a request for a child frame.
231 // TODO(rdevlin.cronin): We should ask for permission somehow. 233 // TODO(rdevlin.cronin): We should ask for permission somehow.
232 if (injector_->CanExecuteOnFrame(extension, frame, tab_id_, top_url) == 234 if (injector_->CanExecuteOnFrame(extension, frame, tab_id_, top_url) ==
233 ScriptInjector::DENY_ACCESS) { 235 PermissionsData::DENY_ACCESS) {
234 DCHECK(frame->parent()); 236 DCHECK(frame->parent());
235 continue; 237 continue;
236 } 238 }
237 if (inject_js) 239 if (inject_js)
238 InjectJs(extension, frame, execution_results.get()); 240 InjectJs(extension, frame, execution_results.get());
239 if (inject_css) 241 if (inject_css)
240 InjectCss(frame); 242 InjectCss(frame);
241 } 243 }
242 244
243 complete_ = true; 245 complete_ = true;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 std::vector<std::string> css_sources = 309 std::vector<std::string> css_sources =
308 injector_->GetCssSources(run_location_); 310 injector_->GetCssSources(run_location_);
309 for (std::vector<std::string>::const_iterator iter = css_sources.begin(); 311 for (std::vector<std::string>::const_iterator iter = css_sources.begin();
310 iter != css_sources.end(); 312 iter != css_sources.end();
311 ++iter) { 313 ++iter) {
312 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter)); 314 frame->document().insertStyleSheet(blink::WebString::fromUTF8(*iter));
313 } 315 }
314 } 316 }
315 317
316 } // namespace extensions 318 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698