Roll DEPS for PDFium to 19ae17578f99621100a26dac3e2c7c3dbf7c7cd1
[chromium-blink-merge.git] / content / child / webmessageportchannel_impl.h
blob4b9c273d01dcac2267d685d82950aceaa1d85364
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_
8 #include <queue>
9 #include <vector>
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"
20 namespace base {
21 class SingleThreadTaskRunner;
22 class Value;
25 namespace content {
26 class ChildThread;
28 // This is thread safe.
29 class WebMessagePortChannelImpl
30 : public blink::WebMessagePortChannel,
31 public IPC::Listener,
32 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> {
33 public:
34 explicit WebMessagePortChannelImpl(
35 const scoped_refptr<base::SingleThreadTaskRunner>&
36 main_thread_task_runner);
37 WebMessagePortChannelImpl(
38 int route_id,
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
55 // received messages.
56 static std::vector<TransferredMessagePort> ExtractMessagePortIDs(
57 const blink::WebMessagePortChannelArray& channels);
59 // Creates WebMessagePortChannelImpl instances for port IDs passed in from the
60 // browser process.
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.
69 void QueueMessages();
70 int message_port_id() const { return message_port_id_; }
72 private:
73 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>;
74 ~WebMessagePortChannelImpl() override;
76 // WebMessagePortChannel implementation.
77 virtual void setClient(blink::WebMessagePortChannelClient* client);
78 virtual void destroy();
79 virtual void postMessage(const blink::WebString& message,
80 blink::WebMessagePortChannelArray* channels);
81 virtual bool tryGetMessage(blink::WebString* message,
82 blink::WebMessagePortChannelArray& channels);
84 void Init();
85 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel);
86 void Send(IPC::Message* message);
87 void PostMessage(const MessagePortMessage& message,
88 blink::WebMessagePortChannelArray* channels);
90 // IPC::Listener implementation.
91 bool OnMessageReceived(const IPC::Message& message) override;
93 void OnMessage(const MessagePortMessage& message,
94 const std::vector<TransferredMessagePort>& sent_message_ports,
95 const std::vector<int>& new_routing_ids);
96 void OnMessagesQueued();
98 struct Message {
99 Message();
100 ~Message();
102 MessagePortMessage message;
103 blink::WebMessagePortChannelArray ports;
106 typedef std::queue<Message> MessageQueue;
107 MessageQueue message_queue_;
109 blink::WebMessagePortChannelClient* client_;
110 base::Lock lock_; // Locks access to above.
112 int route_id_; // The routing id for this object.
113 int message_port_id_; // A globally unique identifier for this message port.
114 // Flag to indicate if messages should be sent to the browser process as
115 // base::Value instances as opposed to being serialized using the default
116 // blink::WebSerializedScriptValue.
117 bool send_messages_as_values_;
118 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
120 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl);
123 } // namespace content
125 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_