Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: chrome/renderer/resources/extensions/send_request.js

Issue 11571014: Lazy load chrome.* APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: android compilation Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 json = require('json'); 7 var json = require('json');
7 var lastError = require('lastError'); 8 var lastError = require('lastError');
8 var natives = requireNative('sendRequest'); 9 var natives = requireNative('sendRequest');
9 var validate = require('schemaUtils').validate; 10 var validate = require('schemaUtils').validate;
10 11
12 // All outstanding requests from sendRequest().
13 var requests = {};
14
11 // Callback handling. 15 // Callback handling.
12 var requests = [];
13 chromeHidden.handleResponse = function(requestId, name, 16 chromeHidden.handleResponse = function(requestId, name,
14 success, responseList, error) { 17 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
15 try { 23 try {
16 var request = requests[requestId]; 24 var request = requests[requestId];
17 if (success) { 25 DCHECK(request != null);
18 lastError.clear(); 26
19 } else { 27 // lastError needs to be set on the caller's chrome object no matter what,
20 if (!error) { 28 // though chances are it's the same as ours (it will be different when
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)
21 error = "Unknown error."; 39 error = "Unknown error.";
22 } 40 chromesForLastError.forEach(function(c) {lastError.set(error, c)});
23 console.error("Error during " + name + ": " + error);
24 lastError.set(error);
25 } 41 }
26 42
27 if (request.customCallback) { 43 if (request.customCallback) {
28 var customCallbackArgs = [name, request].concat(responseList); 44 var customCallbackArgs = [name, request].concat(responseList);
29 request.customCallback.apply(request, customCallbackArgs); 45 request.customCallback.apply(request, customCallbackArgs);
30 } 46 }
31 47
32 if (request.callback) { 48 if (request.callback) {
33 // Validate callback in debug only -- and only when the 49 // Validate callback in debug only -- and only when the
34 // caller has provided a callback. Implementations of api 50 // caller has provided a callback. Implementations of api
35 // calls my not return data if they observe the caller 51 // calls may not return data if they observe the caller
36 // has not provided a callback. 52 // has not provided a callback.
37 if (chromeHidden.validateCallbacks && !error) { 53 if (chromeHidden.validateCallbacks && !error) {
38 try { 54 try {
39 if (!request.callbackSchema.parameters) { 55 if (!request.callbackSchema.parameters) {
40 throw new Error("No callback schemas defined"); 56 throw new Error("No callback schemas defined");
41 } 57 }
42 58
43 validate(responseList, request.callbackSchema.parameters); 59 validate(responseList, request.callbackSchema.parameters);
44 } catch (exception) { 60 } catch (exception) {
45 return "Callback validation error during " + name + " -- " + 61 return "Callback validation error during " + name + " -- " +
46 exception.stack; 62 exception.stack;
47 } 63 }
48 } 64 }
49 65
50 request.callback.apply(request, responseList); 66 request.callback.apply(request, responseList);
51 } 67 }
52 } finally { 68 } finally {
53 delete requests[requestId]; 69 delete requests[requestId];
54 lastError.clear(); 70 chromesForLastError.forEach(function(c) {lastError.clear(c)});
55 } 71 }
56
57 return undefined;
58 }; 72 };
59 73
60 function prepareRequest(args, argSchemas) { 74 function prepareRequest(args, argSchemas) {
61 var request = {}; 75 var request = {};
62 var argCount = args.length; 76 var argCount = args.length;
63 77
64 // Look for callback param. 78 // Look for callback param.
65 if (argSchemas.length > 0 && 79 if (argSchemas.length > 0 &&
66 argSchemas[argSchemas.length - 1].type == "function") { 80 argSchemas[argSchemas.length - 1].type == "function") {
67 request.callback = args[args.length - 1]; 81 request.callback = args[args.length - 1];
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 // v8 values instead of expecting JSON strings. 116 // v8 values instead of expecting JSON strings.
103 var doStringify = false; 117 var doStringify = false;
104 if (optArgs.nativeFunction && !optArgs.noStringify) 118 if (optArgs.nativeFunction && !optArgs.noStringify)
105 doStringify = true; 119 doStringify = true;
106 var requestArgs = doStringify ? json.stringify(request.args) : request.args; 120 var requestArgs = doStringify ? json.stringify(request.args) : request.args;
107 var nativeFunction = optArgs.nativeFunction || natives.StartRequest; 121 var nativeFunction = optArgs.nativeFunction || natives.StartRequest;
108 122
109 var requestId = natives.GetNextRequestId(); 123 var requestId = natives.GetNextRequestId();
110 request.id = requestId; 124 request.id = requestId;
111 requests[requestId] = request; 125 requests[requestId] = request;
126
112 var hasCallback = request.callback || optArgs.customCallback; 127 var hasCallback = request.callback || optArgs.customCallback;
113 return nativeFunction(functionName, 128 return nativeFunction(functionName,
114 requestArgs, 129 requestArgs,
115 requestId, 130 requestId,
116 hasCallback, 131 hasCallback,
117 optArgs.forIOThread, 132 optArgs.forIOThread,
118 optArgs.preserveNullInObjects); 133 optArgs.preserveNullInObjects);
119 } 134 }
120 135
121 exports.sendRequest = sendRequest; 136 exports.sendRequest = sendRequest;
OLDNEW
« no previous file with comments | « chrome/renderer/resources/extensions/schema_utils.js ('k') | chrome/renderer/resources/extensions/storage_custom_bindings.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698