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 // Creates WebMessagePortChannelImpl instances for port IDs passed in from the
61 static blink::WebMessagePortChannelArray
CreatePorts(
62 const std::vector
<TransferredMessagePort
>& message_ports
,
63 const std::vector
<int>& new_routing_ids
,
64 const scoped_refptr
<base::SingleThreadTaskRunner
>&
65 main_thread_task_runner
);
67 // Queues received and incoming messages until there are no more in-flight
68 // messages, then sends all of them to the browser process.
70 int message_port_id() const { return message_port_id_
; }
72 void set_is_stashed() { is_stashed_
= true; }
75 friend class base::RefCountedThreadSafe
<WebMessagePortChannelImpl
>;
76 ~WebMessagePortChannelImpl() override
;
78 // WebMessagePortChannel implementation.
79 virtual void setClient(blink::WebMessagePortChannelClient
* client
);
80 virtual void destroy();
81 virtual void postMessage(const blink::WebString
& message
,
82 blink::WebMessagePortChannelArray
* channels
);
83 virtual bool tryGetMessage(blink::WebString
* message
,
84 blink::WebMessagePortChannelArray
& channels
);
87 void Entangle(scoped_refptr
<WebMessagePortChannelImpl
> channel
);
88 void Send(IPC::Message
* message
);
89 void PostMessage(const MessagePortMessage
& message
,
90 blink::WebMessagePortChannelArray
* channels
);
92 // IPC::Listener implementation.
93 bool OnMessageReceived(const IPC::Message
& message
) override
;
95 void OnMessage(const MessagePortMessage
& message
,
96 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
97 const std::vector
<int>& new_routing_ids
);
98 void OnMessagesQueued();
104 MessagePortMessage message
;
105 blink::WebMessagePortChannelArray ports
;
108 typedef std::queue
<Message
> MessageQueue
;
109 MessageQueue message_queue_
;
111 blink::WebMessagePortChannelClient
* client_
;
112 base::Lock lock_
; // Locks access to above.
114 int route_id_
; // The routing id for this object.
115 int message_port_id_
; // A globally unique identifier for this message port.
116 // Flag to indicate if messages should be sent to the browser process as
117 // base::Value instances as opposed to being serialized using the default
118 // blink::WebSerializedScriptValue.
119 bool send_messages_as_values_
;
121 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
123 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl
);
126 } // namespace content
128 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_