| OLD | NEW |
| 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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 chrome.runtime.onMessageExternal.addListener( | 272 chrome.runtime.onMessageExternal.addListener( |
| 273 function(request, sender, sendResponse) { | 273 function(request, sender, sendResponse) { |
| 274 if (sender.url == blacklistedWebsite) | 274 if (sender.url == blacklistedWebsite) |
| 275 return; // don't allow this web page access | 275 return; // don't allow this web page access |
| 276 if (request.openUrlInEditor) | 276 if (request.openUrlInEditor) |
| 277 openUrl(request.openUrlInEditor); | 277 openUrl(request.openUrlInEditor); |
| 278 }); | 278 }); |
| 279 </pre> | 279 </pre> |
| 280 | 280 |
| 281 | 281 |
| 282 <!-- Anchors to make sure that pages that link to a previous version of the |
| 283 documentation do not break. --> |
| 284 <a id="native-messaging-host"></a> |
| 285 <a id="native-messaging-client"></a> |
| 282 <h2 id="native-messaging">Native messaging</h2> | 286 <h2 id="native-messaging">Native messaging</h2> |
| 283 <p> | 287 <p> |
| 284 Extensions can exchange messages with native applications. Native | 288 Extensions and apps <a href="nativeMessaging#native-messaging-client">can |
| 285 applications that support this feature must register a <em>native messaging | 289 exchange messages</a> with native applications that are registered as a |
| 286 host</em> that knows how to communicate with the extension. Chrome starts the | 290 <a href="nativeMessaging#native-messaging-host">native messaging host</a>. |
| 287 host in a separate process and communicates with it using standard input and | 291 To learn more about this feature, see <a href="nativeMessaging">Native messaging
</a>. |
| 288 standard output streams. | |
| 289 | 292 |
| 290 <h3 id="native-messaging-host">Native messaging host</h3> | |
| 291 <p> | |
| 292 In order to register a native messaging host the application must install a | |
| 293 manifest file that defines the native messaging host configuration. Below is an | |
| 294 example of the manifest file: | |
| 295 <pre data-filename="manifest.json"> | |
| 296 { | |
| 297 "name": "com.my_company.my_application", | |
| 298 "description": "My Application", | |
| 299 "path": "C:\\Program Files\\My Application\\chrome_native_messaging_host.exe", | |
| 300 "type": "stdio", | |
| 301 "allowed_origins": [ | |
| 302 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/" | |
| 303 ] | |
| 304 } | |
| 305 </pre> | |
| 306 | |
| 307 <p>Native messaging host manifest file contains the following fields: | |
| 308 <table class="simple"> | |
| 309 <tr> | |
| 310 <th>Name</th> | |
| 311 <th>Description</th> | |
| 312 </tr> | |
| 313 <tr> | |
| 314 <td><code>name</code></td> | |
| 315 <td>Name of the native messaging host. Clients pass this string to | |
| 316 $(ref:runtime.connectNative) or $(ref:runtime.sendNativeMessage).</td> | |
| 317 </tr> | |
| 318 <tr> | |
| 319 <td><code>description</code></td> | |
| 320 <td>Short application description.</td> | |
| 321 </tr> | |
| 322 <tr> | |
| 323 <td><code>path</code></td> | |
| 324 <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 | |
| 326 manifest file is located.</td> | |
| 327 </tr> | |
| 328 <tr> | |
| 329 <td><code>type</code></td> | |
| 330 <td>Type of the interface used to communicate with the native messaging | |
| 331 host. Currently there is only one possible value for this parameter: | |
| 332 <code>stdio</code>. It indicates that Chrome should use <code>stdin</code> | |
| 333 and <code>stdout</code> to communicate with the host.</td> | |
| 334 </tr> | |
| 335 <tr> | |
| 336 <td><code>allowed_origins</code></td> | |
| 337 <td>List of extensions that should have access to the native messaging host.
</td> | |
| 338 </tr> | |
| 339 </table> | |
| 340 | |
| 341 <p>Location of the manifest file depends on the platform: | |
| 342 | |
| 343 <dl> | |
| 344 <dt>Windows:</dt> | |
| 345 <dd>The manifest file can be located anywhere in the file system. | |
| 346 The application installer must create registry key | |
| 347 <code>HKEY_LOCAL_MACHINE\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>co
m.my_company.my_application</em></code> | |
| 348 or | |
| 349 <code>HKEY_CURRENT_USER\SOFTWARE\Google\Chrome\NativeMessagingHosts\<em>com
.my_company.my_application</em></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> | |
| 367 | |
| 368 <p> | |
| 369 Chrome starts each native messaging host in a separate process and communicates | |
| 370 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 | |
| 372 directions: each message is serialized using JSON, UTF-8 encoded | |
| 373 and is preceded with 32-bit message length in native byte order. | |
| 374 | |
| 375 <p> | |
| 376 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 | |
| 378 destroyed. On the other hand, when a message is sent using | |
| 379 $(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 | |
| 381 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 | |
| 383 $(ref:runtime.sendNativeMessage) is called. All other messages generated by the | |
| 384 native messaging host in that case are ignored. | |
| 385 | |
| 386 <h3 id="native-messaging-client">Connecting to a native application</h3> | |
| 387 <p> | |
| 388 Sending and receiving messages to and from a native application is very similar | |
| 389 to cross-extension messaging. The main difference is that | |
| 390 $(ref:runtime.connectNative) is used instead of $(ref:runtime.connect), | |
| 391 and $(ref:runtime.sendNativeMessage) is used instead of $(ref:runtime.sendMessag
e). | |
| 392 | |
| 393 <p> | |
| 394 The Following example creates a $(ref:runtime.Port) object that's connected to n
ative | |
| 395 messaging host <code>com.my_company.my_application</code>, starts listening for | |
| 396 messages from that port and sends one outgoing message: | |
| 397 <pre> | |
| 398 var port = chrome.runtime.connectNative('com.my_company.my_application'); | |
| 399 port.onMessage.addListener(function(msg) { | |
| 400 console.log("Received" + msg); | |
| 401 }); | |
| 402 port.onDisconnect.addListener(function() { | |
| 403 console.log("Disconnected"); | |
| 404 }); | |
| 405 port.postMessage({ text: "Hello, my_application" }); | |
| 406 </pre> | |
| 407 | |
| 408 <p> | |
| 409 $(ref:runtime.sendNativeMessage) can be used to send a message to native | |
| 410 application without creating a port, e.g.: | |
| 411 <pre> | |
| 412 chrome.runtime.sendNativeMessage('com.my_company.my_application', | |
| 413 { text: "Hello" }, | |
| 414 function(response) { | |
| 415 console.log("Received " + response); | |
| 416 }); | |
| 417 </pre> | |
| 418 | 293 |
| 419 <h2 id="security-considerations">Security considerations</h2> | 294 <h2 id="security-considerations">Security considerations</h2> |
| 420 | 295 |
| 421 <p> | 296 <p> |
| 422 When receiving a message from a content script or another extension, your | 297 When receiving a message from a content script or another extension, your |
| 423 background page should be careful not to fall victim to <a | 298 background page should be careful not to fall victim to <a |
| 424 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site | 299 href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site |
| 425 scripting</a>. Specifically, avoid using dangerous APIs such as the | 300 scripting</a>. Specifically, avoid using dangerous APIs such as the |
| 426 below: | 301 below: |
| 427 </p> | 302 </p> |
| (...skipping 24 matching lines...) Expand all Loading... |
| 452 document.getElementById("resp").innerText = response.farewell; | 327 document.getElementById("resp").innerText = response.farewell; |
| 453 }); | 328 }); |
| 454 </pre> | 329 </pre> |
| 455 | 330 |
| 456 <h2 id="examples">Examples</h2> | 331 <h2 id="examples">Examples</h2> |
| 457 | 332 |
| 458 <p> | 333 <p> |
| 459 You can find simple examples of communication via messages in the | 334 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> | 335 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/messaging/">examples/api/messaging</a> |
| 461 directory. | 336 directory. |
| 462 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension
s/docs/examples/api/nativeMessaging/">examples/api/nativeMessaging</a> | 337 The <a href="nativeMessaging#examples">native messaging sample</a> demonstrates |
| 463 contains an example application that uses native messaging. | 338 how a Chrome app can communicate with a native app. |
| 464 For more examples and for help in viewing the source code, see | 339 For more examples and for help in viewing the source code, see |
| 465 <a href="samples">Samples</a>. | 340 <a href="samples">Samples</a>. |
| 466 </p> | 341 </p> |
| OLD | NEW |