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/child_process_host.h | |
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/child_process_host.h')
-rw-r--r-- | chrome/common/child_process_host.h | 108 |
1 files changed, 108 insertions, 0 deletions
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_ |