michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef CHROME_COMMON_CHILD_THREAD_H_ michael@0: #define CHROME_COMMON_CHILD_THREAD_H_ michael@0: michael@0: #include "base/thread.h" michael@0: #include "chrome/common/ipc_sync_channel.h" michael@0: #include "chrome/common/message_router.h" michael@0: michael@0: class ResourceDispatcher; michael@0: michael@0: // Child processes's background thread should derive from this class. michael@0: class ChildThread : public IPC::Channel::Listener, michael@0: public IPC::Message::Sender, michael@0: public base::Thread { michael@0: public: michael@0: // Creates the thread. michael@0: ChildThread(Thread::Options options); michael@0: virtual ~ChildThread(); michael@0: michael@0: // IPC::Message::Sender implementation: michael@0: virtual bool Send(IPC::Message* msg); michael@0: michael@0: // See documentation on MessageRouter for AddRoute and RemoveRoute michael@0: void AddRoute(int32_t routing_id, IPC::Channel::Listener* listener); michael@0: void RemoveRoute(int32_t routing_id); michael@0: michael@0: MessageLoop* owner_loop() { return owner_loop_; } michael@0: michael@0: protected: michael@0: friend class ChildProcess; michael@0: michael@0: // Starts the thread. michael@0: bool Run(); michael@0: michael@0: // Overrides the channel name. Used for --single-process mode. michael@0: void SetChannelName(const std::wstring& name) { channel_name_ = name; } michael@0: michael@0: // Called when the process refcount is 0. michael@0: void OnProcessFinalRelease(); michael@0: michael@0: protected: michael@0: // The required stack size if V8 runs on a thread. michael@0: static const size_t kV8StackSize; michael@0: michael@0: virtual void OnControlMessageReceived(const IPC::Message& msg) { } michael@0: michael@0: // Returns the one child thread. michael@0: static ChildThread* current(); michael@0: michael@0: IPC::Channel* channel() { return channel_.get(); } michael@0: michael@0: // Thread implementation. michael@0: virtual void Init(); michael@0: virtual void CleanUp(); michael@0: michael@0: private: michael@0: // IPC::Channel::Listener implementation: michael@0: virtual void OnMessageReceived(const IPC::Message& msg); michael@0: virtual void OnChannelError(); michael@0: michael@0: #ifdef MOZ_NUWA_PROCESS michael@0: static void MarkThread(); michael@0: #endif michael@0: michael@0: // The message loop used to run tasks on the thread that started this thread. michael@0: MessageLoop* owner_loop_; michael@0: michael@0: std::wstring channel_name_; michael@0: scoped_ptr channel_; michael@0: michael@0: // Used only on the background render thread to implement message routing michael@0: // functionality to the consumers of the ChildThread. michael@0: MessageRouter router_; michael@0: michael@0: Thread::Options options_; michael@0: michael@0: // If true, checks with the browser process before shutdown. This avoids race michael@0: // conditions if the process refcount is 0 but there's an IPC message inflight michael@0: // that would addref it. michael@0: bool check_with_browser_before_shutdown_; michael@0: michael@0: DISALLOW_EVIL_CONSTRUCTORS(ChildThread); michael@0: }; michael@0: michael@0: #endif // CHROME_COMMON_CHILD_THREAD_H_