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_
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"
21 class DrainableIOBuffer
;
24 class IOBufferWithSize
;
28 namespace extensions
{
30 // Manages the native side of a connection between an extension and a native
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
36 class NativeMessageProcessHost
:
38 public base::MessageLoopForIO::Watcher
,
39 #endif // !defined(OS_POSIX)
40 public NativeMessageHost
{
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
;
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();
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
,
77 base::File write_file
);
79 // Helper methods to read incoming messages.
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.
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
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.
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)
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.
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.
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_