| OLD | NEW |
| 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 13 matching lines...) Expand all Loading... |
| 24 #include "third_party/WebKit/public/web/WebFrame.h" | 24 #include "third_party/WebKit/public/web/WebFrame.h" |
| 25 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 25 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 26 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 26 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
| 27 #include "third_party/WebKit/public/web/WebView.h" | 27 #include "third_party/WebKit/public/web/WebView.h" |
| 28 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 29 | 29 |
| 30 using blink::WebFrame; | 30 using blink::WebFrame; |
| 31 using blink::WebSecurityOrigin; | 31 using blink::WebSecurityOrigin; |
| 32 using blink::WebSecurityPolicy; | 32 using blink::WebSecurityPolicy; |
| 33 using blink::WebString; | 33 using blink::WebString; |
| 34 using blink::WebView; | |
| 35 using content::RenderThread; | 34 using content::RenderThread; |
| 36 | 35 |
| 37 namespace extensions { | 36 namespace extensions { |
| 38 | 37 |
| 39 int UserScriptSlave::GetIsolatedWorldIdForExtension(const Extension* extension, | 38 int UserScriptSlave::GetIsolatedWorldIdForExtension(const Extension* extension, |
| 40 WebFrame* frame) { | 39 WebFrame* frame) { |
| 41 static int g_next_isolated_world_id = | 40 static int g_next_isolated_world_id = |
| 42 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); | 41 ExtensionsRendererClient::Get()->GetLowestIsolatedWorldId(); |
| 43 | 42 |
| 44 int id = 0; | 43 int id = 0; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 DCHECK(!(*iter)->extension_id().empty()); | 94 DCHECK(!(*iter)->extension_id().empty()); |
| 96 extension_ids->insert((*iter)->extension_id()); | 95 extension_ids->insert((*iter)->extension_id()); |
| 97 } | 96 } |
| 98 } | 97 } |
| 99 | 98 |
| 100 const Extension* UserScriptSlave::GetExtension( | 99 const Extension* UserScriptSlave::GetExtension( |
| 101 const std::string& extension_id) { | 100 const std::string& extension_id) { |
| 102 return extensions_->GetByID(extension_id); | 101 return extensions_->GetByID(extension_id); |
| 103 } | 102 } |
| 104 | 103 |
| 105 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { | 104 bool UserScriptSlave::UpdateScripts( |
| 106 script_injections_.clear(); | 105 base::SharedMemoryHandle shared_memory, |
| 107 | 106 const std::set<std::string>& changed_extensions) { |
| 108 bool only_inject_incognito = | 107 bool only_inject_incognito = |
| 109 ExtensionsRendererClient::Get()->IsIncognitoProcess(); | 108 ExtensionsRendererClient::Get()->IsIncognitoProcess(); |
| 110 | 109 |
| 111 // Create the shared memory object (read only). | 110 // Create the shared memory object (read only). |
| 112 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); | 111 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); |
| 113 if (!shared_memory_.get()) | 112 if (!shared_memory_.get()) |
| 114 return false; | 113 return false; |
| 115 | 114 |
| 116 // First get the size of the memory block. | 115 // First get the size of the memory block. |
| 117 if (!shared_memory_->Map(sizeof(Pickle::Header))) | 116 if (!shared_memory_->Map(sizeof(Pickle::Header))) |
| 118 return false; | 117 return false; |
| 119 Pickle::Header* pickle_header = | 118 Pickle::Header* pickle_header = |
| 120 reinterpret_cast<Pickle::Header*>(shared_memory_->memory()); | 119 reinterpret_cast<Pickle::Header*>(shared_memory_->memory()); |
| 121 | 120 |
| 122 // Now map in the rest of the block. | 121 // Now map in the rest of the block. |
| 123 int pickle_size = sizeof(Pickle::Header) + pickle_header->payload_size; | 122 int pickle_size = sizeof(Pickle::Header) + pickle_header->payload_size; |
| 124 shared_memory_->Unmap(); | 123 shared_memory_->Unmap(); |
| 125 if (!shared_memory_->Map(pickle_size)) | 124 if (!shared_memory_->Map(pickle_size)) |
| 126 return false; | 125 return false; |
| 127 | 126 |
| 128 // Unpickle scripts. | 127 // Unpickle scripts. |
| 129 uint64 num_scripts = 0; | 128 uint64 num_scripts = 0; |
| 130 Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), pickle_size); | 129 Pickle pickle(reinterpret_cast<char*>(shared_memory_->memory()), pickle_size); |
| 131 PickleIterator iter(pickle); | 130 PickleIterator iter(pickle); |
| 132 CHECK(pickle.ReadUInt64(&iter, &num_scripts)); | 131 CHECK(pickle.ReadUInt64(&iter, &num_scripts)); |
| 133 | 132 |
| 133 // If we pass no explicit extension ids, we should refresh all extensions. |
| 134 bool include_all_extensions = changed_extensions.empty(); |
| 135 |
| 136 // If we include all extensions, then we clear the script injections and |
| 137 // start from scratch. If not, then clear only the scripts for extension ids |
| 138 // that we are updating. This is important to maintain pending script |
| 139 // injection state for each ScriptInjection. |
| 140 if (include_all_extensions) { |
| 141 script_injections_.clear(); |
| 142 } else { |
| 143 for (ScopedVector<ScriptInjection>::iterator iter = |
| 144 script_injections_.begin(); |
| 145 iter != script_injections_.end();) { |
| 146 if (changed_extensions.count((*iter)->extension_id()) > 0) |
| 147 iter = script_injections_.erase(iter); |
| 148 else |
| 149 ++iter; |
| 150 } |
| 151 } |
| 152 |
| 134 script_injections_.reserve(num_scripts); | 153 script_injections_.reserve(num_scripts); |
| 135 for (uint64 i = 0; i < num_scripts; ++i) { | 154 for (uint64 i = 0; i < num_scripts; ++i) { |
| 136 scoped_ptr<UserScript> script(new UserScript()); | 155 scoped_ptr<UserScript> script(new UserScript()); |
| 137 script->Unpickle(pickle, &iter); | 156 script->Unpickle(pickle, &iter); |
| 138 | 157 |
| 139 // Note that this is a pointer into shared memory. We don't own it. It gets | 158 // Note that this is a pointer into shared memory. We don't own it. It gets |
| 140 // cleared up when the last renderer or browser process drops their | 159 // cleared up when the last renderer or browser process drops their |
| 141 // reference to the shared memory. | 160 // reference to the shared memory. |
| 142 for (size_t j = 0; j < script->js_scripts().size(); ++j) { | 161 for (size_t j = 0; j < script->js_scripts().size(); ++j) { |
| 143 const char* body = NULL; | 162 const char* body = NULL; |
| 144 int body_length = 0; | 163 int body_length = 0; |
| 145 CHECK(pickle.ReadData(&iter, &body, &body_length)); | 164 CHECK(pickle.ReadData(&iter, &body, &body_length)); |
| 146 script->js_scripts()[j].set_external_content( | 165 script->js_scripts()[j].set_external_content( |
| 147 base::StringPiece(body, body_length)); | 166 base::StringPiece(body, body_length)); |
| 148 } | 167 } |
| 149 for (size_t j = 0; j < script->css_scripts().size(); ++j) { | 168 for (size_t j = 0; j < script->css_scripts().size(); ++j) { |
| 150 const char* body = NULL; | 169 const char* body = NULL; |
| 151 int body_length = 0; | 170 int body_length = 0; |
| 152 CHECK(pickle.ReadData(&iter, &body, &body_length)); | 171 CHECK(pickle.ReadData(&iter, &body, &body_length)); |
| 153 script->css_scripts()[j].set_external_content( | 172 script->css_scripts()[j].set_external_content( |
| 154 base::StringPiece(body, body_length)); | 173 base::StringPiece(body, body_length)); |
| 155 } | 174 } |
| 156 | 175 |
| 157 if (only_inject_incognito && !script->is_incognito_enabled()) | 176 if (only_inject_incognito && !script->is_incognito_enabled()) |
| 158 continue; // This script shouldn't run in an incognito tab. | 177 continue; // This script shouldn't run in an incognito tab. |
| 159 | 178 |
| 160 script_injections_.push_back(new ScriptInjection(script.Pass(), this)); | 179 // If we include all extensions or the given extension changed, we add a |
| 180 // new script injection. |
| 181 if (include_all_extensions || |
| 182 changed_extensions.count(script->extension_id()) > 0) { |
| 183 script_injections_.push_back(new ScriptInjection(script.Pass(), this)); |
| 184 } else { |
| 185 // Otherwise, we need to update the existing script injection with the |
| 186 // new user script (since the old content was invalidated). |
| 187 // |
| 188 // Note: Yes, this is O(n^2). But vectors are faster than maps for |
| 189 // relatively few elements, and less than 1% of our users actually have |
| 190 // enough content scripts for it to matter. If this changes, or if |
| 191 // std::maps get a much faster implementation, we should look into |
| 192 // making a map for script injections. |
| 193 for (ScopedVector<ScriptInjection>::iterator iter = |
| 194 script_injections_.begin(); |
| 195 iter != script_injections_.end(); |
| 196 ++iter) { |
| 197 if ((*iter)->script()->id() == script->id()) { |
| 198 (*iter)->SetScript(script.Pass()); |
| 199 break; |
| 200 } |
| 201 } |
| 202 } |
| 161 } | 203 } |
| 162 | |
| 163 return true; | 204 return true; |
| 164 } | 205 } |
| 165 | 206 |
| 166 void UserScriptSlave::InjectScripts(WebFrame* frame, | 207 void UserScriptSlave::InjectScripts(WebFrame* frame, |
| 167 UserScript::RunLocation location) { | 208 UserScript::RunLocation location) { |
| 168 GURL document_url = ScriptInjection::GetDocumentUrlForFrame(frame); | 209 GURL document_url = ScriptInjection::GetDocumentUrlForFrame(frame); |
| 169 if (document_url.is_empty()) | 210 if (document_url.is_empty()) |
| 170 return; | 211 return; |
| 171 | 212 |
| 172 content::RenderView* top_render_view = | |
| 173 content::RenderView::FromWebView(frame->top()->view()); | |
| 174 | |
| 175 ScriptInjection::ScriptsRunInfo scripts_run_info; | 213 ScriptInjection::ScriptsRunInfo scripts_run_info; |
| 176 for (ScopedVector<ScriptInjection>::const_iterator iter = | 214 for (ScopedVector<ScriptInjection>::const_iterator iter = |
| 177 script_injections_.begin(); | 215 script_injections_.begin(); |
| 178 iter != script_injections_.end(); | 216 iter != script_injections_.end(); |
| 179 ++iter) { | 217 ++iter) { |
| 180 ScriptInjection* injection = *iter; | 218 (*iter)->InjectIfAllowed(frame, location, document_url, &scripts_run_info); |
| 181 if (!injection->WantsToRun(frame, location, document_url)) | |
| 182 continue; | |
| 183 | |
| 184 const Extension* extension = GetExtension(injection->extension_id()); | |
| 185 DCHECK(extension); | |
| 186 | |
| 187 if (PermissionsData::RequiresActionForScriptExecution( | |
| 188 extension, | |
| 189 ExtensionHelper::Get(top_render_view)->tab_id(), | |
| 190 document_url)) { | |
| 191 // TODO(rdevlin.cronin): Right now, this is just a notification, but soon | |
| 192 // we should block without user consent. | |
| 193 top_render_view->Send( | |
| 194 new ExtensionHostMsg_NotifyExtensionScriptExecution( | |
| 195 top_render_view->GetRoutingID(), | |
| 196 extension->id(), | |
| 197 top_render_view->GetPageId())); | |
| 198 } | |
| 199 | |
| 200 injection->Inject(frame, location, &scripts_run_info); | |
| 201 } | 219 } |
| 202 | 220 |
| 203 LogScriptsRun(frame, location, scripts_run_info); | 221 LogScriptsRun(frame, location, scripts_run_info); |
| 204 } | 222 } |
| 205 | 223 |
| 224 void UserScriptSlave::OnContentScriptGrantedPermission( |
| 225 content::RenderView* render_view, int request_id) { |
| 226 ScriptInjection::ScriptsRunInfo run_info; |
| 227 blink::WebFrame* frame = NULL; |
| 228 // Notify the injections that a request to inject has been granted. |
| 229 for (ScopedVector<ScriptInjection>::iterator iter = |
| 230 script_injections_.begin(); |
| 231 iter != script_injections_.end(); |
| 232 ++iter) { |
| 233 if ((*iter)->NotifyScriptPermitted(request_id, |
| 234 render_view, |
| 235 &run_info, |
| 236 &frame)) { |
| 237 DCHECK(frame); |
| 238 LogScriptsRun(frame, UserScript::RUN_DEFERRED, run_info); |
| 239 break; |
| 240 } |
| 241 } |
| 242 } |
| 243 |
| 244 void UserScriptSlave::FrameDetached(blink::WebFrame* frame) { |
| 245 for (ScopedVector<ScriptInjection>::iterator iter = |
| 246 script_injections_.begin(); |
| 247 iter != script_injections_.end(); |
| 248 ++iter) { |
| 249 (*iter)->FrameDetached(frame); |
| 250 } |
| 251 } |
| 252 |
| 206 void UserScriptSlave::LogScriptsRun( | 253 void UserScriptSlave::LogScriptsRun( |
| 207 blink::WebFrame* frame, | 254 blink::WebFrame* frame, |
| 208 UserScript::RunLocation location, | 255 UserScript::RunLocation location, |
| 209 const ScriptInjection::ScriptsRunInfo& info) { | 256 const ScriptInjection::ScriptsRunInfo& info) { |
| 210 // Notify the browser if any extensions are now executing scripts. | 257 // Notify the browser if any extensions are now executing scripts. |
| 211 if (!info.executing_scripts.empty()) { | 258 if (!info.executing_scripts.empty()) { |
| 212 content::RenderView* render_view = | 259 content::RenderView* render_view = |
| 213 content::RenderView::FromWebView(frame->view()); | 260 content::RenderView::FromWebView(frame->view()); |
| 214 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( | 261 render_view->Send(new ExtensionHostMsg_ContentScriptsExecuting( |
| 215 render_view->GetRoutingID(), | 262 render_view->GetRoutingID(), |
| 216 info.executing_scripts, | 263 info.executing_scripts, |
| 217 render_view->GetPageId(), | 264 render_view->GetPageId(), |
| 218 ScriptContext::GetDataSourceURLForFrame(frame))); | 265 ScriptContext::GetDataSourceURLForFrame(frame))); |
| 219 } | 266 } |
| 220 | 267 |
| 221 if (location == UserScript::DOCUMENT_START) { | 268 switch (location) { |
| 222 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", | 269 case UserScript::DOCUMENT_START: |
| 223 info.num_css); | 270 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_CssCount", |
| 224 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", info.num_js); | 271 info.num_css); |
| 225 if (info.num_css || info.num_js) | 272 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectStart_ScriptCount", |
| 226 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", info.timer.Elapsed()); | 273 info.num_js); |
| 227 } else if (location == UserScript::DOCUMENT_END) { | 274 if (info.num_css || info.num_js) |
| 228 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", info.num_js); | 275 UMA_HISTOGRAM_TIMES("Extensions.InjectStart_Time", |
| 229 if (info.num_js) | 276 info.timer.Elapsed()); |
| 230 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", info.timer.Elapsed()); | 277 break; |
| 231 } else if (location == UserScript::DOCUMENT_IDLE) { | 278 case UserScript::DOCUMENT_END: |
| 232 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", info.num_js); | 279 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectEnd_ScriptCount", info.num_js); |
| 233 if (info.num_js) | 280 if (info.num_js) |
| 234 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", info.timer.Elapsed()); | 281 UMA_HISTOGRAM_TIMES("Extensions.InjectEnd_Time", info.timer.Elapsed()); |
| 235 } else { | 282 break; |
| 236 NOTREACHED(); | 283 case UserScript::DOCUMENT_IDLE: |
| 284 UMA_HISTOGRAM_COUNTS_100("Extensions.InjectIdle_ScriptCount", |
| 285 info.num_js); |
| 286 if (info.num_js) |
| 287 UMA_HISTOGRAM_TIMES("Extensions.InjectIdle_Time", info.timer.Elapsed()); |
| 288 break; |
| 289 case UserScript::RUN_DEFERRED: |
| 290 // TODO(rdevlin.cronin): Add histograms. |
| 291 break; |
| 292 case UserScript::UNDEFINED: |
| 293 case UserScript::RUN_LOCATION_LAST: |
| 294 NOTREACHED(); |
| 237 } | 295 } |
| 238 } | 296 } |
| 239 | 297 |
| 240 } // namespace extensions | 298 } // namespace extensions |
| OLD | NEW |