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 forEach = require('utils').forEach; | 6 var forEach = require('utils').forEach; |
8 var json = require('json'); | 7 var json = require('json'); |
9 var lastError = require('lastError'); | 8 var lastError = require('lastError'); |
| 9 var logging = requireNative('logging'); |
10 var natives = requireNative('sendRequest'); | 10 var natives = requireNative('sendRequest'); |
11 var validate = require('schemaUtils').validate; | 11 var validate = require('schemaUtils').validate; |
12 | 12 |
13 // All outstanding requests from sendRequest(). | 13 // All outstanding requests from sendRequest(). |
14 var requests = {}; | 14 var requests = {}; |
15 | 15 |
16 // Used to prevent double Activity Logging for API calls that use both custom | 16 // Used to prevent double Activity Logging for API calls that use both custom |
17 // bindings and ExtensionFunctions (via sendRequest). | 17 // bindings and ExtensionFunctions (via sendRequest). |
18 var calledSendRequest = false; | 18 var calledSendRequest = false; |
19 | 19 |
20 // Callback handling. | 20 // Callback handling. |
21 chromeHidden.handleResponse = function(requestId, name, | 21 function handleResponse(requestId, name, success, responseList, error) { |
22 success, responseList, error) { | |
23 // The chrome objects we will set lastError on. Really we should only be | 22 // The chrome objects we will set lastError on. Really we should only be |
24 // setting this on the callback's chrome object, but set on ours too since | 23 // setting this on the callback's chrome object, but set on ours too since |
25 // it's conceivable that something relies on that. | 24 // it's conceivable that something relies on that. |
26 var chromesForLastError = [chrome]; | 25 var chromesForLastError = [chrome]; |
27 | 26 |
28 try { | 27 try { |
29 var request = requests[requestId]; | 28 var request = requests[requestId]; |
30 DCHECK(request != null); | 29 logging.DCHECK(request != null); |
31 | 30 |
32 // lastError needs to be set on the caller's chrome object no matter what, | 31 // lastError needs to be set on the caller's chrome object no matter what, |
33 // though chances are it's the same as ours (it will be different when | 32 // though chances are it's the same as ours (it will be different when |
34 // calling API methods on other contexts). | 33 // calling API methods on other contexts). |
35 if (request.callback) { | 34 if (request.callback) { |
36 var chromeForCallback = natives.GetGlobal(request.callback).chrome; | 35 var chromeForCallback = natives.GetGlobal(request.callback).chrome; |
37 if (chromeForCallback != chrome) | 36 if (chromeForCallback != chrome) |
38 chromesForLastError.push(chromeForCallback); | 37 chromesForLastError.push(chromeForCallback); |
39 } | 38 } |
40 | 39 |
41 forEach(chromesForLastError, function(i, c) {lastError.clear(c)}); | 40 forEach(chromesForLastError, function(i, c) {lastError.clear(c)}); |
42 if (!success) { | 41 if (!success) { |
43 if (!error) | 42 if (!error) |
44 error = "Unknown error."; | 43 error = "Unknown error."; |
45 forEach(chromesForLastError, function(i, c) { | 44 forEach(chromesForLastError, function(i, c) { |
46 lastError.set(name, error, request.stack, c) | 45 lastError.set(name, error, request.stack, c) |
47 }); | 46 }); |
48 } | 47 } |
49 | 48 |
50 if (request.customCallback) { | 49 if (request.customCallback) { |
51 var customCallbackArgs = [name, request].concat(responseList); | 50 var customCallbackArgs = [name, request].concat(responseList); |
52 request.customCallback.apply(request, customCallbackArgs); | 51 request.customCallback.apply(request, customCallbackArgs); |
53 } | 52 } |
54 | 53 |
55 if (request.callback) { | 54 if (request.callback) { |
56 // Validate callback in debug only -- and only when the | 55 // Validate callback in debug only -- and only when the |
57 // caller has provided a callback. Implementations of api | 56 // caller has provided a callback. Implementations of api |
58 // calls may not return data if they observe the caller | 57 // calls may not return data if they observe the caller |
59 // has not provided a callback. | 58 // has not provided a callback. |
60 if (chromeHidden.validateCallbacks && !error) { | 59 if (logging.DCHECK_IS_ON() && !error) { |
61 try { | 60 try { |
62 if (!request.callbackSchema.parameters) { | 61 if (!request.callbackSchema.parameters) { |
63 throw new Error("No callback schemas defined"); | 62 throw new Error("No callback schemas defined"); |
64 } | 63 } |
65 | 64 |
66 validate(responseList, request.callbackSchema.parameters); | 65 validate(responseList, request.callbackSchema.parameters); |
67 } catch (exception) { | 66 } catch (exception) { |
68 return "Callback validation error during " + name + " -- " + | 67 return "Callback validation error during " + name + " -- " + |
69 exception.stack; | 68 exception.stack; |
70 } | 69 } |
71 } | 70 } |
72 | 71 |
73 request.callback.apply(request, responseList); | 72 try { |
| 73 request.callback.apply(request, responseList); |
| 74 } catch (e) { |
| 75 var errorMessage = "Error in response to " + name + ": " + e; |
| 76 if (request.stack && request.stack != '') |
| 77 errorMessage += "\n" + request.stack; |
| 78 console.error(errorMessage); |
| 79 } |
74 } | 80 } |
75 } finally { | 81 } finally { |
76 delete requests[requestId]; | 82 delete requests[requestId]; |
77 forEach(chromesForLastError, function(i, c) {lastError.clear(c)}); | 83 forEach(chromesForLastError, function(i, c) {lastError.clear(c)}); |
78 } | 84 } |
79 }; | 85 }; |
80 | 86 |
81 function getExtensionStackTrace(call_name) { | 87 function getExtensionStackTrace(call_name) { |
82 var stack = new Error().stack.split('\n'); | 88 var stack = new Error().stack.split('\n'); |
83 | 89 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 return calledSendRequest; | 166 return calledSendRequest; |
161 } | 167 } |
162 | 168 |
163 function clearCalledSendRequest() { | 169 function clearCalledSendRequest() { |
164 calledSendRequest = false; | 170 calledSendRequest = false; |
165 } | 171 } |
166 | 172 |
167 exports.sendRequest = sendRequest; | 173 exports.sendRequest = sendRequest; |
168 exports.getCalledSendRequest = getCalledSendRequest; | 174 exports.getCalledSendRequest = getCalledSendRequest; |
169 exports.clearCalledSendRequest = clearCalledSendRequest; | 175 exports.clearCalledSendRequest = clearCalledSendRequest; |
| 176 |
| 177 // Called by C++. |
| 178 exports.handleResponse = handleResponse; |
OLD | NEW |