| 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/notification_service.h" | |
| 12 #include "chrome/common/plugin_messages.h" | 11 #include "chrome/common/plugin_messages.h" |
| 13 #include "chrome/common/socket_stream_dispatcher.h" | |
| 14 #include "ipc/ipc_logging.h" | |
| 15 #include "ipc/ipc_message.h" | |
| 16 #include "ipc/ipc_sync_message_filter.h" | |
| 17 #include "ipc/ipc_switches.h" | |
| 18 #include "webkit/glue/webkit_glue.h" | 12 #include "webkit/glue/webkit_glue.h" |
| 19 | 13 |
| 20 | 14 |
| 21 ChildThread::ChildThread() { | 15 ChildThread::ChildThread() : base::MpChildThread() { |
| 22 channel_name_ = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 23 switches::kProcessChannelID); | |
| 24 Init(); | 16 Init(); |
| 25 } | 17 } |
| 26 | 18 |
| 27 ChildThread::ChildThread(const std::string& channel_name) | 19 ChildThread::ChildThread(const std::string& channel_name) |
| 28 : channel_name_(channel_name) { | 20 : base::MpChildThread(channel_name) { |
| 29 Init(); | 21 Init(); |
| 30 } | 22 } |
| 31 | 23 |
| 24 ChildThread::~ChildThread() { |
| 25 } |
| 26 |
| 32 void ChildThread::Init() { | 27 void ChildThread::Init() { |
| 33 check_with_browser_before_shutdown_ = false; | 28 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) { |
| 34 on_channel_error_called_ = false; | |
| 35 message_loop_ = MessageLoop::current(); | |
| 36 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) { | |
| 37 webkit_glue::SetUserAgent( | 29 webkit_glue::SetUserAgent( |
| 38 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 30 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 39 switches::kUserAgent)); | 31 switches::kUserAgent)); |
| 40 } | 32 } |
| 41 | 33 |
| 42 channel_.reset(new IPC::SyncChannel(channel_name_, | |
| 43 IPC::Channel::MODE_CLIENT, this, NULL, | |
| 44 ChildProcess::current()->io_message_loop(), true, | |
| 45 ChildProcess::current()->GetShutDownEvent())); | |
| 46 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 47 IPC::Logging::current()->SetIPCSender(this); | |
| 48 #endif | |
| 49 | |
| 50 resource_dispatcher_.reset(new ResourceDispatcher(this)); | 34 resource_dispatcher_.reset(new ResourceDispatcher(this)); |
| 51 socket_stream_dispatcher_.reset(new SocketStreamDispatcher()); | 35 socket_stream_dispatcher_.reset(new SocketStreamDispatcher()); |
| 52 | 36 |
| 53 sync_message_filter_ = | |
| 54 new IPC::SyncMessageFilter(ChildProcess::current()->GetShutDownEvent()); | |
| 55 channel_->AddFilter(sync_message_filter_.get()); | |
| 56 | |
| 57 // When running in unit tests, there is already a NotificationService object. | 37 // When running in unit tests, there is already a NotificationService object. |
| 58 // Since only one can exist at a time per thread, check first. | 38 // Since only one can exist at a time per thread, check first. |
| 59 if (!NotificationService::current()) | 39 if (!NotificationService::current()) |
| 60 notification_service_.reset(new NotificationService); | 40 notification_service_.reset(new NotificationService); |
| 61 } | 41 } |
| 62 | 42 |
| 63 ChildThread::~ChildThread() { | |
| 64 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 65 IPC::Logging::current()->SetIPCSender(NULL); | |
| 66 #endif | |
| 67 | |
| 68 channel_->RemoveFilter(sync_message_filter_.get()); | |
| 69 | |
| 70 // The ChannelProxy object caches a pointer to the IPC thread, so need to | |
| 71 // reset it as it's not guaranteed to outlive this object. | |
| 72 // NOTE: this also has the side-effect of not closing the main IPC channel to | |
| 73 // the browser process. This is needed because this is the signal that the | |
| 74 // browser uses to know that this process has died, so we need it to be alive | |
| 75 // until this process is shut down, and the OS closes the handle | |
| 76 // automatically. We used to watch the object handle on Windows to do this, | |
| 77 // but it wasn't possible to do so on POSIX. | |
| 78 channel_->ClearIPCMessageLoop(); | |
| 79 } | |
| 80 | |
| 81 void ChildThread::OnChannelError() { | |
| 82 set_on_channel_error_called(true); | |
| 83 MessageLoop::current()->Quit(); | |
| 84 } | |
| 85 | |
| 86 bool ChildThread::Send(IPC::Message* msg) { | |
| 87 if (!channel_.get()) { | |
| 88 delete msg; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 return channel_->Send(msg); | |
| 93 } | |
| 94 | |
| 95 void ChildThread::AddRoute(int32 routing_id, IPC::Channel::Listener* listener) { | |
| 96 DCHECK(MessageLoop::current() == message_loop()); | |
| 97 | |
| 98 router_.AddRoute(routing_id, listener); | |
| 99 } | |
| 100 | |
| 101 void ChildThread::RemoveRoute(int32 routing_id) { | |
| 102 DCHECK(MessageLoop::current() == message_loop()); | |
| 103 | |
| 104 router_.RemoveRoute(routing_id); | |
| 105 } | |
| 106 | |
| 107 IPC::Channel::Listener* ChildThread::ResolveRoute(int32 routing_id) { | |
| 108 DCHECK(MessageLoop::current() == message_loop()); | |
| 109 | |
| 110 return router_.ResolveRoute(routing_id); | |
| 111 } | |
| 112 | |
| 113 webkit_glue::ResourceLoaderBridge* ChildThread::CreateBridge( | 43 webkit_glue::ResourceLoaderBridge* ChildThread::CreateBridge( |
| 114 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info, | 44 const webkit_glue::ResourceLoaderBridge::RequestInfo& request_info, |
| 115 int host_renderer_id, | 45 int host_renderer_id, |
| 116 int host_render_view_id) { | 46 int host_render_view_id) { |
| 117 return resource_dispatcher()-> | 47 return resource_dispatcher()-> |
| 118 CreateBridge(request_info, host_renderer_id, host_render_view_id); | 48 CreateBridge(request_info, host_renderer_id, host_render_view_id); |
| 119 } | 49 } |
| 120 | 50 |
| 121 | 51 |
| 122 void ChildThread::OnMessageReceived(const IPC::Message& msg) { | 52 void ChildThread::OnMessageReceived(const IPC::Message& msg) { |
| 123 // Resource responses are sent to the resource dispatcher. | 53 // Resource responses are sent to the resource dispatcher. |
| 124 if (resource_dispatcher_->OnMessageReceived(msg)) | 54 if (resource_dispatcher_->OnMessageReceived(msg)) |
| 125 return; | 55 return; |
| 126 if (socket_stream_dispatcher_->OnMessageReceived(msg)) | 56 if (socket_stream_dispatcher_->OnMessageReceived(msg)) |
| 127 return; | 57 return; |
| 128 | 58 |
| 129 bool handled = true; | 59 return base::MpChildThread::OnMessageReceived(msg); |
| 130 IPC_BEGIN_MESSAGE_MAP(ChildThread, msg) | |
| 131 IPC_MESSAGE_HANDLER(PluginProcessMsg_AskBeforeShutdown, OnAskBeforeShutdown) | |
| 132 IPC_MESSAGE_HANDLER(PluginProcessMsg_Shutdown, OnShutdown) | |
| 133 #if defined(IPC_MESSAGE_LOG_ENABLED) | |
| 134 IPC_MESSAGE_HANDLER(PluginProcessMsg_SetIPCLoggingEnabled, | |
| 135 OnSetIPCLoggingEnabled) | |
| 136 #endif | |
| 137 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 138 IPC_END_MESSAGE_MAP() | |
| 139 | |
| 140 if (handled) | |
| 141 return; | |
| 142 | |
| 143 if (msg.routing_id() == MSG_ROUTING_CONTROL) { | |
| 144 OnControlMessageReceived(msg); | |
| 145 } else { | |
| 146 router_.OnMessageReceived(msg); | |
| 147 } | |
| 148 } | 60 } |
| 149 | |
| 150 void ChildThread::OnAskBeforeShutdown() { | |
| 151 check_with_browser_before_shutdown_ = true; | |
| 152 } | |
| 153 | |
| 154 void ChildThread::OnShutdown() { | |
| 155 MessageLoop::current()->Quit(); | |
| 156 } | |
| 157 | |
| 158 #if defined(IPC_MESSAGE_LOG_ENABLED) | |
| 159 void ChildThread::OnSetIPCLoggingEnabled(bool enable) { | |
| 160 if (enable) | |
| 161 IPC::Logging::current()->Enable(); | |
| 162 else | |
| 163 IPC::Logging::current()->Disable(); | |
| 164 } | |
| 165 #endif // IPC_MESSAGE_LOG_ENABLED | |
| 166 | |
| 167 ChildThread* ChildThread::current() { | |
| 168 return ChildProcess::current()->main_thread(); | |
| 169 } | |
| 170 | |
| 171 void ChildThread::OnProcessFinalRelease() { | |
| 172 if (on_channel_error_called_ || !check_with_browser_before_shutdown_) { | |
| 173 MessageLoop::current()->Quit(); | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 // The child process shutdown sequence is a request response based mechanism, | |
| 178 // where we send out an initial feeler request to the child process host | |
| 179 // instance in the browser to verify if it's ok to shutdown the child process. | |
| 180 // The browser then sends back a response if it's ok to shutdown. | |
| 181 Send(new PluginProcessHostMsg_ShutdownRequest); | |
| 182 } | |
| OLD | NEW |