blob: 9d882c1671fc78cf013f142c1e0ccfb72f478b75 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
// Copyright (c) 2012 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/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "third_party/WebKit/public/platform/WebMessagePortChannelClient.h"
#include "third_party/WebKit/public/platform/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();
}
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;
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&TestWebMessagePortChannel::queueMessage,
remote_.get(),
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;
}
TestWebMessagePortChannel::~TestWebMessagePortChannel() {}
void TestWebMessagePortChannel::queueMessage(Message* message) {
bool was_empty = message_queue_.empty();
message_queue_.push(message);
if (client_ && was_empty)
client_->messageAvailable();
}
|