Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(522)

Side by Side Diff: chrome/common/extensions/docs/templates/articles/nativeMessaging.html

Issue 768373004: Improve native messaging documentation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move native messaging docs to new page Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <h1>Native messaging</h1>
2 <p>
3 Extensions and apps can exchange messages with native applications using an API
4 that is similar to the other <a href="messaging">message passing APIs</a>.
5 Native applications that support this feature must register a
6 <em>native messaging host</em> that knows how to communicate with the extension.
7 Chrome starts the host in a separate process and communicates with it using
8 standard input and standard output streams.
9
10 <h2 id="native-messaging-host">Native messaging host</h2>
11 <p>
12 In order to register a native messaging host the application must install a
13 manifest file that defines the native messaging host configuration. Below is an
14 example of the manifest file:
15 <pre data-filename="manifest.json">
16 {
17 "name": "com.my_company.my_application",
18 "description": "My Application",
19 "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
20 "type": "stdio",
21 "allowed_origins": [
22 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
23 ]
24 }
25 </pre>
26
27 <p>The native messaging host manifest file must be valid JSON and contains the
28 following fields:
29 <table class="simple" id="native-messaging-host-manifest">
30 <tr>
31 <th>Name</th>
32 <th>Description</th>
33 </tr>
34 <tr>
35 <td><code>name</code></td>
36 <td>Name of the native messaging host. Clients pass this string to
37 $(ref:runtime.connectNative) or $(ref:runtime.sendNativeMessage).
38 This name can only contain lowercase alphanumeric characters, underscores
39 and dots. The name cannot start or end with a dot, and a dot cannot be
40 followed by another dot.
41 </td>
42 </tr>
43 <tr>
44 <td><code>description</code></td>
45 <td>Short application description.</td>
46 </tr>
47 <tr>
48 <td><code>path</code></td>
49 <td>Path to the native messaging host binary. On Linux and OSX the path must
50 be absolute. On Windows it can be relative to the directory in which the
51 manifest file is located.</td>
52 </tr>
53 <tr>
54 <td><code>type</code></td>
55 <td>Type of the interface used to communicate with the native messaging
56 host. Currently there is only one possible value for this parameter:
57 <code>stdio</code>. It indicates that Chrome should use <code>stdin</code>
58 and <code>stdout</code> to communicate with the host.</td>
59 </tr>
60 <tr>
61 <td><code>allowed_origins</code></td>
62 <td>List of extensions that should have access to the native messaging host.
63 Wildcards such as <code>chrome-extension://*/*</code> are <em>not</em>
64 allowed.</td>
65 </tr>
66 </table>
67
68 <h3 id="native-messaging-host-location">Native messaging host location</h3>
69 <p>The location of the manifest file depends on the platform.
70
71 <p id="native-messaging-host-location-windows">
72 On <b>Windows</b>, the manifest file can be located anywhere in the file system.
73 The application installer must create registry key
74 <code>HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com.my_ company.my_application</em></code>
75 or
76 <code>HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com.my_c ompany.my_application</em></code>,
77 and set default value of that key to the full path to the manifest file.
78 For example, using the following command:<br>
79 <pre>
80 REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\<em>com.my_company.my_ application</em>" /ve /t REG_SZ /d "C:\path\to\nmh-manifest.json" /f
81 </pre>
82 or using the following <code>.reg</code> file:
83 <pre>
84 Windows Registry Editor Version 5.00
85 [HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\<em>com.my_compan y.my_application</em>]
86 @="C:\\path\\to\\nmh-manifest.json"
87 </pre>
88 When Chrome looks for native messaging hosts, first the 32-bit registry is
89 queried, then the 64-bit registry.
90
91 <p id="native-messaging-host-location-nix">
92 On <b>OS X</b> and <b>Linux</b>, the location of the native messaging host's
93 manifest file varies by the browser (Google Chrome or Chromium).
94 The system-wide native messaging hosts are looked up at a fixed location,
95 while the user-level native messaging hosts are looked up in a subdirectory with in the
96 <a href="https://www.chromium.org/user-experience/user-data-directory">user prof ile directory</a>
97 called <code>NativeMessagingHosts</code>.
98
99 <dl>
100 <dt>OS X (system-wide)
101 <dd>Google Chrome: <code>/Library/Google/Chrome/NativeMessagingHosts/<em>com.my_ company.my_application</em>.json</code>
102 <dd>Chromium: <code>/Library/Application Support/Chromium/NativeMessagingHosts/< em>com.my_company.my_application</em>.json</code>
103 <dt>OS X (user-specific, <em>default</em> path)
104 <dd>Google Chrome: <code>~/Library/Application Support/Google/Chrome/Default/Nat iveMessagingHosts/<em>com.my_company.my_application</em>.json</code>
105 <dd>Chromium: <code>~/Library/Application Support/Chromium/Default/NativeMessagi ngHosts/<em>com.my_company.my_application</em>.json</code>
106 </dl>
107
108 <dl>
109 <dt>Linux (system-wide)
110 <dd>Google Chrome: <code>/etc/opt/chrome/native-messaging-hosts/<em>com.my_compa ny.my_application</em>.json</code>
111 <dd>Chromium: <code>/etc/chromium/native-messaging-hosts/<em>com.my_company.my_a pplication</em>.json</code>
112 <dt>Linux (user-specific, <em>default</em> path)
113 <dd>Google Chrome: <code>~/.config/google-chrome/NativeMessagingHosts/<em>com.my _company.my_application</em>.json</code>
114 <dd>Chromium: <code>~/.config/chromium/NativeMessagingHosts/<em>com.my_company.m y_application</em>.json</code>
115 </dl>
116
117 <h3 id="native-messaging-host-protocol">Native messaging protocol</h3>
118 <p>
119 Chrome starts each native messaging host in a separate process and communicates
120 with it using standard input (<code>stdin</code>) and standard output
121 (<code>stdout</code>). The same format is used to send messages in both
122 directions: each message is serialized using JSON, UTF-8 encoded
123 and is preceded with 32-bit message length in native byte order.
124 The maximum size of a single message from the native messaging host is 1 MB,
125 mainly to protect Chrome from misbehaving native applications. The maximum
126 size of the message sent to the native messaging host is 4 GB.
127
128 <p>
129 When a messaging port is created using $(ref:runtime.connectNative) Chrome
130 starts native messaging host process and keeps it running until the port is
131 destroyed. On the other hand, when a message is sent using
132 $(ref:runtime.sendNativeMessage), without creating a messaging port, Chrome star ts
133 a new native messaging host process for each message. In that case the first
134 message generated by the host process is handled as a response to the original
135 request, i.e. Chrome will pass it to the response callback specified when
136 $(ref:runtime.sendNativeMessage) is called. All other messages generated by the
137 native messaging host in that case are ignored.
138
139 <h2 id="native-messaging-client">Connecting to a native application</h2>
140 <p>
141 Sending and receiving messages to and from a native application is very similar
142 to cross-extension messaging. The main difference is that
143 $(ref:runtime.connectNative) is used instead of $(ref:runtime.connect),
144 and $(ref:runtime.sendNativeMessage) is used instead of $(ref:runtime.sendMessag e).
145
146 <p>
147 The Following example creates a $(ref:runtime.Port) object that's connected to n ative
148 messaging host <code>com.my_company.my_application</code>, starts listening for
149 messages from that port and sends one outgoing message:
150 <pre>
151 var port = chrome.runtime.connectNative('com.my_company.my_application');
152 port.onMessage.addListener(function(msg) {
153 console.log("Received" + msg);
154 });
155 port.onDisconnect.addListener(function() {
156 console.log("Disconnected");
157 });
158 port.postMessage({ text: "Hello, my_application" });
159 </pre>
160
161 <p>
162 $(ref:runtime.sendNativeMessage) can be used to send a message to native
163 application without creating a port, e.g.:
164 <pre>
165 chrome.runtime.sendNativeMessage('com.my_company.my_application',
166 { text: "Hello" },
167 function(response) {
168 console.log("Received " + response);
169 });
170 </pre>
171
172 <h2 id="native-messaging-debugging">Debugging native messaging</h2>
173 <p>
174 When the native messaging host fails to start, writes to <code>stderr</code> or
175 when it violates the communication protocol, output is written to the error log
176 of Chrome.
177 On Linux and OS X, this log can easily be accessed by starting Chrome from the
178 command line and watching its output in the terminal.
179 On Windows, use <code>--enable-logging</code> as explained at
180 <a href="https://www.chromium.org/for-testers/enable-logging">How to enable logg ing</a>.
181
182 <p>
183 Here are some errors and tips for solving the issues:
184 <dl>
185 <dt>Failed to start native messaging host.
186 <dd>Check whether you have sufficient permissions to execute the file.
187
188 <dt>Invalid native messaging host name specified.
189 <dd>Check whether the name contains any invalid characters.
190 Only lowercase alphanumeric characters, underscores and dots are allowed.
191 A name cannot start or end with a dot, and a dot cannot be followed by
192 another dot.
193
194 <dt>Native host has exited.
195 <dd>The pipe to the native messaging host was broken before the message was
196 read by Chrome. This is most likely initiated from your native messaging host.
197
198 <dt>Specified native messaging host not found.
199 <dd>
200 <ul>
201 <li>
202 Is the name spelled correctly in the extension and in the manifest file?
203 <li>
204 Is the manifest put in the right directory and with the correct name? See
205 <a href="#native-messaging-host-location">native messaging host location</ a>
206 for the expected formats.
207 <li>
208 Is the manifest file in the correct format? In particular, is the JSON
209 syntax correct and do the values match the definition of a
210 <a href="native-messaging-host-manifest">native messaging host manifest</a >?
211 <li>
212 Does the file specified in <code>path</code> exist? On Windows, paths
213 may be relative, but on OS X and Linux, the paths must be absolute.
214 </ul>
215
216 <dt>Native messaging host <em>host name</em> is not registered. (Windows-only)
217 <dd>The native messaging host was not found in the Windows registry. Double-ch eck
218 using <code>regedit</code> whether the key was really created and matches the
219 required format as documented at
220 <a href="#native-messaging-host-location">native messaging host location</a>.
221
222 <dt>Access to the specified native messaging host is forbidden.
223 <dd>Is the extension's origin listed in <code>allowed_origins</code>?
224
225 <dt>Error when communicating with the native messaging host.
226 <dd>This is a very common error and indicates an incorrect implementation of
227 the communication protocol in the native messaging host.
228 <ul>
229 <li>
230 Make sure that all output in <code>stdout</code> adheres to the native
231 messaging protocol. If you want to print some data for debugging purposes,
232 write to <code>stderr</code>.
233 <li>
234 Make sure that the 32-bit message length is in the platform's native
235 integer format (little-endian / big-endian).
236 <li>
237 The message length must not exceed 1024*1024.
238 <li>
239 The message size must be equal to the number of bytes in the message.
240 This may differ from the "length" of a string, because characters may be
241 represented by multiple bytes.
242 <li>
243 <b>Windows-only: Make sure that the program's I/O mode is set to
244 <code>O_BINARY</code></b>. By default, the I/O mode is <code>O_TEXT</code> ,
245 which corrupts the message format as line breaks (<code>\n</code> =
246 <code>0A</code>) are replaced with Windows-style line endings
247 (<code>\r\n</code> = <code>0D 0A</code>). The I/O mode can be set using
248 <a href="http://msdn.microsoft.com/en-us/library/tw4k6df8.aspx"><code>__se tmode</code></a>.
249 </ul>
250 </dl>
251
252 <h2 id="examples">Examples</h2>
253 <p>
254 The <a href="https://chromium.googlesource.com/chromium/src/+/master/chrome/comm on/extensions/docs/examples/api/nativeMessaging">examples/api/nativeMessaging</a >
255 directory contains an example application that uses native messaging to
256 communicate with a Python script that serves as a native messaging host.
257 The sample host's directory also contains scripts to install/remove the native
258 messaging host.
259
260 <p>
261 To try out the example, first download and extract the
262 <a href="https://developer.chrome.com/extensions/examples/api/nativeMessaging/ap p.zip">sample app</a>
263 and
264 <a href="https://developer.chrome.com/extensions/examples/api/nativeMessaging/ho st.zip">sample host</a>.
265
266 Run <code>install_host.bat</code> (Windows) or
267 <code>install_host.sh</code> (Linux / OS X) to install the native messaging
268 host.
269 Then <a href="getstarted#unpacked">load the app</a> and interact with the app.
270 Run <code>uninstall_host.bat</code> or <code>uninstall_host.sh</code> to
271 unregister the native messaging host when you are done.
272
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698