Home | History | Annotate | Download | only in ipc
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef IPC_IPC_CHANNEL_WIN_H_
      6 #define IPC_IPC_CHANNEL_WIN_H_
      7 
      8 #include "ipc/ipc_channel.h"
      9 
     10 #include <queue>
     11 #include <string>
     12 
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/message_loop/message_loop.h"
     16 #include "ipc/ipc_channel_reader.h"
     17 
     18 namespace base {
     19 class ThreadChecker;
     20 }
     21 
     22 namespace IPC {
     23 
     24 class ChannelWin : public Channel,
     25                    public internal::ChannelReader,
     26                    public base::MessageLoopForIO::IOHandler {
     27  public:
     28   // Mirror methods of Channel, see ipc_channel.h for description.
     29   ChannelWin(const IPC::ChannelHandle &channel_handle, Mode mode,
     30              Listener* listener);
     31   ~ChannelWin();
     32 
     33   // Channel implementation
     34   virtual bool Connect() OVERRIDE;
     35   virtual void Close() OVERRIDE;
     36   virtual bool Send(Message* message) OVERRIDE;
     37   virtual base::ProcessId GetPeerPID() const OVERRIDE;
     38 
     39   static bool IsNamedServerInitialized(const std::string& channel_id);
     40 
     41 
     42  private:
     43   // ChannelReader implementation.
     44   virtual ReadState ReadData(char* buffer,
     45                              int buffer_len,
     46                              int* bytes_read) OVERRIDE;
     47   virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE;
     48   bool DidEmptyInputBuffers() OVERRIDE;
     49   virtual void HandleInternalMessage(const Message& msg) OVERRIDE;
     50 
     51   static const base::string16 PipeName(const std::string& channel_id,
     52                                        int32* secret);
     53   bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode);
     54 
     55   bool ProcessConnection();
     56   bool ProcessOutgoingMessages(base::MessageLoopForIO::IOContext* context,
     57                                DWORD bytes_written);
     58 
     59   // MessageLoop::IOHandler implementation.
     60   virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
     61                              DWORD bytes_transfered,
     62                              DWORD error);
     63 
     64  private:
     65   struct State {
     66     explicit State(ChannelWin* channel);
     67     ~State();
     68     base::MessageLoopForIO::IOContext context;
     69     bool is_pending;
     70   };
     71 
     72   State input_state_;
     73   State output_state_;
     74 
     75   HANDLE pipe_;
     76 
     77   base::ProcessId peer_pid_;
     78 
     79   // Messages to be sent are queued here.
     80   std::queue<Message*> output_queue_;
     81 
     82   // In server-mode, we have to wait for the client to connect before we
     83   // can begin reading.  We make use of the input_state_ when performing
     84   // the connect operation in overlapped mode.
     85   bool waiting_connect_;
     86 
     87   // This flag is set when processing incoming messages.  It is used to
     88   // avoid recursing through ProcessIncomingMessages, which could cause
     89   // problems.  TODO(darin): make this unnecessary
     90   bool processing_incoming_;
     91 
     92   // Determines if we should validate a client's secret on connection.
     93   bool validate_client_;
     94 
     95   // This is a unique per-channel value used to authenticate the client end of
     96   // a connection. If the value is non-zero, the client passes it in the hello
     97   // and the host validates. (We don't send the zero value fto preserve IPC
     98   // compatability with existing clients that don't validate the channel.)
     99   int32 client_secret_;
    100 
    101 
    102   base::WeakPtrFactory<ChannelWin> weak_factory_;
    103 
    104   scoped_ptr<base::ThreadChecker> thread_check_;
    105 
    106   DISALLOW_COPY_AND_ASSIGN(ChannelWin);
    107 };
    108 
    109 }  // namespace IPC
    110 
    111 #endif  // IPC_IPC_CHANNEL_WIN_H_
    112