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

Unified Diff: extensions/renderer/resources/messaging.js

Issue 1567423002: Ensure that sendMessage's callback is called. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/test/data/extensions/api_test/messaging/connect_crash/test.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/resources/messaging.js
diff --git a/extensions/renderer/resources/messaging.js b/extensions/renderer/resources/messaging.js
index a15497d23efac334c9073830fcdd47d3237ac543..c1e93eed2101889ee5cc1e9e84c75af0fc77cdff 100644
--- a/extensions/renderer/resources/messaging.js
+++ b/extensions/renderer/resources/messaging.js
@@ -335,9 +335,14 @@
return;
}
- // Ensure the callback exists for the older sendRequest API.
- if (!responseCallback)
- responseCallback = function() {};
+ function sendResponseIfNeeded(response) {
+ var sendResponse = responseCallback;
Devlin 2016/01/08 17:42:28 Why the copy?
robwu 2016/01/08 17:53:20 This is a bit defensive, to make sure that the cal
Devlin 2016/01/08 18:04:25 That makes sense. I think if that's the intention
robwu 2016/01/08 18:14:24 Added a comment.
+ if (sendResponse) {
+ responseCallback = null;
+ sendResponse(response);
+ }
+ }
+
// Note: make sure to manually remove the onMessage/onDisconnect listeners
// that we added before destroying the Port, a workaround to a bug in Port
@@ -346,14 +351,23 @@
// http://crbug.com/320723 tracks a sustainable fix.
function disconnectListener() {
- // For onDisconnects, we only notify the callback if there was an error.
- if (chrome.runtime && chrome.runtime.lastError)
- responseCallback();
+ if (lastError.hasError(chrome)) {
+ sendResponseIfNeeded();
+ } else {
+ lastError.set(
+ port.name, 'The message port closed before a reponse was received.',
Devlin 2016/01/08 17:42:28 I must be missing something - where is this listen
robwu 2016/01/08 17:53:20 Are you referring to the existing comment above? I
Devlin 2016/01/08 18:04:25 onDisconnect is called when the port is disconnect
robwu 2016/01/08 18:14:24 Good point. Fixed!
+ null, chrome);
+ try {
+ sendResponseIfNeeded();
+ } finally {
+ lastError.clear(chrome);
+ }
+ }
}
function messageListener(response) {
try {
- responseCallback(response);
+ sendResponseIfNeeded(response);
} finally {
port.disconnect();
}
« no previous file with comments | « chrome/test/data/extensions/api_test/messaging/connect_crash/test.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698