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 }; |
119 logging.DCHECK(optArgs.__proto__ == null); | |
robwu
2016/04/25 22:25:53
$Object.getPrototypeOf(optArgs) === null
If not s
Devlin
2016/04/25 22:35:00
I had thought about that, but this is only to chec
robwu
2016/04/25 22:53:27
It's just a debug assertion, so it doesn't matter.
| |
123 var request = prepareRequest(args, argSchemas); | 120 var request = prepareRequest(args, argSchemas); |
124 request.stack = optArgs.stack || exceptionHandler.getExtensionStackTrace(); | 121 request.stack = optArgs.stack || exceptionHandler.getExtensionStackTrace(); |
125 if (optArgs.customCallback) { | 122 if (optArgs.customCallback) { |
126 request.customCallback = optArgs.customCallback; | 123 request.customCallback = optArgs.customCallback; |
127 } | 124 } |
128 | 125 |
129 var hasCallback = request.callback || optArgs.customCallback; | 126 var hasCallback = request.callback || optArgs.customCallback; |
130 var requestId = | 127 var requestId = |
131 natives.StartRequest(functionName, request.args, hasCallback, | 128 natives.StartRequest(functionName, request.args, hasCallback, |
132 optArgs.forIOThread, optArgs.preserveNullInObjects); | 129 optArgs.forIOThread, optArgs.preserveNullInObjects); |
133 request.id = requestId; | 130 request.id = requestId; |
134 requests[requestId] = request; | 131 requests[requestId] = request; |
135 } | 132 } |
136 | 133 |
137 function getCalledSendRequest() { | 134 function getCalledSendRequest() { |
138 return calledSendRequest; | 135 return calledSendRequest; |
139 } | 136 } |
140 | 137 |
141 function clearCalledSendRequest() { | 138 function clearCalledSendRequest() { |
142 calledSendRequest = false; | 139 calledSendRequest = false; |
143 } | 140 } |
144 | 141 |
145 exports.$set('sendRequest', sendRequest); | 142 exports.$set('sendRequest', sendRequest); |
146 exports.$set('getCalledSendRequest', getCalledSendRequest); | 143 exports.$set('getCalledSendRequest', getCalledSendRequest); |
147 exports.$set('clearCalledSendRequest', clearCalledSendRequest); | 144 exports.$set('clearCalledSendRequest', clearCalledSendRequest); |
148 exports.$set('safeCallbackApply', safeCallbackApply); | 145 exports.$set('safeCallbackApply', safeCallbackApply); |
149 | 146 |
150 // Called by C++. | 147 // Called by C++. |
151 exports.$set('handleResponse', handleResponse); | 148 exports.$set('handleResponse', handleResponse); |
OLD | NEW |