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

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

Issue 1874133002: Improve documentation of extension messaging (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix refs, clarify API parameters Created 4 years, 8 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
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 chrome.runtime.onMessage.addListener( 68 chrome.runtime.onMessage.addListener(
69 function(request, sender, sendResponse) { 69 function(request, sender, sendResponse) {
70 console.log(sender.tab ? 70 console.log(sender.tab ?
71 "from a content script:" + sender.tab.url : 71 "from a content script:" + sender.tab.url :
72 "from the extension"); 72 "from the extension");
73 if (request.greeting == "hello") 73 if (request.greeting == "hello")
74 sendResponse({farewell: "goodbye"}); 74 sendResponse({farewell: "goodbye"});
75 }); 75 });
76 </pre> 76 </pre>
77 77
78 In the above example, <code>sendResponse</code> was called synchronously.
Devlin 2016/04/13 20:15:33 Why is this important?
robwu 2016/04/13 21:03:05 I've added another sentence; see also my reply bel
79
78 <p class="note"> 80 <p class="note">
79 <b>Note:</b> If multiple pages are listening for onMessage events, only the 81 <b>Note:</b> If multiple pages are listening for onMessage events, only the
80 first to call sendResponse() for a particular event will succeed in sending the 82 first to call sendResponse() for a particular event will succeed in sending the
81 response. All other responses to that event will be ignored. 83 response. All other responses to that event will be ignored.
82 </p> 84
85 <p class="note">
86 <b>Note:</b> If none of the handlers call <code>sendResponse</code>, then the
Devlin 2016/04/13 20:15:33 I don't know that we should add this to the docume
robwu 2016/04/13 21:03:05 Sending messages within an extension is quite impo
Devlin 2016/04/13 21:41:40 Ah, I see what you're trying to say with this comm
robwu 2016/04/13 22:44:34 Nice wording, I've copied it verbatim.
87 response callback is automatically invoked, unless one of the handlers returns
88 true. Even if a message handler returned true, the callback may still be called
89 automatically if the <code>sendResponse</code> callback is garbage-collected.
83 90
84 91
85 <h2 id="connect">Long-lived connections</h2> 92 <h2 id="connect">Long-lived connections</h2>
86 <p> 93 <p>
87 Sometimes it's useful to have a conversation that lasts longer than a single 94 Sometimes it's useful to have a conversation that lasts longer than a single
88 request and response. In this case, you can open a long-lived channel from 95 request and response. In this case, you can open a long-lived channel from
89 your content script to an extension page 96 your content script to an extension page
90 {{^is_apps}} 97 {{^is_apps}}
91 , or vice versa, 98 , or vice versa,
92 {{/is_apps}} 99 {{/is_apps}}
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 if (msg.joke == "Knock knock") 154 if (msg.joke == "Knock knock")
148 port.postMessage({question: "Who's there?"}); 155 port.postMessage({question: "Who's there?"});
149 else if (msg.answer == "Madame") 156 else if (msg.answer == "Madame")
150 port.postMessage({question: "Madame who?"}); 157 port.postMessage({question: "Madame who?"});
151 else if (msg.answer == "Madame... Bovary") 158 else if (msg.answer == "Madame... Bovary")
152 port.postMessage({question: "I don't get it."}); 159 port.postMessage({question: "I don't get it."});
153 }); 160 });
154 }); 161 });
155 </pre> 162 </pre>
156 163
164 <h3 id="port-lifetime">Port lifetime</h3>
165 <p>
166 Ports are designed as a two-way communication method between different parts of
167 the extension (where a (top-level) frame is viewed as the smallest part).
168 <br>
169 Upon calling $(ref:tabs.connect), $(ref:runtime.connect) or
170 $(ref:runtime.connectNative), a $(ref:runtime.Port Port) is created.
171 This port is immediately usable and messages can be used to send messages to
Devlin 2016/04/13 20:15:33 rephrase this - "... usable and messages can be us
robwu 2016/04/13 21:03:05 Done.
172 the other end via $(ref:runtime.Port.postMessage postMessage).
173
174 <p>
175 If there are multiple frames in a tab, calling $(ref:tabs.connect) results in
176 multiple invocations of the $(ref:runtime.onConnect) event
177 (once for each frame in the tab).
178 Similarly, if $(ref:runtime.connect) is used, then the onConnect may be fired
Devlin 2016/04/13 20:15:33 onConnect *event*? Also maybe linkify?
robwu 2016/04/13 21:03:05 Done. onConnect was already linkified in the prev
179 multiple times (once for every frame in the extension process).
180
157 <p> 181 <p>
158 You may want to find out when a connection is closed, for example if you are 182 You may want to find out when a connection is closed, for example if you are
159 maintaining separate state for each open port. For this you can listen to the 183 maintaining separate state for each open port. For this you can listen to the
160 $(ref:runtime.Port.onDisconnect) 184 $(ref:runtime.Port.onDisconnect)
161 event. This event is fired either when the other side of the channel manually 185 event. This event is fired either when there are no valid ports at the other
162 calls 186 side of the channel. This happens in the following situations:
163 $(ref:runtime.Port.disconnect), or when the page 187 <ul>
164 containing the port is unloaded (for example if the tab is navigated). 188 <li>
165 onDisconnect is guaranteed to be fired only once for any given port. 189 There are no listeners for $(ref:runtime.onConnect) at the other end.
190 <li>
191 The tab containing the port is unloaded (e.g. if the tab is navigated).
192 <li>
193 The frame from where <code>connect</code> was called has unloaded.
194 <li>
195 All frames that received the port (via $(ref:runtime.onConnect)) have
196 unloaded.
197 <li>
198 $(ref:runtime.Port.disconnect) is called by <em>the other end</em>.
199 Note that if a <code>connect</code> call results in multiple ports at the
200 receiver's end, and <code>disconnect()</code> is called on any of these
201 ports, then the <code>onDisconnect</code> event is only fired at the port
202 of the sender, and not fired at the other ports.
203 </ul>
166 204
167 205
168 <h2 id="external">Cross-extension messaging</h2> 206 <h2 id="external">Cross-extension messaging</h2>
169 <p> 207 <p>
170 In addition to sending messages between different components in your 208 In addition to sending messages between different components in your
171 extension, you can use the messaging API to communicate with other extensions. 209 extension, you can use the messaging API to communicate with other extensions.
172 This lets you expose a public API that other extensions can take advantage of. 210 This lets you expose a public API that other extensions can take advantage of.
173 211
174 <p> 212 <p>
175 Listening for incoming requests and connections is similar to the internal 213 Listening for incoming requests and connections is similar to the internal
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 370
333 <p> 371 <p>
334 You can find simple examples of communication via messages in the 372 You can find simple examples of communication via messages in the
335 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/messaging/">examples/api/messaging</a> 373 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extension s/docs/examples/api/messaging/">examples/api/messaging</a>
336 directory. 374 directory.
337 The <a href="nativeMessaging#examples">native messaging sample</a> demonstrates 375 The <a href="nativeMessaging#examples">native messaging sample</a> demonstrates
338 how a Chrome app can communicate with a native app. 376 how a Chrome app can communicate with a native app.
339 For more examples and for help in viewing the source code, see 377 For more examples and for help in viewing the source code, see
340 <a href="samples">Samples</a>. 378 <a href="samples">Samples</a>.
341 </p> 379 </p>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698