Add support for favicon scale factor.
[chromium-blink-merge.git] / webkit / support / test_webmessageportchannel.cc
blob50207f1d4f915ff0ec77af5c952cb6a9553ac40b
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"
7 #include "base/bind.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 {
19 public:
20 Message(WebString data, WebMessagePortChannelArray* ports)
21 : data_(data),
22 ports_(ports) {
24 ~Message() {
25 if (ports_ == NULL)
26 return;
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(); }
33 private:
34 WebString data_;
35 scoped_ptr<WebMessagePortChannelArray> ports_;
36 DISALLOW_COPY_AND_ASSIGN(Message);
39 TestWebMessagePortChannel::TestWebMessagePortChannel()
40 : client_(NULL) {
41 AddRef();
44 void TestWebMessagePortChannel::setClient(WebMessagePortChannelClient* client) {
45 client_ = client;
48 void TestWebMessagePortChannel::destroy() {
49 while (!message_queue_.empty()) {
50 delete message_queue_.front();
51 message_queue_.pop();
53 Release();
56 void TestWebMessagePortChannel::entangle(WebMessagePortChannel* remote) {
57 remote_ = static_cast<TestWebMessagePortChannel*>(remote);
60 void TestWebMessagePortChannel::postMessage(const WebString& data,
61 WebMessagePortChannelArray* ports) {
62 if (remote_ == NULL)
63 return;
64 MessageLoop::current()->PostTask(
65 FROM_HERE,
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())
73 return false;
74 scoped_ptr<Message> message(message_queue_.front());
75 message_queue_.pop();
76 *data = message->data();
77 if (WebMessagePortChannelArray* message_ports = message->ports())
78 ports.swap(*message_ports);
79 return true;
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();