blob: 4b27ab7968b387883ec1f69ae8e9d9e02b6b4ab5 [file] [log] [blame]
[email protected]7ef40ffe12011-03-08 05:05:281// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]8930d472009-02-21 08:05:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7ef40ffe12011-03-08 05:05:285#ifndef CONTENT_COMMON_CHILD_THREAD_H_
6#define CONTENT_COMMON_CHILD_THREAD_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]8930d472009-02-21 08:05:288
[email protected]7bf730952009-05-29 09:31:159#include "base/basictypes.h"
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/scoped_ptr.h"
[email protected]b2e375f2011-03-03 23:20:2411#include "content/common/message_router.h"
[email protected]a83d42292010-08-17 22:51:1012#include "webkit/glue/resource_loader_bridge.h"
[email protected]8930d472009-02-21 08:05:2813
[email protected]52785d4c2010-08-25 19:44:4914class FileSystemDispatcher;
[email protected]a83d42292010-08-17 22:51:1015class MessageLoop;
[email protected]42f1d7822009-07-23 18:17:5516class NotificationService;
[email protected]a83d42292010-08-17 22:51:1017class ResourceDispatcher;
[email protected]7661f672009-12-07 06:13:3918class SocketStreamDispatcher;
[email protected]42f1d7822009-07-23 18:17:5519
[email protected]1e9499c2010-04-06 20:33:3620namespace IPC {
[email protected]a83d42292010-08-17 22:51:1021class SyncChannel;
[email protected]1e9499c2010-04-06 20:33:3622class SyncMessageFilter;
23}
24
[email protected]42f1d7822009-07-23 18:17:5525// The main thread of a child process derives from this class.
[email protected]8930d472009-02-21 08:05:2826class ChildThread : public IPC::Channel::Listener,
[email protected]42f1d7822009-07-23 18:17:5527 public IPC::Message::Sender {
[email protected]8930d472009-02-21 08:05:2828 public:
29 // Creates the thread.
[email protected]42f1d7822009-07-23 18:17:5530 ChildThread();
31 // Used for single-process mode.
[email protected]11f4857282009-11-13 19:56:1732 explicit ChildThread(const std::string& channel_name);
[email protected]8930d472009-02-21 08:05:2833 virtual ~ChildThread();
34
35 // IPC::Message::Sender implementation:
36 virtual bool Send(IPC::Message* msg);
37
38 // See documentation on MessageRouter for AddRoute and RemoveRoute
39 void AddRoute(int32 routing_id, IPC::Channel::Listener* listener);
40 void RemoveRoute(int32 routing_id);
41
[email protected]c1f50aa2010-02-18 03:46:5742 IPC::Channel::Listener* ResolveRoute(int32 routing_id);
43
[email protected]46b0d4a2009-12-19 00:46:3344 // Creates a ResourceLoaderBridge.
45 // Tests can override this method if they want a custom loading behavior.
46 virtual webkit_glue::ResourceLoaderBridge* CreateBridge(
[email protected]50285ff2011-03-11 23:10:5647 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info);
[email protected]46b0d4a2009-12-19 00:46:3348
[email protected]a83d42292010-08-17 22:51:1049 ResourceDispatcher* resource_dispatcher();
[email protected]eb9989092009-03-12 21:42:5250
[email protected]7661f672009-12-07 06:13:3951 SocketStreamDispatcher* socket_stream_dispatcher() {
52 return socket_stream_dispatcher_.get();
53 }
54
[email protected]52785d4c2010-08-25 19:44:4955 FileSystemDispatcher* file_system_dispatcher() const {
56 return file_system_dispatcher_.get();
57 }
58
[email protected]1e9499c2010-04-06 20:33:3659 // Safe to call on any thread, as long as it's guaranteed that the thread's
60 // lifetime is less than the main thread.
[email protected]a83d42292010-08-17 22:51:1061 IPC::SyncMessageFilter* sync_message_filter();
[email protected]1e9499c2010-04-06 20:33:3662
[email protected]a83d42292010-08-17 22:51:1063 MessageLoop* message_loop();
[email protected]42f1d7822009-07-23 18:17:5564
[email protected]f2330fe2009-06-16 00:37:0965 // Returns the one child thread.
66 static ChildThread* current();
67
[email protected]8930d472009-02-21 08:05:2868 protected:
69 friend class ChildProcess;
70
[email protected]51d70e02009-03-27 20:45:5971 // Called when the process refcount is 0.
72 void OnProcessFinalRelease();
73
[email protected]d2f05d02011-01-27 18:51:0174 virtual bool OnControlMessageReceived(const IPC::Message& msg);
[email protected]d55aaa132009-09-28 21:08:0475 virtual void OnAskBeforeShutdown();
76 virtual void OnShutdown();
77
78#ifdef IPC_MESSAGE_LOG_ENABLED
79 virtual void OnSetIPCLoggingEnabled(bool enable);
80#endif
[email protected]8930d472009-02-21 08:05:2881
[email protected]8930d472009-02-21 08:05:2882 IPC::SyncChannel* channel() { return channel_.get(); }
83
[email protected]ecc0738c2010-02-26 19:16:1684 void set_on_channel_error_called(bool on_channel_error_called) {
85 on_channel_error_called_ = on_channel_error_called;
86 }
87
[email protected]9291ed12009-07-23 17:33:2288 private:
[email protected]42f1d7822009-07-23 18:17:5589 void Init();
90
[email protected]8930d472009-02-21 08:05:2891 // IPC::Channel::Listener implementation:
[email protected]a95986a82010-12-24 06:19:2892 virtual bool OnMessageReceived(const IPC::Message& msg);
[email protected]8930d472009-02-21 08:05:2893 virtual void OnChannelError();
94
[email protected]9a3a293b2009-06-04 22:28:1695 std::string channel_name_;
[email protected]8930d472009-02-21 08:05:2896 scoped_ptr<IPC::SyncChannel> channel_;
97
[email protected]1e9499c2010-04-06 20:33:3698 // Allows threads other than the main thread to send sync messages.
99 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
100
[email protected]42f1d7822009-07-23 18:17:55101 // Implements message routing functionality to the consumers of ChildThread.
[email protected]8930d472009-02-21 08:05:28102 MessageRouter router_;
103
[email protected]eb9989092009-03-12 21:42:52104 // Handles resource loads for this process.
[email protected]eb9989092009-03-12 21:42:52105 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
106
[email protected]7661f672009-12-07 06:13:39107 // Handles SocketStream for this process.
108 scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_;
109
[email protected]51d70e02009-03-27 20:45:59110 // If true, checks with the browser process before shutdown. This avoids race
111 // conditions if the process refcount is 0 but there's an IPC message inflight
112 // that would addref it.
113 bool check_with_browser_before_shutdown_;
114
[email protected]ecc0738c2010-02-26 19:16:16115 // The OnChannelError() callback was invoked - the channel is dead, don't
116 // attempt to communicate.
117 bool on_channel_error_called_;
118
[email protected]42f1d7822009-07-23 18:17:55119 MessageLoop* message_loop_;
120
121 scoped_ptr<NotificationService> notification_service_;
122
[email protected]52785d4c2010-08-25 19:44:49123 scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
124
[email protected]7bf730952009-05-29 09:31:15125 DISALLOW_COPY_AND_ASSIGN(ChildThread);
[email protected]8930d472009-02-21 08:05:28126};
127
[email protected]7ef40ffe12011-03-08 05:05:28128#endif // CONTENT_COMMON_CHILD_THREAD_H_