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

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: 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 | « no previous file | 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
Alexander Kashev 2014/12/03 09:28:31 I think the grammar here should be "must be ... an
robwu 2014/12/09 15:11:35 The sentence is correct. Read the sentence as foll
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>
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.<br>
Alexander Kashev 2014/12/03 09:28:31 Since you're updating the documentation: it would
robwu 2014/12/09 15:11:35 The usual Windows path syntax applies. Paths are s
359 When Chrome looks for native messaging hosts, first the 32-bit registry is
360 queried, then the 64-bit registry.
361 </p>
362
363 <p>On <b>OS X</b> and <b>Linux</b>, the location of the native messaging host's
364 manifest file varies by the browser (Google Chrome or Chromium).
365 The system-wide native messaging hosts are looked up at a fixed location,
366 while the user-level native messaging hosts are looked up in a subdirectory with in the
367 <a href="https://www.chromium.org/user-experience/user-data-directory">user prof ile directory</a>
368 called <code>NativeMessagingHosts</code>.
369 </p>
370
371 <p>
343 <dl> 372 <dl>
344 <dt>Windows:</dt> 373 <dt>OS X (system-wide)</dt>
345 <dd>The manifest file can be located anywhere in the file system. 374 <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 375 <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> 376 <dt>OS X (user-specific, <em>default</em> path)</dt>
348 or 377 <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>, 378 <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. 379 </dl>
351 </dd> 380 </p>
352 381
353 <dt>OSX:</dt> 382 <p>
354 <dd>The manifest file must be placed at 383 <dl>
355 <code>/Library/Google/Chrome/NativeMessagingHosts/<em>com.my_company.my_appl ication</em>.json</code>, 384 <dt>Linux (system-wide)</dt>
356 or, for applications installed on user level, 385 <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>. 386 <dd>Chromium: <code>/etc/chromium/native-messaging-hosts/<em>com.my_company.my_a pplication</em>.json</code></dd>
358 </dd> 387 <dt>Linux (user-specific, <em>default</em> path)</dt>
388 <dd>Google Chrome: <code>~/.config/google-chrome/NativeMessagingHosts/<em>com.my _company.my_application</em>.json</code></dd>
389 <dd>Chromium: <code>~/.config/chromium/NativeMessagingHosts/<em>com.my_company.m y_application</em>.json</code></dd>
390 </dl>
391 </p>
359 392
360 <dt>Linux:</dt> 393 <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> 394 <p>
369 Chrome starts each native messaging host in a separate process and communicates 395 Chrome starts each native messaging host in a separate process and communicates
370 with it using standard input (<code>stdin</code>) and standard output 396 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 397 (<code>stdout</code>). The same format is used to send messages in both
372 directions: each message is serialized using JSON, UTF-8 encoded 398 directions: each message is serialized using JSON, UTF-8 encoded
373 and is preceded with 32-bit message length in native byte order. 399 and is preceded with 32-bit message length in native byte order.
400 The maximum size of a single message from the native messaging host is 1 MB,
401 mainly to protect Chrome from misbehaving native applications. There is no
402 hard limit on the size of the message sent to the native messaging host.
Alexander Kashev 2014/12/03 09:28:32 Technically, not true: message sent to the host is
robwu 2014/12/09 15:11:34 Done.
403 </p>
374 404
375 <p> 405 <p>
376 When a messaging port is created using $(ref:runtime.connectNative) Chrome 406 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 407 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 408 destroyed. On the other hand, when a message is sent using
379 $(ref:runtime.sendNativeMessage), without creating a messaging port, Chrome star ts 409 $(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 410 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 411 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 412 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 413 $(ref:runtime.sendNativeMessage) is called. All other messages generated by the
384 native messaging host in that case are ignored. 414 native messaging host in that case are ignored.</p>
385 415
386 <h3 id="native-messaging-client">Connecting to a native application</h3> 416 <h3 id="native-messaging-client">Connecting to a native application</h3>
387 <p> 417 <p>
388 Sending and receiving messages to and from a native application is very similar 418 Sending and receiving messages to and from a native application is very similar
389 to cross-extension messaging. The main difference is that 419 to cross-extension messaging. The main difference is that
390 $(ref:runtime.connectNative) is used instead of $(ref:runtime.connect), 420 $(ref:runtime.connectNative) is used instead of $(ref:runtime.connect),
391 and $(ref:runtime.sendNativeMessage) is used instead of $(ref:runtime.sendMessag e). 421 and $(ref:runtime.sendNativeMessage) is used instead of $(ref:runtime.sendMessag e).
392 422
393 <p> 423 <p>
394 The Following example creates a $(ref:runtime.Port) object that's connected to n ative 424 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 439 $(ref:runtime.sendNativeMessage) can be used to send a message to native
410 application without creating a port, e.g.: 440 application without creating a port, e.g.:
411 <pre> 441 <pre>
412 chrome.runtime.sendNativeMessage('com.my_company.my_application', 442 chrome.runtime.sendNativeMessage('com.my_company.my_application',
413 { text: "Hello" }, 443 { text: "Hello" },
414 function(response) { 444 function(response) {
415 console.log("Received " + response); 445 console.log("Received " + response);
416 }); 446 });
417 </pre> 447 </pre>
418 448
449 <h3 id="native-messaging-debugging">Debugging native messaging</h3>
450 <p>
451 When the native messaging host fails to start, writes to <code>stderr</code> or
452 when it violates the communication protocol, output is written to the error log
453 of Chrome.
454 On Linux and OS X, this log can easily be accessed by starting Chrome from the
455 command line and watching its output in the terminal.
456 On Windows, use <code>--enable-logging</code> as explained at
457 <a href="https://www.chromium.org/for-testers/enable-logging">How to enable logg ing</a>.
458 </p>
459
460 Here are some errors and tips for solving the issues:
461 <dl>
462 <dt>Failed to start native messaging host.</dt>
463 <dd>Check whether you have sufficient permissions to execute the file.</dd>
464
465 <dt>Invalid native messaging host name specified.</dt>
466 <dd>Check whether the name contains any invalid characters.
467 Only lowercase alphanumeric characters, underscores and dots are allowed.
468 A name cannot start or end with a dot, and a dot cannot be followed by
469 another dot.</dd>
470
471 <dt>Native host has exited.</dt>
472 <dd>The pipe to the native messaging host was broken before the message was
473 read by Chrome. This is most likely initiated from your native messaging host.
474 </dd>
475
476 <dt>Specified native messaging host not found.</dt>
477 <dd>
478 <ul>
479 <li>Is the name spelled correctly in the extension and in the manifest file? </li>
480 <li>
481 Is the manifest put in the right directory and with the correct name? See
482 <a href="#native-messaging-host-location">native messaging host location</ a>
483 for the expected formats.
484 </li>
485 <li>
486 Is the manifest file in the correct format? In particular, is the JSON
487 syntax correct and do the values match the definition of a
488 <a href="native-messaging-host-manifest">native messaging host manifest</a >?
489 </li>
490 <li>Does the file specified in <code>path</code> exist? On Windows, paths
491 may be relative, but on OS X and Linux, the paths must be absolute.</li>
492 </ul>
493 </dd>
494
495 <dt>Native messaging host <em>host name</em> is not registered. (Windows-only) </dt>
496 <dd>The native messaging host was not found in the Windows registry. Look at
497 <a href="#native-messaging-host-location">native messaging host location</a>
498 for more information on the required format of the registry key.
499 </dd>
500
501 <dt>Access to the specified native messaging host is forbidden.</dt>
502 <dd>Is the extension's origin listed in <code>allowed_origins</code>?</dd>
503
504 <dt>Error when communicating with the native messaging host.</dt>
505 <dd>This is a very common error and indicates an incorrect implementation of
506 the communication protocol in the native messaging host.
507 <ul>
508 <li>
509 Make sure that all output in <code>stdout</code> adheres to the native
510 messaging protocol. If you want to print some data for debugging purposes,
511 write to <code>stderr</code>.
512 </li>
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 <li>
518 The message length must not exceed 1024*1024.
519 </li>
520 <li>
521 The message size must be equal to the number of bytes in the message.
522 This may differ from the "length" of a string, because characters may be
523 represented by multiple bytes.
524 </li>
525 <li>
526 <b>Windows-only: Make sure that the program's I/O mode is set to
527 <code>O_BINARY</code></b>. By default, the I/O mode is <code>O_TEXT</code> ,
528 which replaces line breaks (<code>\n</code> = <code>0A</code>) with
529 Windows-style line endings (<code>\r\n</code> = <code>0D 0A</code>).<br>
530 This modification is problematic. For example, if your message length is
531 10, then the 32-bit integer is <code>00 00 00 0A</code>.
532 When <code>0D</code> is inserted before <code>0A</code>, the message
533 header becomes <code>00 00 00 0D 0A</code>. Only the first four bytes
534 represent a 32-bit integer, so the length is <code>0D</code> = 13.
535 This length is different from the original message length and incorrect!
536 </li>
537 </ul>
538 </dd>
539 </dl>
540
419 <h2 id="security-considerations">Security considerations</h2> 541 <h2 id="security-considerations">Security considerations</h2>
420 542
421 <p> 543 <p>
422 When receiving a message from a content script or another extension, your 544 When receiving a message from a content script or another extension, your
423 background page should be careful not to fall victim to <a 545 background page should be careful not to fall victim to <a
424 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site 546 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site
425 scripting</a>. Specifically, avoid using dangerous APIs such as the 547 scripting</a>. Specifically, avoid using dangerous APIs such as the
426 below: 548 below:
427 </p> 549 </p>
428 <pre data-filename="background.js"> 550 <pre data-filename="background.js">
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a> 584 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a>
463 contains an example application that uses native messaging. 585 contains an example application that uses native messaging.
464 Also see the 586 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, 587 <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, 588 in which a content script and its parent extension exchange messages,
467 so that the parent extension can perform 589 so that the parent extension can perform
468 cross-site requests on behalf of the content script. 590 cross-site requests on behalf of the content script.
469 For more examples and for help in viewing the source code, see 591 For more examples and for help in viewing the source code, see
470 <a href="samples">Samples</a>. 592 <a href="samples">Samples</a>.
471 </p> 593 </p>
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698