| 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; | |
| 7 var json = require('json'); | 6 var json = require('json'); |
| 8 var lastError = require('lastError'); | 7 var lastError = require('lastError'); |
| 9 var natives = requireNative('sendRequest'); | 8 var natives = requireNative('sendRequest'); |
| 10 var validate = require('schemaUtils').validate; | 9 var validate = require('schemaUtils').validate; |
| 11 | 10 |
| 12 // All outstanding requests from sendRequest(). | |
| 13 var requests = {}; | |
| 14 | |
| 15 // Callback handling. | 11 // Callback handling. |
| 12 var requests = []; |
| 16 chromeHidden.handleResponse = function(requestId, name, | 13 chromeHidden.handleResponse = function(requestId, name, |
| 17 success, responseList, error) { | 14 success, responseList, error) { |
| 18 // The chrome objects we will set lastError on. Really we should only be | |
| 19 // setting this on the callback's chrome object, but set on ours too since | |
| 20 // it's conceivable that something relies on that. | |
| 21 var chromesForLastError = [chrome]; | |
| 22 | |
| 23 try { | 15 try { |
| 24 var request = requests[requestId]; | 16 var request = requests[requestId]; |
| 25 DCHECK(request != null); | 17 if (success) { |
| 26 | 18 lastError.clear(); |
| 27 // lastError needs to be set on the caller's chrome object no matter what, | 19 } else { |
| 28 // though chances are it's the same as ours (it will be different when | 20 if (!error) { |
| 29 // calling API methods on other contexts). | |
| 30 if (request.callback) { | |
| 31 var chromeForCallback = natives.GetGlobal(request.callback).chrome; | |
| 32 if (chromeForCallback != chrome) | |
| 33 chromesForLastError.push(chromeForCallback); | |
| 34 } | |
| 35 | |
| 36 chromesForLastError.forEach(function(c) {lastError.clear(c)}); | |
| 37 if (!success) { | |
| 38 if (!error) | |
| 39 error = "Unknown error."; | 21 error = "Unknown error."; |
| 40 chromesForLastError.forEach(function(c) {lastError.set(error, c)}); | 22 } |
| 23 console.error("Error during " + name + ": " + error); |
| 24 lastError.set(error); |
| 41 } | 25 } |
| 42 | 26 |
| 43 if (request.customCallback) { | 27 if (request.customCallback) { |
| 44 var customCallbackArgs = [name, request].concat(responseList); | 28 var customCallbackArgs = [name, request].concat(responseList); |
| 45 request.customCallback.apply(request, customCallbackArgs); | 29 request.customCallback.apply(request, customCallbackArgs); |
| 46 } | 30 } |
| 47 | 31 |
| 48 if (request.callback) { | 32 if (request.callback) { |
| 49 // Validate callback in debug only -- and only when the | 33 // Validate callback in debug only -- and only when the |
| 50 // caller has provided a callback. Implementations of api | 34 // caller has provided a callback. Implementations of api |
| 51 // calls may not return data if they observe the caller | 35 // calls my not return data if they observe the caller |
| 52 // has not provided a callback. | 36 // has not provided a callback. |
| 53 if (chromeHidden.validateCallbacks && !error) { | 37 if (chromeHidden.validateCallbacks && !error) { |
| 54 try { | 38 try { |
| 55 if (!request.callbackSchema.parameters) { | 39 if (!request.callbackSchema.parameters) { |
| 56 throw new Error("No callback schemas defined"); | 40 throw new Error("No callback schemas defined"); |
| 57 } | 41 } |
| 58 | 42 |
| 59 validate(responseList, request.callbackSchema.parameters); | 43 validate(responseList, request.callbackSchema.parameters); |
| 60 } catch (exception) { | 44 } catch (exception) { |
| 61 return "Callback validation error during " + name + " -- " + | 45 return "Callback validation error during " + name + " -- " + |
| 62 exception.stack; | 46 exception.stack; |
| 63 } | 47 } |
| 64 } | 48 } |
| 65 | 49 |
| 66 request.callback.apply(request, responseList); | 50 request.callback.apply(request, responseList); |
| 67 } | 51 } |
| 68 } finally { | 52 } finally { |
| 69 delete requests[requestId]; | 53 delete requests[requestId]; |
| 70 chromesForLastError.forEach(function(c) {lastError.clear(c)}); | 54 lastError.clear(); |
| 71 } | 55 } |
| 56 |
| 57 return undefined; |
| 72 }; | 58 }; |
| 73 | 59 |
| 74 function prepareRequest(args, argSchemas) { | 60 function prepareRequest(args, argSchemas) { |
| 75 var request = {}; | 61 var request = {}; |
| 76 var argCount = args.length; | 62 var argCount = args.length; |
| 77 | 63 |
| 78 // Look for callback param. | 64 // Look for callback param. |
| 79 if (argSchemas.length > 0 && | 65 if (argSchemas.length > 0 && |
| 80 argSchemas[argSchemas.length - 1].type == "function") { | 66 argSchemas[argSchemas.length - 1].type == "function") { |
| 81 request.callback = args[args.length - 1]; | 67 request.callback = args[args.length - 1]; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // v8 values instead of expecting JSON strings. | 102 // v8 values instead of expecting JSON strings. |
| 117 var doStringify = false; | 103 var doStringify = false; |
| 118 if (optArgs.nativeFunction && !optArgs.noStringify) | 104 if (optArgs.nativeFunction && !optArgs.noStringify) |
| 119 doStringify = true; | 105 doStringify = true; |
| 120 var requestArgs = doStringify ? json.stringify(request.args) : request.args; | 106 var requestArgs = doStringify ? json.stringify(request.args) : request.args; |
| 121 var nativeFunction = optArgs.nativeFunction || natives.StartRequest; | 107 var nativeFunction = optArgs.nativeFunction || natives.StartRequest; |
| 122 | 108 |
| 123 var requestId = natives.GetNextRequestId(); | 109 var requestId = natives.GetNextRequestId(); |
| 124 request.id = requestId; | 110 request.id = requestId; |
| 125 requests[requestId] = request; | 111 requests[requestId] = request; |
| 126 | |
| 127 var hasCallback = request.callback || optArgs.customCallback; | 112 var hasCallback = request.callback || optArgs.customCallback; |
| 128 return nativeFunction(functionName, | 113 return nativeFunction(functionName, |
| 129 requestArgs, | 114 requestArgs, |
| 130 requestId, | 115 requestId, |
| 131 hasCallback, | 116 hasCallback, |
| 132 optArgs.forIOThread, | 117 optArgs.forIOThread, |
| 133 optArgs.preserveNullInObjects); | 118 optArgs.preserveNullInObjects); |
| 134 } | 119 } |
| 135 | 120 |
| 136 exports.sendRequest = sendRequest; | 121 exports.sendRequest = sendRequest; |
| OLD | NEW |