| 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 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 function sendResponseAndClearCallback(response) { | 338 function sendResponseAndClearCallback(response) { |
| 339 // Save a reference so that we don't re-entrantly call responseCallback. | 339 // Save a reference so that we don't re-entrantly call responseCallback. |
| 340 var sendResponse = responseCallback; | 340 var sendResponse = responseCallback; |
| 341 responseCallback = null; | 341 responseCallback = null; |
| 342 sendResponse(response); | 342 if (arguments.length === 0) { |
| 343 // According to the documentation of chrome.runtime.sendMessage, the |
| 344 // callback is invoked without any arguments when an error occurs. |
| 345 sendResponse(); |
| 346 } else { |
| 347 sendResponse(response); |
| 348 } |
| 343 } | 349 } |
| 344 | 350 |
| 345 | 351 |
| 346 // Note: make sure to manually remove the onMessage/onDisconnect listeners | 352 // Note: make sure to manually remove the onMessage/onDisconnect listeners |
| 347 // that we added before destroying the Port, a workaround to a bug in Port | 353 // that we added before destroying the Port, a workaround to a bug in Port |
| 348 // where any onMessage/onDisconnect listeners added but not removed will | 354 // where any onMessage/onDisconnect listeners added but not removed will |
| 349 // be leaked when the Port is destroyed. | 355 // be leaked when the Port is destroyed. |
| 350 // http://crbug.com/320723 tracks a sustainable fix. | 356 // http://crbug.com/320723 tracks a sustainable fix. |
| 351 | 357 |
| 352 function disconnectListener() { | 358 function disconnectListener() { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 exports.$set('Port', Port); | 416 exports.$set('Port', Port); |
| 411 exports.$set('createPort', createPort); | 417 exports.$set('createPort', createPort); |
| 412 exports.$set('sendMessageImpl', sendMessageImpl); | 418 exports.$set('sendMessageImpl', sendMessageImpl); |
| 413 exports.$set('sendMessageUpdateArguments', sendMessageUpdateArguments); | 419 exports.$set('sendMessageUpdateArguments', sendMessageUpdateArguments); |
| 414 | 420 |
| 415 // For C++ code to call. | 421 // For C++ code to call. |
| 416 exports.$set('hasPort', hasPort); | 422 exports.$set('hasPort', hasPort); |
| 417 exports.$set('dispatchOnConnect', dispatchOnConnect); | 423 exports.$set('dispatchOnConnect', dispatchOnConnect); |
| 418 exports.$set('dispatchOnDisconnect', dispatchOnDisconnect); | 424 exports.$set('dispatchOnDisconnect', dispatchOnDisconnect); |
| 419 exports.$set('dispatchOnMessage', dispatchOnMessage); | 425 exports.$set('dispatchOnMessage', dispatchOnMessage); |
| OLD | NEW |