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

Unified Diff: mojo/edk/system/raw_channel.h

Issue 1350023003: Add a Mojo EDK for Chrome that uses one OS pipe per message pipe. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more cleanup Created 5 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: mojo/edk/system/raw_channel.h
diff --git a/third_party/mojo/src/mojo/edk/system/raw_channel.h b/mojo/edk/system/raw_channel.h
similarity index 78%
copy from third_party/mojo/src/mojo/edk/system/raw_channel.h
copy to mojo/edk/system/raw_channel.h
index 35f7a3ebfb620e4d5e22916f92b20f0189173dbb..ccc9f137e48645cac628e411045594e58af03201 100644
--- a/third_party/mojo/src/mojo/edk/system/raw_channel.h
+++ b/mojo/edk/system/raw_channel.h
@@ -2,27 +2,27 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_RAW_CHANNEL_H_
-#define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_RAW_CHANNEL_H_
+#ifndef MOJO_EDK_SYSTEM_RAW_CHANNEL_H_
+#define MOJO_EDK_SYSTEM_RAW_CHANNEL_H_
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
+#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
+#include "mojo/edk/system/message_in_transit.h"
+#include "mojo/edk/system/message_in_transit_queue.h"
+#include "mojo/edk/system/system_impl_export.h"
#include "mojo/public/cpp/system/macros.h"
-#include "third_party/mojo/src/mojo/edk/embedder/platform_handle_vector.h"
-#include "third_party/mojo/src/mojo/edk/embedder/scoped_platform_handle.h"
-#include "third_party/mojo/src/mojo/edk/system/message_in_transit.h"
-#include "third_party/mojo/src/mojo/edk/system/message_in_transit_queue.h"
-#include "third_party/mojo/src/mojo/edk/system/system_impl_export.h"
namespace base {
class MessageLoopForIO;
}
namespace mojo {
-namespace system {
+namespace edk {
// |RawChannel| is an interface and base class for objects that wrap an OS
// "pipe". It presents the following interface to users:
@@ -41,9 +41,6 @@ namespace system {
// on which |Init()| is called).
class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
public:
- // This object may be destroyed on any thread (if |Init()| was called, after
- // |Shutdown()| was called).
- virtual ~RawChannel();
// The |Delegate| is only accessed on the same thread as the message loop
// (passed in on creation).
@@ -63,14 +60,14 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
ERROR_WRITE
};
- // Called when a message is read. This may call the |RawChannel|'s
- // |Shutdown()| and then (if desired) destroy it.
+ // Called when a message is read. The delegate may not call back into this
+ // object synchronously.
virtual void OnReadMessage(
const MessageInTransit::View& message_view,
- embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0;
+ ScopedPlatformHandleVectorPtr platform_handles) = 0;
- // Called when there's a (fatal) error. This may call the |RawChannel|'s
- // |Shutdown()| and then (if desired) destroy it.
+ // Called when there's a (fatal) error. The delegate may not call back into
+ // this object synchronously.
//
// For each raw channel, there'll be at most one |ERROR_READ_...| and at
// most one |ERROR_WRITE| notification. After |OnError(ERROR_READ_...)|,
@@ -84,7 +81,12 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// Static factory method. |handle| should be a handle to a
// (platform-appropriate) bidirectional communication channel (e.g., a socket
// on POSIX, a named pipe on Windows).
- static scoped_ptr<RawChannel> Create(embedder::ScopedPlatformHandle handle);
+ static RawChannel* Create(ScopedPlatformHandle handle);
+
+ // Returns the amount of space needed in the |MessageInTransit|'s
+ // |TransportData|'s "platform handle table" per platform handle (to be
+ // attached to a message). (This amount may be zero.)
+ static size_t GetSerializedPlatformHandleSize();
// This must be called (on an I/O thread) before this object is used. Does
// *not* take ownership of |delegate|. Both the I/O thread and |delegate| must
@@ -95,6 +97,12 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// This must be called (on the I/O thread) before this object is destroyed.
void Shutdown();
+ // Returns the platform handle for the pipe synchronously.
+ // |read_buffer| contains partially read data, if any.
+ // NOTE: After calling this, consider the channel shutdown and don't call into
+ // it anymore
+ ScopedPlatformHandle ReleaseHandle(std::vector<char>* read_buffer);
+
// Writes the given message (or schedules it to be written). |message| must
// have no |Dispatcher|s still attached (i.e.,
// |SerializeAndCloseDispatchers()| should have been called). This method is
@@ -107,10 +115,12 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// becomes empty (or something like that).
bool IsWriteBufferEmpty();
- // Returns the amount of space needed in the |MessageInTransit|'s
- // |TransportData|'s "platform handle table" per platform handle (to be
- // attached to a message). (This amount may be zero.)
- virtual size_t GetSerializedPlatformHandleSize() const = 0;
+ bool IsReadBufferEmpty();
+
+ void SetInitialReadBufferData(char* data, size_t size);
+
+ // Checks if this RawChannel is the other endpoint to |other|.
+ bool IsOtherEndOf(RawChannel* other);
protected:
// Result of I/O operations.
@@ -132,6 +142,14 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
void GetBuffer(char** addr, size_t* size);
+ void Reset() {num_valid_bytes_ = 0; }
+
+ // temp for debugging
+ // TODO(jam): pass in a cleaner way to ReleaseHandle, just like shutdown
+ // case.
+ char* buffer() { return &buffer_[0]; }
+ size_t num_valid_bytes() {return num_valid_bytes_;}
+
private:
friend class RawChannel;
@@ -152,7 +170,7 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
size_t size;
};
- explicit WriteBuffer(size_t serialized_platform_handle_size);
+ WriteBuffer();
~WriteBuffer();
// Returns true if there are (more) platform handles to be sent (from the
@@ -170,7 +188,7 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// |PlatformHandle::CloseIfNecessary()| isn't const (and actually modifies
// state).
void GetPlatformHandlesToSend(size_t* num_platform_handles,
- embedder::PlatformHandle** platform_handles,
+ PlatformHandle** platform_handles,
void** serialization_data);
// Gets buffers to be written. These buffers will always come from the front
@@ -179,10 +197,23 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// |OnWriteCompletedNoLock()|.
void GetBuffers(std::vector<Buffer>* buffers) const;
- private:
+
+
+
+
+ // temp for testing
+ size_t queue_size() {return message_queue_.Size();}
+
+ // TODO(jam): better way of giving buffer on release handle
+ MessageInTransitQueue* message_queue() { return &message_queue_; }
+
+
+
+ // TODO JAM REMOVE AND ADD METHODS
+// private:
friend class RawChannel;
- const size_t serialized_platform_handle_size_;
+ size_t serialized_platform_handle_size_;
MessageInTransitQueue message_queue_;
// Platform handles are sent before the message data, but doing so may
@@ -199,6 +230,11 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
RawChannel();
+ // Shutdown must be called on the IO thread. This object deletes itself once
+ // it's flushed all pending writes and insured that the other side of the pipe
+ // read them.
+ virtual ~RawChannel();
+
// |result| must not be |IO_PENDING|. Must be called on the I/O thread WITHOUT
// |write_lock_| held. This object may be destroyed by this call.
void OnReadCompleted(IOResult io_result, size_t bytes_read);
@@ -210,6 +246,7 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
base::MessageLoopForIO* message_loop_for_io() { return message_loop_for_io_; }
base::Lock& write_lock() { return write_lock_; }
+ base::Lock& read_lock() { return read_lock_; }
// Should only be called on the I/O thread.
ReadBuffer* read_buffer() { return read_buffer_.get(); }
@@ -234,6 +271,13 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
virtual bool OnReadMessageForRawChannel(
const MessageInTransit::View& message_view);
+ virtual PlatformHandle HandleForDebuggingNoLock() = 0;
+
+ // Implementation must write any pending messages synchronously.
+ // TODO(jam): change to return shared memory with pending serialized msgs.
+ virtual ScopedPlatformHandle ReleaseHandleNoLock(
+ std::vector<char>* read_buffer) = 0;
+
// Reads into |read_buffer()|.
// This class guarantees that:
// - the area indicated by |GetBuffer()| will stay valid until read completion
@@ -256,7 +300,7 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// called when |num_platform_handles| is nonzero. Returns null if the
// |num_platform_handles| handles are not available. Only called on the I/O
// thread (without |write_lock_| held).
- virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles(
+ virtual ScopedPlatformHandleVectorPtr GetReadPlatformHandles(
size_t num_platform_handles,
const void* platform_handle_table) = 0;
@@ -289,7 +333,11 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer,
scoped_ptr<WriteBuffer> write_buffer) = 0;
+ bool SendQueuedMessagesNoLock();
+
private:
+ friend class base::DeleteHelper<RawChannel>;
+
// Converts an |IO_FAILED_...| for a read to a |Delegate::Error|.
static Delegate::Error ReadIOResultToError(IOResult io_result);
@@ -297,6 +345,8 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// |write_lock_| held. This object may be destroyed by this call.
void CallOnError(Delegate::Error error);
+ void LockAndCallOnError(Delegate::Error error);
+
// If |io_result| is |IO_SUCCESS|, updates the write buffer and schedules a
// write operation to run later if there is more to write. If |io_result| is
// failure or any other error occurs, cancels pending writes and returns
@@ -306,19 +356,45 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
size_t platform_handles_written,
size_t bytes_written);
+ // Helper method to dispatch messages from the read buffer.
+ // |did_dispatch_message| is true iff it dispatched any messages.
+ // |stop_dispatching| is set to true if the code calling this should stop
+ // dispatching, either because we hit an erorr or the delegate shutdown the
+ // channel.
+ void DispatchMessages(bool* did_dispatch_message, bool* stop_dispatching);
+
// Set in |Init()| and never changed (hence usable on any thread without
// locking):
base::MessageLoopForIO* message_loop_for_io_;
+
+
+
+
+ // TODO(jam): one lock only... but profile first to ensure it doesn't slow
+ // things down compared to fine grained locks.
+
+
+
+
// Only used on the I/O thread:
- Delegate* delegate_;
- bool* set_on_shutdown_;
+
+ base::Lock read_lock_; // Protects read_buffer_.
+ // This is usually only accessed on IO thread, except when ReleaseHandle is
+ // called.
scoped_ptr<ReadBuffer> read_buffer_;
+ // ditto: usually used on io thread except ReleaseHandle
+ Delegate* delegate_;
+
+ // If grabbing both locks, grab read first.
base::Lock write_lock_; // Protects the following members.
+ bool write_ready_;
bool write_stopped_;
scoped_ptr<WriteBuffer> write_buffer_;
+ bool error_occurred_;
+
// This is used for posting tasks from write threads to the I/O thread. It
// must only be accessed under |write_lock_|. The weak pointers it produces
// are only used/invalidated on the I/O thread.
@@ -327,7 +403,7 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
MOJO_DISALLOW_COPY_AND_ASSIGN(RawChannel);
};
-} // namespace system
+} // namespace edk
} // namespace mojo
-#endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_RAW_CHANNEL_H_
+#endif // MOJO_EDK_SYSTEM_RAW_CHANNEL_H_

Powered by Google App Engine
This is Rietveld 408576698