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

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

Issue 288053002: Block content scripts from executing until user grants permission (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase after ScriptInjection refactor Created 6 years, 7 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 <vector> 7 #include <vector>
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"
11 #include "content/public/common/url_constants.h" 11 #include "content/public/common/url_constants.h"
12 #include "content/public/renderer/render_view.h"
12 #include "extensions/common/extension.h" 13 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_messages.h" 14 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/permissions/permissions_data.h" 15 #include "extensions/common/permissions/permissions_data.h"
15 #include "extensions/renderer/dom_activity_logger.h" 16 #include "extensions/renderer/dom_activity_logger.h"
16 #include "extensions/renderer/extension_groups.h" 17 #include "extensions/renderer/extension_groups.h"
17 #include "extensions/renderer/script_context.h" 18 #include "extensions/renderer/script_context.h"
18 #include "extensions/renderer/user_script_slave.h" 19 #include "extensions/renderer/user_script_slave.h"
19 #include "grit/renderer_resources.h" 20 #include "grit/renderer_resources.h"
20 #include "third_party/WebKit/public/web/WebDocument.h" 21 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebFrame.h" 22 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebScriptSource.h" 23 #include "third_party/WebKit/public/web/WebScriptSource.h"
24 #include "third_party/WebKit/public/web/WebView.h"
23 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
24 #include "url/gurl.h" 26 #include "url/gurl.h"
25 27
26 namespace extensions { 28 namespace extensions {
27 29
28 namespace { 30 namespace {
29 31
32 // The id of the next pending injection.
33 int64 g_next_pending_id = 0;
34
30 // These two strings are injected before and after the Greasemonkey API and 35 // These two strings are injected before and after the Greasemonkey API and
31 // user script to wrap it in an anonymous scope. 36 // user script to wrap it in an anonymous scope.
32 const char kUserScriptHead[] = "(function (unsafeWindow) {\n"; 37 const char kUserScriptHead[] = "(function (unsafeWindow) {\n";
33 const char kUserScriptTail[] = "\n})(window);"; 38 const char kUserScriptTail[] = "\n})(window);";
34 39
35 // Greasemonkey API source that is injected with the scripts. 40 // Greasemonkey API source that is injected with the scripts.
36 struct GreasemonkeyApiJsString { 41 struct GreasemonkeyApiJsString {
37 GreasemonkeyApiJsString(); 42 GreasemonkeyApiJsString();
38 blink::WebScriptSource source; 43 blink::WebScriptSource source;
39 }; 44 };
(...skipping 10 matching lines...) Expand all
50 LAZY_INSTANCE_INITIALIZER; 55 LAZY_INSTANCE_INITIALIZER;
51 56
52 } // namespace 57 } // namespace
53 58
54 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) { 59 ScriptInjection::ScriptsRunInfo::ScriptsRunInfo() : num_css(0u), num_js(0u) {
55 } 60 }
56 61
57 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() { 62 ScriptInjection::ScriptsRunInfo::~ScriptsRunInfo() {
58 } 63 }
59 64
65 struct ScriptInjection::PendingInjection {
66 PendingInjection(const blink::WebString& web_frame_name,
67 UserScript::RunLocation run_location,
68 int page_id);
69 ~PendingInjection();
70
71 // The globally-unique id of this request.
72 int64 id;
not at google - send to devlin 2014/05/21 15:01:07 it seems like |page_id| is enough to distinguish r
Devlin 2014/05/21 17:05:11 We'd actually need page id + extension id, because
not at google - send to devlin 2014/05/21 17:36:23 good point.
73
74 // The name of the web frame into which to inject.
75 blink::WebString web_frame_name;
not at google - send to devlin 2014/05/21 15:01:07 why not just hold onto the WebFrame pointer? code
Devlin 2014/05/21 17:05:11 Good point. Let's try that.
76
77 // The run location to inject at.
78 // Note: This could be a lie - we might inject well after this run location
79 // has come and gone. But we need to know it to know which scripts to inject.
80 UserScript::RunLocation run_location;
81
82 // The corresponding page id, to protect against races.
83 int page_id;
84 };
85
86 ScriptInjection::PendingInjection::PendingInjection(
87 const blink::WebString& web_frame_name,
88 UserScript::RunLocation run_location,
89 int page_id)
90 : id(g_next_pending_id++),
91 web_frame_name(web_frame_name),
92 run_location(run_location),
93 page_id(page_id) {
94 }
95
96 ScriptInjection::PendingInjection::~PendingInjection() {
97 }
98
60 // static 99 // static
61 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) { 100 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) {
62 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); 101 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame);
63 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { 102 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) {
64 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + 103 data_source_url = GURL(content::kViewSourceScheme + std::string(":") +
65 data_source_url.spec()); 104 data_source_url.spec());
66 } 105 }
67 106
68 return data_source_url; 107 return data_source_url;
69 } 108 }
70 109
71 ScriptInjection::ScriptInjection( 110 ScriptInjection::ScriptInjection(
72 scoped_ptr<UserScript> script, 111 scoped_ptr<UserScript> script,
73 UserScriptSlave* user_script_slave) 112 UserScriptSlave* user_script_slave)
74 : script_(script.Pass()), 113 : script_(script.Pass()),
75 extension_id_(script_->extension_id()), 114 extension_id_(script_->extension_id()),
76 user_script_slave_(user_script_slave), 115 user_script_slave_(user_script_slave),
77 is_standalone_or_emulate_greasemonkey_( 116 is_standalone_or_emulate_greasemonkey_(
78 script_->is_standalone() || script_->emulate_greasemonkey()) { 117 script_->is_standalone() || script_->emulate_greasemonkey()) {
79 } 118 }
80 119
81 ScriptInjection::~ScriptInjection() { 120 ScriptInjection::~ScriptInjection() {
82 } 121 }
83 122
123 void ScriptInjection::InjectIfAllowed(blink::WebFrame* frame,
124 UserScript::RunLocation run_location,
125 const GURL& document_url,
126 ScriptsRunInfo* scripts_run_info) {
127 if (!WantsToRun(frame, run_location, document_url))
128 return;
129
130 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
131 DCHECK(extension); // WantsToRun() should be false if there's no extension.
not at google - send to devlin 2014/05/21 15:01:07 CHECK
Devlin 2014/05/21 17:05:11 If you insist... My thinking is generally that if
not at google - send to devlin 2014/05/21 17:36:23 I trust many things, but not the existence of exte
132
133 content::RenderView* top_render_view =
134 content::RenderView::FromWebView(frame->top()->view());
not at google - send to devlin 2014/05/21 15:01:07 please write a nice comment explaining why you're
Devlin 2014/05/21 17:05:11 Done.
135 if (PermissionsData::RequiresActionForScriptExecution(extension)) {
136 int page_id = top_render_view->GetPageId();
137 ScopedVector<PendingInjection>::iterator pending_injection =
138 pending_injections_.insert(
139 pending_injections_.end(),
140 new PendingInjection(frame->uniqueName(), run_location, page_id));
141
142 top_render_view->Send(
143 new ExtensionHostMsg_RequestContentScriptPermission(
144 top_render_view->GetRoutingID(),
145 extension->id(),
146 page_id,
147 (*pending_injection)->id));
148 } else {
149 Inject(frame, run_location, scripts_run_info);
150 }
151 }
152
153 bool ScriptInjection::NotifyScriptPermitted(
154 int64 request_id,
155 content::RenderView* render_view,
156 ScriptsRunInfo* scripts_run_info,
157 blink::WebFrame** frame_out) {
158 if (!render_view)
not at google - send to devlin 2014/05/21 15:01:07 does this actually get called with a null render v
Devlin 2014/05/21 17:05:11 I'd certainly hope not, but I hate to make crashin
not at google - send to devlin 2014/05/21 17:36:23 I'd take it out entirely. it's seemingly arbitrary
Devlin 2014/05/21 18:28:28 Done.
159 return false;
160
161 ScopedVector<PendingInjection>::iterator iter = pending_injections_.begin();
162 while (iter != pending_injections_.end() && (*iter)->id != request_id)
163 ++iter;
164
165 // No matching request.
166 if (iter == pending_injections_.end())
167 return false;
168
169 // We found the request, so pull it out of the pending list.
170 scoped_ptr<PendingInjection> pending_injection(*iter);
171 pending_injections_.weak_erase(iter);
172
173 // Ensure the WebView, WebFrame, Extension, and Page ID all still exist and
174 // match. Otherwise, don't inject.
175 if (render_view->GetPageId() != pending_injection->page_id)
176 return false;
177
178 blink::WebView* web_view = render_view->GetWebView();
179 if (!web_view)
180 return false;
181
182 blink::WebFrame* web_frame =
183 web_view->findFrameByName(pending_injection->web_frame_name);
184 if (!web_frame)
185 return false;
186
187 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
188 if (!extension)
189 return false;
190
191 // Everything matches! Inject the script.
192 if (frame_out)
193 *frame_out = web_frame;
194 Inject(web_frame, pending_injection->run_location, scripts_run_info);
195 return true;
196 }
197
198 void ScriptInjection::NotifyFrameDetached(blink::WebFrame* frame) {
not at google - send to devlin 2014/05/21 15:01:07 ditto FrameDetached
Devlin 2014/05/21 17:05:11 Done.
199 // Any pending injections associated with the given frame will never run.
200 // Remove them.
201 for (ScopedVector<PendingInjection>::iterator iter =
202 pending_injections_.begin();
203 iter != pending_injections_.end();) {
204 if ((*iter)->web_frame_name == frame->uniqueName())
205 pending_injections_.erase(iter);
206 else
207 ++iter;
208 }
209 }
210
84 bool ScriptInjection::WantsToRun(blink::WebFrame* frame, 211 bool ScriptInjection::WantsToRun(blink::WebFrame* frame,
85 UserScript::RunLocation run_location, 212 UserScript::RunLocation run_location,
86 const GURL& document_url) const { 213 const GURL& document_url) const {
87 if (frame->parent() && !script_->match_all_frames()) 214 if (frame->parent() && !script_->match_all_frames())
88 return false; // Only match subframes if the script declared it wanted to. 215 return false; // Only match subframes if the script declared it wanted to.
89 216
90 const Extension* extension = user_script_slave_->GetExtension(extension_id_); 217 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
91 // Since extension info is sent separately from user script info, they can 218 // Since extension info is sent separately from user script info, they can
92 // be out of sync. We just ignore this situation. 219 // be out of sync. We just ignore this situation.
93 if (!extension) 220 if (!extension)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 scripts_run_info->num_css += css_scripts.size(); 320 scripts_run_info->num_css += css_scripts.size();
194 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 321 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
195 iter != css_scripts.end(); 322 iter != css_scripts.end();
196 ++iter) { 323 ++iter) {
197 frame->document().insertStyleSheet( 324 frame->document().insertStyleSheet(
198 blink::WebString::fromUTF8(iter->GetContent().as_string())); 325 blink::WebString::fromUTF8(iter->GetContent().as_string()));
199 } 326 }
200 } 327 }
201 328
202 } // namespace extensions 329 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698