Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2697)

Unified Diff: chrome/common/child_thread.cc

Issue 155944: Switch the first thread in a child process to be the main thread... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/child_thread.cc
===================================================================
--- chrome/common/child_thread.cc (revision 21342)
+++ chrome/common/child_thread.cc (working copy)
@@ -8,40 +8,67 @@
#include "base/command_line.h"
#include "chrome/common/child_process.h"
#include "chrome/common/chrome_switches.h"
+#include "chrome/common/notification_service.h"
#include "chrome/common/plugin_messages.h"
#include "ipc/ipc_logging.h"
#include "webkit/glue/webkit_glue.h"
-// V8 needs a 1MB stack size.
-const size_t ChildThread::kV8StackSize = 1024 * 1024;
-
-ChildThread::ChildThread(Thread::Options options)
- : Thread("Chrome_ChildThread"),
- owner_loop_(MessageLoop::current()),
- options_(options),
- check_with_browser_before_shutdown_(false) {
- DCHECK(owner_loop_);
+ChildThread::ChildThread() {
channel_name_ = WideToASCII(
CommandLine::ForCurrentProcess()->GetSwitchValue(
switches::kProcessChannelID));
+ Init();
+}
+ChildThread::ChildThread(const std::string channel_name)
+ : channel_name_(channel_name) {
+ Init();
+}
+
+void ChildThread::Init() {
+ check_with_browser_before_shutdown_ = false;
+ message_loop_ = MessageLoop::current();
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserAgent)) {
webkit_glue::SetUserAgent(WideToUTF8(
CommandLine::ForCurrentProcess()->GetSwitchValue(
switches::kUserAgent)));
}
+
+ channel_.reset(new IPC::SyncChannel(channel_name_,
+ IPC::Channel::MODE_CLIENT, this, NULL,
+ ChildProcess::current()->io_message_loop(), true,
+ ChildProcess::current()->GetShutDownEvent()));
+#ifdef IPC_MESSAGE_LOG_ENABLED
+ IPC::Logging::current()->SetIPCSender(this);
+#endif
+
+ resource_dispatcher_.reset(new ResourceDispatcher(this));
+
+ // When running in unit tests, there is already a NotificationService object.
+ // Since only one can exist at a time per thread, check first.
+ if (!NotificationService::current())
+ notification_service_.reset(new NotificationService);
}
ChildThread::~ChildThread() {
-}
+#ifdef IPC_MESSAGE_LOG_ENABLED
+ IPC::Logging::current()->SetIPCSender(NULL);
+#endif
-bool ChildThread::Run() {
- return StartWithOptions(options_);
+ // The ChannelProxy object caches a pointer to the IPC thread, so need to
+ // reset it as it's not guaranteed to outlive this object.
+ // NOTE: this also has the side-effect of not closing the main IPC channel to
+ // the browser process. This is needed because this is the signal that the
+ // browser uses to know that this process has died, so we need it to be alive
+ // until this process is shut down, and the OS closes the handle
+ // automatically. We used to watch the object handle on Windows to do this,
+ // but it wasn't possible to do so on POSIX.
+ channel_->ClearIPCMessageLoop();
}
void ChildThread::OnChannelError() {
- owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ MessageLoop::current()->Quit();
}
bool ChildThread::Send(IPC::Message* msg) {
@@ -76,7 +103,7 @@
}
if (msg.type() == PluginProcessMsg_Shutdown::ID) {
- owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ MessageLoop::current()->Quit();
return;
}
@@ -88,33 +115,12 @@
}
ChildThread* ChildThread::current() {
- return ChildProcess::current()->child_thread();
+ return ChildProcess::current()->main_thread();
}
-void ChildThread::Init() {
- channel_.reset(new IPC::SyncChannel(channel_name_,
- IPC::Channel::MODE_CLIENT, this, NULL, owner_loop_, true,
- ChildProcess::current()->GetShutDownEvent()));
-#ifdef IPC_MESSAGE_LOG_ENABLED
- IPC::Logging::current()->SetIPCSender(this);
-#endif
-
- resource_dispatcher_.reset(new ResourceDispatcher(this));
-}
-
-void ChildThread::CleanUp() {
-#ifdef IPC_MESSAGE_LOG_ENABLED
- IPC::Logging::current()->SetIPCSender(NULL);
-#endif
- // Need to destruct the SyncChannel to the browser before we go away because
- // it caches a pointer to this thread.
- channel_.reset();
- resource_dispatcher_.reset();
-}
-
void ChildThread::OnProcessFinalRelease() {
if (!check_with_browser_before_shutdown_) {
- owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
+ MessageLoop::current()->Quit();
return;
}

Powered by Google App Engine
This is Rietveld 408576698