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

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: Add extra installation instructions 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
« no previous file with comments | « chrome/common/extensions/docs/examples/api/nativeMessaging/host/uninstall_host.bat ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
347 </p>
340 348
341 <p>Location of the manifest file depends on the platform: 349 <h4 id="native-messaging-host-location">Native messaging host location</h4>
350 <p>The location of the manifest file depends on the platform.</p>
342 351
352 <p id="native-messaging-host-location-windows">
353 On <b>Windows</b>, the manifest file can be located anywhere in the file system.
354 The application installer must create registry key
355 <code>HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com.my_ company.my_application</em></code>
356 or
357 <code>HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com.my_c ompany.my_application</em></code>,
358 and set default value of that key to the full path to the manifest file.
359 For example, using the following command:<br>
360 <pre>
361 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
362 </pre>
363 or using the following <code>.reg</code> file:
364 <pre>
365 Windows Registry Editor Version 5.00
366 [HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\<em>com.my_compan y.my_application</em>]
367 @="C:\\path\\to\\nmh-manifest.json"
368 </pre>
369 When Chrome looks for native messaging hosts, first the 32-bit registry is
370 queried, then the 64-bit registry.
371 </p>
372
373 <p id="native-messaging-host-location-nix">
374 On <b>OS X</b> and <b>Linux</b>, the location of the native messaging host's
375 manifest file varies by the browser (Google Chrome or Chromium).
376 The system-wide native messaging hosts are looked up at a fixed location,
377 while the user-level native messaging hosts are looked up in a subdirectory with in the
378 <a href="https://www.chromium.org/user-experience/user-data-directory">user prof ile directory</a>
379 called <code>NativeMessagingHosts</code>.
380 </p>
Sergey Ulanov 2014/12/10 18:42:47 nit: don't need </p> here and below. http://stacko
robwu 2014/12/10 23:09:41 Done.
381
382 <p>
343 <dl> 383 <dl>
344 <dt>Windows:</dt> 384 <dt>OS X (system-wide)</dt>
345 <dd>The manifest file can be located anywhere in the file system. 385 <dd>Google Chrome: <code>/Library/Google/Chrome/NativeMessagingHosts/<em>com.my_ company.my_application</em>.json</code></dd>
346 The application installer must create registry key 386 <dd>Chromium: <code>/Library/Application Support/Chromium/NativeMessagingHosts/< em>com.my_company.my_application</em>.json</code></dd>
347 <code>HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>co m.my_company.my_application</em></code> 387 <dt>OS X (user-specific, <em>default</em> path)</dt>
348 or 388 <dd>Google Chrome: <code>~/Library/Application Support/Google/Chrome/Default/Nat iveMessagingHosts/<em>com.my_company.my_application</em>.json</code></dd>
349 <code>HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com .my_company.my_application</em></code>, 389 <dd>Chromium: <code>~/Library/Application Support/Chromium/Default/NativeMessagi ngHosts/<em>com.my_company.my_application</em>.json</code></dd>
350 and set default value of that key to the full path to the manifest file. 390 </dl>
351 </dd> 391 </p>
352 392
353 <dt>OSX:</dt> 393 <p>
354 <dd>The manifest file must be placed at 394 <dl>
355 <code>/Library/Google/Chrome/NativeMessagingHosts/<em>com.my_company.my_appl ication</em>.json</code>, 395 <dt>Linux (system-wide)</dt>
356 or, for applications installed on user level, 396 <dd>Google Chrome: <code>/etc/opt/chrome/native-messaging-hosts/<em>com.my_compa ny.my_application</em>.json</code></dd>
357 <code>~/Library/Application Support/Google/Chrome/NativeMessagingHosts/<em>c om.my_company.my_application</em>.json</code>. 397 <dd>Chromium: <code>/etc/chromium/native-messaging-hosts/<em>com.my_company.my_a pplication</em>.json</code></dd>
358 </dd> 398 <dt>Linux (user-specific, <em>default</em> path)</dt>
399 <dd>Google Chrome: <code>~/.config/google-chrome/NativeMessagingHosts/<em>com.my _company.my_application</em>.json</code></dd>
400 <dd>Chromium: <code>~/.config/chromium/NativeMessagingHosts/<em>com.my_company.m y_application</em>.json</code></dd>
401 </dl>
402 </p>
359 403
360 <dt>Linux:</dt> 404 <h4 id="native-messaging-host-protocol">Native messaging protocol</h4>
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>
367
368 <p> 405 <p>
369 Chrome starts each native messaging host in a separate process and communicates 406 Chrome starts each native messaging host in a separate process and communicates
370 with it using standard input (<code>stdin</code>) and standard output 407 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 408 (<code>stdout</code>). The same format is used to send messages in both
372 directions: each message is serialized using JSON, UTF-8 encoded 409 directions: each message is serialized using JSON, UTF-8 encoded
373 and is preceded with 32-bit message length in native byte order. 410 and is preceded with 32-bit message length in native byte order.
411 The maximum size of a single message from the native messaging host is 1 MB,
412 mainly to protect Chrome from misbehaving native applications. The maximum
413 size of the message sent to the native messaging host is 4 GB.
414 </p>
374 415
375 <p> 416 <p>
376 When a messaging port is created using $(ref:runtime.connectNative) Chrome 417 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 418 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 419 destroyed. On the other hand, when a message is sent using
379 $(ref:runtime.sendNativeMessage), without creating a messaging port, Chrome star ts 420 $(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 421 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 422 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 423 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 424 $(ref:runtime.sendNativeMessage) is called. All other messages generated by the
384 native messaging host in that case are ignored. 425 native messaging host in that case are ignored.</p>
385 426
386 <h3 id="native-messaging-client">Connecting to a native application</h3> 427 <h3 id="native-messaging-client">Connecting to a native application</h3>
387 <p> 428 <p>
388 Sending and receiving messages to and from a native application is very similar 429 Sending and receiving messages to and from a native application is very similar
389 to cross-extension messaging. The main difference is that 430 to cross-extension messaging. The main difference is that
390 $(ref:runtime.connectNative) is used instead of $(ref:runtime.connect), 431 $(ref:runtime.connectNative) is used instead of $(ref:runtime.connect),
391 and $(ref:runtime.sendNativeMessage) is used instead of $(ref:runtime.sendMessag e). 432 and $(ref:runtime.sendNativeMessage) is used instead of $(ref:runtime.sendMessag e).
392 433
393 <p> 434 <p>
394 The Following example creates a $(ref:runtime.Port) object that's connected to n ative 435 The Following example creates a $(ref:runtime.Port) object that's connected to n ative
(...skipping 14 matching lines...) Expand all
409 $(ref:runtime.sendNativeMessage) can be used to send a message to native 450 $(ref:runtime.sendNativeMessage) can be used to send a message to native
410 application without creating a port, e.g.: 451 application without creating a port, e.g.:
411 <pre> 452 <pre>
412 chrome.runtime.sendNativeMessage('com.my_company.my_application', 453 chrome.runtime.sendNativeMessage('com.my_company.my_application',
413 { text: "Hello" }, 454 { text: "Hello" },
414 function(response) { 455 function(response) {
415 console.log("Received " + response); 456 console.log("Received " + response);
416 }); 457 });
417 </pre> 458 </pre>
418 459
460 <h3 id="native-messaging-debugging">Debugging native messaging</h3>
461 <p>
462 When the native messaging host fails to start, writes to <code>stderr</code> or
463 when it violates the communication protocol, output is written to the error log
464 of Chrome.
465 On Linux and OS X, this log can easily be accessed by starting Chrome from the
466 command line and watching its output in the terminal.
467 On Windows, use <code>--enable-logging</code> as explained at
468 <a href="https://www.chromium.org/for-testers/enable-logging">How to enable logg ing</a>.
469 </p>
470
471 Here are some errors and tips for solving the issues:
472 <dl>
473 <dt>Failed to start native messaging host.</dt>
Sergey Ulanov 2014/12/10 18:42:47 nit: don't need to close <dt>, same for <dd>
robwu 2014/12/10 23:09:41 Done.
474 <dd>Check whether you have sufficient permissions to execute the file.</dd>
475
476 <dt>Invalid native messaging host name specified.</dt>
477 <dd>Check whether the name contains any invalid characters.
478 Only lowercase alphanumeric characters, underscores and dots are allowed.
479 A name cannot start or end with a dot, and a dot cannot be followed by
480 another dot.</dd>
481
482 <dt>Native host has exited.</dt>
483 <dd>The pipe to the native messaging host was broken before the message was
484 read by Chrome. This is most likely initiated from your native messaging host.
485 </dd>
486
487 <dt>Specified native messaging host not found.</dt>
488 <dd>
489 <ul>
490 <li>Is the name spelled correctly in the extension and in the manifest file? </li>
Sergey Ulanov 2014/12/10 18:42:47 nit: don't need to close <li> in html
robwu 2014/12/10 23:09:41 Done.
491 <li>
492 Is the manifest put in the right directory and with the correct name? See
493 <a href="#native-messaging-host-location">native messaging host location</ a>
494 for the expected formats.
495 </li>
496 <li>
497 Is the manifest file in the correct format? In particular, is the JSON
498 syntax correct and do the values match the definition of a
499 <a href="native-messaging-host-manifest">native messaging host manifest</a >?
500 </li>
501 <li>Does the file specified in <code>path</code> exist? On Windows, paths
502 may be relative, but on OS X and Linux, the paths must be absolute.</li>
503 </ul>
504 </dd>
505
506 <dt>Native messaging host <em>host name</em> is not registered. (Windows-only) </dt>
507 <dd>The native messaging host was not found in the Windows registry. Double-ch eck
508 using <code>regedit</code> whether the key was really created and matches the
509 required format as documented at
510 <a href="#native-messaging-host-location">native messaging host location</a>.
511 </dd>
512
513 <dt>Access to the specified native messaging host is forbidden.</dt>
514 <dd>Is the extension's origin listed in <code>allowed_origins</code>?</dd>
515
516 <dt>Error when communicating with the native messaging host.</dt>
517 <dd>This is a very common error and indicates an incorrect implementation of
518 the communication protocol in the native messaging host.
519 <ul>
520 <li>
521 Make sure that all output in <code>stdout</code> adheres to the native
522 messaging protocol. If you want to print some data for debugging purposes,
523 write to <code>stderr</code>.
524 </li>
525 <li>
526 Make sure that the 32-bit message length is in the platform's native
527 integer format (little-endian / big-endian).
528 </li>
529 <li>
530 The message length must not exceed 1024*1024.
531 </li>
532 <li>
533 The message size must be equal to the number of bytes in the message.
534 This may differ from the "length" of a string, because characters may be
535 represented by multiple bytes.
536 </li>
537 <li>
538 <b>Windows-only: Make sure that the program's I/O mode is set to
539 <code>O_BINARY</code></b>. By default, the I/O mode is <code>O_TEXT</code> ,
Sergey Ulanov 2014/12/10 18:42:47 Please add link to MSDN for how the mode can be ch
robwu 2014/12/10 23:09:41 Done.
540 which replaces line breaks (<code>\n</code> = <code>0A</code>) with
541 Windows-style line endings (<code>\r\n</code> = <code>0D 0A</code>).<br>
542 This modification is problematic. For example, if your message length is
543 10, then the 32-bit integer is <code>00 00 00 0A</code>.
544 When <code>0D</code> is inserted before <code>0A</code>, the message
545 header becomes <code>00 00 00 0D 0A</code>. Only the first four bytes
546 represent a 32-bit integer, so the length is <code>0D</code> = 13.
547 This length is different from the original message length and incorrect!
548 </li>
549 </ul>
550 </dd>
551 </dl>
552
419 <h2 id="security-considerations">Security considerations</h2> 553 <h2 id="security-considerations">Security considerations</h2>
420 554
421 <p> 555 <p>
422 When receiving a message from a content script or another extension, your 556 When receiving a message from a content script or another extension, your
423 background page should be careful not to fall victim to <a 557 background page should be careful not to fall victim to <a
424 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site 558 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site
425 scripting</a>. Specifically, avoid using dangerous APIs such as the 559 scripting</a>. Specifically, avoid using dangerous APIs such as the
426 below: 560 below:
427 </p> 561 </p>
428 <pre data-filename="background.js"> 562 <pre data-filename="background.js">
(...skipping 23 matching lines...) Expand all
452 document.getElementById("resp").innerText = response.farewell; 586 document.getElementById("resp").innerText = response.farewell;
453 }); 587 });
454 </pre> 588 </pre>
455 589
456 <h2 id="examples">Examples</h2> 590 <h2 id="examples">Examples</h2>
457 591
458 <p> 592 <p>
459 You can find simple examples of communication via messages in the 593 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> 594 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/messaging/">examples/api/messaging</a>
461 directory. 595 directory.
462 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a> 596 <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. 597 contains an example application that uses native messaging.
464 Also see the 598 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, 599 <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, 600 in which a content script and its parent extension exchange messages,
467 so that the parent extension can perform 601 so that the parent extension can perform
468 cross-site requests on behalf of the content script. 602 cross-site requests on behalf of the content script.
469 For more examples and for help in viewing the source code, see 603 For more examples and for help in viewing the source code, see
470 <a href="samples">Samples</a>. 604 <a href="samples">Samples</a>.
471 </p> 605 </p>
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/examples/api/nativeMessaging/host/uninstall_host.bat ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698