diff options
-rw-r--r-- | chrome/browser/browser.cc | 8 | ||||
-rw-r--r-- | chrome/browser/memory_details.cc | 9 | ||||
-rw-r--r-- | chrome/browser/plugin_process_host.cc | 142 | ||||
-rw-r--r-- | chrome/browser/plugin_process_host.h | 31 | ||||
-rw-r--r-- | chrome/browser/plugin_service.cc | 6 | ||||
-rw-r--r-- | chrome/browser/renderer_host/browser_render_process_host.cc | 17 | ||||
-rw-r--r-- | chrome/browser/renderer_host/browser_render_process_host.h | 4 | ||||
-rw-r--r-- | chrome/browser/task_manager_resource_providers.cc | 7 | ||||
-rw-r--r-- | chrome/common/child_process_host.cc | 180 | ||||
-rw-r--r-- | chrome/common/child_process_host.h | 108 | ||||
-rw-r--r-- | chrome/common/child_process_info.cc | 55 | ||||
-rw-r--r-- | chrome/common/child_process_info.h | 41 | ||||
-rw-r--r-- | chrome/common/common.scons | 4 | ||||
-rw-r--r-- | chrome/common/common.vcproj | 8 |
14 files changed, 364 insertions, 256 deletions
diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 06b6281..9ac4ed5 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -24,7 +24,6 @@ #include "chrome/browser/tab_contents/site_instance.h" #include "chrome/browser/tab_contents/tab_contents_type.h" #include "chrome/browser/tab_contents/web_contents.h" -#include "chrome/common/child_process_info.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/l10n_util.h" @@ -70,6 +69,7 @@ #include "chrome/browser/views/download_tab_view.h" #include "chrome/browser/views/location_bar_view.h" #include "chrome/browser/window_sizer.h" +#include "chrome/common/child_process_host.h" #include "chrome/common/win_util.h" #include "chromium_strings.h" @@ -97,10 +97,8 @@ class ReduceChildProcessesWorkingSetTask : public Task { public: virtual void Run() { #if defined(OS_WIN) - for (ChildProcessInfo::Iterator iter; !iter.Done(); ++iter) { - DCHECK(iter->process().handle()); - iter->process().ReduceWorkingSet(); - } + for (ChildProcessHost::Iterator iter; !iter.Done(); ++iter) + iter->ReduceWorkingSet(); #endif } }; diff --git a/chrome/browser/memory_details.cc b/chrome/browser/memory_details.cc index 2eb0135..7c81d6f 100644 --- a/chrome/browser/memory_details.cc +++ b/chrome/browser/memory_details.cc @@ -18,7 +18,7 @@ #include "chrome/browser/renderer_host/render_view_host.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/tab_contents/web_contents.h" -#include "chrome/common/child_process_info.h" +#include "chrome/common/child_process_host.h" class RenderViewHostDelegate; @@ -73,12 +73,9 @@ void MemoryDetails::CollectChildInfoOnIOThread() { std::vector<ProcessMemoryInformation> child_info; // Collect the list of child processes. - for (ChildProcessInfo::Iterator iter; !iter.Done(); ++iter) { - if (!iter->process().handle()) - continue; - + for (ChildProcessHost::Iterator iter; !iter.Done(); ++iter) { ProcessMemoryInformation info; - info.pid = iter->process().pid(); + info.pid = iter->pid(); if (!info.pid) continue; diff --git a/chrome/browser/plugin_process_host.cc b/chrome/browser/plugin_process_host.cc index f14524d..03a59ba 100644 --- a/chrome/browser/plugin_process_host.cc +++ b/chrome/browser/plugin_process_host.cc @@ -30,12 +30,8 @@ #include "chrome/common/chrome_plugin_lib.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/debug_flags.h" -#include "chrome/common/ipc_logging.h" #include "chrome/common/logging_chrome.h" -#include "chrome/common/notification_service.h" -#include "chrome/common/notification_type.h" #include "chrome/common/plugin_messages.h" -#include "chrome/common/process_watcher.h" #include "chrome/common/render_messages.h" #include "chrome/common/win_util.h" #include "net/base/cookie_monster.h" @@ -47,26 +43,6 @@ static const char kDefaultPluginFinderURL[] = "http://dl.google.com/chrome/plugins/plugins2.xml"; -// The NotificationTask is used to notify about plugin process connection/ -// disconnection. It is needed because the notifications in the -// NotificationService must happen in the main thread. -class PluginNotificationTask : public Task { - public: - PluginNotificationTask(NotificationType notification_type, - ChildProcessInfo* info) - : notification_type_(notification_type), info_(*info) { } - - virtual void Run() { - NotificationService::current()-> - Notify(notification_type_, NotificationService::AllSources(), - Details<ChildProcessInfo>(&info_)); - } - - private: - NotificationType notification_type_; - ChildProcessInfo info_; -}; - // The PluginDownloadUrlHelper is used to handle one download URL request // from the plugin. Each download request is handled by a new instance @@ -357,32 +333,26 @@ class DestroyWindowTask : public Task { }; -PluginProcessHost::PluginProcessHost() - : ChildProcessInfo(PLUGIN_PROCESS), - opening_channel_(false), +PluginProcessHost::PluginProcessHost(MessageLoop* main_message_loop) + : ChildProcessHost(PLUGIN_PROCESS, main_message_loop), ALLOW_THIS_IN_INITIALIZER_LIST(resolve_proxy_msg_helper_(this, NULL)) { } PluginProcessHost::~PluginProcessHost() { - if (process().handle()) { - watcher_.StopWatching(); - ProcessWatcher::EnsureProcessTerminated(process().handle()); - } + // Cancel all requests for plugin processes. + // TODO(mpcomplete): use a real process ID when http://b/issue?id=1210062 is + // fixed. + PluginService::GetInstance()->resource_dispatcher_host()-> + CancelRequestsForProcess(-1); } bool PluginProcessHost::Init(const WebPluginInfo& info, const std::string& activex_clsid, const std::wstring& locale) { - DCHECK(channel_.get() == NULL); - info_ = info; set_name(info_.name); - channel_id_ = GenerateRandomChannelID(this); - channel_.reset(new IPC::Channel(channel_id_, - IPC::Channel::MODE_SERVER, - this)); - if (!channel_->Connect()) + if (!CreateChannel()) return false; // build command line for plugin, we have to quote the plugin's path to deal @@ -450,8 +420,7 @@ bool PluginProcessHost::Init(const WebPluginInfo& info, cmd_line.AppendSwitchWithValue(switches::kProcessType, switches::kPluginProcess); - cmd_line.AppendSwitchWithValue(switches::kProcessChannelID, - channel_id_); + cmd_line.AppendSwitchWithValue(switches::kProcessChannelID, channel_id()); cmd_line.AppendSwitchWithValue(switches::kPluginPath, info.path.ToWStringHack()); @@ -494,7 +463,7 @@ bool PluginProcessHost::Init(const WebPluginInfo& info, ResumeThread(target.hThread); CloseHandle(target.hThread); - process().set_handle(target.hProcess); + SetHandle(target.hProcess); // Help the process a little. It can't start the debugger by itself if // the process is in a sandbox. @@ -505,11 +474,9 @@ bool PluginProcessHost::Init(const WebPluginInfo& info, HANDLE handle; if (!base::LaunchApp(cmd_line, false, false, &handle)) return false; - process().set_handle(handle); + SetHandle(handle); } - watcher_.StartWatching(process().handle(), this); - FilePath gears_path; if (PathService::Get(chrome::FILE_GEARS_PLUGIN, &gears_path)) { FilePath::StringType gears_path_lc = StringToLowerASCII(gears_path.value()); @@ -518,65 +485,14 @@ bool PluginProcessHost::Init(const WebPluginInfo& info, if (plugin_path_lc == gears_path_lc) { // Give Gears plugins "background" priority. See // http://b/issue?id=1280317. - process().SetProcessBackgrounded(true); + SetProcessBackgrounded(); } } - opening_channel_ = true; - return true; } -bool PluginProcessHost::Send(IPC::Message* msg) { - if (!channel_.get()) { - delete msg; - return false; - } - return channel_->Send(msg); -} - -// indicates the plugin process has exited -void PluginProcessHost::OnObjectSignaled(HANDLE object) { - DCHECK(process().handle()); - DCHECK_EQ(object, process().handle()); - - bool did_crash = base::DidProcessCrash(object); - - if (did_crash) { - // Report that this plugin crashed. - PluginService::GetInstance()->main_message_loop()->PostTask( - FROM_HERE, new PluginNotificationTask( - NotificationType::CHILD_PROCESS_CRASHED, this)); - } - // Notify in the main loop of the disconnection. - PluginService::GetInstance()->main_message_loop()->PostTask( - FROM_HERE, new PluginNotificationTask( - NotificationType::CHILD_PROCESS_HOST_DISCONNECTED, this)); - - // Cancel all requests for plugin processes. - // TODO(mpcomplete): use a real process ID when http://b/issue?id=1210062 is - // fixed. - PluginService::GetInstance()->resource_dispatcher_host()-> - CancelRequestsForProcess(-1); - - delete this; -} - - void PluginProcessHost::OnMessageReceived(const IPC::Message& msg) { -#ifdef IPC_MESSAGE_LOG_ENABLED - IPC::Logging* logger = IPC::Logging::current(); - if (msg.type() == IPC_LOGGING_ID) { - logger->OnReceivedLoggingMessage(msg); - return; - } -#endif - -#ifdef IPC_MESSAGE_LOG_ENABLED - if (logger->Enabled()) - logger->OnPreDispatchMessage(msg); -#endif - IPC_BEGIN_MESSAGE_MAP(PluginProcessHost, msg) IPC_MESSAGE_HANDLER(PluginProcessHostMsg_ChannelCreated, OnChannelCreated) IPC_MESSAGE_HANDLER(PluginProcessHostMsg_DownloadUrl, OnDownloadUrl) @@ -598,16 +514,9 @@ void PluginProcessHost::OnMessageReceived(const IPC::Message& msg) { IPC_MESSAGE_HANDLER(PluginProcessHostMsg_DestroyWindow, OnDestroyWindow) IPC_MESSAGE_UNHANDLED_ERROR() IPC_END_MESSAGE_MAP() - -#ifdef IPC_MESSAGE_LOG_ENABLED - if (logger->Enabled()) - logger->OnPostDispatchMessage(msg, channel_id_); -#endif } void PluginProcessHost::OnChannelConnected(int32 peer_pid) { - opening_channel_ = false; - for (size_t i = 0; i < pending_requests_.size(); ++i) { RequestPluginChannel(pending_requests_[i].renderer_message_filter_.get(), pending_requests_[i].mime_type, @@ -615,15 +524,9 @@ void PluginProcessHost::OnChannelConnected(int32 peer_pid) { } pending_requests_.clear(); - - // Notify in the main loop of the connection. - PluginService::GetInstance()->main_message_loop()->PostTask( - FROM_HERE, new PluginNotificationTask( - NotificationType::CHILD_PROCESS_HOST_CONNECTED, this)); } void PluginProcessHost::OnChannelError() { - opening_channel_ = false; for (size_t i = 0; i < pending_requests_.size(); ++i) { ReplyToRenderer(pending_requests_[i].renderer_message_filter_.get(), std::wstring(), @@ -638,24 +541,13 @@ void PluginProcessHost::OpenChannelToPlugin( ResourceMessageFilter* renderer_message_filter, const std::string& mime_type, IPC::Message* reply_msg) { - // Notify in the main loop of the instantiation. - PluginService::GetInstance()->main_message_loop()->PostTask( - FROM_HERE, new PluginNotificationTask( - NotificationType::CHILD_INSTANCE_CREATED, this)); - - if (opening_channel_) { + InstanceCreated(); + if (opening_channel()) { pending_requests_.push_back( ChannelRequest(renderer_message_filter, mime_type, reply_msg)); return; } - if (!channel_.get()) { - // There was an error opening the channel, tell the renderer. - ReplyToRenderer(renderer_message_filter, std::wstring(), FilePath(), - reply_msg); - return; - } - // We already have an open channel, send a request right away to plugin. RequestPluginChannel(renderer_message_filter, mime_type, reply_msg); } @@ -675,7 +567,7 @@ void PluginProcessHost::OnRequestResource( context = Profile::GetDefaultRequestContext(); PluginService::GetInstance()->resource_dispatcher_host()-> - BeginRequest(this, process().handle(), render_process_host_id, + BeginRequest(this, handle(), render_process_host_id, MSG_ROUTING_CONTROL, request_id, request, context, NULL); } @@ -709,7 +601,7 @@ void PluginProcessHost::OnSyncLoad( context = Profile::GetDefaultRequestContext(); PluginService::GetInstance()->resource_dispatcher_host()-> - BeginRequest(this, process().handle(), render_process_host_id, + BeginRequest(this, handle(), render_process_host_id, MSG_ROUTING_CONTROL, request_id, request, context, sync_result); } @@ -764,7 +656,7 @@ void PluginProcessHost::RequestPluginChannel( HANDLE renderer_handle = NULL; BOOL result = DuplicateHandle(GetCurrentProcess(), renderer_message_filter->renderer_handle(), - process().handle(), &renderer_handle, 0, FALSE, + handle(), &renderer_handle, 0, FALSE, DUPLICATE_SAME_ACCESS); DCHECK(result); diff --git a/chrome/browser/plugin_process_host.h b/chrome/browser/plugin_process_host.h index f9b79f4..78b4f9d 100644 --- a/chrome/browser/plugin_process_host.h +++ b/chrome/browser/plugin_process_host.h @@ -8,13 +8,11 @@ #include <vector> #include "base/basictypes.h" -#include "base/object_watcher.h" #include "base/scoped_ptr.h" #include "base/task.h" #include "chrome/browser/net/resolve_proxy_msg_helper.h" #include "chrome/browser/renderer_host/resource_message_filter.h" -#include "chrome/common/child_process_info.h" -#include "chrome/common/ipc_channel.h" +#include "chrome/common/child_process_host.h" #include "webkit/glue/webplugin.h" class ResourceDispatcherHost; @@ -30,13 +28,10 @@ class GURL; // starting the plugin process when a plugin is created that doesn't already // have a process. After that, most of the communication is directly between // the renderer and plugin processes. -class PluginProcessHost : public ChildProcessInfo, - public IPC::Channel::Listener, - public IPC::Message::Sender, - public base::ObjectWatcher::Delegate, +class PluginProcessHost : public ChildProcessHost, public ResolveProxyMsgHelper::Delegate { public: - PluginProcessHost(); + PluginProcessHost(MessageLoop* main_message_loop); ~PluginProcessHost(); // Initialize the new plugin process, returning true on success. This must @@ -47,13 +42,6 @@ class PluginProcessHost : public ChildProcessInfo, const std::string& activex_clsid, const std::wstring& locale); - // IPC::Message::Sender implementation: - virtual bool Send(IPC::Message* msg); - - // ObjectWatcher::Delegate implementation: - virtual void OnObjectSignaled(HANDLE object); - - // IPC::Channel::Listener implementation: virtual void OnMessageReceived(const IPC::Message& msg); virtual void OnChannelConnected(int32 peer_pid); virtual void OnChannelError(); @@ -136,19 +124,6 @@ class PluginProcessHost : public ChildProcessInfo, // the plugin process, but haven't heard back about yet. std::vector<ChannelRequest> sent_requests_; - // Used to watch the plugin process handle. - base::ObjectWatcher watcher_; - - // true while we're waiting the channel to be opened. In the meantime, - // plugin instance requests will be buffered. - bool opening_channel_; - - // The IPC::Channel. - scoped_ptr<IPC::Channel> channel_; - - // IPC Channel's id. - std::wstring channel_id_; - // Information about the plugin. WebPluginInfo info_; diff --git a/chrome/browser/plugin_service.cc b/chrome/browser/plugin_service.cc index 87d2aeb..a0f7044 100644 --- a/chrome/browser/plugin_service.cc +++ b/chrome/browser/plugin_service.cc @@ -75,7 +75,7 @@ PluginProcessHost* PluginService::FindPluginProcess( return NULL; } - for (ChildProcessInfo::Iterator iter(ChildProcessInfo::PLUGIN_PROCESS); + for (ChildProcessHost::Iterator iter(ChildProcessInfo::PLUGIN_PROCESS); !iter.Done(); ++iter) { PluginProcessHost* plugin = static_cast<PluginProcessHost*>(*iter); if (plugin->info().path == plugin_path) @@ -102,7 +102,7 @@ PluginProcessHost* PluginService::FindOrStartPluginProcess( } // This plugin isn't loaded by any plugin process, so create a new process. - plugin_host = new PluginProcessHost(); + plugin_host = new PluginProcessHost(main_message_loop_); if (!plugin_host->Init(info, clsid, ui_locale_)) { DCHECK(false); // Init is not expected to fail delete plugin_host; @@ -170,7 +170,7 @@ void PluginService::Shutdown() { } void PluginService::OnShutdown() { - for (ChildProcessInfo::Iterator iter(ChildProcessInfo::PLUGIN_PROCESS); + for (ChildProcessHost::Iterator iter(ChildProcessInfo::PLUGIN_PROCESS); !iter.Done(); ++iter) { static_cast<PluginProcessHost*>(*iter)->Shutdown(); } diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc index 20bd867..eda2f1a 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.cc +++ b/chrome/browser/renderer_host/browser_render_process_host.cc @@ -33,6 +33,7 @@ #include "chrome/browser/renderer_host/resource_message_filter.h" #include "chrome/browser/visitedlink_master.h" #include "chrome/common/chrome_switches.h" +#include "chrome/common/child_process_info.h" #include "chrome/common/debug_flags.h" #include "chrome/common/logging_chrome.h" #include "chrome/common/notification_service.h" @@ -218,7 +219,8 @@ bool BrowserRenderProcessHost::Init() { const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); // setup IPC channel - const std::wstring channel_id = GenerateRandomChannelID(this); + const std::wstring channel_id = + ChildProcessInfo::GenerateRandomChannelID(this); channel_.reset( new IPC::SyncChannel(channel_id, IPC::Channel::MODE_SERVER, this, resource_message_filter, @@ -813,16 +815,3 @@ void BrowserRenderProcessHost::Observe(NotificationType type, } } } - -std::wstring GenerateRandomChannelID(void* instance) { - // Note: the string must start with the current process id, this is how - // child processes determine the pid of the parent. - // Build the channel ID. This is composed of a unique identifier for the - // parent browser process, an identifier for the renderer/plugin instance, - // and a random component. We use a random component so that a hacked child - // process can't cause denial of service by causing future named pipe creation - // to fail. - return StringPrintf(L"%d.%x.%d", - base::GetCurrentProcId(), instance, - base::RandInt(0, std::numeric_limits<int>::max())); -} diff --git a/chrome/browser/renderer_host/browser_render_process_host.h b/chrome/browser/renderer_host/browser_render_process_host.h index d089516..be7e225 100644 --- a/chrome/browser/renderer_host/browser_render_process_host.h +++ b/chrome/browser/renderer_host/browser_render_process_host.h @@ -143,8 +143,4 @@ class BrowserRenderProcessHost : public RenderProcessHost, DISALLOW_COPY_AND_ASSIGN(BrowserRenderProcessHost); }; -// Generates a unique channel name for a child renderer/plugin process. -// The "instance" pointer value is baked into the channel id. -std::wstring GenerateRandomChannelID(void* instance); - #endif // CHROME_BROWSER_RENDERER_HOST_BROWSER_RENDER_PROCESS_HOST_H_ diff --git a/chrome/browser/task_manager_resource_providers.cc b/chrome/browser/task_manager_resource_providers.cc index ec95cbe..59b83ec 100644 --- a/chrome/browser/task_manager_resource_providers.cc +++ b/chrome/browser/task_manager_resource_providers.cc @@ -16,6 +16,7 @@ #include "chrome/browser/renderer_host/resource_message_filter.h" #include "chrome/browser/tab_contents/tab_util.h" #include "chrome/browser/tab_contents/web_contents.h" +#include "chrome/common/child_process_host.h" #include "chrome/common/notification_service.h" #include "chrome/common/resource_bundle.h" #include "chrome/common/stl_util-inl.h" @@ -264,7 +265,7 @@ TaskManagerChildProcessResource::TaskManagerChildProcessResource( network_usage_support_(false) { // We cache the process id because it's not cheap to calculate, and it won't // be available when we get the plugin disconnected notification. - pid_ = child_proc.process().pid(); + pid_ = child_proc.pid(); if (!default_icon_) { ResourceBundle& rb = ResourceBundle::GetSharedInstance(); default_icon_ = rb.GetBitmapNamed(IDR_PLUGIN); @@ -288,7 +289,7 @@ SkBitmap TaskManagerChildProcessResource::GetIcon() const { } HANDLE TaskManagerChildProcessResource::GetProcess() const { - return (const_cast<ChildProcessInfo&>(child_process_)).process().handle(); + return child_process_.handle(); } //////////////////////////////////////////////////////////////////////////////// @@ -427,7 +428,7 @@ void TaskManagerChildProcessResourceProvider::AddToTaskManager( // The ChildProcessInfo::Iterator has to be used from the IO thread. void TaskManagerChildProcessResourceProvider::RetrieveChildProcessInfo() { - for (ChildProcessInfo::Iterator iter; !iter.Done(); ++iter) { + for (ChildProcessHost::Iterator iter; !iter.Done(); ++iter) { existing_child_process_info_.push_back(**iter); } // Now notify the UI thread that we have retrieved information about child diff --git a/chrome/common/child_process_host.cc b/chrome/common/child_process_host.cc new file mode 100644 index 0000000..14f2c8c --- /dev/null +++ b/chrome/common/child_process_host.cc @@ -0,0 +1,180 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/child_process_host.h" + +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/singleton.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/common/ipc_logging.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/process_watcher.h" + +typedef std::list<ChildProcessInfo*> ChildProcessList; + +// The NotificationTask is used to notify about plugin process connection/ +// disconnection. It is needed because the notifications in the +// NotificationService must happen in the main thread. +class ChildNotificationTask : public Task { + public: + ChildNotificationTask( + NotificationType notification_type, ChildProcessInfo* info) + : notification_type_(notification_type), info_(*info) { } + + virtual void Run() { + NotificationService::current()-> + Notify(notification_type_, NotificationService::AllSources(), + Details<ChildProcessInfo>(&info_)); + } + + private: + NotificationType notification_type_; + ChildProcessInfo info_; +}; + + +ChildProcessHost::ChildProcessHost( + ProcessType type, MessageLoop* main_message_loop) + : ChildProcessInfo(type), + main_message_loop_(main_message_loop), + opening_channel_(false), + ALLOW_THIS_IN_INITIALIZER_LIST(listener_(this)) { + Singleton<ChildProcessList>::get()->push_back(this); +} + + +ChildProcessHost::~ChildProcessHost() { + Singleton<ChildProcessList>::get()->remove(this); + + if (handle()) { + watcher_.StopWatching(); + ProcessWatcher::EnsureProcessTerminated(handle()); + } +} + +bool ChildProcessHost::CreateChannel() { + channel_id_ = GenerateRandomChannelID(this); + channel_.reset(new IPC::Channel( + channel_id_, IPC::Channel::MODE_SERVER, &listener_)); + if (!channel_->Connect()) + return false; + + opening_channel_ = true; + + return true; +} + +void ChildProcessHost::SetHandle(base::ProcessHandle process) { + DCHECK(handle() == NULL); + set_handle(process); + watcher_.StartWatching(process, this); +} + +void ChildProcessHost::InstanceCreated() { + Notify(NotificationType::CHILD_INSTANCE_CREATED); +} + +bool ChildProcessHost::Send(IPC::Message* msg) { + if (!channel_.get()) { + delete msg; + return false; + } + return channel_->Send(msg); +} + +void ChildProcessHost::Notify(NotificationType type) { + main_message_loop_->PostTask( + FROM_HERE, new ChildNotificationTask(type, this)); +} + +void ChildProcessHost::OnObjectSignaled(HANDLE object) { + DCHECK(handle()); + DCHECK_EQ(object, handle()); + + bool did_crash = base::DidProcessCrash(object); + if (did_crash) { + // Report that this child process crashed. + Notify(NotificationType::CHILD_PROCESS_CRASHED); + } + // Notify in the main loop of the disconnection. + Notify(NotificationType::CHILD_PROCESS_HOST_DISCONNECTED); + + delete this; +} + + + +ChildProcessHost::ListenerHook::ListenerHook(ChildProcessHost* host) + : host_(host) { +} + +void ChildProcessHost::ListenerHook::OnMessageReceived(const IPC::Message& msg) { +#ifdef IPC_MESSAGE_LOG_ENABLED + IPC::Logging* logger = IPC::Logging::current(); + if (msg.type() == IPC_LOGGING_ID) { + logger->OnReceivedLoggingMessage(msg); + return; + } + + if (logger->Enabled()) + logger->OnPreDispatchMessage(msg); +#endif + + host_->OnMessageReceived(msg); + +#ifdef IPC_MESSAGE_LOG_ENABLED + if (logger->Enabled()) + logger->OnPostDispatchMessage(msg, host_->channel_id_); +#endif +} + +void ChildProcessHost::ListenerHook::OnChannelConnected(int32 peer_pid) { + host_->opening_channel_ = false; + host_->OnChannelConnected(peer_pid); + + // Notify in the main loop of the connection. + host_->Notify(NotificationType::CHILD_PROCESS_HOST_CONNECTED); +} + +void ChildProcessHost::ListenerHook::OnChannelError() { + host_->opening_channel_ = false; + host_->OnChannelError(); +} + + +ChildProcessHost::Iterator::Iterator() : all_(true) { + iterator_ = Singleton<ChildProcessList>::get()->begin(); + DCHECK(MessageLoop::current() == + ChromeThread::GetMessageLoop(ChromeThread::IO)) << + "ChildProcessInfo::Iterator must be used on the IO thread."; +} + +ChildProcessHost::Iterator::Iterator(ProcessType type) + : all_(false), type_(type) { + iterator_ = Singleton<ChildProcessList>::get()->begin(); + DCHECK(MessageLoop::current() == + ChromeThread::GetMessageLoop(ChromeThread::IO)) << + "ChildProcessInfo::Iterator must be used on the IO thread."; +} + +ChildProcessInfo* ChildProcessHost::Iterator::operator++() { + do { + ++iterator_; + if (Done()) + break; + + if (!all_ && (*iterator_)->type() != type_) + continue; + + return *iterator_; + } while (true); + + return NULL; +} + +bool ChildProcessHost::Iterator::Done() { + return iterator_ == Singleton<ChildProcessList>::get()->end(); +} diff --git a/chrome/common/child_process_host.h b/chrome/common/child_process_host.h new file mode 100644 index 0000000..01db515 --- /dev/null +++ b/chrome/common/child_process_host.h @@ -0,0 +1,108 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_CHILD_PROCESS_HOST_H_ +#define CHROME_COMMON_CHILD_PROCESS_HOST_H_ + +#include <list> + +#include "base/basictypes.h" +#include "base/object_watcher.h" +#include "chrome/common/child_process_info.h" +#include "chrome/common/ipc_channel.h" + +class MessageLoop; +class NotificationType; + +// Plugins/workers and other child processes that live on the IO thread should +// derive from this class. +class ChildProcessHost : public ChildProcessInfo, + public base::ObjectWatcher::Delegate, + public IPC::Channel::Listener, + public IPC::Message::Sender { + public: + virtual ~ChildProcessHost(); + + // IPC::Message::Sender implementation: + virtual bool Send(IPC::Message* msg); + + // The Iterator class allows iteration through either all child processes, or + // ones of a specific type, depending on which constructor is used. Note that + // this should be done from the IO thread and that the iterator should not be + // kept around as it may be invalidated on subsequent event processing in the + // event loop. + class Iterator { + public: + Iterator(); + Iterator(ProcessType type); + ChildProcessInfo* operator->() { return *iterator_; } + ChildProcessInfo* operator*() { return *iterator_; } + ChildProcessInfo* operator++(); + bool Done(); + + private: + bool all_; + ProcessType type_; + std::list<ChildProcessInfo*>::iterator iterator_; + }; + + protected: + ChildProcessHost(ProcessType type, MessageLoop* main_message_loop); + + // Creates the IPC channel. Returns true iff it succeeded. + bool CreateChannel(); + + // Once the subclass gets a handle to the process, it needs to tell + // ChildProcessHost using this function. + void SetHandle(base::ProcessHandle handle); + + // Notifies us that an instance has been created on this child process. + void InstanceCreated(); + + // IPC::Channel::Listener implementation: + virtual void OnMessageReceived(const IPC::Message& msg) { } + virtual void OnChannelConnected(int32 peer_pid) { } + virtual void OnChannelError() { } + + bool opening_channel() { return opening_channel_; } + const std::wstring& channel_id() { return channel_id_; } + + private: + // Sends the given notification to the notification service on the UI thread. + void Notify(NotificationType type); + + // ObjectWatcher::Delegate implementation: + virtual void OnObjectSignaled(HANDLE object); + + // By using an internal class as the IPC::Channel::Listener, we can intercept + // OnMessageReceived/OnChannelConnected and do our own processing before + // calling the subclass' implementation. + class ListenerHook : public IPC::Channel::Listener { + public: + ListenerHook(ChildProcessHost* host); + virtual void OnMessageReceived(const IPC::Message& msg); + virtual void OnChannelConnected(int32 peer_pid); + virtual void OnChannelError(); + private: + ChildProcessHost* host_; + }; + + ListenerHook listener_; + + MessageLoop* main_message_loop_; + + // True while we're waiting the channel to be opened. + bool opening_channel_; + + // The IPC::Channel. + scoped_ptr<IPC::Channel> channel_; + + // IPC Channel's id. + std::wstring channel_id_; + + // Used to watch the child process handle. + base::ObjectWatcher watcher_; +}; + +#endif // CHROME_COMMON_CHILD_PROCESS_HOST_H_ diff --git a/chrome/common/child_process_info.cc b/chrome/common/child_process_info.cc index af69e10..cc35429 100644 --- a/chrome/common/child_process_info.cc +++ b/chrome/common/child_process_info.cc @@ -4,16 +4,16 @@ #include "chrome/common/child_process_info.h" +#include <limits> + #include "base/logging.h" -#include "base/singleton.h" -#include "chrome/browser/chrome_thread.h" +#include "base/process_util.h" +#include "base/rand_util.h" +#include "base/string_util.h" #include "chrome/common/l10n_util.h" #include "generated_resources.h" -typedef std::list<ChildProcessInfo*> ChildProcessList; - - std::wstring ChildProcessInfo::GetTypeNameInEnglish( ChildProcessInfo::ProcessType type) { switch (type) { @@ -61,45 +61,20 @@ ChildProcessInfo::ChildProcessInfo(ProcessType type) { // just a simple object that contains information about it. So add it to our // list of running processes. type_ = type; - Singleton<ChildProcessList>::get()->push_back(this); } ChildProcessInfo::~ChildProcessInfo() { - Singleton<ChildProcessList>::get()->remove(this); -} - - -ChildProcessInfo::Iterator::Iterator() : all_(true) { - iterator_ = Singleton<ChildProcessList>::get()->begin(); - DCHECK(MessageLoop::current() == - ChromeThread::GetMessageLoop(ChromeThread::IO)) << - "ChildProcessInfo::Iterator must be used on the IO thread."; -} - -ChildProcessInfo::Iterator::Iterator(ProcessType type) - : all_(false), type_(type) { - iterator_ = Singleton<ChildProcessList>::get()->begin(); - DCHECK(MessageLoop::current() == - ChromeThread::GetMessageLoop(ChromeThread::IO)) << - "ChildProcessInfo::Iterator must be used on the IO thread."; -} - -ChildProcessInfo* ChildProcessInfo::Iterator::operator++() { - do { - ++iterator_; - if (Done()) - break; - - if (!all_ && (*iterator_)->type() != type_) - continue; - - return *iterator_; - } while (true); - - return NULL; } -bool ChildProcessInfo::Iterator::Done() { - return iterator_ == Singleton<ChildProcessList>::get()->end(); +std::wstring ChildProcessInfo::GenerateRandomChannelID(void* instance) { + // Note: the string must start with the current process id, this is how + // child processes determine the pid of the parent. + // Build the channel ID. This is composed of a unique identifier for the + // parent browser process, an identifier for the child instance, and a random + // component. We use a random component so that a hacked child process can't + // cause denial of service by causing future named pipe creation to fail. + return StringPrintf(L"%d.%x.%d", + base::GetCurrentProcId(), instance, + base::RandInt(0, std::numeric_limits<int>::max())); } diff --git a/chrome/common/child_process_info.h b/chrome/common/child_process_info.h index cb34238..fff0959 100644 --- a/chrome/common/child_process_info.h +++ b/chrome/common/child_process_info.h @@ -5,16 +5,12 @@ #ifndef CHROME_COMMON_CHILD_PROCESS_INFO_H_ #define CHROME_COMMON_CHILD_PROCESS_INFO_H_ -#include <list> #include <string> #include "base/basictypes.h" #include "base/process.h" -class ChildProcessInfo; - -// Holds information about a child process. Plugins/workers and other child -// processes that live on the IO thread derive from this. +// Holds information about a child process. class ChildProcessInfo { public: enum ProcessType { @@ -32,8 +28,12 @@ class ChildProcessInfo { // for workers it might be the domain that it's from. std::wstring name() const { return name_; } - // Getter to the process. - base::Process& process() { return process_; } + // Getter to the process handle. + base::ProcessHandle handle() const { return process_.handle(); } + + int pid() const { return process_.pid(); } + void SetProcessBackgrounded() const { process_.SetProcessBackgrounded(true); } + void ReduceWorkingSet() const { process_.ReduceWorkingSet(); } // Returns an English name of the process type, should only be used for non // user-visible strings, or debugging pages like about:memory. @@ -58,7 +58,7 @@ class ChildProcessInfo { return *this; } - ~ChildProcessInfo(); + virtual ~ChildProcessInfo(); // We define the < operator so that the ChildProcessInfo can be used as a key // in a std::map. @@ -72,29 +72,14 @@ class ChildProcessInfo { return (process_.handle() == rhs.process_.handle()) && (name_ == rhs.name_); } - // The Iterator class allows iteration through either all child processes, or - // ones of a specific type, depending on which constructor is used. Note that - // this should be done from the IO thread and that the iterator should not be - // kept around as it may be invalidated on subsequent event processing in the - // event loop. - class Iterator { - public: - Iterator(); - Iterator(ProcessType type); - ChildProcessInfo* operator->() { return *iterator_; } - ChildProcessInfo* operator*() { return *iterator_; } - ChildProcessInfo* operator++(); - bool Done(); - - private: - bool all_; - ProcessType type_; - std::list<ChildProcessInfo*>::iterator iterator_; - }; + // Generates a unique channel name for a child renderer/plugin process. + // The "instance" pointer value is baked into the channel id. + static std::wstring GenerateRandomChannelID(void* instance); protected: void set_type(ProcessType type) { type_ = type; } void set_name(const std::wstring& name) { name_ = name; } + void set_handle(base::ProcessHandle handle) { process_.set_handle(handle); } // Derived objects need to use this constructor so we know what type we are. ChildProcessInfo(ProcessType type); @@ -110,7 +95,7 @@ class ChildProcessInfo { std::wstring name_; // The handle to the process. - base::Process process_; + mutable base::Process process_; }; #endif // CHROME_COMMON_CHILD_PROCESS_INFO_H_ diff --git a/chrome/common/common.scons b/chrome/common/common.scons index e5c709f..06a9888 100644 --- a/chrome/common/common.scons +++ b/chrome/common/common.scons @@ -106,6 +106,8 @@ input_files = ChromeFileList([ 'animation.h', 'child_process.cc', 'child_process.h', + 'child_process_host.cc', + 'child_process_host.h', 'child_process_info.cc', 'child_process_info.h', 'chrome_constants.cc', @@ -234,6 +236,8 @@ if not env.Bit('windows'): 'gfx/path.cc', 'os_exchange_data.cc', 'process_watcher.cc', + 'child_process_host.cc', + 'child_process_host.h', ) if not env.Bit('windows'): diff --git a/chrome/common/common.vcproj b/chrome/common/common.vcproj index 283ab6e..8ac8d29 100644 --- a/chrome/common/common.vcproj +++ b/chrome/common/common.vcproj @@ -342,6 +342,14 @@ > </File> <File + RelativePath=".\child_process_host.cc" + > + </File> + <File + RelativePath=".\child_process_host.h" + > + </File> + <File RelativePath=".\child_process_info.cc" > </File> |