ipc/chromium/src/chrome/common/child_process_host.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "chrome/common/child_process_host.h"
michael@0 6
michael@0 7 #include "base/compiler_specific.h"
michael@0 8 #include "base/logging.h"
michael@0 9 #include "base/message_loop.h"
michael@0 10 #include "base/process_util.h"
michael@0 11 #include "base/singleton.h"
michael@0 12 #include "base/waitable_event.h"
michael@0 13 #include "mozilla/ipc/ProcessChild.h"
michael@0 14 #include "mozilla/ipc/BrowserProcessSubThread.h"
michael@0 15 #include "mozilla/ipc/Transport.h"
michael@0 16 typedef mozilla::ipc::BrowserProcessSubThread ChromeThread;
michael@0 17 #include "chrome/common/ipc_logging.h"
michael@0 18 #include "chrome/common/notification_service.h"
michael@0 19 #include "chrome/common/notification_type.h"
michael@0 20 #include "chrome/common/process_watcher.h"
michael@0 21 #include "chrome/common/result_codes.h"
michael@0 22
michael@0 23 using mozilla::ipc::FileDescriptor;
michael@0 24
michael@0 25 namespace {
michael@0 26 typedef std::list<ChildProcessHost*> ChildProcessList;
michael@0 27
michael@0 28 // The NotificationTask is used to notify about plugin process connection/
michael@0 29 // disconnection. It is needed because the notifications in the
michael@0 30 // NotificationService must happen in the main thread.
michael@0 31 class ChildNotificationTask : public Task {
michael@0 32 public:
michael@0 33 ChildNotificationTask(
michael@0 34 NotificationType notification_type, ChildProcessInfo* info)
michael@0 35 : notification_type_(notification_type), info_(*info) { }
michael@0 36
michael@0 37 virtual void Run() {
michael@0 38 NotificationService::current()->
michael@0 39 Notify(notification_type_, NotificationService::AllSources(),
michael@0 40 Details<ChildProcessInfo>(&info_));
michael@0 41 }
michael@0 42
michael@0 43 private:
michael@0 44 NotificationType notification_type_;
michael@0 45 ChildProcessInfo info_;
michael@0 46 };
michael@0 47
michael@0 48 } // namespace
michael@0 49
michael@0 50
michael@0 51
michael@0 52 ChildProcessHost::ChildProcessHost(ProcessType type)
michael@0 53 :
michael@0 54 ChildProcessInfo(type),
michael@0 55 ALLOW_THIS_IN_INITIALIZER_LIST(listener_(this)),
michael@0 56 opening_channel_(false),
michael@0 57 process_event_(NULL) {
michael@0 58 Singleton<ChildProcessList>::get()->push_back(this);
michael@0 59 }
michael@0 60
michael@0 61
michael@0 62 ChildProcessHost::~ChildProcessHost() {
michael@0 63 Singleton<ChildProcessList>::get()->remove(this);
michael@0 64
michael@0 65 if (handle()) {
michael@0 66 watcher_.StopWatching();
michael@0 67 ProcessWatcher::EnsureProcessTerminated(handle());
michael@0 68
michael@0 69 #if defined(OS_WIN)
michael@0 70 // Above call took ownership, so don't want WaitableEvent to assert because
michael@0 71 // the handle isn't valid anymore.
michael@0 72 process_event_->Release();
michael@0 73 #endif
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 bool ChildProcessHost::CreateChannel() {
michael@0 78 channel_id_ = GenerateRandomChannelID(this);
michael@0 79 channel_.reset(new IPC::Channel(
michael@0 80 channel_id_, IPC::Channel::MODE_SERVER, &listener_));
michael@0 81 if (!channel_->Connect())
michael@0 82 return false;
michael@0 83
michael@0 84 opening_channel_ = true;
michael@0 85
michael@0 86 return true;
michael@0 87 }
michael@0 88
michael@0 89 bool ChildProcessHost::CreateChannel(FileDescriptor& aFileDescriptor) {
michael@0 90 if (channel_.get()) {
michael@0 91 channel_->Close();
michael@0 92 }
michael@0 93 channel_.reset(mozilla::ipc::OpenDescriptor(
michael@0 94 aFileDescriptor, IPC::Channel::MODE_SERVER));
michael@0 95 channel_->set_listener(&listener_);
michael@0 96 if (!channel_->Connect()) {
michael@0 97 return false;
michael@0 98 }
michael@0 99
michael@0 100 opening_channel_ = true;
michael@0 101
michael@0 102 return true;
michael@0 103 }
michael@0 104
michael@0 105 void ChildProcessHost::SetHandle(base::ProcessHandle process) {
michael@0 106 #if defined(OS_WIN)
michael@0 107 process_event_.reset(new base::WaitableEvent(process));
michael@0 108
michael@0 109 DCHECK(!handle());
michael@0 110 set_handle(process);
michael@0 111 watcher_.StartWatching(process_event_.get(), this);
michael@0 112 #endif
michael@0 113 }
michael@0 114
michael@0 115 void ChildProcessHost::InstanceCreated() {
michael@0 116 Notify(NotificationType::CHILD_INSTANCE_CREATED);
michael@0 117 }
michael@0 118
michael@0 119 bool ChildProcessHost::Send(IPC::Message* msg) {
michael@0 120 if (!channel_.get()) {
michael@0 121 delete msg;
michael@0 122 return false;
michael@0 123 }
michael@0 124 return channel_->Send(msg);
michael@0 125 }
michael@0 126
michael@0 127 void ChildProcessHost::Notify(NotificationType type) {
michael@0 128 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::IO);
michael@0 129 if (!loop)
michael@0 130 loop = mozilla::ipc::ProcessChild::message_loop();
michael@0 131 if (!loop)
michael@0 132 loop = MessageLoop::current();
michael@0 133 loop->PostTask(
michael@0 134 FROM_HERE, new ChildNotificationTask(type, this));
michael@0 135 }
michael@0 136
michael@0 137 void ChildProcessHost::OnWaitableEventSignaled(base::WaitableEvent *event) {
michael@0 138 #if defined(OS_WIN)
michael@0 139 HANDLE object = event->handle();
michael@0 140 DCHECK(handle());
michael@0 141 DCHECK_EQ(object, handle());
michael@0 142
michael@0 143 bool did_crash = base::DidProcessCrash(NULL, object);
michael@0 144 if (did_crash) {
michael@0 145 // Report that this child process crashed.
michael@0 146 Notify(NotificationType::CHILD_PROCESS_CRASHED);
michael@0 147 }
michael@0 148 // Notify in the main loop of the disconnection.
michael@0 149 Notify(NotificationType::CHILD_PROCESS_HOST_DISCONNECTED);
michael@0 150 #endif
michael@0 151 }
michael@0 152
michael@0 153 ChildProcessHost::ListenerHook::ListenerHook(ChildProcessHost* host)
michael@0 154 : host_(host) {
michael@0 155 }
michael@0 156
michael@0 157 void ChildProcessHost::ListenerHook::OnMessageReceived(
michael@0 158 const IPC::Message& msg) {
michael@0 159 #ifdef IPC_MESSAGE_LOG_ENABLED
michael@0 160 IPC::Logging* logger = IPC::Logging::current();
michael@0 161 if (msg.type() == IPC_LOGGING_ID) {
michael@0 162 logger->OnReceivedLoggingMessage(msg);
michael@0 163 return;
michael@0 164 }
michael@0 165
michael@0 166 if (logger->Enabled())
michael@0 167 logger->OnPreDispatchMessage(msg);
michael@0 168 #endif
michael@0 169
michael@0 170 bool msg_is_ok = true;
michael@0 171 bool handled = false;
michael@0 172
michael@0 173 if (!handled) {
michael@0 174 host_->OnMessageReceived(msg);
michael@0 175 }
michael@0 176
michael@0 177 if (!msg_is_ok)
michael@0 178 base::KillProcess(host_->handle(), ResultCodes::KILLED_BAD_MESSAGE, false);
michael@0 179
michael@0 180 #ifdef IPC_MESSAGE_LOG_ENABLED
michael@0 181 if (logger->Enabled())
michael@0 182 logger->OnPostDispatchMessage(msg, host_->channel_id_);
michael@0 183 #endif
michael@0 184 }
michael@0 185
michael@0 186 void ChildProcessHost::ListenerHook::OnChannelConnected(int32_t peer_pid) {
michael@0 187 host_->opening_channel_ = false;
michael@0 188 host_->OnChannelConnected(peer_pid);
michael@0 189
michael@0 190 // Notify in the main loop of the connection.
michael@0 191 host_->Notify(NotificationType::CHILD_PROCESS_HOST_CONNECTED);
michael@0 192 }
michael@0 193
michael@0 194 void ChildProcessHost::ListenerHook::OnChannelError() {
michael@0 195 host_->opening_channel_ = false;
michael@0 196 host_->OnChannelError();
michael@0 197 }
michael@0 198
michael@0 199 void ChildProcessHost::ListenerHook::GetQueuedMessages(std::queue<IPC::Message>& queue) {
michael@0 200 host_->GetQueuedMessages(queue);
michael@0 201 }
michael@0 202
michael@0 203 ChildProcessHost::Iterator::Iterator() : all_(true) {
michael@0 204 iterator_ = Singleton<ChildProcessList>::get()->begin();
michael@0 205 }
michael@0 206
michael@0 207 ChildProcessHost::Iterator::Iterator(ProcessType type)
michael@0 208 : all_(false), type_(type) {
michael@0 209 iterator_ = Singleton<ChildProcessList>::get()->begin();
michael@0 210 if (!Done() && (*iterator_)->type() != type_)
michael@0 211 ++(*this);
michael@0 212 }
michael@0 213
michael@0 214 ChildProcessHost* ChildProcessHost::Iterator::operator++() {
michael@0 215 do {
michael@0 216 ++iterator_;
michael@0 217 if (Done())
michael@0 218 break;
michael@0 219
michael@0 220 if (!all_ && (*iterator_)->type() != type_)
michael@0 221 continue;
michael@0 222
michael@0 223 return *iterator_;
michael@0 224 } while (true);
michael@0 225
michael@0 226 return NULL;
michael@0 227 }
michael@0 228
michael@0 229 bool ChildProcessHost::Iterator::Done() {
michael@0 230 return iterator_ == Singleton<ChildProcessList>::get()->end();
michael@0 231 }

mercurial