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

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

Issue 9693048: Make sure ports are closed when they're no longer used. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: updates Created 8 years, 9 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/extensions/docs/static/messaging.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <!DOCTYPE html><!-- This page is a placeholder for generated extensions api doc. Note: 1 <!DOCTYPE html><!-- This page is a placeholder for generated extensions api doc. Note:
2 1) The <head> information in this page is significant, should be uniform 2 1) The <head> information in this page is significant, should be uniform
3 across api docs and should be edited only with knowledge of the 3 across api docs and should be edited only with knowledge of the
4 templating mechanism. 4 templating mechanism.
5 3) All <body>.innerHTML is genereated as an rendering step. If viewed in a 5 3) All <body>.innerHTML is genereated as an rendering step. If viewed in a
6 browser, it will be re-generated from the template, json schema and 6 browser, it will be re-generated from the template, json schema and
7 authored overview content. 7 authored overview content.
8 4) The <body>.innerHTML is also generated by an offline step so that this 8 4) The <body>.innerHTML is also generated by an offline step so that this
9 page may easily be indexed by search engines. 9 page may easily be indexed by search engines.
10 --><html xmlns="http://www.w3.org/1999/xhtml"><head> 10 --><html xmlns="http://www.w3.org/1999/xhtml"><head>
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 chrome.tabs.getSelected(null, function(tab) { 266 chrome.tabs.getSelected(null, function(tab) {
267 chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) { 267 chrome.tabs.sendRequest(tab.id, {greeting: "hello"}, function(response) {
268 console.log(response.farewell); 268 console.log(response.farewell);
269 }); 269 });
270 }); 270 });
271 </pre> 271 </pre>
272 <p> 272 <p>
273 On the receiving end, you need to set up an 273 On the receiving end, you need to set up an
274 <a href="extension.html#event-onRequest">chrome.extension.onRequest</a> 274 <a href="extension.html#event-onRequest">chrome.extension.onRequest</a>
275 event listener to handle the message. This looks the same from a content 275 event listener to handle the message. This looks the same from a content
276 script or extension page. The request will remain open until you call 276 script or extension page.
277 sendResponse, so it is good practice to call sendResponse with an empty
278 object to allow the request to be cleaned up.
279 </p><pre>chrome.extension.onRequest.addListener( 277 </p><pre>chrome.extension.onRequest.addListener(
280 function(request, sender, sendResponse) { 278 function(request, sender, sendResponse) {
281 console.log(sender.tab ? 279 console.log(sender.tab ?
282 "from a content script:" + sender.tab.url : 280 "from a content script:" + sender.tab.url :
283 "from the extension"); 281 "from the extension");
284 if (request.greeting == "hello") 282 if (request.greeting == "hello")
285 sendResponse({farewell: "goodbye"}); 283 sendResponse({farewell: "goodbye"});
286 else
287 sendResponse({}); // snub them.
288 }); 284 });
289 </pre> 285 </pre>
290 <p class="note"> 286 <p class="note">
291 <b>Note:</b> If multiple pages are listening for onRequest events, only the 287 <b>Note:</b> If multiple pages are listening for onRequest events, only the
292 first to call sendResponse() for a particular event will succeed in sending the 288 first to call sendResponse() for a particular event will succeed in sending the
293 response. All other responses to that event will be ignored. 289 response. All other responses to that event will be ignored.
294 </p> 290 </p>
295 <h2 id="connect">Long-lived connections</h2> 291 <h2 id="connect">Long-lived connections</h2>
296 <p> 292 <p>
297 Sometimes it's useful to have a conversation that lasts longer than a single 293 Sometimes it's useful to have a conversation that lasts longer than a single
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 Listening for incoming requests and connections is similar to the internal 368 Listening for incoming requests and connections is similar to the internal
373 case, except you use the 369 case, except you use the
374 <a href="extension.html#event-onRequestExternal">chrome.extension.onRequestExter nal</a> 370 <a href="extension.html#event-onRequestExternal">chrome.extension.onRequestExter nal</a>
375 or 371 or
376 <a href="extension.html#event-onConnectExternal">chrome.extension.onConnectExter nal</a> 372 <a href="extension.html#event-onConnectExternal">chrome.extension.onConnectExter nal</a>
377 methods. Here's an example of each: 373 methods. Here's an example of each:
378 </p><pre>// For simple requests: 374 </p><pre>// For simple requests:
379 chrome.extension.onRequestExternal.addListener( 375 chrome.extension.onRequestExternal.addListener(
380 function(request, sender, sendResponse) { 376 function(request, sender, sendResponse) {
381 if (sender.id == blacklistedExtension) 377 if (sender.id == blacklistedExtension)
382 sendResponse({}); // don't allow this extension access 378 return; // don't allow this extension access
383 else if (request.getTargetData) 379 else if (request.getTargetData)
384 sendResponse({targetData: targetData}); 380 sendResponse({targetData: targetData});
385 else if (request.activateLasers) { 381 else if (request.activateLasers) {
386 var success = activateLasers(); 382 var success = activateLasers();
387 sendResponse({activateLasers: success}); 383 sendResponse({activateLasers: success});
388 } 384 }
389 }); 385 });
390 // For long-lived connections: 386 // For long-lived connections:
391 chrome.extension.onConnectExternal.addListener(function(port) { 387 chrome.extension.onConnectExternal.addListener(function(port) {
392 port.onMessage.addListener(function(msg) { 388 port.onMessage.addListener(function(msg) {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 _uff=0; 488 _uff=0;
493 urchinTracker(); 489 urchinTracker();
494 } 490 }
495 catch(e) {/* urchinTracker not available. */} 491 catch(e) {/* urchinTracker not available. */}
496 </script> 492 </script>
497 <!-- end analytics --> 493 <!-- end analytics -->
498 </div> 494 </div>
499 </div> <!-- /gc-footer --> 495 </div> <!-- /gc-footer -->
500 </div> <!-- /gc-container --> 496 </div> <!-- /gc-container -->
501 </body></html> 497 </body></html>
OLDNEW
« no previous file with comments | « no previous file | chrome/common/extensions/docs/static/messaging.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698