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. 4 5 #ifndef CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ 6 #define CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ 7 8 #include <queue> 9 #include <vector> 10 11 #include "base/basictypes.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/strings/string16.h" 14 #include "base/synchronization/lock.h" 15 #include "ipc/ipc_listener.h" 16 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" 17 18 namespace base { 19 class MessageLoopProxy; 20 } 21 22 namespace content { 23 class ChildThread; 24 25 // This is thread safe. 26 class WebMessagePortChannelImpl 27 : public blink::WebMessagePortChannel, 28 public IPC::Listener, 29 public base::RefCountedThreadSafe<WebMessagePortChannelImpl> { 30 public: 31 explicit WebMessagePortChannelImpl( 32 const scoped_refptr<base::MessageLoopProxy>& child_thread_loop); 33 WebMessagePortChannelImpl( 34 int route_id, 35 int message_port_id, 36 const scoped_refptr<base::MessageLoopProxy>& child_thread_loop); 37 38 static void CreatePair( 39 const scoped_refptr<base::MessageLoopProxy>& child_thread_loop, 40 blink::WebMessagePortChannel** channel1, 41 blink::WebMessagePortChannel** channel2); 42 43 // Extracts port IDs for passing on to the browser process, and queues any 44 // received messages. Takes ownership of the passed array (and deletes it). 45 static std::vector<int> ExtractMessagePortIDs( 46 blink::WebMessagePortChannelArray* channels); 47 48 // Queues received and incoming messages until there are no more in-flight 49 // messages, then sends all of them to the browser process. 50 void QueueMessages(); message_port_id()51 int message_port_id() const { return message_port_id_; } 52 53 private: 54 friend class base::RefCountedThreadSafe<WebMessagePortChannelImpl>; 55 virtual ~WebMessagePortChannelImpl(); 56 57 // WebMessagePortChannel implementation. 58 virtual void setClient(blink::WebMessagePortChannelClient* client); 59 virtual void destroy(); 60 virtual void postMessage(const blink::WebString& message, 61 blink::WebMessagePortChannelArray* channels); 62 virtual bool tryGetMessage(blink::WebString* message, 63 blink::WebMessagePortChannelArray& channels); 64 65 void Init(); 66 void Entangle(scoped_refptr<WebMessagePortChannelImpl> channel); 67 void Send(IPC::Message* message); 68 void PostMessage(const base::string16& message, 69 blink::WebMessagePortChannelArray* channels); 70 71 // IPC::Listener implementation. 72 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 73 74 void OnMessage(const base::string16& message, 75 const std::vector<int>& sent_message_port_ids, 76 const std::vector<int>& new_routing_ids); 77 void OnMessagesQueued(); 78 79 struct Message { 80 Message(); 81 ~Message(); 82 83 base::string16 message; 84 std::vector<WebMessagePortChannelImpl*> ports; 85 }; 86 87 typedef std::queue<Message> MessageQueue; 88 MessageQueue message_queue_; 89 90 blink::WebMessagePortChannelClient* client_; 91 base::Lock lock_; // Locks access to above. 92 93 int route_id_; // The routing id for this object. 94 int message_port_id_; // A globally unique identifier for this message port. 95 scoped_refptr<base::MessageLoopProxy> child_thread_loop_; 96 97 DISALLOW_COPY_AND_ASSIGN(WebMessagePortChannelImpl); 98 }; 99 100 } // namespace content 101 102 #endif // CONTENT_CHILD_WEBMESSAGEPORTCHANNEL_IMPL_H_ 103