Fix link in German terms of service.
[chromium-blink-merge.git] / content / child / webmessageportchannel_impl.h
blobc6306e85f624147ef967d3fdac57eaf7d5de20e7
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 void set_is_stashed() { is_stashed_ = true; }
74 private:
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);
86 void Init();
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();
100 struct Message {
101 Message();
102 ~Message();
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_;
120 bool is_stashed_;
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_