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

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

Issue 768373004: Improve native messaging documentation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments #4 + fix Python script for Windows 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
1 <h1>Message Passing</h1> 1 <h1>Message Passing</h1>
2 2
3 3
4 <p> 4 <p>
5 Since content scripts run in the context of a web page and not the extension, 5 Since content scripts run in the context of a web page and not the extension,
6 they often need some way of communicating with the rest of the extension. For 6 they often need some way of communicating with the rest of the extension. For
7 example, an RSS reader extension might use content scripts to detect the 7 example, an RSS reader extension might use content scripts to detect the
8 presence of an RSS feed on a page, then notify the background page in order to 8 presence of an RSS feed on a page, then notify the background page in order to
9 display a page action icon for that page. 9 display a page action icon for that page.
10 10
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 "name": "com.my_company.my_application", 297 "name": "com.my_company.my_application",
298 "description": "My Application", 298 "description": "My Application",
299 "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe", 299 "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe",
300 "type": "stdio", 300 "type": "stdio",
301 "allowed_origins": [ 301 "allowed_origins": [
302 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" 302 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
303 ] 303 ]
304 } 304 }
305 </pre> 305 </pre>
306 306
307 <p>Native messaging host manifest file contains the following fields: 307 <p>The native messaging host manifest file must be valid JSON and contains the
308 <table class="simple"> 308 following fields:
309 <table class="simple" id="native-messaging-host-manifest">
309 <tr> 310 <tr>
310 <th>Name</th> 311 <th>Name</th>
311 <th>Description</th> 312 <th>Description</th>
312 </tr> 313 </tr>
313 <tr> 314 <tr>
314 <td><code>name</code></td> 315 <td><code>name</code></td>
315 <td>Name of the native messaging host. Clients pass this string to 316 <td>Name of the native messaging host. Clients pass this string to
316 $(ref:runtime.connectNative) or $(ref:runtime.sendNativeMessage).</td> 317 $(ref:runtime.connectNative) or $(ref:runtime.sendNativeMessage).
318 This name can only contain lowercase alphanumeric characters, underscores
319 and dots. The name cannot start or end with a dot, and a dot cannot be
320 followed by another dot.
321 </td>
317 </tr> 322 </tr>
318 <tr> 323 <tr>
319 <td><code>description</code></td> 324 <td><code>description</code></td>
320 <td>Short application description.</td> 325 <td>Short application description.</td>
321 </tr> 326 </tr>
322 <tr> 327 <tr>
323 <td><code>path</code></td> 328 <td><code>path</code></td>
324 <td>Path to the native messaging host binary. On Linux and OSX the path must 329 <td>Path to the native messaging host binary. On Linux and OSX the path must
325 be absolute. On Windows it can be relative to the directory in which the 330 be absolute. On Windows it can be relative to the directory in which the
326 manifest file is located.</td> 331 manifest file is located.</td>
327 </tr> 332 </tr>
328 <tr> 333 <tr>
329 <td><code>type</code></td> 334 <td><code>type</code></td>
330 <td>Type of the interface used to communicate with the native messaging 335 <td>Type of the interface used to communicate with the native messaging
331 host. Currently there is only one possible value for this parameter: 336 host. Currently there is only one possible value for this parameter:
332 <code>stdio</code>. It indicates that Chrome should use <code>stdin</code> 337 <code>stdio</code>. It indicates that Chrome should use <code>stdin</code>
333 and <code>stdout</code> to communicate with the host.</td> 338 and <code>stdout</code> to communicate with the host.</td>
334 </tr> 339 </tr>
335 <tr> 340 <tr>
336 <td><code>allowed_origins</code></td> 341 <td><code>allowed_origins</code></td>
337 <td>List of extensions that should have access to the native messaging host. </td> 342 <td>List of extensions that should have access to the native messaging host.
343 Wildcards such as <code>chrome-extension://*/*</code> are <em>not</em>
344 allowed.</td>
338 </tr> 345 </tr>
339 </table> 346 </table>
340 347
341 <p>Location of the manifest file depends on the platform: 348 <h4 id="native-messaging-host-location">Native messaging host location</h4>
349 <p>The location of the manifest file depends on the platform.
350
351 <p id="native-messaging-host-location-windows">
352 On <b>Windows</b>, the manifest file can be located anywhere in the file system.
353 The application installer must create registry key
354 <code>HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com.my_ company.my_application</em></code>
355 or
356 <code>HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com.my_c ompany.my_application</em></code>,
357 and set default value of that key to the full path to the manifest file.
358 For example, using the following command:<br>
359 <pre>
360 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
361 </pre>
362 or using the following <code>.reg</code> file:
363 <pre>
364 Windows Registry Editor Version 5.00
365 [HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\<em>com.my_compan y.my_application</em>]
366 @="C:\\path\\to\\nmh-manifest.json"
367 </pre>
368 When Chrome looks for native messaging hosts, first the 32-bit registry is
369 queried, then the 64-bit registry.
370
371 <p id="native-messaging-host-location-nix">
372 On <b>OS X</b> and <b>Linux</b>, the location of the native messaging host's
373 manifest file varies by the browser (Google Chrome or Chromium).
374 The system-wide native messaging hosts are looked up at a fixed location,
375 while the user-level native messaging hosts are looked up in a subdirectory with in the
376 <a href="https://www.chromium.org/user-experience/user-data-directory">user prof ile directory</a>
377 called <code>NativeMessagingHosts</code>.
342 378
343 <dl> 379 <dl>
344 <dt>Windows:</dt> 380 <dt>OS X (system-wide)
345 <dd>The manifest file can be located anywhere in the file system. 381 <dd>Google Chrome: <code>/Library/Google/Chrome/NativeMessagingHosts/<em>com.my_ company.my_application</em>.json</code>
346 The application installer must create registry key 382 <dd>Chromium: <code>/Library/Application Support/Chromium/NativeMessagingHosts/< em>com.my_company.my_application</em>.json</code>
347 <code>HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>co m.my_company.my_application</em></code> 383 <dt>OS X (user-specific, <em>default</em> path)
348 or 384 <dd>Google Chrome: <code>~/Library/Application Support/Google/Chrome/Default/Nat iveMessagingHosts/<em>com.my_company.my_application</em>.json</code>
349 <code>HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com .my_company.my_application</em></code>, 385 <dd>Chromium: <code>~/Library/Application Support/Chromium/Default/NativeMessagi ngHosts/<em>com.my_company.my_application</em>.json</code>
350 and set default value of that key to the full path to the manifest file.
351 </dd>
352
353 <dt>OSX:</dt>
354 <dd>The manifest file must be placed at
355 <code>/Library/Google/Chrome/NativeMessagingHosts/<em>com.my_company.my_appl ication</em>.json</code>,
356 or, for applications installed on user level,
357 <code>~/Library/Application Support/Google/Chrome/NativeMessagingHosts/<em>c om.my_company.my_application</em>.json</code>.
358 </dd>
359
360 <dt>Linux:</dt>
361 <dd>The manifest file must be placed at
362 <code>/etc/opt/chrome/native-messaging-hosts/<em>com.my_company.my_applicati on</em>.json</code>,
363 or, for applications installed on user level,
364 <code>~/.config/google-chrome/NativeMessagingHosts/<em>com.my_company.my_app lication</em>.json</code>.
365 </dd>
366 </dl> 386 </dl>
367 387
388 <dl>
389 <dt>Linux (system-wide)
390 <dd>Google Chrome: <code>/etc/opt/chrome/native-messaging-hosts/<em>com.my_compa ny.my_application</em>.json</code>
391 <dd>Chromium: <code>/etc/chromium/native-messaging-hosts/<em>com.my_company.my_a pplication</em>.json</code>
392 <dt>Linux (user-specific, <em>default</em> path)
393 <dd>Google Chrome: <code>~/.config/google-chrome/NativeMessagingHosts/<em>com.my _company.my_application</em>.json</code>
394 <dd>Chromium: <code>~/.config/chromium/NativeMessagingHosts/<em>com.my_company.m y_application</em>.json</code>
395 </dl>
396
397 <h4 id="native-messaging-host-protocol">Native messaging protocol</h4>
368 <p> 398 <p>
369 Chrome starts each native messaging host in a separate process and communicates 399 Chrome starts each native messaging host in a separate process and communicates
370 with it using standard input (<code>stdin</code>) and standard output 400 with it using standard input (<code>stdin</code>) and standard output
371 (<code>stdout</code>). The same format is used to send messages in both 401 (<code>stdout</code>). The same format is used to send messages in both
372 directions: each message is serialized using JSON, UTF-8 encoded 402 directions: each message is serialized using JSON, UTF-8 encoded
373 and is preceded with 32-bit message length in native byte order. 403 and is preceded with 32-bit message length in native byte order.
404 The maximum size of a single message from the native messaging host is 1 MB,
405 mainly to protect Chrome from misbehaving native applications. The maximum
406 size of the message sent to the native messaging host is 4 GB.
374 407
375 <p> 408 <p>
376 When a messaging port is created using $(ref:runtime.connectNative) Chrome 409 When a messaging port is created using $(ref:runtime.connectNative) Chrome
377 starts native messaging host process and keeps it running until the port is 410 starts native messaging host process and keeps it running until the port is
378 destroyed. On the other hand, when a message is sent using 411 destroyed. On the other hand, when a message is sent using
379 $(ref:runtime.sendNativeMessage), without creating a messaging port, Chrome star ts 412 $(ref:runtime.sendNativeMessage), without creating a messaging port, Chrome star ts
380 a new native messaging host process for each message. In that case the first 413 a new native messaging host process for each message. In that case the first
381 message generated by the host process is handled as a response to the original 414 message generated by the host process is handled as a response to the original
382 request, i.e. Chrome will pass it to the response callback specified when 415 request, i.e. Chrome will pass it to the response callback specified when
383 $(ref:runtime.sendNativeMessage) is called. All other messages generated by the 416 $(ref:runtime.sendNativeMessage) is called. All other messages generated by the
(...skipping 25 matching lines...) Expand all
409 $(ref:runtime.sendNativeMessage) can be used to send a message to native 442 $(ref:runtime.sendNativeMessage) can be used to send a message to native
410 application without creating a port, e.g.: 443 application without creating a port, e.g.:
411 <pre> 444 <pre>
412 chrome.runtime.sendNativeMessage('com.my_company.my_application', 445 chrome.runtime.sendNativeMessage('com.my_company.my_application',
413 { text: "Hello" }, 446 { text: "Hello" },
414 function(response) { 447 function(response) {
415 console.log("Received " + response); 448 console.log("Received " + response);
416 }); 449 });
417 </pre> 450 </pre>
418 451
452 <h3 id="native-messaging-debugging">Debugging native messaging</h3>
453 <p>
454 When the native messaging host fails to start, writes to <code>stderr</code> or
455 when it violates the communication protocol, output is written to the error log
456 of Chrome.
457 On Linux and OS X, this log can easily be accessed by starting Chrome from the
458 command line and watching its output in the terminal.
459 On Windows, use <code>--enable-logging</code> as explained at
460 <a href="https://www.chromium.org/for-testers/enable-logging">How to enable logg ing</a>.
461
462 <p>
463 Here are some errors and tips for solving the issues:
464 <dl>
465 <dt>Failed to start native messaging host.
466 <dd>Check whether you have sufficient permissions to execute the file.
467
468 <dt>Invalid native messaging host name specified.
469 <dd>Check whether the name contains any invalid characters.
470 Only lowercase alphanumeric characters, underscores and dots are allowed.
471 A name cannot start or end with a dot, and a dot cannot be followed by
472 another dot.
473
474 <dt>Native host has exited.
475 <dd>The pipe to the native messaging host was broken before the message was
476 read by Chrome. This is most likely initiated from your native messaging host.
477
478 <dt>Specified native messaging host not found.
479 <dd>
480 <ul>
481 <li>
482 Is the name spelled correctly in the extension and in the manifest file?
483 <li>
484 Is the manifest put in the right directory and with the correct name? See
485 <a href="#native-messaging-host-location">native messaging host location</ a>
486 for the expected formats.
487 <li>
488 Is the manifest file in the correct format? In particular, is the JSON
489 syntax correct and do the values match the definition of a
490 <a href="native-messaging-host-manifest">native messaging host manifest</a >?
491 <li>
492 Does the file specified in <code>path</code> exist? On Windows, paths
493 may be relative, but on OS X and Linux, the paths must be absolute.
494 </ul>
495
496 <dt>Native messaging host <em>host name</em> is not registered. (Windows-only)
497 <dd>The native messaging host was not found in the Windows registry. Double-ch eck
498 using <code>regedit</code> whether the key was really created and matches the
499 required format as documented at
500 <a href="#native-messaging-host-location">native messaging host location</a>.
501
502 <dt>Access to the specified native messaging host is forbidden.
503 <dd>Is the extension's origin listed in <code>allowed_origins</code>?
504
505 <dt>Error when communicating with the native messaging host.
506 <dd>This is a very common error and indicates an incorrect implementation of
507 the communication protocol in the native messaging host.
508 <ul>
509 <li>
510 Make sure that all output in <code>stdout</code> adheres to the native
511 messaging protocol. If you want to print some data for debugging purposes,
512 write to <code>stderr</code>.
513 <li>
514 Make sure that the 32-bit message length is in the platform's native
515 integer format (little-endian / big-endian).
516 <li>
517 The message length must not exceed 1024*1024.
518 <li>
519 The message size must be equal to the number of bytes in the message.
520 This may differ from the "length" of a string, because characters may be
521 represented by multiple bytes.
522 <li>
523 <b>Windows-only: Make sure that the program's I/O mode is set to
524 <code>O_BINARY</code></b>. By default, the I/O mode is <code>O_TEXT</code> ,
525 which corrupts the message format as line breaks (<code>\n</code> =
526 <code>0A</code>) are replaced with Windows-style line endings
527 (<code>\r\n</code> = <code>0D 0A</code>). The I/O mode can be set using
528 <a href="http://msdn.microsoft.com/en-us/library/tw4k6df8.aspx"><code>__se tmode</code></a>.
529 </ul>
530 </dl>
531
419 <h2 id="security-considerations">Security considerations</h2> 532 <h2 id="security-considerations">Security considerations</h2>
420 533
421 <p> 534 <p>
422 When receiving a message from a content script or another extension, your 535 When receiving a message from a content script or another extension, your
423 background page should be careful not to fall victim to <a 536 background page should be careful not to fall victim to <a
424 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site 537 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site
425 scripting</a>. Specifically, avoid using dangerous APIs such as the 538 scripting</a>. Specifically, avoid using dangerous APIs such as the
426 below: 539 below:
427 </p> 540 </p>
428 <pre data-filename="background.js"> 541 <pre data-filename="background.js">
(...skipping 23 matching lines...) Expand all
452 document.getElementById("resp").innerText = response.farewell; 565 document.getElementById("resp").innerText = response.farewell;
453 }); 566 });
454 </pre> 567 </pre>
455 568
456 <h2 id="examples">Examples</h2> 569 <h2 id="examples">Examples</h2>
457 570
458 <p> 571 <p>
459 You can find simple examples of communication via messages in the 572 You can find simple examples of communication via messages in the
460 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/messaging/">examples/api/messaging</a> 573 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/messaging/">examples/api/messaging</a>
461 directory. 574 directory.
462 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a> 575 <a href="https://chromium.googlesource.com/chromium/src/+/master/chrome/common/e xtensions/docs/examples/api/nativeMessaging">examples/api/nativeMessaging</a>
463 contains an example application that uses native messaging. 576 contains an example application that uses native messaging.
464 Also see the 577 Also see the
465 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example, 578 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/howto/contentscript_xhr">contentscript_xhr</a> example,
466 in which a content script and its parent extension exchange messages, 579 in which a content script and its parent extension exchange messages,
467 so that the parent extension can perform 580 so that the parent extension can perform
468 cross-site requests on behalf of the content script. 581 cross-site requests on behalf of the content script.
469 For more examples and for help in viewing the source code, see 582 For more examples and for help in viewing the source code, see
470 <a href="samples">Samples</a>. 583 <a href="samples">Samples</a>.
471 </p> 584 </p>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698