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

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

Issue 10535030: Allow updateArgumentsPostValidate to support callbacks and added / removed arguments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Proper syntax Created 8 years, 6 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 natives = requireNative('sendRequest'); 6 var natives = requireNative('sendRequest');
7 var validate = require('schemaUtils').validate;
7 8
8 // Callback handling. 9 // Callback handling.
9 var requests = []; 10 var requests = [];
10 chromeHidden.handleResponse = function(requestId, name, 11 chromeHidden.handleResponse = function(requestId, name,
11 success, response, error) { 12 success, response, error) {
12 try { 13 try {
13 var request = requests[requestId]; 14 var request = requests[requestId];
14 if (success) { 15 if (success) {
15 delete chrome.extension.lastError; 16 delete chrome.extension.lastError;
16 } else { 17 } else {
(...skipping 20 matching lines...) Expand all
37 // has not provided a callback. 38 // has not provided a callback.
38 if (chromeHidden.validateCallbacks && !error) { 39 if (chromeHidden.validateCallbacks && !error) {
39 try { 40 try {
40 if (!request.callbackSchema.parameters) { 41 if (!request.callbackSchema.parameters) {
41 throw new Error("No callback schemas defined"); 42 throw new Error("No callback schemas defined");
42 } 43 }
43 44
44 if (request.callbackSchema.parameters.length > 1) { 45 if (request.callbackSchema.parameters.length > 1) {
45 throw new Error("Callbacks may only define one parameter"); 46 throw new Error("Callbacks may only define one parameter");
46 } 47 }
47 chromeHidden.validate(callbackArgs, 48 validate(callbackArgs, request.callbackSchema.parameters);
48 request.callbackSchema.parameters);
49 } catch (exception) { 49 } catch (exception) {
50 return "Callback validation error during " + name + " -- " + 50 return "Callback validation error during " + name + " -- " +
51 exception.stack; 51 exception.stack;
52 } 52 }
53 } 53 }
54 54
55 if (typeof(response) != "undefined") { 55 if (typeof(response) != "undefined") {
56 request.callback(callbackArgs[0]); 56 request.callback(callbackArgs[0]);
57 } else { 57 } else {
58 request.callback(); 58 request.callback();
59 } 59 }
60 } 60 }
61 } finally { 61 } finally {
62 delete requests[requestId]; 62 delete requests[requestId];
63 delete chrome.extension.lastError; 63 delete chrome.extension.lastError;
64 } 64 }
65 65
66 return undefined; 66 return undefined;
67 }; 67 };
68 68
69 function prepareRequest(args, argSchemas) { 69 function prepareRequest(args, argSchemas) {
70 var request = {}; 70 var request = {};
71 var argCount = args.length; 71 var argCount = args.length;
72 72
73 // Look for callback param. 73 // Look for callback param.
74 if (argSchemas.length > 0 && 74 if (argSchemas.length > 0 &&
75 args.length == argSchemas.length &&
76 argSchemas[argSchemas.length - 1].type == "function") { 75 argSchemas[argSchemas.length - 1].type == "function") {
77 request.callback = args[argSchemas.length - 1]; 76 request.callback = args[args.length - 1];
78 request.callbackSchema = argSchemas[argSchemas.length - 1]; 77 request.callbackSchema = argSchemas[argSchemas.length - 1];
79 --argCount; 78 --argCount;
80 } 79 }
81 80
82 request.args = []; 81 request.args = [];
83 for (var k = 0; k < argCount; k++) { 82 for (var k = 0; k < argCount; k++) {
84 request.args[k] = args[k]; 83 request.args[k] = args[k];
85 } 84 }
86 85
87 return request; 86 return request;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 var requestId = natives.GetNextRequestId(); 118 var requestId = natives.GetNextRequestId();
120 request.id = requestId; 119 request.id = requestId;
121 requests[requestId] = request; 120 requests[requestId] = request;
122 var hasCallback = 121 var hasCallback =
123 (request.callback || opt_args.customCallback) ? true : false; 122 (request.callback || opt_args.customCallback) ? true : false;
124 return nativeFunction(functionName, requestArgs, requestId, hasCallback, 123 return nativeFunction(functionName, requestArgs, requestId, hasCallback,
125 opt_args.forIOThread); 124 opt_args.forIOThread);
126 } 125 }
127 126
128 exports.sendRequest = sendRequest; 127 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