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/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/synchronization/lock.h"
16 #include "content/public/common/message_port_types.h"
17 #include "ipc/ipc_listener.h"
18 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
21 class SingleThreadTaskRunner
;
28 // This is thread safe.
29 class WebMessagePortChannelImpl
30 : public blink::WebMessagePortChannel
,
32 public base::RefCountedThreadSafe
<WebMessagePortChannelImpl
> {
34 explicit WebMessagePortChannelImpl(
35 const scoped_refptr
<base::SingleThreadTaskRunner
>&
36 main_thread_task_runner
);
37 WebMessagePortChannelImpl(
39 const TransferredMessagePort
& port
,
40 const scoped_refptr
<base::SingleThreadTaskRunner
>&
41 main_thread_task_runner
);
43 static void CreatePair(
44 const scoped_refptr
<base::SingleThreadTaskRunner
>&
45 main_thread_task_runner
,
46 blink::WebMessagePortChannel
** channel1
,
47 blink::WebMessagePortChannel
** channel2
);
49 // Extracts port IDs for passing on to the browser process, and queues any
51 static std::vector
<TransferredMessagePort
> ExtractMessagePortIDs(
52 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
54 // Extracts port IDs for passing on to the browser process, and queues any
56 static std::vector
<TransferredMessagePort
> ExtractMessagePortIDs(
57 const blink::WebMessagePortChannelArray
& channels
);
59 // Extracts port IDs for passing on to the browser process, but doesn't
60 // send a separate IPC to the browser to initiate queueing messages. Instead
61 // calling code is responsible for initiating the queueing in the browser
62 // process. This is useful when transfering ports over an IPC channel that
63 // does not share ordering guarentees with regular IPC.
64 static std::vector
<TransferredMessagePort
>
65 ExtractMessagePortIDsWithoutQueueing(
66 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
68 // Creates WebMessagePortChannelImpl instances for port IDs passed in from the
70 static blink::WebMessagePortChannelArray
CreatePorts(
71 const std::vector
<TransferredMessagePort
>& message_ports
,
72 const std::vector
<int>& new_routing_ids
,
73 const scoped_refptr
<base::SingleThreadTaskRunner
>&
74 main_thread_task_runner
);
76 // Queues received and incoming messages until there are no more in-flight
77 // messages, then sends all of them to the browser process.
79 int message_port_id() const { return message_port_id_
; }
82 friend class base::RefCountedThreadSafe
<WebMessagePortChannelImpl
>;
83 ~WebMessagePortChannelImpl() override
;
85 // WebMessagePortChannel implementation.
86 virtual void setClient(blink::WebMessagePortChannelClient
* client
);
87 virtual void destroy();
88 virtual void postMessage(const blink::WebString
& message
,
89 blink::WebMessagePortChannelArray
* channels_ptr
);
90 virtual bool tryGetMessage(blink::WebString
* message
,
91 blink::WebMessagePortChannelArray
& channels
);
94 void Entangle(scoped_refptr
<WebMessagePortChannelImpl
> channel
);
95 void Send(IPC::Message
* message
);
96 void PostMessage(const MessagePortMessage
& message
,
97 scoped_ptr
<blink::WebMessagePortChannelArray
> channels
);
99 // IPC::Listener implementation.
100 bool OnMessageReceived(const IPC::Message
& message
) override
;
102 void OnMessage(const MessagePortMessage
& message
,
103 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
104 const std::vector
<int>& new_routing_ids
);
105 void OnMessagesQueued();
111 MessagePortMessage message
;
112 blink::WebMessagePortChannelArray ports
;
115 typedef std::queue
<Message
> MessageQueue
;
116 MessageQueue message_queue_
;
118 blink::WebMessagePortChannelClient
* client_
;
119 base::Lock lock_
; // Locks access to above.
121 int route_id_
; // The routing id for this object.
122 int message_port_id_
; // A globally unique identifier for this message port.
123 // Flag to indicate if messages should be sent to the browser process as
124 // base::Value instances as opposed to being serialized using the default
125 // blink::WebSerializedScriptValue.
126 bool send_messages_as_values_
;
127 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
129 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl
);
132 } // namespace content
134 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_