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