summaryrefslogtreecommitdiffstats
path: root/webkit/support/test_webmessageportchannel.cc
diff options
context:
space:
mode:
authorcaseq@google.com <caseq@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-18 12:09:36 +0000
committercaseq@google.com <caseq@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-18 12:09:36 +0000
commitfc6999fcb6c7056079e532395e118e345a579b7c (patch)
treea6b902ef816cec966cc20cd1b83c779b9b892ea0 /webkit/support/test_webmessageportchannel.cc
parent3807b48f7b925983c8a145ee330df936b4b8c707 (diff)
downloadchromium_src-fc6999fcb6c7056079e532395e118e345a579b7c.zip
chromium_src-fc6999fcb6c7056079e532395e118e345a579b7c.tar.gz
chromium_src-fc6999fcb6c7056079e532395e118e345a579b7c.tar.bz2
Added support for WebMessagePortChannel to WebKit platform support.
BUG=none TEST=third_party/WebKit/LayoutTest/fast/events/message-* Review URL: http://codereview.chromium.org/8137033 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106056 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/support/test_webmessageportchannel.cc')
-rw-r--r--webkit/support/test_webmessageportchannel.cc91
1 files changed, 91 insertions, 0 deletions
diff --git a/webkit/support/test_webmessageportchannel.cc b/webkit/support/test_webmessageportchannel.cc
new file mode 100644
index 0000000..c6b0c1a
--- /dev/null
+++ b/webkit/support/test_webmessageportchannel.cc
@@ -0,0 +1,91 @@
+// Copyright (c) 2011 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 "webkit/support/test_webmessageportchannel.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
+#include "base/task.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannelClient.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
+
+using WebKit::WebMessagePortChannel;
+using WebKit::WebMessagePortChannelArray;
+using WebKit::WebMessagePortChannelClient;
+using WebKit::WebString;
+
+class TestWebMessagePortChannel::Message {
+ public:
+ Message(WebString data, WebMessagePortChannelArray* ports)
+ : data_(data),
+ ports_(ports) {
+ }
+ ~Message() {
+ if (ports_ == NULL)
+ return;
+ for (size_t i = 0; i < ports_->size(); ++i)
+ (*ports_)[i]->destroy();
+ }
+ WebString data() const { return data_; }
+ WebMessagePortChannelArray* ports() { return ports_.get(); }
+
+ private:
+ WebString data_;
+ scoped_ptr<WebMessagePortChannelArray> ports_;
+ DISALLOW_COPY_AND_ASSIGN(Message);
+};
+
+TestWebMessagePortChannel::TestWebMessagePortChannel()
+ : client_(NULL) {
+ AddRef();
+}
+
+TestWebMessagePortChannel::~TestWebMessagePortChannel() {
+}
+
+void TestWebMessagePortChannel::setClient(WebMessagePortChannelClient* client) {
+ client_ = client;
+}
+
+void TestWebMessagePortChannel::destroy() {
+ while (!message_queue_.empty()) {
+ delete message_queue_.front();
+ message_queue_.pop();
+ }
+ Release();
+}
+
+void TestWebMessagePortChannel::entangle(WebMessagePortChannel* remote) {
+ remote_ = static_cast<TestWebMessagePortChannel*>(remote);
+}
+
+void TestWebMessagePortChannel::postMessage(const WebString& data,
+ WebMessagePortChannelArray* ports) {
+ if (remote_ == NULL)
+ return;
+ MessageLoop::current()->PostTask(
+ FROM_HERE,
+ NewRunnableMethod(remote_.get(),
+ &TestWebMessagePortChannel::queueMessage,
+ new Message(data, ports)));
+}
+
+bool TestWebMessagePortChannel::tryGetMessage(WebString* data,
+ WebMessagePortChannelArray& ports) {
+ if (message_queue_.empty())
+ return false;
+ scoped_ptr<Message> message(message_queue_.front());
+ message_queue_.pop();
+ *data = message->data();
+ if (WebMessagePortChannelArray* message_ports = message->ports())
+ ports.swap(*message_ports);
+ return true;
+}
+
+void TestWebMessagePortChannel::queueMessage(Message* message) {
+ bool was_empty = message_queue_.empty();
+ message_queue_.push(message);
+ if (client_ && was_empty)
+ client_->messageAvailable();
+}