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

Side by Side Diff: extensions/renderer/user_script_slave.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/user_script_slave.h" 5 #include "extensions/renderer/user_script_slave.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/shared_memory.h" 10 #include "base/memory/shared_memory.h"
(...skipping 12 matching lines...) Expand all
23 #include "third_party/WebKit/public/web/WebFrame.h" 23 #include "third_party/WebKit/public/web/WebFrame.h"
24 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" 24 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
25 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 25 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
26 #include "third_party/WebKit/public/web/WebView.h" 26 #include "third_party/WebKit/public/web/WebView.h"
27 #include "url/gurl.h" 27 #include "url/gurl.h"
28 28
29 using blink::WebFrame; 29 using blink::WebFrame;
30 using blink::WebSecurityOrigin; 30 using blink::WebSecurityOrigin;
31 using blink::WebSecurityPolicy; 31 using blink::WebSecurityPolicy;
32 using blink::WebString; 32 using blink::WebString;
33 using blink::WebView;
34 using content::RenderThread; 33 using content::RenderThread;
35 34
36 namespace extensions { 35 namespace extensions {
37 36
38 int UserScriptSlave::GetIsolatedWorldIdForExtension(const Extension* extension, 37 int UserScriptSlave::GetIsolatedWorldIdForExtension(const Extension* extension,
39 WebFrame* frame) { 38 WebFrame* frame) {
40 static int g_next_isolated_world_id = 39 static int g_next_isolated_world_id =
41 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); 40 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId();
42 41
43 int id = 0; 42 int id = 0;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 DCHECK(!(*iter)->extension_id().empty()); 93 DCHECK(!(*iter)->extension_id().empty());
95 extension_ids->insert((*iter)->extension_id()); 94 extension_ids->insert((*iter)->extension_id());
96 } 95 }
97 } 96 }
98 97
99 const Extension* UserScriptSlave::GetExtension( 98 const Extension* UserScriptSlave::GetExtension(
100 const std::string& extension_id) { 99 const std::string& extension_id) {
101 return extensions_->GetByID(extension_id); 100 return extensions_->GetByID(extension_id);
102 } 101 }
103 102
104 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { 103 bool UserScriptSlave::UpdateScripts(
105 script_injections_.clear(); 104 base::SharedMemoryHandle shared_memory,
106 105 const std::set<std::string>& changed_extensions) {
107 bool only_inject_incognito = 106 bool only_inject_incognito =
108 ExtensionsRendererClient::Get()->IsIncognitoProcess(); 107 ExtensionsRendererClient::Get()->IsIncognitoProcess();
109 108
110 // Create the shared memory object (read only). 109 // Create the shared memory object (read only).
111 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); 110 shared_memory_.reset(new base::SharedMemory(shared_memory, true));
112 if (!shared_memory_.get()) 111 if (!shared_memory_.get())
113 return false; 112 return false;
114 113
115 // First get the size of the memory block. 114 // First get the size of the memory block.
116 if (!shared_memory_->Map(sizeof(Pickle::Header))) 115 if (!shared_memory_->Map(sizeof(Pickle::Header)))
117 return false; 116 return false;
118 Pickle::Header* pickle_header = 117 Pickle::Header* pickle_header =
119 reinterpret_cast<Pickle::Header*>(shared_memory_->memory()); 118 reinterpret_cast<Pickle::Header*>(shared_memory_->memory());
120 119
121 // Now map in the rest of the block. 120 // Now map in the rest of the block.
122 int pickle_size = sizeof(Pickle::Header) + pickle_header->payload_size; 121 int pickle_size = sizeof(Pickle::Header) + pickle_header->payload_size;
123 shared_memory_->Unmap(); 122 shared_memory_->Unmap();
124 if (!shared_memory_->Map(pickle_size)) 123 if (!shared_memory_->Map(pickle_size))
125 return false; 124 return false;
126 125
127 // Unpickle scripts. 126 // Unpickle scripts.
128 uint64 num_scripts = 0; 127 uint64 num_scripts = 0;
129 Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), pickle_size); 128 Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), pickle_size);
130 PickleIterator iter(pickle); 129 PickleIterator iter(pickle);
131 CHECK(pickle.ReadUInt64(&iter, &num_scripts)); 130 CHECK(pickle.ReadUInt64(&iter, &num_scripts));
132 131
133 script_injections_.reserve(num_scripts); 132 // If we pass no explicit extension ids, we should refresh all extensions.
133 bool include_all_extensions = changed_extensions.empty();
134
135 // If we include all extensions, then we clear the script injections and
136 // start from scratch. If not, then clear only the scripts for extension ids
137 // that we are updating.
not at google - send to devlin 2014/05/21 15:01:07 could you mention "this is important to maintain p
Devlin 2014/05/21 17:05:11 Good call. Done.
138 if (include_all_extensions) {
139 script_injections_.clear();
140 } else {
141 for (ScopedVector<ScriptInjection>::iterator iter =
142 script_injections_.begin();
143 iter != script_injections_.end();) {
144 if (changed_extensions.count((*iter)->extension_id()) > 0)
145 iter = script_injections_.erase(iter);
146 else
147 ++iter;
148 }
149 }
150
not at google - send to devlin 2014/05/21 15:01:07 you might as well still call reserve()
Devlin 2014/05/21 17:05:11 Done.
134 for (uint64 i = 0; i < num_scripts; ++i) { 151 for (uint64 i = 0; i < num_scripts; ++i) {
135 scoped_ptr<UserScript> script(new UserScript()); 152 scoped_ptr<UserScript> script(new UserScript());
136 script->Unpickle(pickle, &iter); 153 script->Unpickle(pickle, &iter);
137 154
138 // Note that this is a pointer into shared memory. We don't own it. It gets 155 // Note that this is a pointer into shared memory. We don't own it. It gets
139 // cleared up when the last renderer or browser process drops their 156 // cleared up when the last renderer or browser process drops their
140 // reference to the shared memory. 157 // reference to the shared memory.
141 for (size_t j = 0; j < script->js_scripts().size(); ++j) { 158 for (size_t j = 0; j < script->js_scripts().size(); ++j) {
142 const char* body = NULL; 159 const char* body = NULL;
143 int body_length = 0; 160 int body_length = 0;
144 CHECK(pickle.ReadData(&iter, &body, &body_length)); 161 CHECK(pickle.ReadData(&iter, &body, &body_length));
145 script->js_scripts()[j].set_external_content( 162 script->js_scripts()[j].set_external_content(
146 base::StringPiece(body, body_length)); 163 base::StringPiece(body, body_length));
147 } 164 }
148 for (size_t j = 0; j < script->css_scripts().size(); ++j) { 165 for (size_t j = 0; j < script->css_scripts().size(); ++j) {
149 const char* body = NULL; 166 const char* body = NULL;
150 int body_length = 0; 167 int body_length = 0;
151 CHECK(pickle.ReadData(&iter, &body, &body_length)); 168 CHECK(pickle.ReadData(&iter, &body, &body_length));
152 script->css_scripts()[j].set_external_content( 169 script->css_scripts()[j].set_external_content(
153 base::StringPiece(body, body_length)); 170 base::StringPiece(body, body_length));
154 } 171 }
155 172
156 if (only_inject_incognito && !script->is_incognito_enabled()) 173 // Don't add the script if it shouldn't shouldn't run in this tab, or if
157 continue; // This script shouldn't run in an incognito tab. 174 // we don't need to reload that extension.
175 // It's a shame we don't catch this sooner, but since we lump all the user
176 // scripts together, we can't skip parsing any.
177 if ((only_inject_incognito && !script->is_incognito_enabled()) ||
178 (!include_all_extensions &&
179 changed_extensions.count(script->extension_id()) == 0)) {
180 continue;
181 }
158 182
159 script_injections_.push_back(new ScriptInjection(script.Pass(), this)); 183 script_injections_.push_back(new ScriptInjection(script.Pass(), this));
160 } 184 }
161 185
162 return true; 186 return true;
163 } 187 }
164 188
165 void UserScriptSlave::InjectScripts(WebFrame* frame, 189 void UserScriptSlave::InjectScripts(WebFrame* frame,
166 UserScript::RunLocation location) { 190 UserScript::RunLocation location) {
167 GURL document_url = ScriptInjection::GetDocumentUrlForFrame(frame); 191 GURL document_url = ScriptInjection::GetDocumentUrlForFrame(frame);
168 if (document_url.is_empty()) 192 if (document_url.is_empty())
169 return; 193 return;
170 194
171 content::RenderView* top_render_view =
172 content::RenderView::FromWebView(frame->top()->view());
173
174 ScriptInjection::ScriptsRunInfo scripts_run_info; 195 ScriptInjection::ScriptsRunInfo scripts_run_info;
175 for (ScopedVector<ScriptInjection>::const_iterator iter = 196 for (ScopedVector<ScriptInjection>::const_iterator iter =
176 script_injections_.begin(); 197 script_injections_.begin();
177 iter != script_injections_.end(); 198 iter != script_injections_.end();
178 ++iter) { 199 ++iter) {
179 ScriptInjection* injection = *iter; 200 (*iter)->InjectIfAllowed(frame, location, document_url, &scripts_run_info);
180 if (!injection->WantsToRun(frame, location, document_url))
181 continue;
182
183 const Extension* extension = GetExtension(injection->extension_id());
184 DCHECK(extension);
185
186 if (PermissionsData::RequiresActionForScriptExecution(extension)) {
187 // TODO(rdevlin.cronin): Right now, this is just a notification, but soon
188 // we should block without user consent.
189 top_render_view->Send(
190 new ExtensionHostMsg_NotifyExtensionScriptExecution(
191 top_render_view->GetRoutingID(),
192 extension->id(),
193 top_render_view->GetPageId()));
194 }
195
196 injection->Inject(frame, location, &scripts_run_info);
197 } 201 }
198 202
199 LogScriptsRun(frame, location, scripts_run_info); 203 LogScriptsRun(frame, location, scripts_run_info);
200 } 204 }
201 205
206 void UserScriptSlave::OnContentScriptGrantedPermission(
207 content::RenderView* render_view, int request_id) {
208 ScriptInjection::ScriptsRunInfo run_info;
209 blink::WebFrame* frame = NULL;
210 // Notify the injections that a request to inject has been granted.
211 for (ScopedVector<ScriptInjection>::iterator iter =
212 script_injections_.begin();
213 iter != script_injections_.end();
214 ++iter) {
215 if ((*iter)->NotifyScriptPermitted(request_id,
216 render_view,
217 &run_info,
218 &frame)) {
219 DCHECK(frame);
220 LogScriptsRun(frame, UserScript::UNDEFINED, run_info);
not at google - send to devlin 2014/05/21 15:01:07 could you define a new run location here like DEFE
Devlin 2014/05/21 17:05:11 New location added. It okay to add histograms in
not at google - send to devlin 2014/05/21 17:36:23 yes!
221 break;
222 }
223 }
224 }
225
226 void UserScriptSlave::NotifyFrameDetached(blink::WebFrame* frame) {
not at google - send to devlin 2014/05/21 15:01:07 everywhere else in the codebase this is called "Fr
Devlin 2014/05/21 17:05:11 Logic was to make it clear that this wasn't OVERRI
227 for (ScopedVector<ScriptInjection>::iterator iter =
228 script_injections_.begin();
229 iter != script_injections_.end();
230 ++iter) {
231 (*iter)->NotifyFrameDetached(frame);
232 }
233 }
234
202 void UserScriptSlave::LogScriptsRun( 235 void UserScriptSlave::LogScriptsRun(
203 blink::WebFrame* frame, 236 blink::WebFrame* frame,
204 UserScript::RunLocation location, 237 UserScript::RunLocation location,
205 const ScriptInjection::ScriptsRunInfo& info) { 238 const ScriptInjection::ScriptsRunInfo& info) {
206 // Notify the browser if any extensions are now executing scripts. 239 // Notify the browser if any extensions are now executing scripts.
207 if (!info.executing_scripts.empty()) { 240 if (!info.executing_scripts.empty()) {
208 content::RenderView* render_view = 241 content::RenderView* render_view =
209 content::RenderView::FromWebView(frame->view()); 242 content::RenderView::FromWebView(frame->view());
210 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( 243 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting(
211 render_view->GetRoutingID(), 244 render_view->GetRoutingID(),
212 info.executing_scripts, 245 info.executing_scripts,
213 render_view->GetPageId(), 246 render_view->GetPageId(),
214 ScriptContext::GetDataSourceURLForFrame(frame))); 247 ScriptContext::GetDataSourceURLForFrame(frame)));
215 } 248 }
216 249
217 if (location == UserScript::DOCUMENT_START) { 250 if (location == UserScript::DOCUMENT_START) {
not at google - send to devlin 2014/05/21 15:01:07 bleh, can we change this to a switch() along the w
Devlin 2014/05/21 17:05:11 Done.
218 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", 251 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount",
219 info.num_css); 252 info.num_css);
220 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", info.num_js); 253 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", info.num_js);
221 if (info.num_css || info.num_js) 254 if (info.num_css || info.num_js)
222 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", info.timer.Elapsed()); 255 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", info.timer.Elapsed());
223 } else if (location == UserScript::DOCUMENT_END) { 256 } else if (location == UserScript::DOCUMENT_END) {
224 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", info.num_js); 257 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", info.num_js);
225 if (info.num_js) 258 if (info.num_js)
226 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", info.timer.Elapsed()); 259 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", info.timer.Elapsed());
227 } else if (location == UserScript::DOCUMENT_IDLE) { 260 } else if (location == UserScript::DOCUMENT_IDLE) {
228 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", info.num_js); 261 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", info.num_js);
229 if (info.num_js) 262 if (info.num_js)
230 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", info.timer.Elapsed()); 263 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", info.timer.Elapsed());
not at google - send to devlin 2014/05/21 15:01:07 see comment about deferred
Devlin 2014/05/21 17:05:11 Done.
231 } else {
232 NOTREACHED();
233 } 264 }
234 } 265 }
235 266
236 } // namespace extensions 267 } // namespace extensions
OLDNEW
« extensions/renderer/script_injection.cc ('K') | « extensions/renderer/user_script_slave.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698