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

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

Issue 10392008: Move declarative API into events API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 7 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 // This script contains privileged chrome extension related javascript APIs. 5 // This script contains privileged chrome extension related javascript APIs.
6 // It is loaded by pages whose URL has the chrome-extension protocol. 6 // It is loaded by pages whose URL has the chrome-extension protocol.
7 7
8 require('json_schema'); 8 require('json_schema');
9 require('event_bindings'); 9 require('event_bindings');
10 var GetExtensionAPIDefinition = 10 var GetExtensionAPIDefinition =
(...skipping 11 matching lines...) Expand all
22 var internalAPIs = {}; 22 var internalAPIs = {};
23 chromeHidden.internalAPIs = internalAPIs; 23 chromeHidden.internalAPIs = internalAPIs;
24 24
25 function forEach(dict, f) { 25 function forEach(dict, f) {
26 for (key in dict) { 26 for (key in dict) {
27 if (dict.hasOwnProperty(key)) 27 if (dict.hasOwnProperty(key))
28 f(key, dict[key]); 28 f(key, dict[key]);
29 } 29 }
30 } 30 }
31 31
32 // Assuming |array_of_dictionaries| is structured like this:
33 // [{id: 1, ... }, {id: 2, ...}, ...], you can use
34 // lookup(array_of_dictionaries, 'id', 2) to get the dictionary with id == 2.
35 chromeHidden.lookup = function(array_of_dictionaries, field, value) {
36 var filter = function (dict) {return dict[field] == value;};
37 var matches = array_of_dictionaries.filter(filter);
38 if (matches.length == 0) {
39 return undefined;
40 } else if (matches.length == 1) {
41 return matches[0]
42 } else {
43 throw new Error("Failed lookup of field '" + field + "' with value '" +
44 value + "'");
battre 2012/05/09 16:28:08 TODO(battre): update error message.
45 }
not at google - send to devlin 2012/05/10 09:01:25 hmmmm I want to avoid putting random utilities on
battre 2012/05/10 16:40:29 I like that idea very much. Done.
46 }
47
32 // Validate arguments. 48 // Validate arguments.
33 var schemaValidator = new chromeHidden.JSONSchemaValidator(); 49 var schemaValidator = new chromeHidden.JSONSchemaValidator();
34 chromeHidden.validate = function(args, parameterSchemas) { 50 chromeHidden.validate = function(args, parameterSchemas) {
35 if (args.length > parameterSchemas.length) 51 if (args.length > parameterSchemas.length)
36 throw new Error("Too many arguments."); 52 throw new Error("Too many arguments.");
37 53
38 for (var i = 0; i < parameterSchemas.length; i++) { 54 for (var i = 0; i < parameterSchemas.length; i++) {
39 if (i in args && args[i] !== null && args[i] !== undefined) { 55 if (i in args && args[i] !== null && args[i] !== undefined) {
40 schemaValidator.resetErrors(); 56 schemaValidator.resetErrors();
41 schemaValidator.validate(args[i], parameterSchemas[i]); 57 schemaValidator.validate(args[i], parameterSchemas[i]);
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 manifestVersion) { 368 manifestVersion) {
353 var apiDefinitions = GetExtensionAPIDefinition(); 369 var apiDefinitions = GetExtensionAPIDefinition();
354 370
355 // Read api definitions and setup api functions in the chrome namespace. 371 // Read api definitions and setup api functions in the chrome namespace.
356 // TODO(rafaelw): Consider defining a json schema for an api definition 372 // TODO(rafaelw): Consider defining a json schema for an api definition
357 // and validating either here, in a unit_test or both. 373 // and validating either here, in a unit_test or both.
358 // TODO(rafaelw): Handle synchronous functions. 374 // TODO(rafaelw): Handle synchronous functions.
359 // TODO(rafaelw): Consider providing some convenient override points 375 // TODO(rafaelw): Consider providing some convenient override points
360 // for api functions that wish to insert themselves into the call. 376 // for api functions that wish to insert themselves into the call.
361 var platform = getPlatform(); 377 var platform = getPlatform();
378 var eventsSchema = chromeHidden.lookup(apiDefinitions,
379 'namespace', 'events');
not at google - send to devlin 2012/05/10 09:01:25 nit: arguments 1 line or vertical thing
362 380
363 apiDefinitions.forEach(function(apiDef) { 381 apiDefinitions.forEach(function(apiDef) {
364 // TODO(kalman): Remove this, or refactor schema_generated_bindings.js so 382 // TODO(kalman): Remove this, or refactor schema_generated_bindings.js so
365 // that it isn't necessary. For now, chrome.app is entirely handwritten. 383 // that it isn't necessary. For now, chrome.app is entirely handwritten.
366 if (apiDef.namespace === 'app') 384 if (apiDef.namespace === 'app')
367 return; 385 return;
368 386
369 if (!isSchemaNodeSupported(apiDef, platform, manifestVersion)) 387 if (!isSchemaNodeSupported(apiDef, platform, manifestVersion))
370 return; 388 return;
371 389
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 if (!isSchemaAccessAllowed(eventDef)) { 506 if (!isSchemaAccessAllowed(eventDef)) {
489 addUnprivilegedAccessGetter(mod, eventDef.name); 507 addUnprivilegedAccessGetter(mod, eventDef.name);
490 return; 508 return;
491 } 509 }
492 510
493 var eventName = apiDef.namespace + "." + eventDef.name; 511 var eventName = apiDef.namespace + "." + eventDef.name;
494 var customEvent = customEvents[apiDef.namespace]; 512 var customEvent = customEvents[apiDef.namespace];
495 if (customEvent) { 513 if (customEvent) {
496 mod[eventDef.name] = new customEvent( 514 mod[eventDef.name] = new customEvent(
497 eventName, eventDef.parameters, eventDef.extraParameters, 515 eventName, eventDef.parameters, eventDef.extraParameters,
498 eventDef.options); 516 eventDef.options, eventsSchema);
499 } else if (eventDef.anonymous) { 517 } else if (eventDef.anonymous) {
500 mod[eventDef.name] = new chrome.Event(); 518 mod[eventDef.name] = new chrome.Event();
501 } else { 519 } else {
502 mod[eventDef.name] = new chrome.Event( 520 mod[eventDef.name] = new chrome.Event(
503 eventName, eventDef.parameters, eventDef.options); 521 eventName, eventDef.parameters, eventDef.options, eventsSchema);
504 } 522 }
505 }); 523 });
506 } 524 }
507 525
508 function addProperties(m, parentDef) { 526 function addProperties(m, parentDef) {
509 var properties = parentDef.properties; 527 var properties = parentDef.properties;
510 if (!properties) 528 if (!properties)
511 return; 529 return;
512 530
513 forEach(properties, function(propertyName, propertyDef) { 531 forEach(properties, function(propertyName, propertyDef) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 // beginInstallWithManifest2. 601 // beginInstallWithManifest2.
584 // See http://crbug.com/100242 602 // See http://crbug.com/100242
585 if (chrome.webstorePrivate) { 603 if (chrome.webstorePrivate) {
586 chrome.webstorePrivate.beginInstallWithManifest2 = 604 chrome.webstorePrivate.beginInstallWithManifest2 =
587 chrome.webstorePrivate.beginInstallWithManifest3; 605 chrome.webstorePrivate.beginInstallWithManifest3;
588 } 606 }
589 607
590 if (chrome.test) 608 if (chrome.test)
591 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; 609 chrome.test.getApiDefinitions = GetExtensionAPIDefinition;
592 }); 610 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698