| OLD | NEW | 
|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 5 #ifndef MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 
| 6 #define MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 6 #define MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 
| 7 | 7 | 
| 8 #include <memory> | 8 #include <memory> | 
| 9 #include <vector> | 9 #include <vector> | 
| 10 | 10 | 
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" | 
| 12 #include "mojo/edk/embedder/scoped_platform_handle.h" | 12 #include "mojo/edk/platform/platform_handle.h" | 
|  | 13 #include "mojo/edk/platform/scoped_platform_handle.h" | 
| 13 #include "mojo/edk/system/message_in_transit.h" | 14 #include "mojo/edk/system/message_in_transit.h" | 
| 14 #include "mojo/edk/system/message_in_transit_queue.h" | 15 #include "mojo/edk/system/message_in_transit_queue.h" | 
| 15 #include "mojo/edk/util/mutex.h" | 16 #include "mojo/edk/util/mutex.h" | 
| 16 #include "mojo/edk/util/thread_annotations.h" | 17 #include "mojo/edk/util/thread_annotations.h" | 
| 17 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" | 
| 18 | 19 | 
| 19 namespace base { | 20 namespace base { | 
| 20 class MessageLoopForIO; | 21 class MessageLoopForIO; | 
| 21 } | 22 } | 
| 22 | 23 | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 59       // Unknown read error. | 60       // Unknown read error. | 
| 60       ERROR_READ_UNKNOWN, | 61       ERROR_READ_UNKNOWN, | 
| 61       // Generic write error. | 62       // Generic write error. | 
| 62       ERROR_WRITE | 63       ERROR_WRITE | 
| 63     }; | 64     }; | 
| 64 | 65 | 
| 65     // Called when a message is read. This may call the |RawChannel|'s | 66     // Called when a message is read. This may call the |RawChannel|'s | 
| 66     // |Shutdown()| and then (if desired) destroy it. | 67     // |Shutdown()| and then (if desired) destroy it. | 
| 67     virtual void OnReadMessage( | 68     virtual void OnReadMessage( | 
| 68         const MessageInTransit::View& message_view, | 69         const MessageInTransit::View& message_view, | 
| 69         std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>> | 70         std::unique_ptr<std::vector<platform::ScopedPlatformHandle>> | 
| 70             platform_handles) = 0; | 71             platform_handles) = 0; | 
| 71 | 72 | 
| 72     // Called when there's a (fatal) error. This may call the |RawChannel|'s | 73     // Called when there's a (fatal) error. This may call the |RawChannel|'s | 
| 73     // |Shutdown()| and then (if desired) destroy it. | 74     // |Shutdown()| and then (if desired) destroy it. | 
| 74     // | 75     // | 
| 75     // For each raw channel, there'll be at most one |ERROR_READ_...| and at | 76     // For each raw channel, there'll be at most one |ERROR_READ_...| and at | 
| 76     // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, | 77     // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|, | 
| 77     // |OnReadMessage()| won't be called again. | 78     // |OnReadMessage()| won't be called again. | 
| 78     virtual void OnError(Error error) = 0; | 79     virtual void OnError(Error error) = 0; | 
| 79 | 80 | 
| 80    protected: | 81    protected: | 
| 81     virtual ~Delegate() {} | 82     virtual ~Delegate() {} | 
| 82   }; | 83   }; | 
| 83 | 84 | 
| 84   // Static factory method. |handle| should be a handle to a | 85   // Static factory method. |handle| should be a handle to a | 
| 85   // (platform-appropriate) bidirectional communication channel (e.g., a socket | 86   // (platform-appropriate) bidirectional communication channel (e.g., a socket | 
| 86   // on POSIX, a named pipe on Windows). | 87   // on POSIX, a named pipe on Windows). | 
| 87   static std::unique_ptr<RawChannel> Create( | 88   static std::unique_ptr<RawChannel> Create( | 
| 88       embedder::ScopedPlatformHandle handle); | 89       platform::ScopedPlatformHandle handle); | 
| 89 | 90 | 
| 90   // This must be called (on an I/O thread) before this object is used. Does | 91   // This must be called (on an I/O thread) before this object is used. Does | 
| 91   // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must | 92   // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must | 
| 92   // remain alive until |Shutdown()| is called (unless this fails); |delegate| | 93   // remain alive until |Shutdown()| is called (unless this fails); |delegate| | 
| 93   // will no longer be used after |Shutdown()|. | 94   // will no longer be used after |Shutdown()|. | 
| 94   void Init(Delegate* delegate) MOJO_NOT_THREAD_SAFE; | 95   void Init(Delegate* delegate) MOJO_NOT_THREAD_SAFE; | 
| 95 | 96 | 
| 96   // This must be called (on the I/O thread) before this object is destroyed. | 97   // This must be called (on the I/O thread) before this object is destroyed. | 
| 97   void Shutdown() MOJO_NOT_THREAD_SAFE; | 98   void Shutdown() MOJO_NOT_THREAD_SAFE; | 
| 98 | 99 | 
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 164     // true. There are two components to this: the actual |PlatformHandle|s | 165     // true. There are two components to this: the actual |PlatformHandle|s | 
| 165     // (which should be closed once sent) and any additional serialization | 166     // (which should be closed once sent) and any additional serialization | 
| 166     // information (which will be embedded in the message's data; there are | 167     // information (which will be embedded in the message's data; there are | 
| 167     // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform | 168     // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform | 
| 168     // handles have been sent, the message data should be written next (see | 169     // handles have been sent, the message data should be written next (see | 
| 169     // |GetBuffers()|). | 170     // |GetBuffers()|). | 
| 170     // TODO(vtl): Maybe this method should be const, but | 171     // TODO(vtl): Maybe this method should be const, but | 
| 171     // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies | 172     // |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies | 
| 172     // state). | 173     // state). | 
| 173     void GetPlatformHandlesToSend(size_t* num_platform_handles, | 174     void GetPlatformHandlesToSend(size_t* num_platform_handles, | 
| 174                                   embedder::PlatformHandle** platform_handles, | 175                                   platform::PlatformHandle** platform_handles, | 
| 175                                   void** serialization_data); | 176                                   void** serialization_data); | 
| 176 | 177 | 
| 177     // Gets buffers to be written. These buffers will always come from the front | 178     // Gets buffers to be written. These buffers will always come from the front | 
| 178     // of |message_queue_|. Once they are completely written, the front | 179     // of |message_queue_|. Once they are completely written, the front | 
| 179     // |MessageInTransit| should be popped (and destroyed); this is done in | 180     // |MessageInTransit| should be popped (and destroyed); this is done in | 
| 180     // |OnWriteCompletedNoLock()|. | 181     // |OnWriteCompletedNoLock()|. | 
| 181     void GetBuffers(std::vector<Buffer>* buffers) const; | 182     void GetBuffers(std::vector<Buffer>* buffers) const; | 
| 182 | 183 | 
| 183    private: | 184    private: | 
| 184     friend class RawChannel; | 185     friend class RawChannel; | 
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 254   // Similar to |Read()|, except that the implementing subclass must also | 255   // Similar to |Read()|, except that the implementing subclass must also | 
| 255   // guarantee that the method doesn't succeed synchronously, i.e., it only | 256   // guarantee that the method doesn't succeed synchronously, i.e., it only | 
| 256   // returns |IO_FAILED_...| or |IO_PENDING|. | 257   // returns |IO_FAILED_...| or |IO_PENDING|. | 
| 257   virtual IOResult ScheduleRead() MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; | 258   virtual IOResult ScheduleRead() MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; | 
| 258 | 259 | 
| 259   // Called by |OnReadCompleted()| to get the platform handles associated with | 260   // Called by |OnReadCompleted()| to get the platform handles associated with | 
| 260   // the given platform handle table (from a message). This should only be | 261   // the given platform handle table (from a message). This should only be | 
| 261   // called when |num_platform_handles| is nonzero. Returns null if the | 262   // called when |num_platform_handles| is nonzero. Returns null if the | 
| 262   // |num_platform_handles| handles are not available. Only called on the I/O | 263   // |num_platform_handles| handles are not available. Only called on the I/O | 
| 263   // thread. | 264   // thread. | 
| 264   virtual std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>> | 265   virtual std::unique_ptr<std::vector<platform::ScopedPlatformHandle>> | 
| 265   GetReadPlatformHandles(size_t num_platform_handles, | 266   GetReadPlatformHandles(size_t num_platform_handles, | 
| 266                          const void* platform_handle_table) | 267                          const void* platform_handle_table) | 
| 267       MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; | 268       MOJO_LOCKS_EXCLUDED(write_mutex_) = 0; | 
| 268 | 269 | 
| 269   // Writes contents in |write_buffer_no_lock()|. | 270   // Writes contents in |write_buffer_no_lock()|. | 
| 270   // This class guarantees that: | 271   // This class guarantees that: | 
| 271   // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the | 272   // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the | 
| 272   //   buffer(s) given by |GetBuffers()| will remain valid until write | 273   //   buffer(s) given by |GetBuffers()| will remain valid until write | 
| 273   //   completion (see also the comments for |OnShutdownNoLock()|); | 274   //   completion (see also the comments for |OnShutdownNoLock()|); | 
| 274   // - a second write is not started if there is a pending write. | 275   // - a second write is not started if there is a pending write. | 
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 331   base::WeakPtrFactory<RawChannel> weak_ptr_factory_ | 332   base::WeakPtrFactory<RawChannel> weak_ptr_factory_ | 
| 332       MOJO_GUARDED_BY(write_mutex_); | 333       MOJO_GUARDED_BY(write_mutex_); | 
| 333 | 334 | 
| 334   MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 335   MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel); | 
| 335 }; | 336 }; | 
| 336 | 337 | 
| 337 }  // namespace system | 338 }  // namespace system | 
| 338 }  // namespace mojo | 339 }  // namespace mojo | 
| 339 | 340 | 
| 340 #endif  // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 341 #endif  // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_ | 
| OLD | NEW | 
|---|