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 <queue> | 8 #include <queue> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 class WebMessagePortChannelImpl | 29 class WebMessagePortChannelImpl |
30 : public blink::WebMessagePortChannel, | 30 : public blink::WebMessagePortChannel, |
31 public IPC::Listener, | 31 public IPC::Listener, |
32 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> { | 32 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> { |
33 public: | 33 public: |
34 explicit WebMessagePortChannelImpl( | 34 explicit WebMessagePortChannelImpl( |
35 const scoped_refptr<base::SingleThreadTaskRunner>& | 35 const scoped_refptr<base::SingleThreadTaskRunner>& |
36 main_thread_task_runner); | 36 main_thread_task_runner); |
37 WebMessagePortChannelImpl( | 37 WebMessagePortChannelImpl( |
38 int route_id, | 38 int route_id, |
39 int message_port_id, | 39 const TransferredMessagePort& port, |
40 const scoped_refptr<base::SingleThreadTaskRunner>& | 40 const scoped_refptr<base::SingleThreadTaskRunner>& |
41 main_thread_task_runner); | 41 main_thread_task_runner); |
42 | 42 |
43 static void CreatePair( | 43 static void CreatePair( |
44 const scoped_refptr<base::SingleThreadTaskRunner>& | 44 const scoped_refptr<base::SingleThreadTaskRunner>& |
45 main_thread_task_runner, | 45 main_thread_task_runner, |
46 blink::WebMessagePortChannel** channel1, | 46 blink::WebMessagePortChannel** channel1, |
47 blink::WebMessagePortChannel** channel2); | 47 blink::WebMessagePortChannel** channel2); |
48 | 48 |
49 // Extracts port IDs for passing on to the browser process, and queues any | 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). | 50 // received messages. Takes ownership of the passed array (and deletes it). |
51 static std::vector<int> ExtractMessagePortIDs( | 51 static std::vector<TransferredMessagePort> ExtractMessagePortIDs( |
52 blink::WebMessagePortChannelArray* channels); | 52 blink::WebMessagePortChannelArray* channels); |
53 | 53 |
| 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); |
| 58 |
| 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); |
| 66 |
54 // Queues received and incoming messages until there are no more in-flight | 67 // Queues received and incoming messages until there are no more in-flight |
55 // messages, then sends all of them to the browser process. | 68 // messages, then sends all of them to the browser process. |
56 void QueueMessages(); | 69 void QueueMessages(); |
57 int message_port_id() const { return message_port_id_; } | 70 int message_port_id() const { return message_port_id_; } |
58 | 71 |
59 private: | 72 private: |
60 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>; | 73 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>; |
61 ~WebMessagePortChannelImpl() override; | 74 ~WebMessagePortChannelImpl() override; |
62 | 75 |
63 // WebMessagePortChannel implementation. | 76 // WebMessagePortChannel implementation. |
64 virtual void setClient(blink::WebMessagePortChannelClient* client); | 77 virtual void setClient(blink::WebMessagePortChannelClient* client); |
65 virtual void destroy(); | 78 virtual void destroy(); |
66 virtual void postMessage(const blink::WebString& message, | 79 virtual void postMessage(const blink::WebString& message, |
67 blink::WebMessagePortChannelArray* channels); | 80 blink::WebMessagePortChannelArray* channels); |
68 virtual bool tryGetMessage(blink::WebString* message, | 81 virtual bool tryGetMessage(blink::WebString* message, |
69 blink::WebMessagePortChannelArray& channels); | 82 blink::WebMessagePortChannelArray& channels); |
70 | 83 |
71 void Init(); | 84 void Init(); |
72 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel); | 85 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel); |
73 void Send(IPC::Message* message); | 86 void Send(IPC::Message* message); |
74 void PostMessage(const MessagePortMessage& message, | 87 void PostMessage(const MessagePortMessage& message, |
75 blink::WebMessagePortChannelArray* channels); | 88 blink::WebMessagePortChannelArray* channels); |
76 | 89 |
77 // IPC::Listener implementation. | 90 // IPC::Listener implementation. |
78 bool OnMessageReceived(const IPC::Message& message) override; | 91 bool OnMessageReceived(const IPC::Message& message) override; |
79 | 92 |
80 void OnMessage(const MessagePortMessage& message, | 93 void OnMessage(const MessagePortMessage& message, |
81 const std::vector<int>& sent_message_port_ids, | 94 const std::vector<TransferredMessagePort>& sent_message_ports, |
82 const std::vector<int>& new_routing_ids); | 95 const std::vector<int>& new_routing_ids); |
83 void OnMessagesQueued(); | 96 void OnMessagesQueued(); |
84 | 97 |
85 struct Message { | 98 struct Message { |
86 Message(); | 99 Message(); |
87 ~Message(); | 100 ~Message(); |
88 | 101 |
89 MessagePortMessage message; | 102 MessagePortMessage message; |
90 std::vector<WebMessagePortChannelImpl*> ports; | 103 blink::WebMessagePortChannelArray ports; |
91 }; | 104 }; |
92 | 105 |
93 typedef std::queue<Message> MessageQueue; | 106 typedef std::queue<Message> MessageQueue; |
94 MessageQueue message_queue_; | 107 MessageQueue message_queue_; |
95 | 108 |
96 blink::WebMessagePortChannelClient* client_; | 109 blink::WebMessagePortChannelClient* client_; |
97 base::Lock lock_; // Locks access to above. | 110 base::Lock lock_; // Locks access to above. |
98 | 111 |
99 int route_id_; // The routing id for this object. | 112 int route_id_; // The routing id for this object. |
100 int message_port_id_; // A globally unique identifier for this message port. | 113 int message_port_id_; // A globally unique identifier for this message port. |
101 // Flag to indicate if messages should be sent to the browser process as | 114 // Flag to indicate if messages should be sent to the browser process as |
102 // base::Value instances as opposed to being serialized using the default | 115 // base::Value instances as opposed to being serialized using the default |
103 // blink::WebSerializedScriptValue. | 116 // blink::WebSerializedScriptValue. |
104 bool send_messages_as_values_; | 117 bool send_messages_as_values_; |
105 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 118 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
106 | 119 |
107 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl); | 120 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl); |
108 }; | 121 }; |
109 | 122 |
110 } // namespace content | 123 } // namespace content |
111 | 124 |
112 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ | 125 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ |
OLD | NEW |