Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); | 5 var chromeHidden = requireNative('chrome_hidden').GetChromeHidden(); |
| 6 var DCHECK = requireNative('logging').DCHECK; | 6 var DCHECK = requireNative('logging').DCHECK; |
| 7 var json = require('json'); | 7 var json = require('json'); |
| 8 var lastError = require('lastError'); | 8 var lastError = require('lastError'); |
| 9 var natives = requireNative('sendRequest'); | 9 var natives = requireNative('sendRequest'); |
| 10 var validate = require('schemaUtils').validate; | 10 var validate = require('schemaUtils').validate; |
| 11 | 11 |
| 12 // All outstanding requests from sendRequest(). | |
| 13 var requests = {}; | |
| 14 | |
| 12 // Callback handling. | 15 // Callback handling. |
| 13 var requests = []; | |
| 14 chromeHidden.handleResponse = function(requestId, name, | 16 chromeHidden.handleResponse = function(requestId, name, |
| 15 success, responseList, error) { | 17 success, responseList, error) { |
| 18 var request = requests[requestId]; | |
|
Matt Perry
2013/03/05 21:51:38
Why pull all this out of the try? Might as well be
not at google - send to devlin
2013/03/05 22:05:38
I pulled this out because the finally{} block refe
| |
| 19 delete requests[requestId]; | |
| 20 DCHECK(request != null); | |
| 21 | |
| 22 // lastError needs to be set on the caller's global object, which may not | |
| 23 // necessarily be ours. | |
| 24 var callerChrome = natives.GetGlobal( | |
| 25 request.callback || request.customCallback || chrome).chrome; | |
|
Matt Perry
2013/03/05 21:51:38
does GetGlobal() return the global object, or chro
not at google - send to devlin
2013/03/05 22:05:38
Yeah, chrome. There is a .chrome at the end of the
| |
| 26 | |
| 16 try { | 27 try { |
| 17 var request = requests[requestId]; | 28 lastError.clear(callerChrome); |
| 18 DCHECK(request != null); | 29 if (!success) { |
| 19 if (success) { | 30 if (!error) |
| 20 lastError.clear(); | |
| 21 } else { | |
| 22 if (!error) { | |
| 23 error = "Unknown error."; | 31 error = "Unknown error."; |
| 24 } | 32 lastError.set(error, callerChrome); |
| 25 console.error("Error during " + name + ": " + error); | |
| 26 lastError.set(error); | |
| 27 } | 33 } |
| 28 | 34 |
| 29 if (request.customCallback) { | 35 if (request.customCallback) { |
| 30 var customCallbackArgs = [name, request].concat(responseList); | 36 var customCallbackArgs = [name, request].concat(responseList); |
| 31 request.customCallback.apply(request, customCallbackArgs); | 37 request.customCallback.apply(request, customCallbackArgs); |
| 32 } | 38 } |
| 33 | 39 |
| 34 if (request.callback) { | 40 if (request.callback) { |
| 35 // Validate callback in debug only -- and only when the | 41 // Validate callback in debug only -- and only when the |
| 36 // caller has provided a callback. Implementations of api | 42 // caller has provided a callback. Implementations of api |
| 37 // calls may not return data if they observe the caller | 43 // calls may not return data if they observe the caller |
| 38 // has not provided a callback. | 44 // has not provided a callback. |
| 39 if (chromeHidden.validateCallbacks && !error) { | 45 if (chromeHidden.validateCallbacks && !error) { |
| 40 try { | 46 try { |
| 41 if (!request.callbackSchema.parameters) { | 47 if (!request.callbackSchema.parameters) { |
| 42 throw new Error("No callback schemas defined"); | 48 throw new Error("No callback schemas defined"); |
| 43 } | 49 } |
| 44 | 50 |
| 45 validate(responseList, request.callbackSchema.parameters); | 51 validate(responseList, request.callbackSchema.parameters); |
| 46 } catch (exception) { | 52 } catch (exception) { |
| 47 return "Callback validation error during " + name + " -- " + | 53 return "Callback validation error during " + name + " -- " + |
| 48 exception.stack; | 54 exception.stack; |
| 49 } | 55 } |
| 50 } | 56 } |
| 51 | 57 |
| 52 request.callback.apply(request, responseList); | 58 request.callback.apply(request, responseList); |
| 53 } | 59 } |
| 54 } finally { | 60 } finally { |
| 55 delete requests[requestId]; | 61 lastError.clear(callerChrome); |
| 56 lastError.clear(); | |
| 57 } | 62 } |
| 58 | |
| 59 return undefined; | |
| 60 }; | 63 }; |
| 61 | 64 |
| 62 function prepareRequest(args, argSchemas) { | 65 function prepareRequest(args, argSchemas) { |
| 63 var request = {}; | 66 var request = {}; |
| 64 var argCount = args.length; | 67 var argCount = args.length; |
| 65 | 68 |
| 66 // Look for callback param. | 69 // Look for callback param. |
| 67 if (argSchemas.length > 0 && | 70 if (argSchemas.length > 0 && |
| 68 argSchemas[argSchemas.length - 1].type == "function") { | 71 argSchemas[argSchemas.length - 1].type == "function") { |
| 69 request.callback = args[args.length - 1]; | 72 request.callback = args[args.length - 1]; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 104 // v8 values instead of expecting JSON strings. | 107 // v8 values instead of expecting JSON strings. |
| 105 var doStringify = false; | 108 var doStringify = false; |
| 106 if (optArgs.nativeFunction && !optArgs.noStringify) | 109 if (optArgs.nativeFunction && !optArgs.noStringify) |
| 107 doStringify = true; | 110 doStringify = true; |
| 108 var requestArgs = doStringify ? json.stringify(request.args) : request.args; | 111 var requestArgs = doStringify ? json.stringify(request.args) : request.args; |
| 109 var nativeFunction = optArgs.nativeFunction || natives.StartRequest; | 112 var nativeFunction = optArgs.nativeFunction || natives.StartRequest; |
| 110 | 113 |
| 111 var requestId = natives.GetNextRequestId(); | 114 var requestId = natives.GetNextRequestId(); |
| 112 request.id = requestId; | 115 request.id = requestId; |
| 113 requests[requestId] = request; | 116 requests[requestId] = request; |
| 117 | |
| 114 var hasCallback = request.callback || optArgs.customCallback; | 118 var hasCallback = request.callback || optArgs.customCallback; |
| 115 return nativeFunction(functionName, | 119 return nativeFunction(functionName, |
| 116 requestArgs, | 120 requestArgs, |
| 117 requestId, | 121 requestId, |
| 118 hasCallback, | 122 hasCallback, |
| 119 optArgs.forIOThread, | 123 optArgs.forIOThread, |
| 120 optArgs.preserveNullInObjects); | 124 optArgs.preserveNullInObjects); |
| 121 } | 125 } |
| 122 | 126 |
| 123 exports.sendRequest = sendRequest; | 127 exports.sendRequest = sendRequest; |
| OLD | NEW |