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_COMMON_WEBMESSAGEPORTCHANNEL_IMPL_H_
6 #define CONTENT_COMMON_WEBMESSAGEPORTCHANNEL_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/string16.h"
14 #include "base/synchronization/lock.h"
15 #include "ipc/ipc_listener.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebMessagePortChannel.h"
20 // This is thread safe.
21 class WebMessagePortChannelImpl
22 : public WebKit::WebMessagePortChannel
,
24 public base::RefCountedThreadSafe
<WebMessagePortChannelImpl
> {
26 WebMessagePortChannelImpl();
27 WebMessagePortChannelImpl(int route_id
, int message_port_id
);
29 // Queues received and incoming messages until there are no more in-flight
30 // messages, then sends all of them to the browser process.
32 int message_port_id() const { return message_port_id_
; }
35 friend class base::RefCountedThreadSafe
<WebMessagePortChannelImpl
>;
36 virtual ~WebMessagePortChannelImpl();
38 // WebMessagePortChannel implementation.
39 virtual void setClient(WebKit::WebMessagePortChannelClient
* client
);
40 virtual void destroy();
41 virtual void entangle(WebKit::WebMessagePortChannel
* channel
);
42 virtual void postMessage(const WebKit::WebString
& message
,
43 WebKit::WebMessagePortChannelArray
* channels
);
44 virtual bool tryGetMessage(WebKit::WebString
* message
,
45 WebKit::WebMessagePortChannelArray
& channels
);
48 void Entangle(scoped_refptr
<WebMessagePortChannelImpl
> channel
);
49 void Send(IPC::Message
* message
);
51 // IPC::Listener implementation.
52 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
54 void OnMessage(const string16
& message
,
55 const std::vector
<int>& sent_message_port_ids
,
56 const std::vector
<int>& new_routing_ids
);
57 void OnMessagedQueued();
64 std::vector
<WebMessagePortChannelImpl
*> ports
;
67 typedef std::queue
<Message
> MessageQueue
;
68 MessageQueue message_queue_
;
70 WebKit::WebMessagePortChannelClient
* client_
;
71 base::Lock lock_
; // Locks access to above.
73 int route_id_
; // The routing id for this object.
74 int message_port_id_
; // A globally unique identifier for this message port.
76 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl
);
79 } // namespace content
81 #endif // CONTENT_COMMON_WEBMESSAGEPORTCHANNEL_IMPL_H_