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

Side by Side Diff: extensions/browser/user_script_loader.cc

Issue 959413003: Implement <webview>.addContentScript/removeContentScript API [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make the sync_IPC handled in IO thread. Created 5 years, 8 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
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/browser/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"
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 // though it removes the possibility that freeing the shared memory block 437 // though it removes the possibility that freeing the shared memory block
438 // would open up enough FDs for long enough for a retry to succeed. 438 // would open up enough FDs for long enough for a retry to succeed.
439 439
440 // Pretend the extension change didn't happen. 440 // Pretend the extension change didn't happen.
441 return; 441 return;
442 } 442 }
443 443
444 // We've got scripts ready to go. 444 // We've got scripts ready to go.
445 shared_memory_.reset(shared_memory.release()); 445 shared_memory_.reset(shared_memory.release());
446 446
447 // If user scripts are coming from a <webview>, will only notify the 447 for (content::RenderProcessHost::iterator i(
448 // RenderProcessHost of that <webview>; otherwise will notify all of the 448 content::RenderProcessHost::AllHostsIterator());
449 // RenderProcessHosts. 449 !i.IsAtEnd(); i.Advance()) {
450 if (user_scripts_ && !user_scripts_->empty() && 450 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_hosts_);
Fady Samuel 2015/04/01 00:00:32 SendUpdate should contain a flag indicating whethe
Xi Han 2015/04/01 22:14:42 Done.
451 (*user_scripts_)[0].consumer_instance_type() ==
452 UserScript::ConsumerInstanceType::WEBVIEW) {
453 DCHECK_EQ(1u, user_scripts_->size());
454 int render_process_id =
455 (*user_scripts_)[0].routing_info().render_process_id;
456 content::RenderProcessHost* host =
457 content::RenderProcessHost::FromID(render_process_id);
458 if (host)
459 SendUpdate(host, shared_memory_.get(), changed_hosts_);
460 } else {
461 for (content::RenderProcessHost::iterator i(
462 content::RenderProcessHost::AllHostsIterator());
463 !i.IsAtEnd(); i.Advance()) {
464 SendUpdate(i.GetCurrentValue(), shared_memory_.get(), changed_hosts_);
465 }
466 } 451 }
467 changed_hosts_.clear(); 452 changed_hosts_.clear();
468 453
469 content::NotificationService::current()->Notify( 454 content::NotificationService::current()->Notify(
470 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, 455 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
471 content::Source<BrowserContext>(browser_context_), 456 content::Source<BrowserContext>(browser_context_),
472 content::Details<base::SharedMemory>(shared_memory_.get())); 457 content::Details<base::SharedMemory>(shared_memory_.get()));
473 } 458 }
474 459
475 void UserScriptLoader::SendUpdate(content::RenderProcessHost* process, 460 void UserScriptLoader::SendUpdate(content::RenderProcessHost* process,
476 base::SharedMemory* shared_memory, 461 base::SharedMemory* shared_memory,
477 const std::set<HostID>& changed_hosts) { 462 const std::set<HostID>& changed_hosts) {
478 // Don't allow injection of content scripts into <webview>.
479 if (process->IsIsolatedGuest())
480 return;
481
482 // Make sure we only send user scripts to processes in our browser_context. 463 // Make sure we only send user scripts to processes in our browser_context.
483 if (!ExtensionsBrowserClient::Get()->IsSameContext( 464 if (!ExtensionsBrowserClient::Get()->IsSameContext(
484 browser_context_, process->GetBrowserContext())) 465 browser_context_, process->GetBrowserContext()))
485 return; 466 return;
486 467
487 // If the process is being started asynchronously, early return. We'll end up 468 // If the process is being started asynchronously, early return. We'll end up
488 // calling InitUserScripts when it's created which will call this again. 469 // calling InitUserScripts when it's created which will call this again.
489 base::ProcessHandle handle = process->GetHandle(); 470 base::ProcessHandle handle = process->GetHandle();
490 if (!handle) 471 if (!handle)
491 return; 472 return;
492 473
493 base::SharedMemoryHandle handle_for_process; 474 base::SharedMemoryHandle handle_for_process;
494 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) 475 if (!shared_memory->ShareToProcess(handle, &handle_for_process))
495 return; // This can legitimately fail if the renderer asserts at startup. 476 return; // This can legitimately fail if the renderer asserts at startup.
496 477
497 if (base::SharedMemory::IsHandleValid(handle_for_process)) { 478 if (base::SharedMemory::IsHandleValid(handle_for_process)) {
498 process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process, 479 process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process,
499 host_id(), changed_hosts)); 480 host_id(), changed_hosts));
500 } 481 }
501 } 482 }
502 483
503 } // namespace extensions 484 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698