1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "webkit/support/test_webmessageportchannel.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMessagePortChannelClient.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
13 using WebKit::WebMessagePortChannel
;
14 using WebKit::WebMessagePortChannelArray
;
15 using WebKit::WebMessagePortChannelClient
;
16 using WebKit::WebString
;
18 class TestWebMessagePortChannel::Message
{
20 Message(WebString data
, WebMessagePortChannelArray
* ports
)
27 for (size_t i
= 0; i
< ports_
->size(); ++i
)
28 (*ports_
)[i
]->destroy();
30 WebString
data() const { return data_
; }
31 WebMessagePortChannelArray
* ports() { return ports_
.get(); }
35 scoped_ptr
<WebMessagePortChannelArray
> ports_
;
36 DISALLOW_COPY_AND_ASSIGN(Message
);
39 TestWebMessagePortChannel::TestWebMessagePortChannel()
44 void TestWebMessagePortChannel::setClient(WebMessagePortChannelClient
* client
) {
48 void TestWebMessagePortChannel::destroy() {
49 while (!message_queue_
.empty()) {
50 delete message_queue_
.front();
56 void TestWebMessagePortChannel::entangle(WebMessagePortChannel
* remote
) {
57 remote_
= static_cast<TestWebMessagePortChannel
*>(remote
);
60 void TestWebMessagePortChannel::postMessage(const WebString
& data
,
61 WebMessagePortChannelArray
* ports
) {
64 MessageLoop::current()->PostTask(
66 base::Bind(&TestWebMessagePortChannel::queueMessage
, remote_
.get(),
67 new Message(data
, ports
)));
70 bool TestWebMessagePortChannel::tryGetMessage(WebString
* data
,
71 WebMessagePortChannelArray
& ports
) {
72 if (message_queue_
.empty())
74 scoped_ptr
<Message
> message(message_queue_
.front());
76 *data
= message
->data();
77 if (WebMessagePortChannelArray
* message_ports
= message
->ports())
78 ports
.swap(*message_ports
);
82 TestWebMessagePortChannel::~TestWebMessagePortChannel() {}
84 void TestWebMessagePortChannel::queueMessage(Message
* message
) {
85 bool was_empty
= message_queue_
.empty();
86 message_queue_
.push(message
);
87 if (client_
&& was_empty
)
88 client_
->messageAvailable();