OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 var sources = new Array(scriptNames.length); | 131 var sources = new Array(scriptNames.length); |
132 var scriptToEval = 0; | 132 var scriptToEval = 0; |
133 for (var i = 0; i < scriptNames.length; ++i) { | 133 for (var i = 0; i < scriptNames.length; ++i) { |
134 var scriptName = scriptNames[i]; | 134 var scriptName = scriptNames[i]; |
135 var sourceURL = self._importScriptPathPrefix + scriptName; | 135 var sourceURL = self._importScriptPathPrefix + scriptName; |
136 var schemaIndex = sourceURL.indexOf("://") + 3; | 136 var schemaIndex = sourceURL.indexOf("://") + 3; |
137 sourceURL = sourceURL.substring(0, schemaIndex) + normalizePath(sourceUR
L.substring(schemaIndex)); | 137 sourceURL = sourceURL.substring(0, schemaIndex) + normalizePath(sourceUR
L.substring(schemaIndex)); |
138 if (_loadedScripts[sourceURL]) | 138 if (_loadedScripts[sourceURL]) |
139 continue; | 139 continue; |
140 urls.push(sourceURL); | 140 urls.push(sourceURL); |
141 promises.push(loadResourcePromise(sourceURL).thenOrCatch(scriptSourceLoa
ded.bind(null, i))); | 141 promises.push(loadResourcePromise(sourceURL).then(scriptSourceLoaded.bin
d(null, i), scriptSourceLoaded.bind(null, i, undefined))); |
142 } | 142 } |
143 return Promise.all(promises).then(undefined); | 143 return Promise.all(promises).then(undefined); |
144 | 144 |
145 /** | 145 /** |
146 * @param {number} scriptNumber | 146 * @param {number} scriptNumber |
147 * @param {string=} scriptSource | 147 * @param {string=} scriptSource |
148 */ | 148 */ |
149 function scriptSourceLoaded(scriptNumber, scriptSource) | 149 function scriptSourceLoaded(scriptNumber, scriptSource) |
150 { | 150 { |
151 sources[scriptNumber] = scriptSource || ""; | 151 sources[scriptNumber] = scriptSource || ""; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 this._cachedTypeClasses = {}; | 203 this._cachedTypeClasses = {}; |
204 | 204 |
205 /** | 205 /** |
206 * @type {!Object.<string, !Runtime.ModuleDescriptor>} | 206 * @type {!Object.<string, !Runtime.ModuleDescriptor>} |
207 */ | 207 */ |
208 this._descriptorsMap = {}; | 208 this._descriptorsMap = {}; |
209 | 209 |
210 for (var i = 0; i < descriptors.length; ++i) | 210 for (var i = 0; i < descriptors.length; ++i) |
211 this._registerModule(descriptors[i]); | 211 this._registerModule(descriptors[i]); |
212 if (coreModuleNames) | 212 if (coreModuleNames) |
213 this._loadAutoStartModules(coreModuleNames).done(); | 213 this._loadAutoStartModules(coreModuleNames).catch(Runtime._reportError); |
214 } | 214 } |
215 | 215 |
216 /** | 216 /** |
217 * @type {!Object.<string, string>} | 217 * @type {!Object.<string, string>} |
218 */ | 218 */ |
219 Runtime._queryParamsObject = { __proto__: null }; | 219 Runtime._queryParamsObject = { __proto__: null }; |
220 | 220 |
221 /** | 221 /** |
222 * @type {!Object.<string, string>} | 222 * @type {!Object.<string, string>} |
223 */ | 223 */ |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 var name = descriptor["name"]; | 334 var name = descriptor["name"]; |
335 var moduleJSON = allDescriptorsByName[name]; | 335 var moduleJSON = allDescriptorsByName[name]; |
336 if (moduleJSON) | 336 if (moduleJSON) |
337 moduleJSONPromises.push(Promise.resolve(moduleJSON)); | 337 moduleJSONPromises.push(Promise.resolve(moduleJSON)); |
338 else | 338 else |
339 moduleJSONPromises.push(loadResourcePromise(name + "/module.json
").then(JSON.parse.bind(JSON))); | 339 moduleJSONPromises.push(loadResourcePromise(name + "/module.json
").then(JSON.parse.bind(JSON))); |
340 if (descriptor["type"] === "autostart") | 340 if (descriptor["type"] === "autostart") |
341 coreModuleNames.push(name); | 341 coreModuleNames.push(name); |
342 } | 342 } |
343 | 343 |
344 Promise.all(moduleJSONPromises).then(instantiateRuntime).done(); | 344 Promise.all(moduleJSONPromises).then(instantiateRuntime).catch(Runtime._
reportError); |
345 /** | 345 /** |
346 * @param {!Array.<!Object>} moduleDescriptors | 346 * @param {!Array.<!Object>} moduleDescriptors |
347 */ | 347 */ |
348 function instantiateRuntime(moduleDescriptors) | 348 function instantiateRuntime(moduleDescriptors) |
349 { | 349 { |
350 for (var i = 0; !Runtime.isReleaseMode() && i < moduleDescriptors.le
ngth; ++i) | 350 for (var i = 0; !Runtime.isReleaseMode() && i < moduleDescriptors.le
ngth; ++i) |
351 moduleDescriptors[i]["name"] = configuration[i]["name"]; | 351 moduleDescriptors[i]["name"] = configuration[i]["name"]; |
352 self.runtime = new Runtime(moduleDescriptors, coreModuleNames); | 352 self.runtime = new Runtime(moduleDescriptors, coreModuleNames); |
353 } | 353 } |
354 } | 354 } |
(...skipping 14 matching lines...) Expand all Loading... |
369 Runtime._experimentsSetting = function() | 369 Runtime._experimentsSetting = function() |
370 { | 370 { |
371 try { | 371 try { |
372 return /** @type {!Object} */ (JSON.parse(self.localStorage && self.loca
lStorage["experiments"] ? self.localStorage["experiments"] : "{}")); | 372 return /** @type {!Object} */ (JSON.parse(self.localStorage && self.loca
lStorage["experiments"] ? self.localStorage["experiments"] : "{}")); |
373 } catch (e) { | 373 } catch (e) { |
374 console.error("Failed to parse localStorage['experiments']"); | 374 console.error("Failed to parse localStorage['experiments']"); |
375 return {}; | 375 return {}; |
376 } | 376 } |
377 } | 377 } |
378 | 378 |
| 379 /** |
| 380 * @param {!Array.<!Promise.<T, !Error>>} promises |
| 381 * @return {!Promise.<!Array.<T>>} |
| 382 * @template T |
| 383 */ |
| 384 Runtime._some = function(promises) |
| 385 { |
| 386 var all = []; |
| 387 var wasRejected = []; |
| 388 for (var i = 0; i < promises.length; ++i) { |
| 389 // Workaround closure compiler bug. |
| 390 var handlerFunction = /** @type {function()} */ (handler.bind(promises[i
], i)); |
| 391 all.push(promises[i].catch(handlerFunction)); |
| 392 } |
| 393 |
| 394 return Promise.all(all).then(filterOutFailuresResults); |
| 395 |
| 396 /** |
| 397 * @param {!Array.<T>} results |
| 398 * @return {!Array.<T>} |
| 399 * @template T |
| 400 */ |
| 401 function filterOutFailuresResults(results) |
| 402 { |
| 403 var filtered = []; |
| 404 for (var i = 0; i < results.length; ++i) { |
| 405 if (!wasRejected[i]) |
| 406 filtered.push(results[i]); |
| 407 } |
| 408 return filtered; |
| 409 } |
| 410 |
| 411 /** |
| 412 * @this {!Promise} |
| 413 * @param {number} index |
| 414 * @param {!Error} e |
| 415 */ |
| 416 function handler(index, e) |
| 417 { |
| 418 wasRejected[index] = true; |
| 419 console.error(e.stack); |
| 420 } |
| 421 } |
| 422 |
| 423 Runtime._console = console; |
| 424 Runtime._originalAssert = console.assert; |
| 425 Runtime._assert = function(value, message) |
| 426 { |
| 427 if (value) |
| 428 return; |
| 429 Runtime._originalAssert.call(Runtime._console, value, message); |
| 430 } |
| 431 |
| 432 /** |
| 433 * @param {*} e |
| 434 */ |
| 435 Runtime._reportError = function(e) |
| 436 { |
| 437 if (e instanceof Error) |
| 438 console.error(e.stack); |
| 439 else |
| 440 console.error(e); |
| 441 } |
| 442 |
379 Runtime.prototype = { | 443 Runtime.prototype = { |
380 | 444 |
381 /** | 445 /** |
382 * @param {!Runtime.ModuleDescriptor} descriptor | 446 * @param {!Runtime.ModuleDescriptor} descriptor |
383 */ | 447 */ |
384 _registerModule: function(descriptor) | 448 _registerModule: function(descriptor) |
385 { | 449 { |
386 var module = new Runtime.Module(this, descriptor); | 450 var module = new Runtime.Module(this, descriptor); |
387 this._modules.push(module); | 451 this._modules.push(module); |
388 this._modulesMap[descriptor["name"]] = module; | 452 this._modulesMap[descriptor["name"]] = module; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 * @param {*} type | 594 * @param {*} type |
531 * @param {?Object=} context | 595 * @param {?Object=} context |
532 * @return {!Promise.<!Array.<!Object>>} | 596 * @return {!Promise.<!Array.<!Object>>} |
533 */ | 597 */ |
534 instancesPromise: function(type, context) | 598 instancesPromise: function(type, context) |
535 { | 599 { |
536 var extensions = this.extensions(type, context); | 600 var extensions = this.extensions(type, context); |
537 var promises = []; | 601 var promises = []; |
538 for (var i = 0; i < extensions.length; ++i) | 602 for (var i = 0; i < extensions.length; ++i) |
539 promises.push(extensions[i].instancePromise()); | 603 promises.push(extensions[i].instancePromise()); |
540 return Promise.some(promises); | 604 return Runtime._some(promises); |
541 }, | 605 }, |
542 | 606 |
543 /** | 607 /** |
544 * @param {*} type | 608 * @param {*} type |
545 * @param {?Object=} context | 609 * @param {?Object=} context |
546 * @return {!Promise.<!Object>} | 610 * @return {!Promise.<!Object>} |
547 */ | 611 */ |
548 instancePromise: function(type, context) | 612 instancePromise: function(type, context) |
549 { | 613 { |
550 var extension = this.extension(type, context); | 614 var extension = this.extension(type, context); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 * @return {!Promise.<undefined>} | 744 * @return {!Promise.<undefined>} |
681 */ | 745 */ |
682 _loadScripts: function() | 746 _loadScripts: function() |
683 { | 747 { |
684 if (!this._descriptor.scripts) | 748 if (!this._descriptor.scripts) |
685 return Promise.resolve(undefined); | 749 return Promise.resolve(undefined); |
686 | 750 |
687 if (Runtime.isReleaseMode()) | 751 if (Runtime.isReleaseMode()) |
688 return loadScriptsPromise([this._name + "_module.js"]); | 752 return loadScriptsPromise([this._name + "_module.js"]); |
689 | 753 |
690 return loadScriptsPromise(this._descriptor.scripts.map(modularizeURL, th
is)).catchAndReport(); | 754 return loadScriptsPromise(this._descriptor.scripts.map(modularizeURL, th
is)).catch(Runtime._reportError); |
691 | 755 |
692 /** | 756 /** |
693 * @param {string} scriptName | 757 * @param {string} scriptName |
694 * @this {Runtime.Module} | 758 * @this {Runtime.Module} |
695 */ | 759 */ |
696 function modularizeURL(scriptName) | 760 function modularizeURL(scriptName) |
697 { | 761 { |
698 return this._name + "/" + scriptName; | 762 return this._name + "/" + scriptName; |
699 } | 763 } |
700 }, | 764 }, |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 self.localStorage["experiments"] = JSON.stringify(value); | 910 self.localStorage["experiments"] = JSON.stringify(value); |
847 }, | 911 }, |
848 | 912 |
849 /** | 913 /** |
850 * @param {string} experimentName | 914 * @param {string} experimentName |
851 * @param {string} experimentTitle | 915 * @param {string} experimentTitle |
852 * @param {boolean=} hidden | 916 * @param {boolean=} hidden |
853 */ | 917 */ |
854 register: function(experimentName, experimentTitle, hidden) | 918 register: function(experimentName, experimentTitle, hidden) |
855 { | 919 { |
856 console.assert(!this._experimentNames[experimentName], "Duplicate regist
ration of experiment " + experimentName); | 920 Runtime._assert(!this._experimentNames[experimentName], "Duplicate regis
tration of experiment " + experimentName); |
857 this._experimentNames[experimentName] = true; | 921 this._experimentNames[experimentName] = true; |
858 this._experiments.push(new Runtime.Experiment(this, experimentName, expe
rimentTitle, !!hidden)); | 922 this._experiments.push(new Runtime.Experiment(this, experimentName, expe
rimentTitle, !!hidden)); |
859 }, | 923 }, |
860 | 924 |
861 /** | 925 /** |
862 * @param {string} experimentName | 926 * @param {string} experimentName |
863 * @return {boolean} | 927 * @return {boolean} |
864 */ | 928 */ |
865 isEnabled: function(experimentName) | 929 isEnabled: function(experimentName) |
866 { | 930 { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
916 cleanedUpExperimentSetting[experimentName] = true; | 980 cleanedUpExperimentSetting[experimentName] = true; |
917 } | 981 } |
918 this._setExperimentsSetting(cleanedUpExperimentSetting); | 982 this._setExperimentsSetting(cleanedUpExperimentSetting); |
919 }, | 983 }, |
920 | 984 |
921 /** | 985 /** |
922 * @param {string} experimentName | 986 * @param {string} experimentName |
923 */ | 987 */ |
924 _checkExperiment: function(experimentName) | 988 _checkExperiment: function(experimentName) |
925 { | 989 { |
926 console.assert(this._experimentNames[experimentName], "Unknown experimen
t " + experimentName); | 990 Runtime._assert(this._experimentNames[experimentName], "Unknown experime
nt " + experimentName); |
927 } | 991 } |
928 } | 992 } |
929 | 993 |
930 /** | 994 /** |
931 * @constructor | 995 * @constructor |
932 * @param {!Runtime.ExperimentsSupport} experiments | 996 * @param {!Runtime.ExperimentsSupport} experiments |
933 * @param {string} name | 997 * @param {string} name |
934 * @param {string} title | 998 * @param {string} title |
935 * @param {boolean} hidden | 999 * @param {boolean} hidden |
936 */ | 1000 */ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 try { | 1041 try { |
978 var settings = JSON.parse(window.decodeURI(settingsParam)); | 1042 var settings = JSON.parse(window.decodeURI(settingsParam)); |
979 for (var key in settings) | 1043 for (var key in settings) |
980 window.localStorage[key] = settings[key]; | 1044 window.localStorage[key] = settings[key]; |
981 } catch(e) { | 1045 } catch(e) { |
982 // Ignore malformed settings. | 1046 // Ignore malformed settings. |
983 } | 1047 } |
984 } | 1048 } |
985 })();} | 1049 })();} |
986 | 1050 |
987 /** | |
988 * @param {string} error | |
989 * @return {!Promise.<T>} | |
990 * @template T | |
991 */ | |
992 Promise.rejectWithError = function(error) | |
993 { | |
994 return Promise.reject(new Error(error)); | |
995 } | |
996 | |
997 /** | |
998 * @param {function((T|undefined))} callback | |
999 * @return {!Promise.<T>} | |
1000 * @template T | |
1001 */ | |
1002 Promise.prototype.thenOrCatch = function(callback) | |
1003 { | |
1004 return this.then(callback, reject.bind(this)); | |
1005 | |
1006 /** | |
1007 * @param {*} e | |
1008 */ | |
1009 function reject(e) | |
1010 { | |
1011 this._reportError(e); | |
1012 callback(undefined); | |
1013 } | |
1014 } | |
1015 | |
1016 Promise.prototype.done = function() | |
1017 { | |
1018 this.catchAndReport(); | |
1019 } | |
1020 | |
1021 Promise.prototype.catchAndReport = function() | |
1022 { | |
1023 return this.catch(this._reportError.bind(this)); | |
1024 } | |
1025 | |
1026 /** | |
1027 * @param {*} e | |
1028 */ | |
1029 Promise.prototype._reportError = function(e) | |
1030 { | |
1031 if (e instanceof Error) | |
1032 console.error(e.stack); | |
1033 else | |
1034 console.error(e); | |
1035 } | |
1036 | |
1037 /** | |
1038 * @param {!Array.<!Promise.<T, !Error>>} promises | |
1039 * @return {!Promise.<!Array.<T>>} | |
1040 * @template T | |
1041 */ | |
1042 Promise.some = function(promises) | |
1043 { | |
1044 var all = []; | |
1045 var wasRejected = []; | |
1046 for (var i = 0; i < promises.length; ++i) { | |
1047 // Workaround closure compiler bug. | |
1048 var handlerFunction = /** @type {function()} */ (handler.bind(promises[i
], i)); | |
1049 all.push(promises[i].catch(handlerFunction)); | |
1050 } | |
1051 | |
1052 return Promise.all(all).then(filterOutFailuresResults); | |
1053 | |
1054 /** | |
1055 * @param {!Array.<T>} results | |
1056 * @return {!Array.<T>} | |
1057 * @template T | |
1058 */ | |
1059 function filterOutFailuresResults(results) | |
1060 { | |
1061 var filtered = []; | |
1062 for (var i = 0; i < results.length; ++i) { | |
1063 if (!wasRejected[i]) | |
1064 filtered.push(results[i]); | |
1065 } | |
1066 return filtered; | |
1067 } | |
1068 | |
1069 /** | |
1070 * @this {!Promise} | |
1071 * @param {number} index | |
1072 * @param {!Error} e | |
1073 */ | |
1074 function handler(index, e) | |
1075 { | |
1076 wasRejected[index] = true; | |
1077 this._reportError(e); | |
1078 } | |
1079 } | |
1080 | |
1081 // FIXME: This performance optimization should be moved to blink so that all dev
elopers could enjoy it. | |
1082 // console is retrieved with V8Window.getAttribute method which is slow. Here we
copy it to a js variable for faster access. | |
1083 console = console; | |
1084 console.__originalAssert = console.assert; | |
1085 console.assert = function(value, message) | |
1086 { | |
1087 if (value) | |
1088 return; | |
1089 console.__originalAssert(value, message); | |
1090 } | |
1091 | 1051 |
1092 // This must be constructed after the query parameters have been parsed. | 1052 // This must be constructed after the query parameters have been parsed. |
1093 Runtime.experiments = new Runtime.ExperimentsSupport(); | 1053 Runtime.experiments = new Runtime.ExperimentsSupport(); |
1094 | 1054 |
1095 /** @type {!Runtime} */ | 1055 /** @type {!Runtime} */ |
1096 var runtime; | 1056 var runtime; |
OLD | NEW |