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 #ifndef CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_
6 #define CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "base/synchronization/lock.h"
15 #include "ipc/ipc_listener.h"
16 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
19 class MessageLoopProxy;
25 // This is thread safe.
26 class WebMessagePortChannelImpl
27 : public blink::WebMessagePortChannel,
29 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> {
31 explicit WebMessagePortChannelImpl(base::MessageLoopProxy* child_thread_loop);
32 WebMessagePortChannelImpl(int route_id,
34 base::MessageLoopProxy* child_thread_loop);
36 // Extracts port IDs for passing on to the browser process, and queues any
37 // received messages. Takes ownership of the passed array (and deletes it).
38 static std::vector<int> ExtractMessagePortIDs(
39 blink::WebMessagePortChannelArray* channels);
41 // Queues received and incoming messages until there are no more in-flight
42 // messages, then sends all of them to the browser process.
44 int message_port_id() const { return message_port_id_; }
47 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>;
48 virtual ~WebMessagePortChannelImpl();
50 // WebMessagePortChannel implementation.
51 virtual void setClient(blink::WebMessagePortChannelClient* client);
52 virtual void destroy();
53 virtual void entangle(blink::WebMessagePortChannel* channel);
54 virtual void postMessage(const blink::WebString& message,
55 blink::WebMessagePortChannelArray* channels);
56 virtual bool tryGetMessage(blink::WebString* message,
57 blink::WebMessagePortChannelArray& channels);
60 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel);
61 void Send(IPC::Message* message);
62 void PostMessage(const base::string16& message,
63 blink::WebMessagePortChannelArray* channels);
65 // IPC::Listener implementation.
66 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
68 void OnMessage(const base::string16& message,
69 const std::vector<int>& sent_message_port_ids,
70 const std::vector<int>& new_routing_ids);
71 void OnMessagesQueued();
77 base::string16 message;
78 std::vector<WebMessagePortChannelImpl*> ports;
81 typedef std::queue<Message> MessageQueue;
82 MessageQueue message_queue_;
84 blink::WebMessagePortChannelClient* client_;
85 base::Lock lock_; // Locks access to above.
87 int route_id_; // The routing id for this object.
88 int message_port_id_; // A globally unique identifier for this message port.
89 scoped_refptr<base::MessageLoopProxy> child_thread_loop_;
91 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl);
94 } // namespace content
96 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_