Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / messaging / native_message_process_host.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 CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_
7
8 #include <queue>
9 #include <string>
10
11 #include "base/files/file.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/process/process.h"
15 #include "chrome/browser/extensions/api/messaging/native_process_launcher.h"
16 #include "extensions/browser/api/messaging/native_message_host.h"
17 #include "ui/gfx/native_widget_types.h"
18
19 namespace net {
20
21 class DrainableIOBuffer;
22 class FileStream;
23 class IOBuffer;
24 class IOBufferWithSize;
25
26 }  // namespace net
27
28 namespace extensions {
29
30 // Manages the native side of a connection between an extension and a native
31 // process.
32 //
33 // This class must only be created, called, and deleted on the IO thread.
34 // Public methods typically accept callbacks which will be invoked on the UI
35 // thread.
36 class NativeMessageProcessHost :
37 #if defined(OS_POSIX)
38     public base::MessageLoopForIO::Watcher,
39 #endif  // !defined(OS_POSIX)
40     public NativeMessageHost {
41  public:
42   ~NativeMessageProcessHost() override;
43
44   // Create using specified |launcher|. Used in tests.
45   static scoped_ptr<NativeMessageHost> CreateWithLauncher(
46       const std::string& source_extension_id,
47       const std::string& native_host_name,
48       scoped_ptr<NativeProcessLauncher> launcher);
49
50   // extensions::NativeMessageHost implementation.
51   void OnMessage(const std::string& message) override;
52   void Start(Client* client) override;
53   scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override;
54
55 #if defined(OS_POSIX)
56   // MessageLoopForIO::Watcher interface
57   void OnFileCanReadWithoutBlocking(int fd) override;
58   void OnFileCanWriteWithoutBlocking(int fd) override;
59 #endif  // !defined(OS_POSIX)
60
61   // Try and read a single message from |read_file_|. This should only be called
62   // in unittests when you know there is data in the file.
63   void ReadNowForTesting();
64
65  private:
66   NativeMessageProcessHost(const std::string& source_extension_id,
67                            const std::string& native_host_name,
68                            scoped_ptr<NativeProcessLauncher> launcher);
69
70   // Starts the host process.
71   void LaunchHostProcess();
72
73   // Callback for NativeProcessLauncher::Launch().
74   void OnHostProcessLaunched(NativeProcessLauncher::LaunchResult result,
75                              base::ProcessHandle process_handle,
76                              base::File read_file,
77                              base::File write_file);
78
79   // Helper methods to read incoming messages.
80   void WaitRead();
81   void DoRead();
82   void OnRead(int result);
83   void HandleReadResult(int result);
84   void ProcessIncomingData(const char* data, int data_size);
85
86   // Helper methods to write outgoing messages.
87   void DoWrite();
88   void HandleWriteResult(int result);
89   void OnWritten(int result);
90
91   // Closes the connection. Called from OnError() and destructor.
92   void Close(const std::string& error_message);
93
94   // The Client messages will be posted to. Should only be accessed from the
95   // UI thread.
96   Client* client_;
97
98   // ID of the calling extension.
99   std::string source_extension_id_;
100
101   // Name of the native messaging host.
102   std::string native_host_name_;
103
104   // Launcher used to launch the native process.
105   scoped_ptr<NativeProcessLauncher> launcher_;
106
107   // Set to true after the native messaging connection has been stopped, e.g.
108   // due to an error.
109   bool closed_;
110
111   base::ProcessHandle process_handle_;
112
113   // Input stream reader.
114   scoped_ptr<net::FileStream> read_stream_;
115
116 #if defined(OS_POSIX)
117   base::PlatformFile read_file_;
118   base::MessageLoopForIO::FileDescriptorWatcher read_watcher_;
119 #endif  // !defined(OS_POSIX)
120
121   // Write stream.
122   scoped_ptr<net::FileStream> write_stream_;
123
124   // Read buffer passed to FileStream::Read().
125   scoped_refptr<net::IOBuffer> read_buffer_;
126
127   // Set to true when a read is pending.
128   bool read_pending_;
129
130   // Buffer for incomplete incoming messages.
131   std::string incoming_data_;
132
133   // Queue for outgoing messages.
134   std::queue<scoped_refptr<net::IOBufferWithSize> > write_queue_;
135
136   // The message that's currently being sent.
137   scoped_refptr<net::DrainableIOBuffer> current_write_buffer_;
138
139   // Set to true when a write is pending.
140   bool write_pending_;
141
142   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
143
144   base::WeakPtrFactory<NativeMessageProcessHost> weak_factory_;
145
146   DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost);
147 };
148
149 }  // namespace extensions
150
151 #endif  // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_