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 SingleThreadTaskRunner
;
25 // This is thread safe.
26 class WebMessagePortChannelImpl
27 : public blink::WebMessagePortChannel
,
29 public base::RefCountedThreadSafe
<WebMessagePortChannelImpl
> {
31 explicit WebMessagePortChannelImpl(
32 const scoped_refptr
<base::SingleThreadTaskRunner
>&
33 main_thread_task_runner
);
34 WebMessagePortChannelImpl(
37 const scoped_refptr
<base::SingleThreadTaskRunner
>&
38 main_thread_task_runner
);
40 static void CreatePair(
41 const scoped_refptr
<base::SingleThreadTaskRunner
>&
42 main_thread_task_runner
,
43 blink::WebMessagePortChannel
** channel1
,
44 blink::WebMessagePortChannel
** channel2
);
46 // Extracts port IDs for passing on to the browser process, and queues any
47 // received messages. Takes ownership of the passed array (and deletes it).
48 static std::vector
<int> ExtractMessagePortIDs(
49 blink::WebMessagePortChannelArray
* channels
);
51 // Queues received and incoming messages until there are no more in-flight
52 // messages, then sends all of them to the browser process.
54 int message_port_id() const { return message_port_id_
; }
57 friend class base::RefCountedThreadSafe
<WebMessagePortChannelImpl
>;
58 ~WebMessagePortChannelImpl() override
;
60 // WebMessagePortChannel implementation.
61 virtual void setClient(blink::WebMessagePortChannelClient
* client
);
62 virtual void destroy();
63 virtual void postMessage(const blink::WebString
& message
,
64 blink::WebMessagePortChannelArray
* channels
);
65 virtual bool tryGetMessage(blink::WebString
* message
,
66 blink::WebMessagePortChannelArray
& channels
);
69 void Entangle(scoped_refptr
<WebMessagePortChannelImpl
> channel
);
70 void Send(IPC::Message
* message
);
71 void PostMessage(const base::string16
& message
,
72 blink::WebMessagePortChannelArray
* channels
);
74 // IPC::Listener implementation.
75 bool OnMessageReceived(const IPC::Message
& message
) override
;
77 void OnMessage(const base::string16
& message
,
78 const std::vector
<int>& sent_message_port_ids
,
79 const std::vector
<int>& new_routing_ids
);
80 void OnMessagesQueued();
86 base::string16 message
;
87 std::vector
<WebMessagePortChannelImpl
*> ports
;
90 typedef std::queue
<Message
> MessageQueue
;
91 MessageQueue message_queue_
;
93 blink::WebMessagePortChannelClient
* client_
;
94 base::Lock lock_
; // Locks access to above.
96 int route_id_
; // The routing id for this object.
97 int message_port_id_
; // A globally unique identifier for this message port.
98 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
100 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl
);
103 } // namespace content
105 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_