diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-18 03:47:48 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-18 03:47:48 +0000 |
commit | 6dffde32e9bf00108ebe936b06f02fc80ed09fb4 (patch) | |
tree | d2da2ecf38102a7b7997bbf5a3542a588fbeb330 /chrome/common | |
parent | 8c6add3b6648649eeadb60abdecbf6a1bec01860 (diff) | |
download | chromium_src-6dffde32e9bf00108ebe936b06f02fc80ed09fb4.zip chromium_src-6dffde32e9bf00108ebe936b06f02fc80ed09fb4.tar.gz chromium_src-6dffde32e9bf00108ebe936b06f02fc80ed09fb4.tar.bz2 |
Take out common functionality from PluginProcessHost and move it to ChildProcessHost.
Review URL: http://codereview.chromium.org/21443
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9935 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-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 |
6 files changed, 328 insertions, 68 deletions
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> |