| 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 #include "mojo/system/raw_channel.h" |    5 #include "mojo/system/raw_channel.h" | 
|    6  |    6  | 
|    7 #include <errno.h> |    7 #include <errno.h> | 
|    8 #include <string.h> |    8 #include <string.h> | 
|    9 #include <unistd.h> |    9 #include <unistd.h> | 
|   10  |   10  | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
|   32  |   32  | 
|   33 class RawChannelPosix : public RawChannel, |   33 class RawChannelPosix : public RawChannel, | 
|   34                         public base::MessageLoopForIO::Watcher { |   34                         public base::MessageLoopForIO::Watcher { | 
|   35  public: |   35  public: | 
|   36   RawChannelPosix(const PlatformChannelHandle& handle, |   36   RawChannelPosix(const PlatformChannelHandle& handle, | 
|   37                   Delegate* delegate, |   37                   Delegate* delegate, | 
|   38                   base::MessageLoop* message_loop); |   38                   base::MessageLoop* message_loop); | 
|   39   virtual ~RawChannelPosix(); |   39   virtual ~RawChannelPosix(); | 
|   40  |   40  | 
|   41   // |RawChannel| implementation: |   41   // |RawChannel| implementation: | 
|   42   virtual void Init() OVERRIDE; |   42   virtual bool Init() OVERRIDE; | 
|   43   virtual void Shutdown() OVERRIDE; |   43   virtual void Shutdown() OVERRIDE; | 
|   44   virtual bool WriteMessage(MessageInTransit* message) OVERRIDE; |   44   virtual bool WriteMessage(MessageInTransit* message) OVERRIDE; | 
|   45  |   45  | 
|   46  private: |   46  private: | 
|   47   // |base::MessageLoopForIO::Watcher| implementation: |   47   // |base::MessageLoopForIO::Watcher| implementation: | 
|   48   virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |   48   virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 
|   49   virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |   49   virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 
|   50  |   50  | 
|   51   // Watches for |fd_| to become writable. Must be called on the I/O thread. |   51   // Watches for |fd_| to become writable. Must be called on the I/O thread. | 
|   52   void WaitToWrite(); |   52   void WaitToWrite(); | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  115   // No need to take the |write_lock_| here -- if there are still weak pointers |  115   // No need to take the |write_lock_| here -- if there are still weak pointers | 
|  116   // outstanding, then we're hosed anyway (since we wouldn't be able to |  116   // outstanding, then we're hosed anyway (since we wouldn't be able to | 
|  117   // invalidate them cleanly, since we might not be on the I/O thread). |  117   // invalidate them cleanly, since we might not be on the I/O thread). | 
|  118   DCHECK(!weak_ptr_factory_.HasWeakPtrs()); |  118   DCHECK(!weak_ptr_factory_.HasWeakPtrs()); | 
|  119  |  119  | 
|  120   // These must have been shut down/destroyed on the I/O thread. |  120   // These must have been shut down/destroyed on the I/O thread. | 
|  121   DCHECK(!read_watcher_.get()); |  121   DCHECK(!read_watcher_.get()); | 
|  122   DCHECK(!write_watcher_.get()); |  122   DCHECK(!write_watcher_.get()); | 
|  123 } |  123 } | 
|  124  |  124  | 
|  125 void RawChannelPosix::Init() { |  125 bool RawChannelPosix::Init() { | 
|  126   DCHECK_EQ(base::MessageLoop::current(), message_loop()); |  126   DCHECK_EQ(base::MessageLoop::current(), message_loop()); | 
|  127  |  127  | 
|  128   DCHECK(!read_watcher_.get()); |  128   DCHECK(!read_watcher_.get()); | 
|  129   read_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher()); |  129   read_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher()); | 
|  130   DCHECK(!write_watcher_.get()); |  130   DCHECK(!write_watcher_.get()); | 
|  131   write_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher()); |  131   write_watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher()); | 
|  132  |  132  | 
|  133   // No need to take the lock. No one should be using us yet. |  133   // No need to take the lock. No one should be using us yet. | 
|  134   DCHECK(write_message_queue_.empty()); |  134   DCHECK(write_message_queue_.empty()); | 
|  135  |  135  | 
|  136   bool result = message_loop_for_io()->WatchFileDescriptor( |  136   if (!message_loop_for_io()->WatchFileDescriptor(fd_, true, | 
|  137       fd_, true, base::MessageLoopForIO::WATCH_READ, read_watcher_.get(), this); |  137           base::MessageLoopForIO::WATCH_READ, read_watcher_.get(), this)) { | 
|  138   DCHECK(result); |  138     // TODO(vtl): I'm not sure |WatchFileDescriptor()| actually fails cleanly | 
 |  139     // (in the sense of returning the message loop's state to what it was before | 
 |  140     // it was called). | 
 |  141     read_watcher_.reset(); | 
 |  142     write_watcher_.reset(); | 
 |  143     return false; | 
 |  144   } | 
 |  145  | 
 |  146   return true; | 
|  139 } |  147 } | 
|  140  |  148  | 
|  141 void RawChannelPosix::Shutdown() { |  149 void RawChannelPosix::Shutdown() { | 
|  142   DCHECK_EQ(base::MessageLoop::current(), message_loop()); |  150   DCHECK_EQ(base::MessageLoop::current(), message_loop()); | 
|  143  |  151  | 
|  144   base::AutoLock locker(write_lock_); |  152   base::AutoLock locker(write_lock_); | 
|  145   if (!is_dead_) |  153   if (!is_dead_) | 
|  146     CancelPendingWritesNoLock(); |  154     CancelPendingWritesNoLock(); | 
|  147  |  155  | 
|  148   DCHECK_NE(fd_, -1); |  156   DCHECK_NE(fd_, -1); | 
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  389 // Static factory method declared in raw_channel.h. |  397 // Static factory method declared in raw_channel.h. | 
|  390 // static |  398 // static | 
|  391 RawChannel* RawChannel::Create(const PlatformChannelHandle& handle, |  399 RawChannel* RawChannel::Create(const PlatformChannelHandle& handle, | 
|  392                                Delegate* delegate, |  400                                Delegate* delegate, | 
|  393                                base::MessageLoop* message_loop) { |  401                                base::MessageLoop* message_loop) { | 
|  394   return new RawChannelPosix(handle, delegate, message_loop); |  402   return new RawChannelPosix(handle, delegate, message_loop); | 
|  395 } |  403 } | 
|  396  |  404  | 
|  397 }  // namespace system |  405 }  // namespace system | 
|  398 }  // namespace mojo |  406 }  // namespace mojo | 
| OLD | NEW |