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 var exceptionHandler = require('uncaught_exception_handler'); | 5 var exceptionHandler = require('uncaught_exception_handler'); |
| 6 var lastError = require('lastError'); | 6 var lastError = require('lastError'); |
| 7 var logging = requireNative('logging'); | 7 var logging = requireNative('logging'); |
| 8 var natives = requireNative('sendRequest'); | 8 var natives = requireNative('sendRequest'); |
| 9 var validate = require('schemaUtils').validate; | 9 var validate = require('schemaUtils').validate; |
| 10 | 10 |
| 11 // All outstanding requests from sendRequest(). | 11 // All outstanding requests from sendRequest(). |
| 12 var requests = {}; | 12 var requests = { __proto__: null }; |
| 13 | 13 |
| 14 // Used to prevent double Activity Logging for API calls that use both custom | 14 // Used to prevent double Activity Logging for API calls that use both custom |
| 15 // bindings and ExtensionFunctions (via sendRequest). | 15 // bindings and ExtensionFunctions (via sendRequest). |
| 16 var calledSendRequest = false; | 16 var calledSendRequest = false; |
| 17 | 17 |
| 18 // Runs a user-supplied callback safely. | 18 // Runs a user-supplied callback safely. |
| 19 function safeCallbackApply(name, request, callback, args) { | 19 function safeCallbackApply(name, request, callback, args) { |
| 20 try { | 20 try { |
| 21 $Function.apply(callback, request, args); | 21 $Function.apply(callback, request, args); |
| 22 } catch (e) { | 22 } catch (e) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 } | 81 } |
| 82 } finally { | 82 } finally { |
| 83 delete requests[requestId]; | 83 delete requests[requestId]; |
| 84 lastError.clear(chrome); | 84 lastError.clear(chrome); |
| 85 if (callerChrome !== chrome) | 85 if (callerChrome !== chrome) |
| 86 lastError.clear(callerChrome); | 86 lastError.clear(callerChrome); |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 | 89 |
| 90 function prepareRequest(args, argSchemas) { | 90 function prepareRequest(args, argSchemas) { |
| 91 var request = {}; | 91 var request = { __proto__: null }; |
| 92 var argCount = args.length; | 92 var argCount = args.length; |
| 93 | 93 |
| 94 // Look for callback param. | 94 // Look for callback param. |
| 95 if (argSchemas.length > 0 && | 95 if (argSchemas.length > 0 && |
| 96 argSchemas[argSchemas.length - 1].type == "function") { | 96 argSchemas[argSchemas.length - 1].type == "function") { |
| 97 request.callback = args[args.length - 1]; | 97 request.callback = args[args.length - 1]; |
| 98 request.callbackSchema = argSchemas[argSchemas.length - 1]; | 98 request.callbackSchema = argSchemas[argSchemas.length - 1]; |
| 99 --argCount; | 99 --argCount; |
| 100 } | 100 } |
| 101 | 101 |
| 102 request.args = []; | 102 request.args = $Array.slice(args, 0, argCount); |
| 103 for (var k = 0; k < argCount; k++) { | |
| 104 request.args[k] = args[k]; | |
| 105 } | |
| 106 | |
| 107 return request; | 103 return request; |
| 108 } | 104 } |
| 109 | 105 |
| 110 // Send an API request and optionally register a callback. | 106 // Send an API request and optionally register a callback. |
| 111 // |optArgs| is an object with optional parameters as follows: | 107 // |optArgs| is an object with optional parameters as follows: |
| 112 // - customCallback: a callback that should be called instead of the standard | 108 // - customCallback: a callback that should be called instead of the standard |
| 113 // callback. | 109 // callback. |
| 114 // - forIOThread: true if this function should be handled on the browser IO | 110 // - forIOThread: true if this function should be handled on the browser IO |
| 115 // thread. | 111 // thread. |
| 116 // - preserveNullInObjects: true if it is safe for null to be in objects. | 112 // - preserveNullInObjects: true if it is safe for null to be in objects. |
| 117 // - stack: An optional string that contains the stack trace, to be displayed | 113 // - stack: An optional string that contains the stack trace, to be displayed |
| 118 // to the user if an error occurs. | 114 // to the user if an error occurs. |
| 119 function sendRequest(functionName, args, argSchemas, optArgs) { | 115 function sendRequest(functionName, args, argSchemas, optArgs) { |
| 120 calledSendRequest = true; | 116 calledSendRequest = true; |
| 121 if (!optArgs) | 117 if (!optArgs) |
| 122 optArgs = {}; | 118 optArgs = { __proto__: null }; |
|
robwu
2016/04/23 09:54:38
Can you also check the callers of sendRequest and
Devlin
2016/04/25 21:50:35
That wasn't as painful as I thought it'd be, given
| |
| 123 var request = prepareRequest(args, argSchemas); | 119 var request = prepareRequest(args, argSchemas); |
| 124 request.stack = optArgs.stack || exceptionHandler.getExtensionStackTrace(); | 120 request.stack = optArgs.stack || exceptionHandler.getExtensionStackTrace(); |
| 125 if (optArgs.customCallback) { | 121 if (optArgs.customCallback) { |
| 126 request.customCallback = optArgs.customCallback; | 122 request.customCallback = optArgs.customCallback; |
| 127 } | 123 } |
| 128 | 124 |
| 129 var hasCallback = request.callback || optArgs.customCallback; | 125 var hasCallback = request.callback || optArgs.customCallback; |
| 130 var requestId = | 126 var requestId = |
| 131 natives.StartRequest(functionName, request.args, hasCallback, | 127 natives.StartRequest(functionName, request.args, hasCallback, |
| 132 optArgs.forIOThread, optArgs.preserveNullInObjects); | 128 optArgs.forIOThread, optArgs.preserveNullInObjects); |
| 133 request.id = requestId; | 129 request.id = requestId; |
| 134 requests[requestId] = request; | 130 requests[requestId] = request; |
| 135 } | 131 } |
| 136 | 132 |
| 137 function getCalledSendRequest() { | 133 function getCalledSendRequest() { |
| 138 return calledSendRequest; | 134 return calledSendRequest; |
| 139 } | 135 } |
| 140 | 136 |
| 141 function clearCalledSendRequest() { | 137 function clearCalledSendRequest() { |
| 142 calledSendRequest = false; | 138 calledSendRequest = false; |
| 143 } | 139 } |
| 144 | 140 |
| 145 exports.$set('sendRequest', sendRequest); | 141 exports.$set('sendRequest', sendRequest); |
| 146 exports.$set('getCalledSendRequest', getCalledSendRequest); | 142 exports.$set('getCalledSendRequest', getCalledSendRequest); |
| 147 exports.$set('clearCalledSendRequest', clearCalledSendRequest); | 143 exports.$set('clearCalledSendRequest', clearCalledSendRequest); |
| 148 exports.$set('safeCallbackApply', safeCallbackApply); | 144 exports.$set('safeCallbackApply', safeCallbackApply); |
| 149 | 145 |
| 150 // Called by C++. | 146 // Called by C++. |
| 151 exports.$set('handleResponse', handleResponse); | 147 exports.$set('handleResponse', handleResponse); |
| OLD | NEW |