summaryrefslogtreecommitdiffstats
path: root/components/message_port
diff options
context:
space:
mode:
authorrockot <rockot@chromium.org>2015-06-17 13:56:53 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-17 20:57:30 +0000
commitd3bd2cb5bc98903bbd9788ca01c0d1e9b15536ba (patch)
tree20156b13a0d69272d39bcf05fdffc3e0307f344a /components/message_port
parent9219ddcabb17046227446df263de5ab4ee8fac3d (diff)
downloadchromium_src-d3bd2cb5bc98903bbd9788ca01c0d1e9b15536ba.zip
chromium_src-d3bd2cb5bc98903bbd9788ca01c0d1e9b15536ba.tar.gz
chromium_src-d3bd2cb5bc98903bbd9788ca01c0d1e9b15536ba.tar.bz2
Componentize html_viewer's MessagePort
We should transition to using the same implementation in content, and this is the first step to doing that. BUG=361001 R=jam@chromium.org TBR=jochen@chromium.org (+WebKit DEPS) Review URL: https://codereview.chromium.org/1182303012 Cr-Commit-Position: refs/heads/master@{#334914}
Diffstat (limited to 'components/message_port')
-rw-r--r--components/message_port/BUILD.gn17
-rw-r--r--components/message_port/DEPS6
-rw-r--r--components/message_port/OWNERS2
-rw-r--r--components/message_port/web_message_port_channel_impl.cc132
-rw-r--r--components/message_port/web_message_port_channel_impl.h45
5 files changed, 202 insertions, 0 deletions
diff --git a/components/message_port/BUILD.gn b/components/message_port/BUILD.gn
new file mode 100644
index 0000000..3a81750
--- /dev/null
+++ b/components/message_port/BUILD.gn
@@ -0,0 +1,17 @@
+# Copyright 2015 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.
+
+source_set("message_port") {
+ sources = [
+ "web_message_port_channel_impl.cc",
+ "web_message_port_channel_impl.h",
+ ]
+
+ public_deps = [
+ "//base",
+ "//mojo/common",
+ "//third_party/WebKit/public:blink",
+ "//third_party/mojo/src/mojo/public/cpp/system",
+ ]
+}
diff --git a/components/message_port/DEPS b/components/message_port/DEPS
new file mode 100644
index 0000000..eba876f
--- /dev/null
+++ b/components/message_port/DEPS
@@ -0,0 +1,6 @@
+include_rules = [
+ "+base",
+ "+mojo/common",
+ "+third_party/WebKit/public",
+ "+third_party/mojo/src/mojo/public/cpp/system",
+]
diff --git a/components/message_port/OWNERS b/components/message_port/OWNERS
new file mode 100644
index 0000000..80b8751
--- /dev/null
+++ b/components/message_port/OWNERS
@@ -0,0 +1,2 @@
+jam@chromium.org
+rockot@chromium.org
diff --git a/components/message_port/web_message_port_channel_impl.cc b/components/message_port/web_message_port_channel_impl.cc
new file mode 100644
index 0000000..93a6212
--- /dev/null
+++ b/components/message_port/web_message_port_channel_impl.cc
@@ -0,0 +1,132 @@
+// Copyright 2015 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 "components/message_port/web_message_port_channel_impl.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "base/strings/string16.h"
+#include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h"
+#include "third_party/WebKit/public/platform/WebString.h"
+#include "third_party/mojo/src/mojo/public/cpp/system/message_pipe.h"
+
+using blink::WebMessagePortChannel;
+using blink::WebMessagePortChannelArray;
+using blink::WebMessagePortChannelClient;
+using blink::WebString;
+
+namespace message_port {
+
+void WebMessagePortChannelImpl::CreatePair(
+ blink::WebMessagePortChannel** channel1,
+ blink::WebMessagePortChannel** channel2) {
+ mojo::ScopedMessagePipeHandle pipe1;
+ mojo::ScopedMessagePipeHandle pipe2;
+ MojoResult result = mojo::CreateMessagePipe(nullptr, &pipe1, &pipe2);
+ if (result != MOJO_RESULT_OK) {
+ NOTREACHED();
+ return;
+ }
+
+ *channel1 = new WebMessagePortChannelImpl(pipe1.Pass());;
+ *channel2 = new WebMessagePortChannelImpl(pipe2.Pass());
+}
+
+WebMessagePortChannelImpl::WebMessagePortChannelImpl(
+ mojo::ScopedMessagePipeHandle pipe)
+ : client_(nullptr), pipe_(pipe.Pass()) {
+ WaitForNextMessage();
+}
+
+WebMessagePortChannelImpl::~WebMessagePortChannelImpl() {
+}
+
+void WebMessagePortChannelImpl::setClient(WebMessagePortChannelClient* client) {
+ client_ = client;
+}
+
+void WebMessagePortChannelImpl::destroy() {
+ setClient(nullptr);
+ delete this;
+}
+
+void WebMessagePortChannelImpl::postMessage(
+ const WebString& message_as_string,
+ WebMessagePortChannelArray* channels) {
+ base::string16 string = message_as_string;
+
+ std::vector<MojoHandle> handles;
+ if (channels) {
+ for (size_t i = 0; i < channels->size(); ++i) {
+ WebMessagePortChannelImpl* channel =
+ static_cast<WebMessagePortChannelImpl*>((*channels)[i]);
+ handles.push_back(channel->pipe_.release().value());
+ channel->handle_watcher_.Stop();
+ }
+ delete channels;
+ }
+
+ uint32_t num_handles = static_cast<uint32_t>(handles.size());
+ MojoHandle* handles_ptr = handles.empty() ? nullptr : &handles[0];
+
+ MojoResult result = MojoWriteMessage(
+ pipe_.get().value(), string.c_str(),
+ static_cast<uint32_t>(string.length() * sizeof(base::char16)),
+ handles_ptr, num_handles, MOJO_WRITE_MESSAGE_FLAG_NONE);
+ DCHECK_EQ(MOJO_RESULT_OK, result);
+}
+
+bool WebMessagePortChannelImpl::tryGetMessage(
+ WebString* message,
+ WebMessagePortChannelArray& channels) {
+ uint32_t num_bytes = 0;
+ uint32_t num_handles = 0;
+ MojoResult result = MojoReadMessage(
+ pipe_.get().value(), nullptr, &num_bytes, nullptr, &num_handles,
+ MOJO_READ_MESSAGE_FLAG_NONE);
+ if (result != MOJO_RESULT_RESOURCE_EXHAUSTED)
+ return false;
+
+ base::string16 message16;
+ CHECK(num_bytes % sizeof(base::char16) == 0);
+ message16.resize(num_bytes / sizeof(base::char16));
+ std::vector<MojoHandle> handles;
+ handles.resize(num_handles);
+
+ MojoHandle* handles_ptr = handles.empty() ? nullptr : &handles[0];
+ result = MojoReadMessage(
+ pipe_.get().value(), &message16[0], &num_bytes, handles_ptr, &num_handles,
+ MOJO_READ_MESSAGE_FLAG_NONE);
+ if (result != MOJO_RESULT_OK) {
+ NOTREACHED();
+ return false;
+ }
+
+ *message = message16;
+ WebMessagePortChannelArray ports(handles.size());
+ for (size_t i = 0; i < handles.size(); ++i) {
+ mojo::MessagePipeHandle mph(handles[i]);
+ mojo::ScopedMessagePipeHandle handle(mph);
+ ports[i] = new WebMessagePortChannelImpl(handle.Pass());
+ }
+ channels = ports;
+ return true;
+}
+
+void WebMessagePortChannelImpl::WaitForNextMessage() {
+ handle_watcher_.Start(
+ pipe_.get(),
+ MOJO_HANDLE_SIGNAL_READABLE,
+ MOJO_DEADLINE_INDEFINITE,
+ base::Bind(&WebMessagePortChannelImpl::OnMessageAvailable,
+ base::Unretained(this)));
+}
+
+void WebMessagePortChannelImpl::OnMessageAvailable(MojoResult result) {
+ DCHECK_EQ(MOJO_RESULT_OK, result);
+ client_->messageAvailable();
+ WaitForNextMessage();
+}
+
+} // namespace message_port
diff --git a/components/message_port/web_message_port_channel_impl.h b/components/message_port/web_message_port_channel_impl.h
new file mode 100644
index 0000000..49fe97e
--- /dev/null
+++ b/components/message_port/web_message_port_channel_impl.h
@@ -0,0 +1,45 @@
+// Copyright 2015 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 COMPONENTS_MESSAGE_PORT_WEB_MESSAGE_PORT_CHANNEL_IMPL_H_
+#define COMPONENTS_MESSAGE_PORT_WEB_MESSAGE_PORT_CHANNEL_IMPL_H_
+
+#include "base/basictypes.h"
+#include "mojo/common/handle_watcher.h"
+#include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
+#include "third_party/mojo/src/mojo/public/cpp/system/message_pipe.h"
+
+namespace message_port {
+
+class WebMessagePortChannelImpl : public blink::WebMessagePortChannel {
+ public:
+ static void CreatePair(
+ blink::WebMessagePortChannel** channel1,
+ blink::WebMessagePortChannel** channel2);
+
+ private:
+ explicit WebMessagePortChannelImpl(mojo::ScopedMessagePipeHandle pipe);
+ virtual ~WebMessagePortChannelImpl();
+
+ // blink::WebMessagePortChannel implementation.
+ virtual void setClient(blink::WebMessagePortChannelClient* client);
+ virtual void destroy();
+ virtual void postMessage(const blink::WebString& message,
+ blink::WebMessagePortChannelArray* channels);
+ virtual bool tryGetMessage(blink::WebString* message,
+ blink::WebMessagePortChannelArray& channels);
+
+ void WaitForNextMessage();
+ void OnMessageAvailable(MojoResult result);
+
+ blink::WebMessagePortChannelClient* client_;
+ mojo::ScopedMessagePipeHandle pipe_;
+ mojo::common::HandleWatcher handle_watcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl);
+};
+
+} // namespace message_port
+
+#endif // COMPONENTS_MESSAGE_PORT_WEB_MESSAGE_PORT_CHANNEL_IMPL_H_