14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
24e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
34e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// found in the LICENSE file.
44e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#ifndef MOJO_SYSTEM_RAW_CHANNEL_H_
64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#define MOJO_SYSTEM_RAW_CHANNEL_H_
74e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <deque>
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <vector>
104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/macros.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/weak_ptr.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/synchronization/lock.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "mojo/embedder/platform_handle_vector.h"
1623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "mojo/embedder/scoped_platform_handle.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "mojo/system/constants.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/system/message_in_transit.h"
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "mojo/system/system_impl_export.h"
204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace base {
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class MessageLoopForIO;
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace mojo {
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace system {
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// |RawChannel| is an interface and base class for objects that wrap an OS
2923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)// "pipe". It presents the following interface to users:
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//  - Receives and dispatches messages on an I/O thread (running a
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//    |MessageLoopForIO|.
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//  - Provides a thread-safe way of writing messages (|WriteMessage()|);
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//    writing/queueing messages will not block and is atomic from the point of
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//    view of the caller. If necessary, messages are queued (to be written on
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//    the aforementioned thread).
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// OS-specific implementation subclasses are to be instantiated using the
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// |Create()| static factory method.
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)//
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// With the exception of |WriteMessage()|, this class is thread-unsafe (and in
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// general its methods should only be used on the I/O thread, i.e., the thread
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// on which |Init()| is called).
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) public:
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~RawChannel();
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The |Delegate| is only accessed on the same thread as the message loop
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // (passed in on creation).
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  class MOJO_SYSTEM_IMPL_EXPORT Delegate {
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   public:
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    enum Error {
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Failed read due to raw channel shutdown (e.g., on the other side).
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ERROR_READ_SHUTDOWN,
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Failed read due to raw channel being broken (e.g., if the other side
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // died without shutting down).
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ERROR_READ_BROKEN,
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Received a bad message.
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ERROR_READ_BAD_MESSAGE,
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Unknown read error.
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ERROR_READ_UNKNOWN,
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      // Generic write error.
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ERROR_WRITE
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    };
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // Called when a message is read. This may call |Shutdown()| (on the
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    // |RawChannel|), but must not destroy it.
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    virtual void OnReadMessage(
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        const MessageInTransit::View& message_view,
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0;
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Called when there's a (fatal) error. This may call the raw channel's
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // |Shutdown()|, but must not destroy it.
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    //
746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // For each raw channel, there'll be at most one |ERROR_READ_...| and at
756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|,
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // |OnReadMessage()| won't be called again.
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    virtual void OnError(Error error) = 0;
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   protected:
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    virtual ~Delegate() {}
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Static factory method. |handle| should be a handle to a
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // (platform-appropriate) bidirectional communication channel (e.g., a socket
85c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // on POSIX, a named pipe on Windows).
86c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle);
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
88c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // This must be called (on an I/O thread) before this object is used. Does
89c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // *not* take ownership of |delegate|. Both the I/O thread and |delegate| must
905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // remain alive until |Shutdown()| is called (unless this fails); |delegate|
915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // will no longer be used after |Shutdown()|. Returns true on success. On
92c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // failure, |Shutdown()| should *not* be called.
93c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool Init(Delegate* delegate);
944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // This must be called (on the I/O thread) before this object is destroyed.
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void Shutdown();
974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Writes the given message (or schedules it to be written). |message| must
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // have no |Dispatcher|s still attached (i.e.,
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |SerializeAndCloseDispatchers()| should have been called). This method is
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // thread-safe and may be called from any thread. Returns true on success.
102a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool WriteMessage(scoped_ptr<MessageInTransit> message);
1034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
104effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Returns true if the write buffer is empty (i.e., all messages written using
105effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |WriteMessage()| have actually been sent.
106effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // TODO(vtl): We should really also notify our delegate when the write buffer
107effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // becomes empty (or something like that).
108effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  bool IsWriteBufferEmpty();
109effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
1100de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // Returns the amount of space needed in the |MessageInTransit|'s
1110de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // |TransportData|'s "platform handle table" per platform handle (to be
1120de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // attached to a message). (This amount may be zero.)
1130de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  virtual size_t GetSerializedPlatformHandleSize() const = 0;
1140de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) protected:
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Result of I/O operations.
1176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  enum IOResult {
1186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    IO_SUCCEEDED,
1196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Failed due to a (probably) clean shutdown (e.g., of the other end).
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    IO_FAILED_SHUTDOWN,
1216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Failed due to the connection being broken (e.g., the other end dying).
1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    IO_FAILED_BROKEN,
1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    // Failed due to some other (unexpected) reason.
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    IO_FAILED_UNKNOWN,
1256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    IO_PENDING
1266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  };
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  class MOJO_SYSTEM_IMPL_EXPORT ReadBuffer {
129a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   public:
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ReadBuffer();
131a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ~ReadBuffer();
132a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
133a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void GetBuffer(char** addr, size_t* size);
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   private:
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    friend class RawChannel;
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // We store data from |[Schedule]Read()|s in |buffer_|. The start of
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // |buffer_| is always aligned with a message boundary (we will copy memory
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // to ensure this), but |buffer_| may be larger than the actual number of
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // bytes we have.
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    std::vector<char> buffer_;
143a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    size_t num_valid_bytes_;
144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(ReadBuffer);
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  class MOJO_SYSTEM_IMPL_EXPORT WriteBuffer {
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   public:
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    struct Buffer {
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const char* addr;
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      size_t size;
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    };
1544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1550de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    explicit WriteBuffer(size_t serialized_platform_handle_size);
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ~WriteBuffer();
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1580de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // Returns true if there are (more) platform handles to be sent (from the
1590de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // front of |message_queue_|).
1600de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    bool HavePlatformHandlesToSend() const;
1610de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // Gets platform handles to be sent (from the front of |message_queue_|).
1620de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // This should only be called if |HavePlatformHandlesToSend()| returned
1630de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // true. There are two components to this: the actual |PlatformHandle|s
1640de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // (which should be closed once sent) and any additional serialization
1650de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // information (which will be embedded in the message's data; there are
1660de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // |GetSerializedPlatformHandleSize()| bytes per handle). Once all platform
1670de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // handles have been sent, the message data should be written next (see
1680de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // |GetBuffers()|).
1690de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    void GetPlatformHandlesToSend(size_t* num_platform_handles,
1700de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)                                  embedder::PlatformHandle** platform_handles,
1710de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)                                  void** serialization_data);
1720de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
173010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // Gets buffers to be written. These buffers will always come from the front
174010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // of |message_queue_|. Once they are completely written, the front
175010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // |MessageInTransit| should be popped (and destroyed); this is done in
176010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // |OnWriteCompletedNoLock()|.
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void GetBuffers(std::vector<Buffer>* buffers) const;
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   private:
180a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    friend class RawChannel;
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1820de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    const size_t serialized_platform_handle_size_;
1830de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // TODO(vtl): When C++11 is available, switch this to a deque of
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // |scoped_ptr|/|unique_ptr|s.
186a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    std::deque<MessageInTransit*> message_queue_;
1870de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // Platform handles are sent before the message data, but doing so may
1880de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // require several passes. |platform_handles_offset_| indicates the position
1890de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // in the first message's vector of platform handles to send next.
1900de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    size_t platform_handles_offset_;
1910de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // The first message's data may have been partially sent. |data_offset_|
1920de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // indicates the position in the first message's data to start the next
1930de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    // write.
1940de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)    size_t data_offset_;
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(WriteBuffer);
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
199c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  RawChannel();
200a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT
2026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // |write_lock_| held.
2036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void OnReadCompleted(IOResult io_result, size_t bytes_read);
2046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT
2056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // |write_lock_| held.
2066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void OnWriteCompleted(IOResult io_result,
207cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                        size_t platform_handles_written,
208cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                        size_t bytes_written);
209cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; }
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::Lock& write_lock() { return write_lock_; }
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
213cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Should only be called on the I/O thread.
214cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ReadBuffer* read_buffer() { return read_buffer_.get(); }
215cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
216cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Only called under |write_lock_|.
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  WriteBuffer* write_buffer_no_lock() {
218cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    write_lock_.AssertAcquired();
219cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return write_buffer_.get();
220cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
221cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
222cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Adds |message| to the write message queue. Implementation subclasses may
223cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // override this to add any additional "control" messages needed. This is
224cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // called (on any thread) with |write_lock_| held.
225cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void EnqueueMessageNoLock(scoped_ptr<MessageInTransit> message);
226cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
227cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Handles any control messages targeted to the |RawChannel| (or
228cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // implementation subclass). Implementation subclasses may override this to
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // handle any implementation-specific control messages, but should call
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |RawChannel::OnReadMessageForRawChannel()| for any remaining messages.
231cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Returns true on success and false on error (e.g., invalid control message).
232cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This is only called on the I/O thread.
233cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool OnReadMessageForRawChannel(
234cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const MessageInTransit::View& message_view);
235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Reads into |read_buffer()|.
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This class guarantees that:
238a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // - the area indicated by |GetBuffer()| will stay valid until read completion
239a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //   (but please also see the comments for |OnShutdownNoLock()|);
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // - a second read is not started if there is a pending read;
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // - the method is called on the I/O thread WITHOUT |write_lock_| held.
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The implementing subclass must guarantee that:
2440de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // - |bytes_read| is untouched unless |Read()| returns |IO_SUCCEEDED|;
2450de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // - if the method returns |IO_PENDING|, |OnReadCompleted()| will be called on
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //   the I/O thread to report the result, unless |Shutdown()| is called.
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual IOResult Read(size_t* bytes_read) = 0;
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Similar to |Read()|, except that the implementing subclass must also
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // guarantee that the method doesn't succeed synchronously, i.e., it only
2506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // returns |IO_FAILED_...| or |IO_PENDING|.
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual IOResult ScheduleRead() = 0;
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
253cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Called by |OnReadCompleted()| to get the platform handles associated with
254cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // the given platform handle table (from a message). This should only be
255cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // called when |num_platform_handles| is nonzero. Returns null if the
256cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |num_platform_handles| handles are not available. Only called on the I/O
257cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // thread (without |write_lock_| held).
258cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles(
259cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      size_t num_platform_handles,
260cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const void* platform_handle_table) = 0;
261cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Writes contents in |write_buffer_no_lock()|.
263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This class guarantees that:
2640de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // - the |PlatformHandle|s given by |GetPlatformHandlesToSend()| and the
2650de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  //   buffer(s) given by |GetBuffers()| will remain valid until write
2660de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  //   completion (see also the comments for |OnShutdownNoLock()|);
267a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // - a second write is not started if there is a pending write;
268a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // - the method is called under |write_lock_|.
269a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  //
270a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // The implementing subclass must guarantee that:
2710de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // - |platform_handles_written| and |bytes_written| are untouched unless
2720de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  //   |WriteNoLock()| returns |IO_SUCCEEDED|;
2730de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  // - if the method returns |IO_PENDING|, |OnWriteCompleted()| will be called
2740de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  //   on the I/O thread to report the result, unless |Shutdown()| is called.
2750de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  virtual IOResult WriteNoLock(size_t* platform_handles_written,
2760de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)                               size_t* bytes_written) = 0;
277a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Similar to |WriteNoLock()|, except that the implementing subclass must also
278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // guarantee that the method doesn't succeed synchronously, i.e., it only
2796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // returns |IO_FAILED_...| or |IO_PENDING|.
280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual IOResult ScheduleWriteNoLock() = 0;
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Must be called on the I/O thread WITHOUT |write_lock_| held.
283a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool OnInit() = 0;
2845c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // On shutdown, passes the ownership of the buffers to subclasses, which may
2855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // want to preserve them if there are pending read/write. Must be called on
2865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // the I/O thread under |write_lock_|.
2875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer,
2885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                scoped_ptr<WriteBuffer> write_buffer) = 0;
289a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) private:
2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Converts an |IO_FAILED_...| for a read to a |Delegate::Error|.
2926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  static Delegate::Error ReadIOResultToError(IOResult io_result);
2936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2946e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Calls |delegate_->OnError(error)|. Must be called on the I/O thread WITHOUT
2956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // |write_lock_| held.
2966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void CallOnError(Delegate::Error error);
2976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a
2996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // write operation to run later if there is more to write. If |io_result| is
3006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // failure or any other error occurs, cancels pending writes and returns
3016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // false. Must be called under |write_lock_| and only if |write_stopped_| is
3026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // false.
3036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool OnWriteCompletedNoLock(IOResult io_result,
3040de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)                              size_t platform_handles_written,
3050de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)                              size_t bytes_written);
306a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
307c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // Set in |Init()| and never changed (hence usable on any thread without
308c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  // locking):
309c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  base::MessageLoopForIO* message_loop_for_io_;
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Only used on the I/O thread:
3125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  Delegate* delegate_;
313a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool read_stopped_;
314a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<ReadBuffer> read_buffer_;
315a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
316a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::Lock write_lock_;  // Protects the following members.
317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool write_stopped_;
318a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<WriteBuffer> write_buffer_;
319a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
320a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This is used for posting tasks from write threads to the I/O thread. It
321a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // must only be accessed under |write_lock_|. The weak pointers it produces
322a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // are only used/invalidated on the I/O thread.
323a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::WeakPtrFactory<RawChannel> weak_ptr_factory_;
3244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(RawChannel);
3264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)};
3274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}  // namespace system
3294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}  // namespace mojo
3304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#endif  // MOJO_SYSTEM_RAW_CHANNEL_H_
332