Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / ipc / ipc_channel_win.h
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 "base/win/scoped_handle.h"
17 #include "ipc/ipc_channel_reader.h"
18
19 namespace base {
20 class ThreadChecker;
21 }
22
23 namespace IPC {
24
25 class ChannelWin : public Channel,
26                    public internal::ChannelReader,
27                    public base::MessageLoopForIO::IOHandler {
28  public:
29   // Mirror methods of Channel, see ipc_channel.h for description.
30   ChannelWin(const IPC::ChannelHandle &channel_handle, Mode mode,
31              Listener* listener);
32   ~ChannelWin();
33
34   // Channel implementation
35   virtual bool Connect() OVERRIDE;
36   virtual void Close() OVERRIDE;
37   virtual bool Send(Message* message) OVERRIDE;
38   virtual base::ProcessId GetPeerPID() const OVERRIDE;
39   virtual base::ProcessId GetSelfPID() const OVERRIDE;
40
41   static bool IsNamedServerInitialized(const std::string& channel_id);
42
43
44  private:
45   // ChannelReader implementation.
46   virtual ReadState ReadData(char* buffer,
47                              int buffer_len,
48                              int* bytes_read) OVERRIDE;
49   virtual bool WillDispatchInputMessage(Message* msg) OVERRIDE;
50   bool DidEmptyInputBuffers() OVERRIDE;
51   virtual void HandleInternalMessage(const Message& msg) OVERRIDE;
52
53   static const base::string16 PipeName(const std::string& channel_id,
54                                        int32* secret);
55   bool CreatePipe(const IPC::ChannelHandle &channel_handle, Mode mode);
56
57   bool ProcessConnection();
58   bool ProcessOutgoingMessages(base::MessageLoopForIO::IOContext* context,
59                                DWORD bytes_written);
60
61   // MessageLoop::IOHandler implementation.
62   virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
63                              DWORD bytes_transfered,
64                              DWORD error);
65
66  private:
67   struct State {
68     explicit State(ChannelWin* channel);
69     ~State();
70     base::MessageLoopForIO::IOContext context;
71     bool is_pending;
72   };
73
74   State input_state_;
75   State output_state_;
76
77   base::win::ScopedHandle pipe_;
78
79   base::ProcessId peer_pid_;
80
81   // Messages to be sent are queued here.
82   std::queue<Message*> output_queue_;
83
84   // In server-mode, we have to wait for the client to connect before we
85   // can begin reading.  We make use of the input_state_ when performing
86   // the connect operation in overlapped mode.
87   bool waiting_connect_;
88
89   // This flag is set when processing incoming messages.  It is used to
90   // avoid recursing through ProcessIncomingMessages, which could cause
91   // problems.  TODO(darin): make this unnecessary
92   bool processing_incoming_;
93
94   // Determines if we should validate a client's secret on connection.
95   bool validate_client_;
96
97   // True if there is a write in progress. TODO(rvargas): remove this.
98   bool writing_;
99
100   // Tracks the lifetime of this object, for debugging purposes.
101   uint32 debug_flags_;
102
103   // OS result for the current write. TODO(rvargas): remove this.
104   uint32 write_error_;
105
106   // OS result for a previous failed write. TODO(rvargas): remove this.
107   uint32 last_write_error_;
108
109   // Size of the current write. TODO(rvargas): remove this.
110   uint32 write_size_;
111
112   // This is a unique per-channel value used to authenticate the client end of
113   // a connection. If the value is non-zero, the client passes it in the hello
114   // and the host validates. (We don't send the zero value fto preserve IPC
115   // compatability with existing clients that don't validate the channel.)
116   int32 client_secret_;
117
118   scoped_ptr<base::ThreadChecker> thread_check_;
119   base::WeakPtrFactory<ChannelWin> weak_factory_;
120
121   DISALLOW_COPY_AND_ASSIGN(ChannelWin);
122 };
123
124 }  // namespace IPC
125
126 #endif  // IPC_IPC_CHANNEL_WIN_H_