| 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" |
| 11 #include "chrome/common/plugin_messages.h" | 12 #include "chrome/common/plugin_messages.h" |
| 12 #include "ipc/ipc_logging.h" | 13 #include "ipc/ipc_logging.h" |
| 13 #include "webkit/glue/webkit_glue.h" | 14 #include "webkit/glue/webkit_glue.h" |
| 14 | 15 |
| 15 | 16 |
| 16 // V8 needs a 1MB stack size. | 17 ChildThread::ChildThread() { |
| 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_); | |
| 25 channel_name_ = WideToASCII( | 18 channel_name_ = WideToASCII( |
| 26 CommandLine::ForCurrentProcess()->GetSwitchValue( | 19 CommandLine::ForCurrentProcess()->GetSwitchValue( |
| 27 switches::kProcessChannelID)); | 20 switches::kProcessChannelID)); |
| 21 Init(); |
| 22 } |
| 28 | 23 |
| 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(); |
| 29 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) { | 32 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) { |
| 30 webkit_glue::SetUserAgent(WideToUTF8( | 33 webkit_glue::SetUserAgent(WideToUTF8( |
| 31 CommandLine::ForCurrentProcess()->GetSwitchValue( | 34 CommandLine::ForCurrentProcess()->GetSwitchValue( |
| 32 switches::kUserAgent))); | 35 switches::kUserAgent))); |
| 33 } | 36 } |
| 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); |
| 34 } | 52 } |
| 35 | 53 |
| 36 ChildThread::~ChildThread() { | 54 ChildThread::~ChildThread() { |
| 37 } | 55 #ifdef IPC_MESSAGE_LOG_ENABLED |
| 56 IPC::Logging::current()->SetIPCSender(NULL); |
| 57 #endif |
| 38 | 58 |
| 39 bool ChildThread::Run() { | 59 // The ChannelProxy object caches a pointer to the IPC thread, so need to |
| 40 return StartWithOptions(options_); | 60 // reset it as it's not guaranteed to outlive this object. |
| 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(); |
| 41 } | 68 } |
| 42 | 69 |
| 43 void ChildThread::OnChannelError() { | 70 void ChildThread::OnChannelError() { |
| 44 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 71 MessageLoop::current()->Quit(); |
| 45 } | 72 } |
| 46 | 73 |
| 47 bool ChildThread::Send(IPC::Message* msg) { | 74 bool ChildThread::Send(IPC::Message* msg) { |
| 48 if (!channel_.get()) { | 75 if (!channel_.get()) { |
| 49 delete msg; | 76 delete msg; |
| 50 return false; | 77 return false; |
| 51 } | 78 } |
| 52 | 79 |
| 53 return channel_->Send(msg); | 80 return channel_->Send(msg); |
| 54 } | 81 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 69 // Resource responses are sent to the resource dispatcher. | 96 // Resource responses are sent to the resource dispatcher. |
| 70 if (resource_dispatcher_->OnMessageReceived(msg)) | 97 if (resource_dispatcher_->OnMessageReceived(msg)) |
| 71 return; | 98 return; |
| 72 | 99 |
| 73 if (msg.type() == PluginProcessMsg_AskBeforeShutdown::ID) { | 100 if (msg.type() == PluginProcessMsg_AskBeforeShutdown::ID) { |
| 74 check_with_browser_before_shutdown_ = true; | 101 check_with_browser_before_shutdown_ = true; |
| 75 return; | 102 return; |
| 76 } | 103 } |
| 77 | 104 |
| 78 if (msg.type() == PluginProcessMsg_Shutdown::ID) { | 105 if (msg.type() == PluginProcessMsg_Shutdown::ID) { |
| 79 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 106 MessageLoop::current()->Quit(); |
| 80 return; | 107 return; |
| 81 } | 108 } |
| 82 | 109 |
| 83 if (msg.routing_id() == MSG_ROUTING_CONTROL) { | 110 if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 84 OnControlMessageReceived(msg); | 111 OnControlMessageReceived(msg); |
| 85 } else { | 112 } else { |
| 86 router_.OnMessageReceived(msg); | 113 router_.OnMessageReceived(msg); |
| 87 } | 114 } |
| 88 } | 115 } |
| 89 | 116 |
| 90 ChildThread* ChildThread::current() { | 117 ChildThread* ChildThread::current() { |
| 91 return ChildProcess::current()->child_thread(); | 118 return ChildProcess::current()->main_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(); | |
| 113 } | 119 } |
| 114 | 120 |
| 115 void ChildThread::OnProcessFinalRelease() { | 121 void ChildThread::OnProcessFinalRelease() { |
| 116 if (!check_with_browser_before_shutdown_) { | 122 if (!check_with_browser_before_shutdown_) { |
| 117 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 123 MessageLoop::current()->Quit(); |
| 118 return; | 124 return; |
| 119 } | 125 } |
| 120 | 126 |
| 121 // The child process shutdown sequence is a request response based mechanism, | 127 // The child process shutdown sequence is a request response based mechanism, |
| 122 // where we send out an initial feeler request to the child process host | 128 // where we send out an initial feeler request to the child process host |
| 123 // instance in the browser to verify if it's ok to shutdown the child process. | 129 // instance in the browser to verify if it's ok to shutdown the child process. |
| 124 // The browser then sends back a response if it's ok to shutdown. | 130 // The browser then sends back a response if it's ok to shutdown. |
| 125 Send(new PluginProcessHostMsg_ShutdownRequest); | 131 Send(new PluginProcessHostMsg_ShutdownRequest); |
| 126 } | 132 } |
| OLD | NEW |