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

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: Ben's 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(blink::WebFrame* web_frame,
67 UserScript::RunLocation run_location,
68 int page_id);
69 ~PendingInjection();
70
71 // The globally-unique id of this request.
72 int64 id;
73
74 // The pointer to the web frame into which the script should be injected.
75 // This is weak, but safe because we remove pending requests when a frame is
76 // terminated.
77 blink::WebFrame* web_frame;
78
79 // The run location to inject at.
80 // Note: This could be a lie - we might inject well after this run location
81 // has come and gone. But we need to know it to know which scripts to inject.
82 UserScript::RunLocation run_location;
83
84 // The corresponding page id, to protect against races.
85 int page_id;
86 };
87
88 ScriptInjection::PendingInjection::PendingInjection(
89 blink::WebFrame* web_frame,
90 UserScript::RunLocation run_location,
91 int page_id)
92 : id(g_next_pending_id++),
93 web_frame(web_frame),
94 run_location(run_location),
95 page_id(page_id) {
96 }
97
98 ScriptInjection::PendingInjection::~PendingInjection() {
99 }
100
60 // static 101 // static
61 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) { 102 GURL ScriptInjection::GetDocumentUrlForFrame(blink::WebFrame* frame) {
62 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame); 103 GURL data_source_url = ScriptContext::GetDataSourceURLForFrame(frame);
63 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) { 104 if (!data_source_url.is_empty() && frame->isViewSourceModeEnabled()) {
64 data_source_url = GURL(content::kViewSourceScheme + std::string(":") + 105 data_source_url = GURL(content::kViewSourceScheme + std::string(":") +
65 data_source_url.spec()); 106 data_source_url.spec());
66 } 107 }
67 108
68 return data_source_url; 109 return data_source_url;
69 } 110 }
70 111
71 ScriptInjection::ScriptInjection( 112 ScriptInjection::ScriptInjection(
72 scoped_ptr<UserScript> script, 113 scoped_ptr<UserScript> script,
73 UserScriptSlave* user_script_slave) 114 UserScriptSlave* user_script_slave)
74 : script_(script.Pass()), 115 : script_(script.Pass()),
75 extension_id_(script_->extension_id()), 116 extension_id_(script_->extension_id()),
76 user_script_slave_(user_script_slave), 117 user_script_slave_(user_script_slave),
77 is_standalone_or_emulate_greasemonkey_( 118 is_standalone_or_emulate_greasemonkey_(
78 script_->is_standalone() || script_->emulate_greasemonkey()) { 119 script_->is_standalone() || script_->emulate_greasemonkey()) {
79 } 120 }
80 121
81 ScriptInjection::~ScriptInjection() { 122 ScriptInjection::~ScriptInjection() {
82 } 123 }
83 124
125 void ScriptInjection::InjectIfAllowed(blink::WebFrame* frame,
126 UserScript::RunLocation run_location,
127 const GURL& document_url,
128 ScriptsRunInfo* scripts_run_info) {
129 if (!WantsToRun(frame, run_location, document_url))
130 return;
131
132 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
133 CHECK(extension); // WantsToRun() should be false if there's no extension.
134
135 if (PermissionsData::RequiresActionForScriptExecution(extension)) {
136 // We use the top render view here (instead of the render view for the
137 // frame), because script injection on any frame requires permission for
138 // the top frame. Additionally, if we have to show any UI for permissions,
139 // it should only be done on the top frame.
140 content::RenderView* top_render_view =
141 content::RenderView::FromWebView(frame->top()->view());
142 int page_id = top_render_view->GetPageId();
143 ScopedVector<PendingInjection>::iterator pending_injection =
144 pending_injections_.insert(
145 pending_injections_.end(),
146 new PendingInjection(frame, run_location, page_id));
147
148 top_render_view->Send(
149 new ExtensionHostMsg_RequestContentScriptPermission(
150 top_render_view->GetRoutingID(),
151 extension->id(),
152 page_id,
153 (*pending_injection)->id));
154 } else {
155 Inject(frame, run_location, scripts_run_info);
156 }
157 }
158
159 bool ScriptInjection::NotifyScriptPermitted(
160 int64 request_id,
161 content::RenderView* render_view,
162 ScriptsRunInfo* scripts_run_info,
163 blink::WebFrame** frame_out) {
164 if (!render_view)
165 return false;
166
167 ScopedVector<PendingInjection>::iterator iter = pending_injections_.begin();
168 while (iter != pending_injections_.end() && (*iter)->id != request_id)
169 ++iter;
170
171 // No matching request.
172 if (iter == pending_injections_.end())
173 return false;
174
175 // We found the request, so pull it out of the pending list.
176 scoped_ptr<PendingInjection> pending_injection(*iter);
177 pending_injections_.weak_erase(iter);
178
179 // Ensure the Page ID and Extension are still valid. Otherwise, don't inject.
180 if (render_view->GetPageId() != pending_injection->page_id)
181 return false;
182
183 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
184 if (!extension)
185 return false;
186
187 // Everything matches! Inject the script.
188 if (frame_out)
189 *frame_out = pending_injection->web_frame;
190 Inject(pending_injection->web_frame,
191 pending_injection->run_location,
192 scripts_run_info);
193 return true;
194 }
195
196 void ScriptInjection::FrameDetached(blink::WebFrame* frame) {
197 // Any pending injections associated with the given frame will never run.
198 // Remove them.
199 for (ScopedVector<PendingInjection>::iterator iter =
200 pending_injections_.begin();
201 iter != pending_injections_.end();) {
202 if ((*iter)->web_frame == frame)
203 pending_injections_.erase(iter);
204 else
205 ++iter;
206 }
207 }
208
84 bool ScriptInjection::WantsToRun(blink::WebFrame* frame, 209 bool ScriptInjection::WantsToRun(blink::WebFrame* frame,
85 UserScript::RunLocation run_location, 210 UserScript::RunLocation run_location,
86 const GURL& document_url) const { 211 const GURL& document_url) const {
87 if (frame->parent() && !script_->match_all_frames()) 212 if (frame->parent() && !script_->match_all_frames())
88 return false; // Only match subframes if the script declared it wanted to. 213 return false; // Only match subframes if the script declared it wanted to.
89 214
90 const Extension* extension = user_script_slave_->GetExtension(extension_id_); 215 const Extension* extension = user_script_slave_->GetExtension(extension_id_);
91 // Since extension info is sent separately from user script info, they can 216 // Since extension info is sent separately from user script info, they can
92 // be out of sync. We just ignore this situation. 217 // be out of sync. We just ignore this situation.
93 if (!extension) 218 if (!extension)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 scripts_run_info->num_css += css_scripts.size(); 318 scripts_run_info->num_css += css_scripts.size();
194 for (UserScript::FileList::const_iterator iter = css_scripts.begin(); 319 for (UserScript::FileList::const_iterator iter = css_scripts.begin();
195 iter != css_scripts.end(); 320 iter != css_scripts.end();
196 ++iter) { 321 ++iter) {
197 frame->document().insertStyleSheet( 322 frame->document().insertStyleSheet(
198 blink::WebString::fromUTF8(iter->GetContent().as_string())); 323 blink::WebString::fromUTF8(iter->GetContent().as_string()));
199 } 324 }
200 } 325 }
201 326
202 } // namespace extensions 327 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698