OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // Represents the browser side of the browser <--> renderer communication | 5 // Represents the browser side of the browser <--> renderer communication |
6 // channel. There will be one RenderProcessHost per renderer process. | 6 // channel. There will be one RenderProcessHost per renderer process. |
7 | 7 |
8 #include "chrome/browser/render_process_host.h" | 8 #include "chrome/browser/render_process_host.h" |
9 | 9 |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 widget_helper_ = new RenderWidgetHelper(host_id_); | 159 widget_helper_ = new RenderWidgetHelper(host_id_); |
160 | 160 |
161 CacheManagerHost::GetInstance()->Add(host_id_); | 161 CacheManagerHost::GetInstance()->Add(host_id_); |
162 RendererSecurityPolicy::GetInstance()->Add(host_id_); | 162 RendererSecurityPolicy::GetInstance()->Add(host_id_); |
163 | 163 |
164 PrefService* prefs = profile->GetPrefs(); | 164 PrefService* prefs = profile->GetPrefs(); |
165 prefs->AddPrefObserver(prefs::kBlockPopups, this); | 165 prefs->AddPrefObserver(prefs::kBlockPopups, this); |
166 widget_helper_->set_block_popups( | 166 widget_helper_->set_block_popups( |
167 profile->GetPrefs()->GetBoolean(prefs::kBlockPopups)); | 167 profile->GetPrefs()->GetBoolean(prefs::kBlockPopups)); |
168 | 168 |
| 169 NotificationService::current()->AddObserver(this, |
| 170 NOTIFY_NEW_USER_SCRIPTS, |
| 171 NotificationService::AllSources()); |
| 172 |
169 // Note: When we create the RenderProcessHost, it's technically backgrounded, | 173 // Note: When we create the RenderProcessHost, it's technically backgrounded, |
170 // because it has no visible listeners. But the process doesn't | 174 // because it has no visible listeners. But the process doesn't |
171 // actually exist yet, so we'll Background it later, after creation. | 175 // actually exist yet, so we'll Background it later, after creation. |
172 } | 176 } |
173 | 177 |
174 RenderProcessHost::~RenderProcessHost() { | 178 RenderProcessHost::~RenderProcessHost() { |
175 // Some tests hold RenderProcessHost in a scoped_ptr, so we must call | 179 // Some tests hold RenderProcessHost in a scoped_ptr, so we must call |
176 // Unregister here as well as in response to Release(). | 180 // Unregister here as well as in response to Release(). |
177 Unregister(); | 181 Unregister(); |
178 | 182 |
179 // We may have some unsent messages at this point, but that's OK. | 183 // We may have some unsent messages at this point, but that's OK. |
180 channel_.reset(); | 184 channel_.reset(); |
181 | 185 |
182 if (process_.handle() && !run_renderer_in_process_) { | 186 if (process_.handle() && !run_renderer_in_process_) { |
183 watcher_.StopWatching(); | 187 watcher_.StopWatching(); |
184 ProcessWatcher::EnsureProcessTerminated(process_.handle()); | 188 ProcessWatcher::EnsureProcessTerminated(process_.handle()); |
185 } | 189 } |
186 | 190 |
187 profile_->GetPrefs()->RemovePrefObserver(prefs::kBlockPopups, this); | 191 profile_->GetPrefs()->RemovePrefObserver(prefs::kBlockPopups, this); |
| 192 |
| 193 NotificationService::current()->RemoveObserver(this, |
| 194 NOTIFY_NEW_USER_SCRIPTS, |
| 195 NotificationService::AllSources()); |
188 } | 196 } |
189 | 197 |
190 void RenderProcessHost::Unregister() { | 198 void RenderProcessHost::Unregister() { |
191 if (host_id_ >= 0) { | 199 if (host_id_ >= 0) { |
192 CacheManagerHost::GetInstance()->Remove(host_id_); | 200 CacheManagerHost::GetInstance()->Remove(host_id_); |
193 RendererSecurityPolicy::GetInstance()->Remove(host_id_); | 201 RendererSecurityPolicy::GetInstance()->Remove(host_id_); |
194 all_hosts.Remove(host_id_); | 202 all_hosts.Remove(host_id_); |
195 host_id_ = -1; | 203 host_id_ = -1; |
196 } | 204 } |
197 } | 205 } |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 process_.set_handle(process); | 427 process_.set_handle(process); |
420 } | 428 } |
421 | 429 |
422 watcher_.StartWatching(process_.handle(), this); | 430 watcher_.StartWatching(process_.handle(), this); |
423 } | 431 } |
424 } | 432 } |
425 | 433 |
426 // Now that the process is created, set it's backgrounding accordingly. | 434 // Now that the process is created, set it's backgrounding accordingly. |
427 SetBackgrounded(backgrounded_); | 435 SetBackgrounded(backgrounded_); |
428 | 436 |
429 // Send the process its initial VisitedLink and Greasemonkey data. | 437 InitVisitedLinks(); |
430 HANDLE target_process = process_.handle(); | 438 InitGreasemonkeyScripts(); |
431 if (!target_process) { | |
432 // Target process can be null if it's started with the --single-process | |
433 // flag. | |
434 target_process = GetCurrentProcess(); | |
435 } | |
436 | |
437 InitVisitedLinks(target_process); | |
438 InitGreasemonkeyScripts(target_process); | |
439 | 439 |
440 if (max_page_id_ != -1) | 440 if (max_page_id_ != -1) |
441 channel_->Send(new ViewMsg_SetNextPageID(max_page_id_ + 1)); | 441 channel_->Send(new ViewMsg_SetNextPageID(max_page_id_ + 1)); |
442 | 442 |
443 return true; | 443 return true; |
444 } | 444 } |
445 | 445 |
446 void RenderProcessHost::InitVisitedLinks(HANDLE target_process) { | 446 HANDLE RenderProcessHost::GetRendererProcessHandle() { |
| 447 HANDLE result = process_.handle(); |
| 448 if (!result) { |
| 449 // Renderer process can be null if it's started with the --single-process |
| 450 // flag. |
| 451 result = GetCurrentProcess(); |
| 452 } |
| 453 return result; |
| 454 } |
| 455 |
| 456 void RenderProcessHost::InitVisitedLinks() { |
447 VisitedLinkMaster* visitedlink_master = profile_->GetVisitedLinkMaster(); | 457 VisitedLinkMaster* visitedlink_master = profile_->GetVisitedLinkMaster(); |
448 if (!visitedlink_master) { | 458 if (!visitedlink_master) { |
449 return; | 459 return; |
450 } | 460 } |
451 | 461 |
452 SharedMemoryHandle handle_for_process = NULL; | 462 SharedMemoryHandle handle_for_process = NULL; |
453 visitedlink_master->ShareToProcess(target_process, &handle_for_process); | 463 visitedlink_master->ShareToProcess(GetRendererProcessHandle(), |
| 464 &handle_for_process); |
454 DCHECK(handle_for_process); | 465 DCHECK(handle_for_process); |
455 if (handle_for_process) { | 466 if (handle_for_process) { |
456 channel_->Send(new ViewMsg_VisitedLink_NewTable(handle_for_process)); | 467 channel_->Send(new ViewMsg_VisitedLink_NewTable(handle_for_process)); |
457 } | 468 } |
458 } | 469 } |
459 | 470 |
460 void RenderProcessHost::InitGreasemonkeyScripts(HANDLE target_process) { | 471 void RenderProcessHost::InitGreasemonkeyScripts() { |
461 CommandLine command_line; | 472 CommandLine command_line; |
462 if (!command_line.HasSwitch(switches::kEnableGreasemonkey)) { | 473 if (!command_line.HasSwitch(switches::kEnableGreasemonkey)) { |
463 return; | 474 return; |
464 } | 475 } |
465 | 476 |
466 // TODO(aa): Figure out lifetime and ownership of this object | 477 // TODO(aa): Figure out lifetime and ownership of this object |
467 // - VisitedLinkMaster is owned by Profile, but there has been talk of | 478 // - VisitedLinkMaster is owned by Profile, but there has been talk of |
468 // having scripts live elsewhere besides the profile. | 479 // having scripts live elsewhere besides the profile. |
469 // - File IO should be asynchronous (see VisitedLinkMaster), but how do we | 480 // - File IO should be asynchronous (see VisitedLinkMaster), but how do we |
470 // get scripts to the first renderer without blocking startup? Should we | 481 // get scripts to the first renderer without blocking startup? Should we |
471 // cache some information across restarts? | 482 // cache some information across restarts? |
472 GreasemonkeyMaster* greasemonkey_master = profile_->GetGreasemonkeyMaster(); | 483 GreasemonkeyMaster* greasemonkey_master = profile_->GetGreasemonkeyMaster(); |
473 if (!greasemonkey_master) { | 484 if (!greasemonkey_master) { |
474 return; | 485 return; |
475 } | 486 } |
476 | 487 |
477 if (!greasemonkey_master->ScriptsReady()) { | 488 if (!greasemonkey_master->ScriptsReady()) { |
478 // No scripts ready. :( | 489 // No scripts ready. :( |
479 return; | 490 return; |
480 } | 491 } |
481 | 492 |
| 493 // Update the renderer process with the current scripts. |
| 494 SendGreasemonkeyScriptsUpdate(greasemonkey_master->GetSharedMemory()); |
| 495 } |
| 496 |
| 497 void RenderProcessHost::SendGreasemonkeyScriptsUpdate( |
| 498 SharedMemory *shared_memory) { |
482 SharedMemoryHandle handle_for_process = NULL; | 499 SharedMemoryHandle handle_for_process = NULL; |
483 greasemonkey_master->ShareToProcess(target_process, &handle_for_process); | 500 shared_memory->ShareToProcess(GetRendererProcessHandle(), |
| 501 &handle_for_process); |
484 DCHECK(handle_for_process); | 502 DCHECK(handle_for_process); |
485 if (handle_for_process) { | 503 if (handle_for_process) { |
486 channel_->Send(new ViewMsg_Greasemonkey_NewScripts(handle_for_process)); | 504 channel_->Send(new ViewMsg_Greasemonkey_NewScripts(handle_for_process)); |
487 } | 505 } |
488 } | 506 } |
489 | 507 |
490 void RenderProcessHost::Attach(IPC::Channel::Listener* listener, | 508 void RenderProcessHost::Attach(IPC::Channel::Listener* listener, |
491 int routing_id) { | 509 int routing_id) { |
492 listeners_.AddWithID(listener, routing_id); | 510 listeners_.AddWithID(listener, routing_id); |
493 } | 511 } |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
795 std::wstring* pref_name_in = Details<std::wstring>(details).ptr(); | 813 std::wstring* pref_name_in = Details<std::wstring>(details).ptr(); |
796 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs()); | 814 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs()); |
797 if (*pref_name_in == prefs::kBlockPopups) { | 815 if (*pref_name_in == prefs::kBlockPopups) { |
798 widget_helper_->set_block_popups( | 816 widget_helper_->set_block_popups( |
799 profile()->GetPrefs()->GetBoolean(prefs::kBlockPopups)); | 817 profile()->GetPrefs()->GetBoolean(prefs::kBlockPopups)); |
800 } else { | 818 } else { |
801 NOTREACHED() << "unexpected pref change notification" << *pref_name_in; | 819 NOTREACHED() << "unexpected pref change notification" << *pref_name_in; |
802 } | 820 } |
803 break; | 821 break; |
804 } | 822 } |
| 823 case NOTIFY_NEW_USER_SCRIPTS: { |
| 824 SharedMemory* shared_memory = Details<SharedMemory>(details).ptr(); |
| 825 DCHECK(shared_memory); |
| 826 if (shared_memory) { |
| 827 SendGreasemonkeyScriptsUpdate(shared_memory); |
| 828 } |
| 829 break; |
| 830 } |
805 default: { | 831 default: { |
806 NOTREACHED(); | 832 NOTREACHED(); |
807 break; | 833 break; |
808 } | 834 } |
809 } | 835 } |
810 } | 836 } |
811 | 837 |
812 // static | 838 // static |
813 bool RenderProcessHost::ShouldTryToUseExistingProcessHost() { | 839 bool RenderProcessHost::ShouldTryToUseExistingProcessHost() { |
814 unsigned int renderer_process_count = | 840 unsigned int renderer_process_count = |
815 static_cast<unsigned int>(all_hosts.size()); | 841 static_cast<unsigned int>(all_hosts.size()); |
816 | 842 |
817 // NOTE: Sometimes it's necessary to create more render processes than | 843 // NOTE: Sometimes it's necessary to create more render processes than |
818 // GetMaxRendererProcessCount(), for instance when we want to create | 844 // GetMaxRendererProcessCount(), for instance when we want to create |
819 // a renderer process for a profile that has no existing renderers. | 845 // a renderer process for a profile that has no existing renderers. |
820 // This is OK in moderation, since the GetMaxRendererProcessCount() | 846 // This is OK in moderation, since the GetMaxRendererProcessCount() |
821 // is conservative. | 847 // is conservative. |
822 | 848 |
823 return run_renderer_in_process() || | 849 return run_renderer_in_process() || |
824 (renderer_process_count >= GetMaxRendererProcessCount()); | 850 (renderer_process_count >= GetMaxRendererProcessCount()); |
825 } | 851 } |
826 | 852 |
OLD | NEW |