OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/child_thread.h" | 5 #include "chrome/common/child_thread.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "chrome/common/child_process.h" | 9 #include "chrome/common/child_process.h" |
10 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
11 #include "chrome/common/ipc_logging.h" | 11 #include "chrome/common/ipc_logging.h" |
12 #include "chrome/common/notification_service.h" | |
13 #include "chrome/common/plugin_messages.h" | 12 #include "chrome/common/plugin_messages.h" |
14 #include "webkit/glue/webkit_glue.h" | 13 #include "webkit/glue/webkit_glue.h" |
15 | 14 |
16 | 15 |
17 ChildThread::ChildThread() { | 16 // V8 needs a 1MB stack size. |
| 17 const size_t ChildThread::kV8StackSize = 1024 * 1024; |
| 18 |
| 19 ChildThread::ChildThread(Thread::Options options) |
| 20 : Thread("Chrome_ChildThread"), |
| 21 owner_loop_(MessageLoop::current()), |
| 22 options_(options), |
| 23 check_with_browser_before_shutdown_(false) { |
| 24 DCHECK(owner_loop_); |
18 channel_name_ = WideToASCII( | 25 channel_name_ = WideToASCII( |
19 CommandLine::ForCurrentProcess()->GetSwitchValue( | 26 CommandLine::ForCurrentProcess()->GetSwitchValue( |
20 switches::kProcessChannelID)); | 27 switches::kProcessChannelID)); |
21 Init(); | |
22 } | |
23 | 28 |
24 ChildThread::ChildThread(const std::string channel_name) | |
25 : channel_name_(channel_name) { | |
26 Init(); | |
27 } | |
28 | |
29 void ChildThread::Init() { | |
30 check_with_browser_before_shutdown_ = false; | |
31 message_loop_ = MessageLoop::current(); | |
32 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) { | 29 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) { |
33 webkit_glue::SetUserAgent(WideToUTF8( | 30 webkit_glue::SetUserAgent(WideToUTF8( |
34 CommandLine::ForCurrentProcess()->GetSwitchValue( | 31 CommandLine::ForCurrentProcess()->GetSwitchValue( |
35 switches::kUserAgent))); | 32 switches::kUserAgent))); |
36 } | 33 } |
37 | |
38 channel_.reset(new IPC::SyncChannel(channel_name_, | |
39 IPC::Channel::MODE_CLIENT, this, NULL, | |
40 ChildProcess::current()->io_message_loop(), true, | |
41 ChildProcess::current()->GetShutDownEvent())); | |
42 #ifdef IPC_MESSAGE_LOG_ENABLED | |
43 IPC::Logging::current()->SetIPCSender(this); | |
44 #endif | |
45 | |
46 resource_dispatcher_.reset(new ResourceDispatcher(this)); | |
47 | |
48 // When running in unit tests, there is already a NotificationService object. | |
49 // Since only one can exist at a time per thread, check first. | |
50 if (!NotificationService::current()) | |
51 notification_service_.reset(new NotificationService); | |
52 } | 34 } |
53 | 35 |
54 ChildThread::~ChildThread() { | 36 ChildThread::~ChildThread() { |
55 #ifdef IPC_MESSAGE_LOG_ENABLED | 37 } |
56 IPC::Logging::current()->SetIPCSender(NULL); | |
57 #endif | |
58 | 38 |
59 // The ChannelProxy object caches a pointer to the IPC thread, so need to | 39 bool ChildThread::Run() { |
60 // reset it as it's not guaranteed to outlive this object. | 40 return StartWithOptions(options_); |
61 // NOTE: this also has the side-effect of not closing the main IPC channel to | |
62 // the browser process. This is needed because this is the signal that the | |
63 // browser uses to know that this process has died, so we need it to be alive | |
64 // until this process is shut down, and the OS closes the handle | |
65 // automatically. We used to watch the object handle on Windows to do this, | |
66 // but it wasn't possible to do so on POSIX. | |
67 channel_->ClearIPCMessageLoop(); | |
68 } | 41 } |
69 | 42 |
70 void ChildThread::OnChannelError() { | 43 void ChildThread::OnChannelError() { |
71 MessageLoop::current()->Quit(); | 44 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
72 } | 45 } |
73 | 46 |
74 bool ChildThread::Send(IPC::Message* msg) { | 47 bool ChildThread::Send(IPC::Message* msg) { |
75 if (!channel_.get()) { | 48 if (!channel_.get()) { |
76 delete msg; | 49 delete msg; |
77 return false; | 50 return false; |
78 } | 51 } |
79 | 52 |
80 return channel_->Send(msg); | 53 return channel_->Send(msg); |
81 } | 54 } |
(...skipping 14 matching lines...) Expand all Loading... |
96 // Resource responses are sent to the resource dispatcher. | 69 // Resource responses are sent to the resource dispatcher. |
97 if (resource_dispatcher_->OnMessageReceived(msg)) | 70 if (resource_dispatcher_->OnMessageReceived(msg)) |
98 return; | 71 return; |
99 | 72 |
100 if (msg.type() == PluginProcessMsg_AskBeforeShutdown::ID) { | 73 if (msg.type() == PluginProcessMsg_AskBeforeShutdown::ID) { |
101 check_with_browser_before_shutdown_ = true; | 74 check_with_browser_before_shutdown_ = true; |
102 return; | 75 return; |
103 } | 76 } |
104 | 77 |
105 if (msg.type() == PluginProcessMsg_Shutdown::ID) { | 78 if (msg.type() == PluginProcessMsg_Shutdown::ID) { |
106 MessageLoop::current()->Quit(); | 79 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
107 return; | 80 return; |
108 } | 81 } |
109 | 82 |
110 if (msg.routing_id() == MSG_ROUTING_CONTROL) { | 83 if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
111 OnControlMessageReceived(msg); | 84 OnControlMessageReceived(msg); |
112 } else { | 85 } else { |
113 router_.OnMessageReceived(msg); | 86 router_.OnMessageReceived(msg); |
114 } | 87 } |
115 } | 88 } |
116 | 89 |
117 ChildThread* ChildThread::current() { | 90 ChildThread* ChildThread::current() { |
118 return ChildProcess::current()->main_thread(); | 91 return ChildProcess::current()->child_thread(); |
| 92 } |
| 93 |
| 94 void ChildThread::Init() { |
| 95 channel_.reset(new IPC::SyncChannel(channel_name_, |
| 96 IPC::Channel::MODE_CLIENT, this, NULL, owner_loop_, true, |
| 97 ChildProcess::current()->GetShutDownEvent())); |
| 98 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 99 IPC::Logging::current()->SetIPCSender(this); |
| 100 #endif |
| 101 |
| 102 resource_dispatcher_.reset(new ResourceDispatcher(this)); |
| 103 } |
| 104 |
| 105 void ChildThread::CleanUp() { |
| 106 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 107 IPC::Logging::current()->SetIPCSender(NULL); |
| 108 #endif |
| 109 // Need to destruct the SyncChannel to the browser before we go away because |
| 110 // it caches a pointer to this thread. |
| 111 channel_.reset(); |
| 112 resource_dispatcher_.reset(); |
119 } | 113 } |
120 | 114 |
121 void ChildThread::OnProcessFinalRelease() { | 115 void ChildThread::OnProcessFinalRelease() { |
122 if (!check_with_browser_before_shutdown_) { | 116 if (!check_with_browser_before_shutdown_) { |
123 MessageLoop::current()->Quit(); | 117 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
124 return; | 118 return; |
125 } | 119 } |
126 | 120 |
127 // The child process shutdown sequence is a request response based mechanism, | 121 // The child process shutdown sequence is a request response based mechanism, |
128 // where we send out an initial feeler request to the child process host | 122 // where we send out an initial feeler request to the child process host |
129 // instance in the browser to verify if it's ok to shutdown the child process. | 123 // instance in the browser to verify if it's ok to shutdown the child process. |
130 // The browser then sends back a response if it's ok to shutdown. | 124 // The browser then sends back a response if it's ok to shutdown. |
131 Send(new PluginProcessHostMsg_ShutdownRequest); | 125 Send(new PluginProcessHostMsg_ShutdownRequest); |
132 } | 126 } |
OLD | NEW |