OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 // | 33 // |
34 // This class must only be created, called, and deleted on the IO thread. | 34 // This class must only be created, called, and deleted on the IO thread. |
35 // Public methods typically accept callbacks which will be invoked on the UI | 35 // Public methods typically accept callbacks which will be invoked on the UI |
36 // thread. | 36 // thread. |
37 class NativeMessageProcessHost | 37 class NativeMessageProcessHost |
38 #if defined(OS_POSIX) | 38 #if defined(OS_POSIX) |
39 : public MessageLoopForIO::Watcher | 39 : public MessageLoopForIO::Watcher |
40 #endif // !defined(OS_POSIX) | 40 #endif // !defined(OS_POSIX) |
41 { | 41 { |
42 public: | 42 public: |
43 // Interface for classes that which to recieve messages from the native | 43 // Interface for the object that receives messages from the native process. |
44 // process. | |
45 class Client { | 44 class Client { |
46 public: | 45 public: |
47 virtual ~Client() {} | 46 virtual ~Client() {} |
48 // Called on the UI thread. | 47 // Called on the UI thread. |
49 virtual void PostMessageFromNativeProcess(int port_id, | 48 virtual void PostMessageFromNativeProcess(int port_id, |
50 const std::string& message) = 0; | 49 const std::string& message) = 0; |
51 virtual void CloseChannel(int port_id, bool error) = 0; | 50 virtual void CloseChannel(int port_id, bool error) = 0; |
52 }; | 51 }; |
53 | 52 |
54 virtual ~NativeMessageProcessHost(); | 53 virtual ~NativeMessageProcessHost(); |
55 | 54 |
56 static scoped_ptr<NativeMessageProcessHost> Create( | 55 static scoped_ptr<NativeMessageProcessHost> Create( |
57 base::WeakPtr<Client> weak_client_ui, | 56 base::WeakPtr<Client> weak_client_ui, |
| 57 const std::string& source_extension_id, |
58 const std::string& native_host_name, | 58 const std::string& native_host_name, |
59 int destination_port); | 59 int destination_port); |
60 | 60 |
61 // Create using specified |launcher|. Used in tests. | 61 // Create using specified |launcher|. Used in tests. |
62 static scoped_ptr<NativeMessageProcessHost> CreateWithLauncher( | 62 static scoped_ptr<NativeMessageProcessHost> CreateWithLauncher( |
63 base::WeakPtr<Client> weak_client_ui, | 63 base::WeakPtr<Client> weak_client_ui, |
| 64 const std::string& source_extension_id, |
64 const std::string& native_host_name, | 65 const std::string& native_host_name, |
65 int destination_port, | 66 int destination_port, |
66 scoped_ptr<NativeProcessLauncher> launcher); | 67 scoped_ptr<NativeProcessLauncher> launcher); |
67 | 68 |
68 // Send a message with the specified payload. | 69 // Send a message with the specified payload. |
69 void Send(const std::string& json); | 70 void Send(const std::string& json); |
70 | 71 |
71 #if defined(OS_POSIX) | 72 #if defined(OS_POSIX) |
72 // MessageLoopForIO::Watcher interface | 73 // MessageLoopForIO::Watcher interface |
73 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 74 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
74 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 75 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
75 #endif // !defined(OS_POSIX) | 76 #endif // !defined(OS_POSIX) |
76 | 77 |
77 // Try and read a single message from |read_file_|. This should only be called | 78 // Try and read a single message from |read_file_|. This should only be called |
78 // in unittests when you know there is data in the file. | 79 // in unittests when you know there is data in the file. |
79 void ReadNowForTesting(); | 80 void ReadNowForTesting(); |
80 | 81 |
81 private: | 82 private: |
82 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, | 83 NativeMessageProcessHost(base::WeakPtr<Client> weak_client_ui, |
| 84 const std::string& source_extension_id, |
83 const std::string& native_host_name, | 85 const std::string& native_host_name, |
84 int destination_port, | 86 int destination_port, |
85 scoped_ptr<NativeProcessLauncher> launcher); | 87 scoped_ptr<NativeProcessLauncher> launcher); |
86 | 88 |
87 // Starts the host process. | 89 // Starts the host process. |
88 void LaunchHostProcess(); | 90 void LaunchHostProcess(); |
89 | 91 |
90 // Callback for NativeProcessLauncher::Launch(). | 92 // Callback for NativeProcessLauncher::Launch(). |
91 void OnHostProcessLaunched(bool result, | 93 void OnHostProcessLaunched(bool result, |
92 base::PlatformFile read_file, | 94 base::PlatformFile read_file, |
(...skipping 15 matching lines...) Expand all Loading... |
108 // write to/from it. Closes IO pipes and schedules CloseChannel() call. | 110 // write to/from it. Closes IO pipes and schedules CloseChannel() call. |
109 void OnError(); | 111 void OnError(); |
110 | 112 |
111 // Closes the connection. Called from OnError() and destructor. | 113 // Closes the connection. Called from OnError() and destructor. |
112 void Close(); | 114 void Close(); |
113 | 115 |
114 // The Client messages will be posted to. Should only be accessed from the | 116 // The Client messages will be posted to. Should only be accessed from the |
115 // UI thread. | 117 // UI thread. |
116 base::WeakPtr<Client> weak_client_ui_; | 118 base::WeakPtr<Client> weak_client_ui_; |
117 | 119 |
| 120 // ID of the calling extension. |
| 121 std::string source_extension_id_; |
| 122 |
118 // Name of the native messaging host. | 123 // Name of the native messaging host. |
119 std::string native_host_name_; | 124 std::string native_host_name_; |
120 | 125 |
121 // The id of the port on the other side of this connection. This is passed to | 126 // The id of the port on the other side of this connection. This is passed to |
122 // |weak_client_ui_| when posting messages. | 127 // |weak_client_ui_| when posting messages. |
123 int destination_port_; | 128 int destination_port_; |
124 | 129 |
125 // Launcher used to launch the native process. | 130 // Launcher used to launch the native process. |
126 scoped_ptr<NativeProcessLauncher> launcher_; | 131 scoped_ptr<NativeProcessLauncher> launcher_; |
127 | 132 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 165 |
161 // Set to true when a write is pending. | 166 // Set to true when a write is pending. |
162 bool write_pending_; | 167 bool write_pending_; |
163 | 168 |
164 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); | 169 DISALLOW_COPY_AND_ASSIGN(NativeMessageProcessHost); |
165 }; | 170 }; |
166 | 171 |
167 } // namespace extensions | 172 } // namespace extensions |
168 | 173 |
169 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
_ | 174 #endif // CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_MESSAGE_PROCESS_HOST_H
_ |
OLD | NEW |