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
50 // received messages. Takes ownership of the passed array (and deletes it).
51 static std::vector
<TransferredMessagePort
> ExtractMessagePortIDs(
52 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_
; }
81 void set_is_stashed() { is_stashed_
= true; }
84 friend class base::RefCountedThreadSafe
<WebMessagePortChannelImpl
>;
85 ~WebMessagePortChannelImpl() override
;
87 // WebMessagePortChannel implementation.
88 virtual void setClient(blink::WebMessagePortChannelClient
* client
);
89 virtual void destroy();
90 virtual void postMessage(const blink::WebString
& message
,
91 blink::WebMessagePortChannelArray
* channels
);
92 virtual bool tryGetMessage(blink::WebString
* message
,
93 blink::WebMessagePortChannelArray
& channels
);
96 void Entangle(scoped_refptr
<WebMessagePortChannelImpl
> channel
);
97 void Send(IPC::Message
* message
);
98 void PostMessage(const MessagePortMessage
& message
,
99 blink::WebMessagePortChannelArray
* channels
);
101 // IPC::Listener implementation.
102 bool OnMessageReceived(const IPC::Message
& message
) override
;
104 void OnMessage(const MessagePortMessage
& message
,
105 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
106 const std::vector
<int>& new_routing_ids
);
107 void OnMessagesQueued();
113 MessagePortMessage message
;
114 blink::WebMessagePortChannelArray ports
;
117 typedef std::queue
<Message
> MessageQueue
;
118 MessageQueue message_queue_
;
120 blink::WebMessagePortChannelClient
* client_
;
121 base::Lock lock_
; // Locks access to above.
123 int route_id_
; // The routing id for this object.
124 int message_port_id_
; // A globally unique identifier for this message port.
125 // Flag to indicate if messages should be sent to the browser process as
126 // base::Value instances as opposed to being serialized using the default
127 // blink::WebSerializedScriptValue.
128 bool send_messages_as_values_
;
130 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
132 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl
);
135 } // namespace content
137 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_