| 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 "chrome/browser/extensions/user_script_loader.h" | 5 #include "extensions/browser/user_script_loader.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/version.h" | 14 #include "base/version.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 18 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 20 #include "extensions/browser/content_verifier.h" | 20 #include "extensions/browser/content_verifier.h" |
| 21 #include "extensions/browser/extensions_browser_client.h" |
| 22 #include "extensions/browser/notification_types.h" |
| 21 #include "extensions/common/extension_messages.h" | 23 #include "extensions/common/extension_messages.h" |
| 22 #include "extensions/common/file_util.h" | 24 #include "extensions/common/file_util.h" |
| 23 | 25 |
| 24 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 using content::BrowserContext; |
| 25 | 28 |
| 26 namespace extensions { | 29 namespace extensions { |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 | 32 |
| 30 using LoadScriptsCallback = | 33 using LoadScriptsCallback = |
| 31 base::Callback<void(scoped_ptr<UserScriptList>, | 34 base::Callback<void(scoped_ptr<UserScriptList>, |
| 32 scoped_ptr<base::SharedMemory>)>; | 35 scoped_ptr<base::SharedMemory>)>; |
| 33 | 36 |
| 34 UserScriptLoader::SubstitutionMap* GetLocalizationMessages( | 37 UserScriptLoader::SubstitutionMap* GetLocalizationMessages( |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 it != user_scripts->end(); | 257 it != user_scripts->end(); |
| 255 ++it) { | 258 ++it) { |
| 256 added_script_ids.insert(it->id()); | 259 added_script_ids.insert(it->id()); |
| 257 } | 260 } |
| 258 LoadUserScripts(user_scripts, info, added_script_ids, | 261 LoadUserScripts(user_scripts, info, added_script_ids, |
| 259 NULL /* no verifier for testing */, | 262 NULL /* no verifier for testing */, |
| 260 GetLoadUserScriptsFunction()); | 263 GetLoadUserScriptsFunction()); |
| 261 } | 264 } |
| 262 | 265 |
| 263 UserScriptLoader::UserScriptLoader( | 266 UserScriptLoader::UserScriptLoader( |
| 264 Profile* profile, | 267 BrowserContext* browser_context, |
| 265 const HostID& host_id, | 268 const HostID& host_id, |
| 266 const scoped_refptr<ContentVerifier>& content_verifier) | 269 const scoped_refptr<ContentVerifier>& content_verifier) |
| 267 : user_scripts_(new UserScriptList()), | 270 : user_scripts_(new UserScriptList()), |
| 268 clear_scripts_(false), | 271 clear_scripts_(false), |
| 269 ready_(false), | 272 ready_(false), |
| 270 pending_load_(false), | 273 pending_load_(false), |
| 271 profile_(profile), | 274 browser_context_(browser_context), |
| 272 host_id_(host_id), | 275 host_id_(host_id), |
| 273 content_verifier_(content_verifier), | 276 content_verifier_(content_verifier), |
| 274 weak_factory_(this) { | 277 weak_factory_(this) { |
| 275 registrar_.Add(this, | 278 registrar_.Add(this, |
| 276 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 279 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 277 content::NotificationService::AllBrowserContextsAndSources()); | 280 content::NotificationService::AllBrowserContextsAndSources()); |
| 278 } | 281 } |
| 279 | 282 |
| 280 UserScriptLoader::~UserScriptLoader() { | 283 UserScriptLoader::~UserScriptLoader() { |
| 281 } | 284 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 306 removed_scripts_.clear(); | 309 removed_scripts_.clear(); |
| 307 AttemptLoad(); | 310 AttemptLoad(); |
| 308 } | 311 } |
| 309 | 312 |
| 310 void UserScriptLoader::Observe(int type, | 313 void UserScriptLoader::Observe(int type, |
| 311 const content::NotificationSource& source, | 314 const content::NotificationSource& source, |
| 312 const content::NotificationDetails& details) { | 315 const content::NotificationDetails& details) { |
| 313 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); | 316 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); |
| 314 content::RenderProcessHost* process = | 317 content::RenderProcessHost* process = |
| 315 content::Source<content::RenderProcessHost>(source).ptr(); | 318 content::Source<content::RenderProcessHost>(source).ptr(); |
| 316 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); | 319 if (!ExtensionsBrowserClient::Get()->IsSameContext( |
| 317 if (!profile_->IsSameProfile(profile)) | 320 browser_context_, process->GetBrowserContext())) |
| 318 return; | 321 return; |
| 319 if (scripts_ready()) { | 322 if (scripts_ready()) { |
| 320 SendUpdate(process, shared_memory_.get(), | 323 SendUpdate(process, shared_memory_.get(), |
| 321 std::set<HostID>()); // Include all hosts. | 324 std::set<HostID>()); // Include all hosts. |
| 322 } | 325 } |
| 323 } | 326 } |
| 324 | 327 |
| 325 bool UserScriptLoader::ScriptsMayHaveChanged() const { | 328 bool UserScriptLoader::ScriptsMayHaveChanged() const { |
| 326 // Scripts may have changed if there are scripts added, scripts removed, or | 329 // Scripts may have changed if there are scripts added, scripts removed, or |
| 327 // if scripts were cleared and either: | 330 // if scripts were cleared and either: |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 for (content::RenderProcessHost::iterator i( | 447 for (content::RenderProcessHost::iterator i( |
| 445 content::RenderProcessHost::AllHostsIterator()); | 448 content::RenderProcessHost::AllHostsIterator()); |
| 446 !i.IsAtEnd(); | 449 !i.IsAtEnd(); |
| 447 i.Advance()) { | 450 i.Advance()) { |
| 448 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_hosts_); | 451 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_hosts_); |
| 449 } | 452 } |
| 450 changed_hosts_.clear(); | 453 changed_hosts_.clear(); |
| 451 | 454 |
| 452 content::NotificationService::current()->Notify( | 455 content::NotificationService::current()->Notify( |
| 453 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, | 456 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, |
| 454 content::Source<Profile>(profile_), | 457 content::Source<BrowserContext>(browser_context_), |
| 455 content::Details<base::SharedMemory>(shared_memory_.get())); | 458 content::Details<base::SharedMemory>(shared_memory_.get())); |
| 456 } | 459 } |
| 457 | 460 |
| 458 void UserScriptLoader::SendUpdate(content::RenderProcessHost* process, | 461 void UserScriptLoader::SendUpdate(content::RenderProcessHost* process, |
| 459 base::SharedMemory* shared_memory, | 462 base::SharedMemory* shared_memory, |
| 460 const std::set<HostID>& changed_hosts) { | 463 const std::set<HostID>& changed_hosts) { |
| 461 // Don't allow injection of content scripts into <webview>. | 464 // Don't allow injection of content scripts into <webview>. |
| 462 if (process->IsIsolatedGuest()) | 465 if (process->IsIsolatedGuest()) |
| 463 return; | 466 return; |
| 464 | 467 |
| 465 Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext()); | 468 // Make sure we only send user scripts to processes in our browser_context. |
| 466 // Make sure we only send user scripts to processes in our profile. | 469 if (!ExtensionsBrowserClient::Get()->IsSameContext( |
| 467 if (!profile_->IsSameProfile(profile)) | 470 browser_context_, process->GetBrowserContext())) |
| 468 return; | 471 return; |
| 469 | 472 |
| 470 // If the process is being started asynchronously, early return. We'll end up | 473 // If the process is being started asynchronously, early return. We'll end up |
| 471 // calling InitUserScripts when it's created which will call this again. | 474 // calling InitUserScripts when it's created which will call this again. |
| 472 base::ProcessHandle handle = process->GetHandle(); | 475 base::ProcessHandle handle = process->GetHandle(); |
| 473 if (!handle) | 476 if (!handle) |
| 474 return; | 477 return; |
| 475 | 478 |
| 476 base::SharedMemoryHandle handle_for_process; | 479 base::SharedMemoryHandle handle_for_process; |
| 477 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) | 480 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) |
| 478 return; // This can legitimately fail if the renderer asserts at startup. | 481 return; // This can legitimately fail if the renderer asserts at startup. |
| 479 | 482 |
| 480 // TODO(hanxi): update the IPC message to send a set of HostIDs to render. | 483 // TODO(hanxi): update the IPC message to send a set of HostIDs to render. |
| 481 // Also, remove this function when the refactor is done on render side. | 484 // Also, remove this function when the refactor is done on render side. |
| 482 std::set<std::string> changed_ids_set; | 485 std::set<std::string> changed_ids_set; |
| 483 for (const HostID& id : changed_hosts) | 486 for (const HostID& id : changed_hosts) |
| 484 changed_ids_set.insert(id.id()); | 487 changed_ids_set.insert(id.id()); |
| 485 | 488 |
| 486 if (base::SharedMemory::IsHandleValid(handle_for_process)) { | 489 if (base::SharedMemory::IsHandleValid(handle_for_process)) { |
| 487 process->Send(new ExtensionMsg_UpdateUserScripts( | 490 process->Send(new ExtensionMsg_UpdateUserScripts( |
| 488 handle_for_process, host_id().id(), changed_ids_set)); | 491 handle_for_process, host_id().id(), changed_ids_set)); |
| 489 } | 492 } |
| 490 } | 493 } |
| 491 | 494 |
| 492 } // namespace extensions | 495 } // namespace extensions |
| OLD | NEW |