| 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/browser/user_script_loader.h" | 5 #include "extensions/browser/user_script_loader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/version.h" | 14 #include "base/version.h" |
| 14 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
| 18 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 19 #include "extensions/browser/extensions_browser_client.h" | 20 #include "extensions/browser/extensions_browser_client.h" |
| 20 #include "extensions/browser/notification_types.h" | 21 #include "extensions/browser/notification_types.h" |
| 21 #include "extensions/common/extension_messages.h" | 22 #include "extensions/common/extension_messages.h" |
| 22 | 23 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 base::Bind(&UserScriptLoader::OnScriptsLoaded, | 270 base::Bind(&UserScriptLoader::OnScriptsLoaded, |
| 270 weak_factory_.GetWeakPtr())); | 271 weak_factory_.GetWeakPtr())); |
| 271 | 272 |
| 272 clear_scripts_ = false; | 273 clear_scripts_ = false; |
| 273 added_scripts_.clear(); | 274 added_scripts_.clear(); |
| 274 removed_scripts_.clear(); | 275 removed_scripts_.clear(); |
| 275 user_scripts_.reset(); | 276 user_scripts_.reset(); |
| 276 } | 277 } |
| 277 | 278 |
| 278 // static | 279 // static |
| 279 scoped_ptr<base::SharedMemory> UserScriptLoader::Serialize( | 280 std::unique_ptr<base::SharedMemory> UserScriptLoader::Serialize( |
| 280 const UserScriptList& scripts) { | 281 const UserScriptList& scripts) { |
| 281 base::Pickle pickle; | 282 base::Pickle pickle; |
| 282 pickle.WriteUInt32(scripts.size()); | 283 pickle.WriteUInt32(scripts.size()); |
| 283 for (const UserScript& script : scripts) { | 284 for (const UserScript& script : scripts) { |
| 284 // TODO(aa): This can be replaced by sending content script metadata to | 285 // TODO(aa): This can be replaced by sending content script metadata to |
| 285 // renderers along with other extension data in ExtensionMsg_Loaded. | 286 // renderers along with other extension data in ExtensionMsg_Loaded. |
| 286 // See crbug.com/70516. | 287 // See crbug.com/70516. |
| 287 script.Pickle(&pickle); | 288 script.Pickle(&pickle); |
| 288 // Write scripts as 'data' so that we can read it out in the slave without | 289 // Write scripts as 'data' so that we can read it out in the slave without |
| 289 // allocating a new string. | 290 // allocating a new string. |
| 290 for (const UserScript::File& script_file : script.js_scripts()) { | 291 for (const UserScript::File& script_file : script.js_scripts()) { |
| 291 base::StringPiece contents = script_file.GetContent(); | 292 base::StringPiece contents = script_file.GetContent(); |
| 292 pickle.WriteData(contents.data(), contents.length()); | 293 pickle.WriteData(contents.data(), contents.length()); |
| 293 } | 294 } |
| 294 for (const UserScript::File& script_file : script.css_scripts()) { | 295 for (const UserScript::File& script_file : script.css_scripts()) { |
| 295 base::StringPiece contents = script_file.GetContent(); | 296 base::StringPiece contents = script_file.GetContent(); |
| 296 pickle.WriteData(contents.data(), contents.length()); | 297 pickle.WriteData(contents.data(), contents.length()); |
| 297 } | 298 } |
| 298 } | 299 } |
| 299 | 300 |
| 300 // Create the shared memory object. | 301 // Create the shared memory object. |
| 301 base::SharedMemory shared_memory; | 302 base::SharedMemory shared_memory; |
| 302 | 303 |
| 303 base::SharedMemoryCreateOptions options; | 304 base::SharedMemoryCreateOptions options; |
| 304 options.size = pickle.size(); | 305 options.size = pickle.size(); |
| 305 options.share_read_only = true; | 306 options.share_read_only = true; |
| 306 if (!shared_memory.Create(options)) | 307 if (!shared_memory.Create(options)) |
| 307 return scoped_ptr<base::SharedMemory>(); | 308 return std::unique_ptr<base::SharedMemory>(); |
| 308 | 309 |
| 309 if (!shared_memory.Map(pickle.size())) | 310 if (!shared_memory.Map(pickle.size())) |
| 310 return scoped_ptr<base::SharedMemory>(); | 311 return std::unique_ptr<base::SharedMemory>(); |
| 311 | 312 |
| 312 // Copy the pickle to shared memory. | 313 // Copy the pickle to shared memory. |
| 313 memcpy(shared_memory.memory(), pickle.data(), pickle.size()); | 314 memcpy(shared_memory.memory(), pickle.data(), pickle.size()); |
| 314 | 315 |
| 315 base::SharedMemoryHandle readonly_handle; | 316 base::SharedMemoryHandle readonly_handle; |
| 316 if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(), | 317 if (!shared_memory.ShareReadOnlyToProcess(base::GetCurrentProcessHandle(), |
| 317 &readonly_handle)) | 318 &readonly_handle)) |
| 318 return scoped_ptr<base::SharedMemory>(); | 319 return std::unique_ptr<base::SharedMemory>(); |
| 319 | 320 |
| 320 return make_scoped_ptr(new base::SharedMemory(readonly_handle, | 321 return base::WrapUnique(new base::SharedMemory(readonly_handle, |
| 321 /*read_only=*/true)); | 322 /*read_only=*/true)); |
| 322 } | 323 } |
| 323 | 324 |
| 324 void UserScriptLoader::AddObserver(Observer* observer) { | 325 void UserScriptLoader::AddObserver(Observer* observer) { |
| 325 observers_.AddObserver(observer); | 326 observers_.AddObserver(observer); |
| 326 } | 327 } |
| 327 | 328 |
| 328 void UserScriptLoader::RemoveObserver(Observer* observer) { | 329 void UserScriptLoader::RemoveObserver(Observer* observer) { |
| 329 observers_.RemoveObserver(observer); | 330 observers_.RemoveObserver(observer); |
| 330 } | 331 } |
| 331 | 332 |
| 332 void UserScriptLoader::SetReady(bool ready) { | 333 void UserScriptLoader::SetReady(bool ready) { |
| 333 bool was_ready = ready_; | 334 bool was_ready = ready_; |
| 334 ready_ = ready; | 335 ready_ = ready; |
| 335 if (ready_ && !was_ready) | 336 if (ready_ && !was_ready) |
| 336 AttemptLoad(); | 337 AttemptLoad(); |
| 337 } | 338 } |
| 338 | 339 |
| 339 void UserScriptLoader::OnScriptsLoaded( | 340 void UserScriptLoader::OnScriptsLoaded( |
| 340 scoped_ptr<UserScriptList> user_scripts, | 341 std::unique_ptr<UserScriptList> user_scripts, |
| 341 scoped_ptr<base::SharedMemory> shared_memory) { | 342 std::unique_ptr<base::SharedMemory> shared_memory) { |
| 342 user_scripts_.reset(user_scripts.release()); | 343 user_scripts_.reset(user_scripts.release()); |
| 343 if (pending_load_) { | 344 if (pending_load_) { |
| 344 // While we were loading, there were further changes. Don't bother | 345 // While we were loading, there were further changes. Don't bother |
| 345 // notifying about these scripts and instead just immediately reload. | 346 // notifying about these scripts and instead just immediately reload. |
| 346 pending_load_ = false; | 347 pending_load_ = false; |
| 347 StartLoad(); | 348 StartLoad(); |
| 348 return; | 349 return; |
| 349 } | 350 } |
| 350 | 351 |
| 351 if (shared_memory.get() == NULL) { | 352 if (shared_memory.get() == NULL) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) | 401 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) |
| 401 return; // This can legitimately fail if the renderer asserts at startup. | 402 return; // This can legitimately fail if the renderer asserts at startup. |
| 402 | 403 |
| 403 if (base::SharedMemory::IsHandleValid(handle_for_process)) { | 404 if (base::SharedMemory::IsHandleValid(handle_for_process)) { |
| 404 process->Send(new ExtensionMsg_UpdateUserScripts( | 405 process->Send(new ExtensionMsg_UpdateUserScripts( |
| 405 handle_for_process, host_id(), changed_hosts, whitelisted_only)); | 406 handle_for_process, host_id(), changed_hosts, whitelisted_only)); |
| 406 } | 407 } |
| 407 } | 408 } |
| 408 | 409 |
| 409 } // namespace extensions | 410 } // namespace extensions |
| OLD | NEW |