OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 var chrome = chrome || {}; | 8 var chrome = chrome || {}; |
9 (function() { | 9 (function() { |
10 native function GetExtensionAPIDefinition(); | 10 native function GetExtensionAPIDefinition(); |
(...skipping 22 matching lines...) Expand all Loading... |
33 } | 33 } |
34 chrome.initExtension(extensionId, false); | 34 chrome.initExtension(extensionId, false); |
35 }); | 35 }); |
36 return; | 36 return; |
37 } | 37 } |
38 | 38 |
39 if (!chrome) | 39 if (!chrome) |
40 chrome = {}; | 40 chrome = {}; |
41 | 41 |
42 // Validate arguments. | 42 // Validate arguments. |
43 chromeHidden.validationTypes = []; | 43 chromeHidden.validationTypes = {}; |
44 chromeHidden.validate = function(args, schemas) { | 44 chromeHidden.validate = function(args, schemas) { |
45 if (args.length > schemas.length) | 45 if (args.length > schemas.length) |
46 throw new Error("Too many arguments."); | 46 throw new Error("Too many arguments."); |
47 | 47 |
48 for (var i = 0; i < schemas.length; i++) { | 48 for (var i = 0; i < schemas.length; i++) { |
49 if (i in args && args[i] !== null && args[i] !== undefined) { | 49 if (i in args && args[i] !== null && args[i] !== undefined) { |
50 var validator = new chromeHidden.JSONSchemaValidator(); | 50 var validator = new chromeHidden.JSONSchemaValidator(); |
51 validator.addTypes(chromeHidden.validationTypes); | 51 forEach(chromeHidden.validationTypes, function(id, type) { |
| 52 validator.addTypes(type); |
| 53 }); |
52 validator.validate(args[i], schemas[i]); | 54 validator.validate(args[i], schemas[i]); |
53 if (validator.errors.length == 0) | 55 if (validator.errors.length == 0) |
54 continue; | 56 continue; |
55 | 57 |
56 var message = "Invalid value for argument " + (i + 1) + ". "; | 58 var message = "Invalid value for argument " + (i + 1) + ". "; |
57 for (var i = 0, err; err = validator.errors[i]; i++) { | 59 for (var i = 0, err; err = validator.errors[i]; i++) { |
58 if (err.path) { | 60 if (err.path) { |
59 message += "Property '" + err.path + "': "; | 61 message += "Property '" + err.path + "': "; |
60 } | 62 } |
61 message += err.message; | 63 message += err.message; |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 var callback = function(errorMessage) { | 361 var callback = function(errorMessage) { |
360 if (errorMessage) | 362 if (errorMessage) |
361 chrome.experimental.tts.speakCompleted(requestId, errorMessage); | 363 chrome.experimental.tts.speakCompleted(requestId, errorMessage); |
362 else | 364 else |
363 chrome.experimental.tts.speakCompleted(requestId); | 365 chrome.experimental.tts.speakCompleted(requestId); |
364 }; | 366 }; |
365 chrome.Event.prototype.dispatch.apply(this, [text, options, callback]); | 367 chrome.Event.prototype.dispatch.apply(this, [text, options, callback]); |
366 }; | 368 }; |
367 } | 369 } |
368 | 370 |
| 371 function forEach(dict, f) { |
| 372 for (key in dict) { |
| 373 if (dict.hasOwnProperty(key)) |
| 374 f(key, dict[key]); |
| 375 } |
| 376 } |
| 377 |
| 378 function setupPreferences() { |
| 379 // We declare the constructor so that a Preference shows up as such in the |
| 380 // Javascript console, but build the Preference in a separate function so |
| 381 // that an extension can't construct arbitrary Preference objects. |
| 382 function Preference() { |
| 383 }; |
| 384 |
| 385 var parameters = {}; |
| 386 chromeHidden.validationTypes['Preference'].functions.forEach(function(f) { |
| 387 parameters[f.name] = f.parameters; |
| 388 }); |
| 389 |
| 390 function buildPreference(prefKey, valueSchema) { |
| 391 var pref = new Preference(); |
| 392 var getSchema = parameters.get; |
| 393 var extendedGetSchema = getSchema.slice(); |
| 394 extendedGetSchema.unshift({'type': 'string'}); |
| 395 pref.get = function(details, callback) { |
| 396 chromeHidden.validate([details, callback], getSchema); |
| 397 return sendRequest('experimental.preferences.get', |
| 398 [prefKey, details, callback], |
| 399 extendedGetSchema); |
| 400 }; |
| 401 var setSchema = parameters.set; |
| 402 var extendedSetSchema = setSchema.slice(); |
| 403 extendedSetSchema.unshift({'type': 'string'}); |
| 404 extendedSetSchema[1].properties.value = valueSchema; |
| 405 pref.set = function(details, callback) { |
| 406 chromeHidden.validate([details, callback], setSchema); |
| 407 return sendRequest('experimental.preferences.set', |
| 408 [prefKey, details, callback], |
| 409 extendedSetSchema); |
| 410 }; |
| 411 return pref; |
| 412 } |
| 413 |
| 414 // TODO(bauerb): Automatically instantiate these preferences |
| 415 // from chrome/common/extensions/api/extension_api.json. |
| 416 chrome.experimental.contentSettings.misc.blockThirdPartyCookies = |
| 417 buildPreference('blockThirdPartyCookies', {'type':'boolean'}); |
| 418 } |
| 419 |
369 chromeHidden.onLoad.addListener(function (extensionId) { | 420 chromeHidden.onLoad.addListener(function (extensionId) { |
370 if (!extensionId) { | 421 if (!extensionId) { |
371 return; | 422 return; |
372 } | 423 } |
373 chrome.initExtension(extensionId, false, IsIncognitoProcess()); | 424 chrome.initExtension(extensionId, false, IsIncognitoProcess()); |
374 | 425 |
375 // |apiFunctions| is a hash of name -> object that stores the | 426 // |apiFunctions| is a hash of name -> object that stores the |
376 // name & definition of the apiFunction. Custom handling of api functions | 427 // name & definition of the apiFunction. Custom handling of api functions |
377 // is implemented by adding a "handleRequest" function to the object. | 428 // is implemented by adding a "handleRequest" function to the object. |
378 var apiFunctions = {}; | 429 var apiFunctions = {}; |
(...skipping 10 matching lines...) Expand all Loading... |
389 var module = chrome; | 440 var module = chrome; |
390 var namespaces = apiDef.namespace.split('.'); | 441 var namespaces = apiDef.namespace.split('.'); |
391 for (var index = 0, name; name = namespaces[index]; index++) { | 442 for (var index = 0, name; name = namespaces[index]; index++) { |
392 module[name] = module[name] || {}; | 443 module[name] = module[name] || {}; |
393 module = module[name]; | 444 module = module[name]; |
394 } | 445 } |
395 | 446 |
396 // Add types to global validationTypes | 447 // Add types to global validationTypes |
397 if (apiDef.types) { | 448 if (apiDef.types) { |
398 apiDef.types.forEach(function(t) { | 449 apiDef.types.forEach(function(t) { |
399 chromeHidden.validationTypes.push(t); | 450 chromeHidden.validationTypes[t.id] = t; |
400 }); | 451 }); |
401 } | 452 } |
402 | 453 |
403 // Setup Functions. | 454 // Setup Functions. |
404 if (apiDef.functions) { | 455 if (apiDef.functions) { |
405 apiDef.functions.forEach(function(functionDef) { | 456 apiDef.functions.forEach(function(functionDef) { |
406 // Module functions may have been defined earlier by hand. Don't | 457 // Module functions may have been defined earlier by hand. Don't |
407 // clobber them. | 458 // clobber them. |
408 if (module[functionDef.name]) | 459 if (module[functionDef.name]) |
409 return; | 460 return; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 | 502 |
452 var eventName = apiDef.namespace + "." + eventDef.name; | 503 var eventName = apiDef.namespace + "." + eventDef.name; |
453 module[eventDef.name] = new chrome.Event(eventName, | 504 module[eventDef.name] = new chrome.Event(eventName, |
454 eventDef.parameters); | 505 eventDef.parameters); |
455 }); | 506 }); |
456 } | 507 } |
457 | 508 |
458 | 509 |
459 // Parse any values defined for properties. | 510 // Parse any values defined for properties. |
460 if (apiDef.properties) { | 511 if (apiDef.properties) { |
461 for (var prop in apiDef.properties) { | 512 forEach(apiDef.properties, function(prop, property) { |
462 if (!apiDef.properties.hasOwnProperty(prop)) | |
463 continue; | |
464 | |
465 var property = apiDef.properties[prop]; | |
466 if (property.value) { | 513 if (property.value) { |
467 var value = property.value; | 514 var value = property.value; |
468 if (property.type === 'integer') { | 515 if (property.type === 'integer') { |
469 value = parseInt(value); | 516 value = parseInt(value); |
470 } else if (property.type === 'boolean') { | 517 } else if (property.type === 'boolean') { |
471 value = value === "true"; | 518 value = value === "true"; |
472 } else if (property.type !== 'string') { | 519 } else if (property.type !== 'string') { |
473 throw "NOT IMPLEMENTED (extension_api.json error): Cannot " + | 520 throw "NOT IMPLEMENTED (extension_api.json error): Cannot " + |
474 "parse values for type \"" + property.type + "\""; | 521 "parse values for type \"" + property.type + "\""; |
475 } | 522 } |
476 module[prop] = value; | 523 module[prop] = value; |
477 } | 524 } |
478 } | 525 }); |
479 } | 526 } |
480 | 527 |
481 // getTabContentses is retained for backwards compatibility | 528 // getTabContentses is retained for backwards compatibility |
482 // See http://crbug.com/21433 | 529 // See http://crbug.com/21433 |
483 chrome.extension.getTabContentses = chrome.extension.getExtensionTabs; | 530 chrome.extension.getTabContentses = chrome.extension.getExtensionTabs; |
484 }); | 531 }); |
485 | 532 |
486 apiFunctions["tabs.connect"].handleRequest = function(tabId, connectInfo) { | 533 apiFunctions["tabs.connect"].handleRequest = function(tabId, connectInfo) { |
487 var name = ""; | 534 var name = ""; |
488 if (connectInfo) { | 535 if (connectInfo) { |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 if (chrome.test) { | 838 if (chrome.test) { |
792 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; | 839 chrome.test.getApiDefinitions = GetExtensionAPIDefinition; |
793 } | 840 } |
794 | 841 |
795 setupPageActionEvents(extensionId); | 842 setupPageActionEvents(extensionId); |
796 setupToolstripEvents(GetRenderViewId()); | 843 setupToolstripEvents(GetRenderViewId()); |
797 setupPopupEvents(GetRenderViewId()); | 844 setupPopupEvents(GetRenderViewId()); |
798 setupHiddenContextMenuEvent(extensionId); | 845 setupHiddenContextMenuEvent(extensionId); |
799 setupOmniboxEvents(); | 846 setupOmniboxEvents(); |
800 setupTtsEvents(); | 847 setupTtsEvents(); |
| 848 setupPreferences(); |
801 }); | 849 }); |
802 | 850 |
803 if (!chrome.experimental) | 851 if (!chrome.experimental) |
804 chrome.experimental = {}; | 852 chrome.experimental = {}; |
805 | 853 |
806 if (!chrome.experimental.accessibility) | 854 if (!chrome.experimental.accessibility) |
807 chrome.experimental.accessibility = {}; | 855 chrome.experimental.accessibility = {}; |
808 | 856 |
809 if (!chrome.experimental.tts) | 857 if (!chrome.experimental.tts) |
810 chrome.experimental.tts = {}; | 858 chrome.experimental.tts = {}; |
811 })(); | 859 })(); |
OLD | NEW |