Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / system / channel.h
index 61895d6..a322b2b 100644 (file)
 #include "base/threading/thread_checker.h"
 #include "mojo/embedder/scoped_platform_handle.h"
 #include "mojo/public/c/system/types.h"
+#include "mojo/system/channel_endpoint.h"
 #include "mojo/system/message_in_transit.h"
 #include "mojo/system/message_pipe.h"
 #include "mojo/system/raw_channel.h"
 #include "mojo/system/system_impl_export.h"
 
 namespace mojo {
+
+namespace embedder {
+class PlatformSupport;
+}
+
 namespace system {
 
+class ChannelEndpoint;
+
 // This class is mostly thread-safe. It must be created on an I/O thread.
 // |Init()| must be called on that same thread before it becomes thread-safe (in
 // particular, before references are given to any other thread) and |Shutdown()|
 // must be called on that same thread before destruction. Its public methods are
-// otherwise thread-safe. It may be destroyed on any thread, in the sense that
-// the last reference to it may be released on any thread, with the proviso that
+// otherwise thread-safe. (Many private methods are restricted to the creation
+// thread.) It may be destroyed on any thread, in the sense that the last
+// reference to it may be released on any thread, with the proviso that
 // |Shutdown()| must have been called first (so the pattern is that a "main"
 // reference is kept on its creation thread and is released after |Shutdown()|
 // is called, but other threads may have temporarily "dangling" references).
 //
-// Note that |MessagePipe| calls into |Channel| and the former's |lock_| must be
-// acquired before the latter's. When |Channel| wants to call into a
-// |MessagePipe|, it must obtain a reference to the |MessagePipe| (from
-// |local_id_to_endpoint_info_map_|) under |Channel::lock_| and then release the
-// lock.
-//
-// Also, care must be taken with respect to references: While a |Channel| has
-// references to |MessagePipe|s, |MessagePipe|s (via |ProxyMessagePipeEndpoint|)
-// may also have references to |Channel|s. These references are set up by
-// calling |AttachMessagePipeEndpoint()|. The reference to |MessagePipe| owned
-// by |Channel| must be removed by calling |DetachMessagePipeEndpoint()| (which
-// is done by |MessagePipe|/|ProxyMessagePipeEndpoint|, which simultaneously
-// removes its reference to |Channel|).
+// Note the lock order (in order of allowable acquisition): |MessagePipe|,
+// |ChannelEndpoint|, |Channel|. Thus |Channel| may not call into
+// |ChannelEndpoint| with |Channel|'s lock held.
 class MOJO_SYSTEM_IMPL_EXPORT Channel
     : public base::RefCountedThreadSafe<Channel>,
       public RawChannel::Delegate {
@@ -55,7 +54,9 @@ class MOJO_SYSTEM_IMPL_EXPORT Channel
   // The first message pipe endpoint attached will have this as its local ID.
   static const MessageInTransit::EndpointId kBootstrapEndpointId = 1;
 
-  Channel();
+  // |platform_support| (typically owned by |Core|) must remain alive until
+  // after |Shutdown()| is called.
+  explicit Channel(embedder::PlatformSupport* platform_support);
 
   // This must be called on the creation thread before any other methods are
   // called, and before references to this object are given to any other
@@ -73,17 +74,17 @@ class MOJO_SYSTEM_IMPL_EXPORT Channel
   // may be called multiple times, or not at all.)
   void WillShutdownSoon();
 
-  // Attaches the given message pipe/port's endpoint (which must be a
-  // |ProxyMessagePipeEndpoint|) to this channel. This assigns it a local ID,
-  // which it returns. The first message pipe endpoint attached will always have
+  // Attaches the given endpoint to this channel. This assigns it a local ID,
+  // which it returns. The first endpoint attached will always have
   // |kBootstrapEndpointId| as its local ID. (For bootstrapping, this occurs on
   // both sides, so one should use |kBootstrapEndpointId| for the remote ID for
   // the first message pipe across a channel.) Returns |kInvalidEndpointId| on
   // failure.
+  // TODO(vtl): This should be combined with "run", and it should take a
+  // |ChannelEndpoint| instead.
   // TODO(vtl): Maybe limit the number of attached message pipes.
-  MessageInTransit::EndpointId AttachMessagePipeEndpoint(
-      scoped_refptr<MessagePipe> message_pipe,
-      unsigned port);
+  MessageInTransit::EndpointId AttachEndpoint(
+      scoped_refptr<ChannelEndpoint> endpoint);
 
   // Runs the message pipe with the given |local_id| (previously attached), with
   // the given |remote_id| (negotiated using some other means, e.g., over an
@@ -112,48 +113,29 @@ class MOJO_SYSTEM_IMPL_EXPORT Channel
 
   // This removes the message pipe/port's endpoint (with the given local ID and
   // given remote ID, which should be |kInvalidEndpointId| if not yet running),
-  // returned by |AttachMessagePipeEndpoint()| from this channel. After this is
-  // called, |local_id| may be reused for another message pipe.
+  // returned by |AttachEndpoint()| from this channel. After this is called,
+  // |local_id| may be reused for another message pipe.
   void DetachMessagePipeEndpoint(MessageInTransit::EndpointId local_id,
                                  MessageInTransit::EndpointId remote_id);
 
   // See |RawChannel::GetSerializedPlatformHandleSize()|.
   size_t GetSerializedPlatformHandleSize() const;
 
- private:
-  struct EndpointInfo {
-    enum State {
-      // Attached, possibly running or not.
-      STATE_NORMAL,
-      // "Zombie" states:
-      // Waiting for |DetachMessagePipeEndpoint()| before removing.
-      STATE_WAIT_LOCAL_DETACH,
-      // Waiting for a |kSubtypeChannelRemoveMessagePipeEndpointAck| before
-      // removing.
-      STATE_WAIT_REMOTE_REMOVE_ACK,
-      // Waiting for both of the above conditions before removing.
-      STATE_WAIT_LOCAL_DETACH_AND_REMOTE_REMOVE_ACK,
-    };
-
-    EndpointInfo();
-    EndpointInfo(scoped_refptr<MessagePipe> message_pipe, unsigned port);
-    ~EndpointInfo();
-
-    State state;
-    scoped_refptr<MessagePipe> message_pipe;
-    unsigned port;
-  };
+  embedder::PlatformSupport* platform_support() const {
+    return platform_support_;
+  }
 
+ private:
   friend class base::RefCountedThreadSafe<Channel>;
   virtual ~Channel();
 
-  // |RawChannel::Delegate| implementation:
+  // |RawChannel::Delegate| implementation (only called on the creation thread):
   virtual void OnReadMessage(
       const MessageInTransit::View& message_view,
       embedder::ScopedPlatformHandleVectorPtr platform_handles) OVERRIDE;
   virtual void OnError(Error error) OVERRIDE;
 
-  // Helpers for |OnReadMessage|:
+  // Helpers for |OnReadMessage| (only called on the creation thread):
   void OnReadMessageForDownstream(
       const MessageInTransit::View& message_view,
       embedder::ScopedPlatformHandleVectorPtr platform_handles);
@@ -163,28 +145,32 @@ class MOJO_SYSTEM_IMPL_EXPORT Channel
 
   // Removes the message pipe endpoint with the given local ID, which must exist
   // and be a zombie, and given remote ID. Returns false on failure, in
-  // particular if no message pipe with |local_id| is attached.
+  // particular if no message pipe with |local_id| is attached. Only called on
+  // the creation thread.
   bool RemoveMessagePipeEndpoint(MessageInTransit::EndpointId local_id,
                                  MessageInTransit::EndpointId remote_id);
 
-  // Handles errors (e.g., invalid messages) from the remote side.
+  // Handles errors (e.g., invalid messages) from the remote side. Callable from
+  // any thread.
   void HandleRemoteError(const base::StringPiece& error_message);
-  // Handles internal errors/failures from the local side.
+  // Handles internal errors/failures from the local side. Callable from any
+  // thread.
   void HandleLocalError(const base::StringPiece& error_message);
 
   // Helper to send channel control messages. Returns true on success. Should be
-  // called *without* |lock_| held.
+  // called *without* |lock_| held. Callable from any thread.
   bool SendControlMessage(MessageInTransit::Subtype subtype,
                           MessageInTransit::EndpointId source_id,
                           MessageInTransit::EndpointId destination_id);
 
   base::ThreadChecker creation_thread_checker_;
 
+  embedder::PlatformSupport* const platform_support_;
+
   // Note: |MessagePipe|s MUST NOT be used under |lock_|. I.e., |lock_| can only
   // be acquired after |MessagePipe::lock_|, never before. Thus to call into a
-  // |MessagePipe|, a reference should be acquired from
-  // |local_id_to_endpoint_info_map_| under |lock_| (e.g., by copying the
-  // |EndpointInfo|) and then the lock released.
+  // |MessagePipe|, a reference to the |MessagePipe| should be acquired from
+  // |local_id_to_endpoint_map_| under |lock_| and then the lock released.
   base::Lock lock_;  // Protects the members below.
 
   scoped_ptr<RawChannel> raw_channel_;
@@ -192,9 +178,9 @@ class MOJO_SYSTEM_IMPL_EXPORT Channel
   // Set when |WillShutdownSoon()| is called.
   bool is_shutting_down_;
 
-  typedef base::hash_map<MessageInTransit::EndpointId, EndpointInfo>
-      IdToEndpointInfoMap;
-  IdToEndpointInfoMap local_id_to_endpoint_info_map_;
+  typedef base::hash_map<MessageInTransit::EndpointId,
+                         scoped_refptr<ChannelEndpoint>> IdToEndpointMap;
+  IdToEndpointMap local_id_to_endpoint_map_;
   // The next local ID to try (when allocating new local IDs). Note: It should
   // be checked for existence before use.
   MessageInTransit::EndpointId next_local_id_;