summaryrefslogtreecommitdiffstats
path: root/mojo/common
diff options
context:
space:
mode:
authorcmasone@chromium.org <cmasone@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-16 23:27:07 +0000
committercmasone@chromium.org <cmasone@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-16 23:27:07 +0000
commit6d77881320023a8ed096644024db22696c30c146 (patch)
tree9bce91b96f4adc9c2c61d5af9dd7979b2855e5e9 /mojo/common
parent08bdf1b7a457f3ad5db55b43fb7e0ed0abaf9d2b (diff)
downloadchromium_src-6d77881320023a8ed096644024db22696c30c146.zip
chromium_src-6d77881320023a8ed096644024db22696c30c146.tar.gz
chromium_src-6d77881320023a8ed096644024db22696c30c146.tar.bz2
Move MojoChannelInit to mojo/common
This class has general utility for bootstrapping mojo services over a variety of communication channels, so pull it into mojo itself. BUG=None TEST=None R=sky@chromium.org Review URL: https://codereview.chromium.org/232073002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@264342 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/common')
-rw-r--r--mojo/common/DEPS1
-rw-r--r--mojo/common/mojo_channel_init.cc59
-rw-r--r--mojo/common/mojo_channel_init.h66
3 files changed, 126 insertions, 0 deletions
diff --git a/mojo/common/DEPS b/mojo/common/DEPS
index a1d21b5..26456c6 100644
--- a/mojo/common/DEPS
+++ b/mojo/common/DEPS
@@ -1,5 +1,6 @@
include_rules = [
"-mojo",
"+mojo/common",
+ "+mojo/embedder",
"+mojo/public",
]
diff --git a/mojo/common/mojo_channel_init.cc b/mojo/common/mojo_channel_init.cc
new file mode 100644
index 0000000..96190f9
--- /dev/null
+++ b/mojo/common/mojo_channel_init.cc
@@ -0,0 +1,59 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/common/mojo_channel_init.h"
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "mojo/embedder/embedder.h"
+
+namespace mojo {
+namespace common {
+
+MojoChannelInit::MojoChannelInit()
+ : channel_info_(NULL),
+ weak_factory_(this) {
+}
+
+MojoChannelInit::~MojoChannelInit() {
+ bootstrap_message_pipe_.reset();
+ if (channel_info_) {
+ io_thread_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel_info_));
+ }
+}
+
+void MojoChannelInit::Init(
+ base::PlatformFile file,
+ scoped_refptr<base::TaskRunner> io_thread_task_runner) {
+ DCHECK(!io_thread_task_runner_.get()); // Should only init once.
+ io_thread_task_runner_ = io_thread_task_runner;
+ bootstrap_message_pipe_ = mojo::embedder::CreateChannel(
+ mojo::embedder::ScopedPlatformHandle(
+ mojo::embedder::PlatformHandle(file)),
+ io_thread_task_runner,
+ base::Bind(&MojoChannelInit::OnCreatedChannel, weak_factory_.GetWeakPtr(),
+ io_thread_task_runner),
+ base::MessageLoop::current()->message_loop_proxy()).Pass();
+}
+
+// static
+void MojoChannelInit::OnCreatedChannel(
+ base::WeakPtr<MojoChannelInit> host,
+ scoped_refptr<base::TaskRunner> io_thread,
+ embedder::ChannelInfo* channel) {
+ // By the time we get here |host| may have been destroyed. If so, shutdown the
+ // channel.
+ if (!host.get()) {
+ io_thread->PostTask(
+ FROM_HERE,
+ base::Bind(&mojo::embedder::DestroyChannelOnIOThread, channel));
+ return;
+ }
+ host->channel_info_ = channel;
+}
+
+} // namespace common
+} // namespace mojo
diff --git a/mojo/common/mojo_channel_init.h b/mojo/common/mojo_channel_init.h
new file mode 100644
index 0000000..8fa0094
--- /dev/null
+++ b/mojo/common/mojo_channel_init.h
@@ -0,0 +1,66 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_COMMON_MOJO_CHANNEL_INIT_H_
+#define MOJO_COMMON_MOJO_CHANNEL_INIT_H_
+
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "base/platform_file.h"
+#include "mojo/common/mojo_common_export.h"
+#include "mojo/public/cpp/system/core.h"
+
+namespace base {
+class MessageLoopProxy;
+class TaskRunner;
+}
+
+namespace mojo {
+namespace embedder {
+struct ChannelInfo;
+}
+
+namespace common {
+
+// MojoChannelInit handle creation (and destruction) of the mojo channel. It is
+// expected that this class is created and destroyed on the main thread.
+class MOJO_COMMON_EXPORT MojoChannelInit {
+ public:
+ MojoChannelInit();
+ ~MojoChannelInit();
+
+ // Inits the channel. This takes ownership of |file|.
+ void Init(base::PlatformFile file,
+ scoped_refptr<base::TaskRunner> io_thread_task_runner);
+
+ bool is_handle_valid() const { return bootstrap_message_pipe_.is_valid(); }
+
+ mojo::ScopedMessagePipeHandle bootstrap_message_pipe() {
+ return bootstrap_message_pipe_.Pass();
+ }
+
+ private:
+ // Invoked on the main thread once the channel has been established.
+ static void OnCreatedChannel(
+ base::WeakPtr<MojoChannelInit> host,
+ scoped_refptr<base::TaskRunner> io_thread,
+ embedder::ChannelInfo* channel);
+
+ scoped_refptr<base::TaskRunner> io_thread_task_runner_;
+
+ // If non-null the channel has been established.
+ embedder::ChannelInfo* channel_info_;
+
+ // The handle from channel creation.
+ mojo::ScopedMessagePipeHandle bootstrap_message_pipe_;
+
+ base::WeakPtrFactory<MojoChannelInit> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(MojoChannelInit);
+};
+
+} // namespace common
+} // namespace mojo
+
+#endif // MOJO_COMMON_MOJO_CHANNEL_INIT_H_