OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ | 5 #ifndef CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ |
6 #define CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ | 6 #define CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "content/public/common/message_port_types.h" | |
17 #include "ipc/ipc_listener.h" | 16 #include "ipc/ipc_listener.h" |
18 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" | 17 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" |
19 | 18 |
20 namespace base { | 19 namespace base { |
21 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
22 class Value; | |
23 } | 21 } |
24 | 22 |
25 namespace content { | 23 namespace content { |
26 class ChildThread; | 24 class ChildThread; |
27 | 25 |
28 // This is thread safe. | 26 // This is thread safe. |
29 class WebMessagePortChannelImpl | 27 class WebMessagePortChannelImpl |
30 : public blink::WebMessagePortChannel, | 28 : public blink::WebMessagePortChannel, |
31 public IPC::Listener, | 29 public IPC::Listener, |
32 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> { | 30 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> { |
33 public: | 31 public: |
34 explicit WebMessagePortChannelImpl( | 32 explicit WebMessagePortChannelImpl( |
35 const scoped_refptr<base::SingleThreadTaskRunner>& | 33 const scoped_refptr<base::SingleThreadTaskRunner>& |
36 main_thread_task_runner); | 34 main_thread_task_runner); |
37 WebMessagePortChannelImpl( | 35 WebMessagePortChannelImpl( |
38 int route_id, | 36 int route_id, |
39 const TransferredMessagePort& port, | 37 int port_id, |
40 const scoped_refptr<base::SingleThreadTaskRunner>& | 38 const scoped_refptr<base::SingleThreadTaskRunner>& |
41 main_thread_task_runner); | 39 main_thread_task_runner); |
42 | 40 |
43 static void CreatePair( | 41 static void CreatePair( |
44 const scoped_refptr<base::SingleThreadTaskRunner>& | 42 const scoped_refptr<base::SingleThreadTaskRunner>& |
45 main_thread_task_runner, | 43 main_thread_task_runner, |
46 blink::WebMessagePortChannel** channel1, | 44 blink::WebMessagePortChannel** channel1, |
47 blink::WebMessagePortChannel** channel2); | 45 blink::WebMessagePortChannel** channel2); |
48 | 46 |
49 // Extracts port IDs for passing on to the browser process, and queues any | 47 // Extracts port IDs for passing on to the browser process, and queues any |
50 // received messages. | 48 // received messages. |
51 static std::vector<TransferredMessagePort> ExtractMessagePortIDs( | 49 static std::vector<int> ExtractMessagePortIDs( |
52 std::unique_ptr<blink::WebMessagePortChannelArray> channels); | 50 std::unique_ptr<blink::WebMessagePortChannelArray> channels); |
53 | 51 |
54 // Extracts port IDs for passing on to the browser process, and queues any | 52 // Extracts port IDs for passing on to the browser process, and queues any |
55 // received messages. | 53 // received messages. |
56 static std::vector<TransferredMessagePort> ExtractMessagePortIDs( | 54 static std::vector<int> ExtractMessagePortIDs( |
57 const blink::WebMessagePortChannelArray& channels); | 55 const blink::WebMessagePortChannelArray& channels); |
58 | 56 |
59 // Extracts port IDs for passing on to the browser process, but doesn't | 57 // 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 | 58 // send a separate IPC to the browser to initiate queueing messages. Instead |
61 // calling code is responsible for initiating the queueing in the browser | 59 // calling code is responsible for initiating the queueing in the browser |
62 // process. This is useful when transfering ports over an IPC channel that | 60 // process. This is useful when transfering ports over an IPC channel that |
63 // does not share ordering guarentees with regular IPC. | 61 // does not share ordering guarentees with regular IPC. |
64 static std::vector<TransferredMessagePort> | 62 static std::vector<int> |
65 ExtractMessagePortIDsWithoutQueueing( | 63 ExtractMessagePortIDsWithoutQueueing( |
66 std::unique_ptr<blink::WebMessagePortChannelArray> channels); | 64 std::unique_ptr<blink::WebMessagePortChannelArray> channels); |
67 | 65 |
68 // Creates WebMessagePortChannelImpl instances for port IDs passed in from the | 66 // Creates WebMessagePortChannelImpl instances for port IDs passed in from the |
69 // browser process. | 67 // browser process. |
70 static blink::WebMessagePortChannelArray CreatePorts( | 68 static blink::WebMessagePortChannelArray CreatePorts( |
71 const std::vector<TransferredMessagePort>& message_ports, | 69 const std::vector<int>& message_ports, |
72 const std::vector<int>& new_routing_ids, | 70 const std::vector<int>& new_routing_ids, |
73 const scoped_refptr<base::SingleThreadTaskRunner>& | 71 const scoped_refptr<base::SingleThreadTaskRunner>& |
74 main_thread_task_runner); | 72 main_thread_task_runner); |
75 | 73 |
76 // Queues received and incoming messages until there are no more in-flight | 74 // Queues received and incoming messages until there are no more in-flight |
77 // messages, then sends all of them to the browser process. | 75 // messages, then sends all of them to the browser process. |
78 void QueueMessages(); | 76 void QueueMessages(); |
79 int message_port_id() const { return message_port_id_; } | 77 int message_port_id() const { return message_port_id_; } |
80 | 78 |
81 private: | 79 private: |
82 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>; | 80 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>; |
83 ~WebMessagePortChannelImpl() override; | 81 ~WebMessagePortChannelImpl() override; |
84 | 82 |
85 // WebMessagePortChannel implementation. | 83 // WebMessagePortChannel implementation. |
86 void setClient(blink::WebMessagePortChannelClient* client) override; | 84 void setClient(blink::WebMessagePortChannelClient* client) override; |
87 void destroy() override; | 85 void destroy() override; |
88 void postMessage(const blink::WebString& message, | 86 void postMessage(const blink::WebString& message, |
89 blink::WebMessagePortChannelArray* channels_ptr) override; | 87 blink::WebMessagePortChannelArray* channels_ptr) override; |
90 bool tryGetMessage(blink::WebString* message, | 88 bool tryGetMessage(blink::WebString* message, |
91 blink::WebMessagePortChannelArray& channels) override; | 89 blink::WebMessagePortChannelArray& channels) override; |
92 | 90 |
93 void Init(); | 91 void Init(); |
94 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel); | 92 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel); |
95 void Send(IPC::Message* message); | 93 void Send(IPC::Message* message); |
96 void SendPostMessage( | 94 void SendPostMessage( |
97 const MessagePortMessage& message, | 95 const base::string16& message, |
98 std::unique_ptr<blink::WebMessagePortChannelArray> channels); | 96 std::unique_ptr<blink::WebMessagePortChannelArray> channels); |
99 | 97 |
100 // IPC::Listener implementation. | 98 // IPC::Listener implementation. |
101 bool OnMessageReceived(const IPC::Message& message) override; | 99 bool OnMessageReceived(const IPC::Message& message) override; |
102 | 100 |
103 void OnMessage(const MessagePortMessage& message, | 101 void OnMessage(const base::string16& message, |
104 const std::vector<TransferredMessagePort>& sent_message_ports, | 102 const std::vector<int>& sent_message_ports, |
105 const std::vector<int>& new_routing_ids); | 103 const std::vector<int>& new_routing_ids); |
106 void OnMessagesQueued(); | 104 void OnMessagesQueued(); |
107 | 105 |
108 struct Message { | 106 struct Message { |
109 Message(); | 107 Message(); |
110 Message(const Message& other); | 108 Message(const Message& other); |
111 ~Message(); | 109 ~Message(); |
112 | 110 |
113 MessagePortMessage message; | 111 base::string16 message; |
114 blink::WebMessagePortChannelArray ports; | 112 blink::WebMessagePortChannelArray ports; |
115 }; | 113 }; |
116 | 114 |
117 typedef std::queue<Message> MessageQueue; | 115 typedef std::queue<Message> MessageQueue; |
118 MessageQueue message_queue_; | 116 MessageQueue message_queue_; |
119 | 117 |
120 blink::WebMessagePortChannelClient* client_; | 118 blink::WebMessagePortChannelClient* client_; |
121 base::Lock lock_; // Locks access to above. | 119 base::Lock lock_; // Locks access to above. |
122 | 120 |
123 int route_id_; // The routing id for this object. | 121 int route_id_; // The routing id for this object. |
124 int message_port_id_; // A globally unique identifier for this message port. | 122 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_; | |
129 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 123 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
130 | 124 |
131 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl); | 125 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl); |
132 }; | 126 }; |
133 | 127 |
134 } // namespace content | 128 } // namespace content |
135 | 129 |
136 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ | 130 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ |
OLD | NEW |