blob: f612c353ea2b7c87c6cddf44d4166397ed8532f8 [file] [log] [blame]
[email protected]8930d472009-02-21 08:05:281// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_COMMON_CHILD_THREAD_H_
6#define CHROME_COMMON_CHILD_THREAD_H_
7
[email protected]7bf730952009-05-29 09:31:158#include "base/basictypes.h"
9#include "base/scoped_ptr.h"
[email protected]8930d472009-02-21 08:05:2810#include "base/thread.h"
[email protected]82e5ee82009-04-03 02:29:4511#include "chrome/common/ipc_sync_channel.h"
[email protected]8930d472009-02-21 08:05:2812#include "chrome/common/message_router.h"
[email protected]eb9989092009-03-12 21:42:5213#include "chrome/common/resource_dispatcher.h"
[email protected]8930d472009-02-21 08:05:2814
15// Child processes's background thread should derive from this class.
16class ChildThread : public IPC::Channel::Listener,
17 public IPC::Message::Sender,
18 public base::Thread {
19 public:
20 // Creates the thread.
21 ChildThread(Thread::Options options);
22 virtual ~ChildThread();
23
24 // IPC::Message::Sender implementation:
25 virtual bool Send(IPC::Message* msg);
26
27 // See documentation on MessageRouter for AddRoute and RemoveRoute
28 void AddRoute(int32 routing_id, IPC::Channel::Listener* listener);
29 void RemoveRoute(int32 routing_id);
30
31 MessageLoop* owner_loop() { return owner_loop_; }
32
[email protected]eb9989092009-03-12 21:42:5233 ResourceDispatcher* resource_dispatcher() {
34 return resource_dispatcher_.get();
35 }
36
[email protected]8930d472009-02-21 08:05:2837 protected:
38 friend class ChildProcess;
39
40 // Starts the thread.
41 bool Run();
42
43 // Overrides the channel name. Used for --single-process mode.
[email protected]9a3a293b2009-06-04 22:28:1644 void SetChannelName(const std::string& name) { channel_name_ = name; }
[email protected]8930d472009-02-21 08:05:2845
[email protected]51d70e02009-03-27 20:45:5946 // Called when the process refcount is 0.
47 void OnProcessFinalRelease();
48
[email protected]8930d472009-02-21 08:05:2849 protected:
[email protected]eb47a132009-03-04 00:39:5650 // The required stack size if V8 runs on a thread.
51 static const size_t kV8StackSize;
52
[email protected]8930d472009-02-21 08:05:2853 virtual void OnControlMessageReceived(const IPC::Message& msg) { }
54
55 // Returns the one child thread.
56 static ChildThread* current();
57
58 IPC::SyncChannel* channel() { return channel_.get(); }
59
[email protected]8930d472009-02-21 08:05:2860 // Thread implementation.
61 virtual void Init();
62 virtual void CleanUp();
63
64 private:
65 // IPC::Channel::Listener implementation:
66 virtual void OnMessageReceived(const IPC::Message& msg);
67 virtual void OnChannelError();
68
69 // The message loop used to run tasks on the thread that started this thread.
70 MessageLoop* owner_loop_;
71
[email protected]9a3a293b2009-06-04 22:28:1672 std::string channel_name_;
[email protected]8930d472009-02-21 08:05:2873 scoped_ptr<IPC::SyncChannel> channel_;
74
75 // Used only on the background render thread to implement message routing
76 // functionality to the consumers of the ChildThread.
77 MessageRouter router_;
78
[email protected]8930d472009-02-21 08:05:2879 Thread::Options options_;
80
[email protected]eb9989092009-03-12 21:42:5281 // Handles resource loads for this process.
82 // NOTE: this object lives on the owner thread.
83 scoped_ptr<ResourceDispatcher> resource_dispatcher_;
84
[email protected]51d70e02009-03-27 20:45:5985 // If true, checks with the browser process before shutdown. This avoids race
86 // conditions if the process refcount is 0 but there's an IPC message inflight
87 // that would addref it.
88 bool check_with_browser_before_shutdown_;
89
[email protected]7bf730952009-05-29 09:31:1590 DISALLOW_COPY_AND_ASSIGN(ChildThread);
[email protected]8930d472009-02-21 08:05:2891};
92
93#endif // CHROME_COMMON_CHILD_THREAD_H_