Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: content/child/webmessageportchannel_impl.h

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Add metrics and support for non-ASCII text messages to Java endpoints Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/public/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(
49 std::unique_ptr<blink::WebMessagePortChannelArray> channels); 28 std::unique_ptr<blink::WebMessagePortChannelArray> channels);
50 29
51 // Extracts port IDs for passing on to the browser process, and queues any 30 // Extracts MessagePorts for passing on to other processes.
52 // received messages. 31 static std::vector<MessagePort> ExtractMessagePorts(
53 static std::vector<int> ExtractMessagePortIDs(
54 const blink::WebMessagePortChannelArray& channels); 32 const blink::WebMessagePortChannelArray& channels);
55 33
56 // Extracts port IDs for passing on to the browser process, but doesn't 34 // Creates WebMessagePortChannelImpl instances for MessagePorts passed in from
57 // send a separate IPC to the browser to initiate queueing messages. Instead 35 // other processes.
58 // calling code is responsible for initiating the queueing in the browser 36 static blink::WebMessagePortChannelArray CreateFromMessagePorts(
59 // process. This is useful when transfering ports over an IPC channel that 37 const std::vector<MessagePort>& message_ports);
60 // does not share ordering guarentees with regular IPC. 38 static blink::WebMessagePortChannelArray CreateFromMessagePipeHandles(
61 static std::vector<int> 39 std::vector<mojo::ScopedMessagePipeHandle> handles);
62 ExtractMessagePortIDsWithoutQueueing(
63 std::unique_ptr<blink::WebMessagePortChannelArray> channels);
64 40
65 // Creates WebMessagePortChannelImpl instances for port IDs passed in from the 41 MessagePort ReleaseMessagePort();
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 42
78 private: 43 private:
79 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>; 44 explicit WebMessagePortChannelImpl(mojo::ScopedMessagePipeHandle handle);
80 ~WebMessagePortChannelImpl() override;
81 45
82 // WebMessagePortChannel implementation. 46 // WebMessagePortChannel implementation.
83 void setClient(blink::WebMessagePortChannelClient* client) override; 47 void setClient(blink::WebMessagePortChannelClient* client) override;
84 void destroy() override; 48 void postMessage(const blink::WebString& encoded_message,
85 void postMessage(const blink::WebString& message,
86 blink::WebMessagePortChannelArray* channels_ptr) override; 49 blink::WebMessagePortChannelArray* channels_ptr) override;
87 bool tryGetMessage(blink::WebString* message, 50 bool tryGetMessage(blink::WebString* encoded_message,
88 blink::WebMessagePortChannelArray& channels) override; 51 blink::WebMessagePortChannelArray& channels) override;
89 52
90 void Init(); 53 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 54
124 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl); 55 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl);
125 }; 56 };
126 57
127 } // namespace content 58 } // namespace content
128 59
129 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ 60 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698