Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / messaging / native_message_process_host.h
blobd3dbf17db187ce6c041e85941e96887b86739394
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_
8 #include <queue>
9 #include <string>
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"
19 namespace net {
21 class DrainableIOBuffer;
22 class FileStream;
23 class IOBuffer;
24 class IOBufferWithSize;
26 } // namespace net
28 namespace extensions {
30 // Manages the native side of a connection between an extension and a native
31 // process.
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;
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);
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;
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)
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();
65 private:
66 NativeMessageProcessHost(const std::string& source_extension_id,
67 const std::string& native_host_name,
68 scoped_ptr<NativeProcessLauncher> launcher);
70 // Starts the host process.
71 void LaunchHostProcess();
73 // Callback for NativeProcessLauncher::Launch().
74 void OnHostProcessLaunched(NativeProcessLauncher::LaunchResult result,
75 base::Process process,
76 base::File read_file,
77 base::File write_file);
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);
86 // Helper methods to write outgoing messages.
87 void DoWrite();
88 void HandleWriteResult(int result);
89 void OnWritten(int result);
91 // Closes the connection and reports the |error_message| to the client.
92 void Close(const std::string& error_message);
94 // The Client messages will be posted to. Should only be accessed from the
95 // UI thread.
96 Client* client_;
98 // ID of the calling extension.
99 std::string source_extension_id_;
101 // Name of the native messaging host.
102 std::string native_host_name_;
104 // Launcher used to launch the native process.
105 scoped_ptr<NativeProcessLauncher> launcher_;
107 // Set to true after the native messaging connection has been stopped, e.g.
108 // due to an error.
109 bool closed_;
111 base::Process process_;
113 // Input stream reader.
114 scoped_ptr<net::FileStream> read_stream_;
116 #if defined(OS_POSIX)
117 base::PlatformFile read_file_;
118 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_;
119 #endif // !defined(OS_POSIX)
121 // Write stream.
122 scoped_ptr<net::FileStream> write_stream_;
124 // Read buffer passed to FileStream::Read().
125 scoped_refptr<net::IOBuffer> read_buffer_;
127 // Set to true when a read is pending.
128 bool read_pending_;
130 // Buffer for incomplete incoming messages.
131 std::string incoming_data_;
133 // Queue for outgoing messages.
134 std::queue<scoped_refptr<net::IOBufferWithSize> > write_queue_;
136 // The message that's currently being sent.
137 scoped_refptr<net::DrainableIOBuffer> current_write_buffer_;
139 // Set to true when a write is pending.
140 bool write_pending_;
142 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
144 base::WeakPtrFactory<NativeMessageProcessHost> weak_factory_;
146 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost);
149 } // namespace extensions
151 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_