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

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: convert remaining MP tests and simplify RawChannel destruction Created 5 years, 3 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 83%
copy from third_party/mojo/src/mojo/edk/system/raw_channel.h
copy to mojo/edk/system/raw_channel.h
index cbcc1b3fc63d543ad03560161d57f611a3e83490..367f95f2e790c7ff5681dfffb49dbad2af9c81c9 100644
--- a/third_party/mojo/src/mojo/edk/system/raw_channel.h
+++ b/mojo/edk/system/raw_channel.h
@@ -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).
@@ -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(embedder::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,20 @@ 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.
+
+
+ // Flushes all pending writes synchronously.
+
+
+ // TODO(jam): change above to write it to shared memory and return handle as
+ // writing synchronously won't work for lots of pending data.
+ // Returns any partially read data.
+ // NOTE: After calling this, consider the channel shutdown and don't call into
+ // it anymore
+ embedder::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 +123,9 @@ 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);
protected:
// Result of I/O operations.
@@ -132,6 +147,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 +175,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
@@ -179,10 +202,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 +235,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 +251,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(); }
@@ -233,6 +275,13 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
// This is only called on the I/O thread.
virtual bool OnReadMessageForRawChannel(
const MessageInTransit::View& message_view);
+
+ virtual embedder::PlatformHandle HandleForDebuggingNoLock() = 0;
+
+ // Implementation must write any pending messages synchronously.
+ // TODO(jam): change to return shared memory with pending serialized msgs.
+ virtual embedder::ScopedPlatformHandle ReleaseHandleNoLock(
+ std::vector<char>* read_buffer) = 0;
// Reads into |read_buffer()|.
// This class guarantees that:
@@ -289,7 +338,11 @@ class MOJO_SYSTEM_IMPL_EXPORT RawChannel {
virtual void OnShutdownNoLock(scoped_ptr<ReadBuffer> read_buffer,
scoped_ptr<WriteBuffer> write_buffer) = 0;
+ void 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 +350,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 +361,46 @@ 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....
+
+
+
+
// 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.

Powered by Google App Engine
This is Rietveld 408576698