Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // chrome.runtime.messaging API implementation. | 5 // chrome.runtime.messaging API implementation. |
| 6 | 6 |
| 7 // TODO(kalman): factor requiring chrome out of here. | 7 // TODO(kalman): factor requiring chrome out of here. |
| 8 var chrome = requireNative('chrome').GetChrome(); | 8 var chrome = requireNative('chrome').GetChrome(); |
| 9 var Event = require('event_bindings').Event; | 9 var Event = require('event_bindings').Event; |
| 10 var lastError = require('lastError'); | 10 var lastError = require('lastError'); |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 | 328 |
| 329 if (port.name == kMessageChannel && !responseCallback) { | 329 if (port.name == kMessageChannel && !responseCallback) { |
| 330 // TODO(mpcomplete): Do this for the old sendRequest API too, after | 330 // TODO(mpcomplete): Do this for the old sendRequest API too, after |
| 331 // verifying it doesn't break anything. | 331 // verifying it doesn't break anything. |
| 332 // Go ahead and disconnect immediately if the sender is not expecting | 332 // Go ahead and disconnect immediately if the sender is not expecting |
| 333 // a response. | 333 // a response. |
| 334 port.disconnect(); | 334 port.disconnect(); |
| 335 return; | 335 return; |
| 336 } | 336 } |
| 337 | 337 |
| 338 // Ensure the callback exists for the older sendRequest API. | 338 function sendResponseIfNeeded(response) { |
| 339 if (!responseCallback) | 339 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.
| |
| 340 responseCallback = function() {}; | 340 if (sendResponse) { |
| 341 responseCallback = null; | |
| 342 sendResponse(response); | |
| 343 } | |
| 344 } | |
| 345 | |
| 341 | 346 |
| 342 // Note: make sure to manually remove the onMessage/onDisconnect listeners | 347 // Note: make sure to manually remove the onMessage/onDisconnect listeners |
| 343 // that we added before destroying the Port, a workaround to a bug in Port | 348 // that we added before destroying the Port, a workaround to a bug in Port |
| 344 // where any onMessage/onDisconnect listeners added but not removed will | 349 // where any onMessage/onDisconnect listeners added but not removed will |
| 345 // be leaked when the Port is destroyed. | 350 // be leaked when the Port is destroyed. |
| 346 // http://crbug.com/320723 tracks a sustainable fix. | 351 // http://crbug.com/320723 tracks a sustainable fix. |
| 347 | 352 |
| 348 function disconnectListener() { | 353 function disconnectListener() { |
| 349 // For onDisconnects, we only notify the callback if there was an error. | 354 if (lastError.hasError(chrome)) { |
| 350 if (chrome.runtime && chrome.runtime.lastError) | 355 sendResponseIfNeeded(); |
| 351 responseCallback(); | 356 } else { |
| 357 lastError.set( | |
| 358 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!
| |
| 359 null, chrome); | |
| 360 try { | |
| 361 sendResponseIfNeeded(); | |
| 362 } finally { | |
| 363 lastError.clear(chrome); | |
| 364 } | |
| 365 } | |
| 352 } | 366 } |
| 353 | 367 |
| 354 function messageListener(response) { | 368 function messageListener(response) { |
| 355 try { | 369 try { |
| 356 responseCallback(response); | 370 sendResponseIfNeeded(response); |
| 357 } finally { | 371 } finally { |
| 358 port.disconnect(); | 372 port.disconnect(); |
| 359 } | 373 } |
| 360 } | 374 } |
| 361 | 375 |
| 362 privates(port).impl.onDestroy_ = function() { | 376 privates(port).impl.onDestroy_ = function() { |
| 363 port.onDisconnect.removeListener(disconnectListener); | 377 port.onDisconnect.removeListener(disconnectListener); |
| 364 port.onMessage.removeListener(messageListener); | 378 port.onMessage.removeListener(messageListener); |
| 365 }; | 379 }; |
| 366 port.onDisconnect.addListener(disconnectListener); | 380 port.onDisconnect.addListener(disconnectListener); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 393 exports.$set('Port', Port); | 407 exports.$set('Port', Port); |
| 394 exports.$set('createPort', createPort); | 408 exports.$set('createPort', createPort); |
| 395 exports.$set('sendMessageImpl', sendMessageImpl); | 409 exports.$set('sendMessageImpl', sendMessageImpl); |
| 396 exports.$set('sendMessageUpdateArguments', sendMessageUpdateArguments); | 410 exports.$set('sendMessageUpdateArguments', sendMessageUpdateArguments); |
| 397 | 411 |
| 398 // For C++ code to call. | 412 // For C++ code to call. |
| 399 exports.$set('hasPort', hasPort); | 413 exports.$set('hasPort', hasPort); |
| 400 exports.$set('dispatchOnConnect', dispatchOnConnect); | 414 exports.$set('dispatchOnConnect', dispatchOnConnect); |
| 401 exports.$set('dispatchOnDisconnect', dispatchOnDisconnect); | 415 exports.$set('dispatchOnDisconnect', dispatchOnDisconnect); |
| 402 exports.$set('dispatchOnMessage', dispatchOnMessage); | 416 exports.$set('dispatchOnMessage', dispatchOnMessage); |
| OLD | NEW |