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