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 17 matching lines...) Expand all Loading... |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 // This gets all concatenated module descriptors in the release mode. | 31 // This gets all concatenated module descriptors in the release mode. |
32 var allDescriptors = []; | 32 var allDescriptors = []; |
33 var applicationDescriptor; | 33 var applicationDescriptor; |
34 var _loadedScripts = {}; | 34 var _loadedScripts = {}; |
35 | 35 |
36 /** | 36 /** |
37 * @param {string} url | 37 * @param {string} url |
38 * @return {string} | |
39 */ | |
40 function loadResource(url) | |
41 { | |
42 var xhr = new XMLHttpRequest(); | |
43 xhr.open("GET", url, false); | |
44 try { | |
45 xhr.send(null); | |
46 } catch (e) { | |
47 console.error(url + " -> " + new Error().stack); | |
48 throw e; | |
49 } | |
50 // xhr.status === 0 if loading from bundle. | |
51 return xhr.status < 400 ? xhr.responseText : ""; | |
52 } | |
53 | |
54 /** | |
55 * @param {string} url | |
56 * @return {!Promise.<string>} | 38 * @return {!Promise.<string>} |
57 */ | 39 */ |
58 function loadResourcePromise(url) | 40 function loadResourcePromise(url) |
59 { | 41 { |
60 return new Promise(load); | 42 return new Promise(load); |
61 | 43 |
62 /** | 44 /** |
63 * @param {function(?)} fulfill | 45 * @param {function(?)} fulfill |
64 * @param {function(*)} reject | 46 * @param {function(*)} reject |
65 */ | 47 */ |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 207 |
226 /** | 208 /** |
227 * @return {boolean} | 209 * @return {boolean} |
228 */ | 210 */ |
229 Runtime.isReleaseMode = function() | 211 Runtime.isReleaseMode = function() |
230 { | 212 { |
231 return !!allDescriptors.length; | 213 return !!allDescriptors.length; |
232 } | 214 } |
233 | 215 |
234 /** | 216 /** |
235 * @param {string} moduleName | |
236 * @param {string} workerName | |
237 * @return {!SharedWorker} | |
238 */ | |
239 Runtime.startSharedWorker = function(moduleName, workerName) | |
240 { | |
241 if (Runtime.isReleaseMode()) | |
242 return new SharedWorker(moduleName + "_module.js", workerName); | |
243 | |
244 var content = loadResource(moduleName + "/module.json"); | |
245 if (!content) | |
246 throw new Error("Worker is not defined: " + moduleName + " " + new Error
().stack); | |
247 var scripts = JSON.parse(content)["scripts"]; | |
248 if (scripts.length !== 1) | |
249 throw Error("Runtime.startSharedWorker supports modules with only one sc
ript!"); | |
250 return new SharedWorker(moduleName + "/" + scripts[0], workerName); | |
251 } | |
252 | |
253 /** | |
254 * @param {string} moduleName | |
255 * @return {!Worker} | |
256 */ | |
257 Runtime.startWorker = function(moduleName) | |
258 { | |
259 if (Runtime.isReleaseMode()) | |
260 return new Worker(moduleName + "_module.js"); | |
261 | |
262 var content = loadResource(moduleName + "/module.json"); | |
263 if (!content) | |
264 throw new Error("Worker is not defined: " + moduleName + " " + new Error
().stack); | |
265 var message = []; | |
266 var scripts = JSON.parse(content)["scripts"]; | |
267 for (var i = 0; i < scripts.length; ++i) { | |
268 var url = self._importScriptPathPrefix + moduleName + "/" + scripts[i]; | |
269 var parts = url.split("://"); | |
270 url = parts.length === 1 ? url : parts[0] + "://" + normalizePath(parts[
1]); | |
271 message.push({ | |
272 source: loadResource(moduleName + "/" + scripts[i]), | |
273 url: url | |
274 }); | |
275 } | |
276 | |
277 /** | |
278 * @suppress {checkTypes} | |
279 */ | |
280 var loader = function() { | |
281 self.onmessage = function(event) { | |
282 self.onmessage = null; | |
283 var scripts = event.data; | |
284 for (var i = 0; i < scripts.length; ++i) { | |
285 var source = scripts[i]["source"]; | |
286 self.eval(source + "\n//# sourceURL=" + scripts[i]["url"]); | |
287 } | |
288 }; | |
289 }; | |
290 | |
291 var blob = new Blob(["(" + loader.toString() + ")()\n//# sourceURL=" + modul
eName], { type: "text/javascript" }); | |
292 var workerURL = window.URL.createObjectURL(blob); | |
293 try { | |
294 var worker = new Worker(workerURL); | |
295 worker.postMessage(message); | |
296 return worker; | |
297 } finally { | |
298 window.URL.revokeObjectURL(workerURL); | |
299 } | |
300 } | |
301 | |
302 /** | |
303 * @param {string} appName | 217 * @param {string} appName |
304 */ | 218 */ |
305 Runtime.startApplication = function(appName) | 219 Runtime.startApplication = function(appName) |
306 { | 220 { |
307 console.timeStamp("Runtime.startApplication"); | 221 console.timeStamp("Runtime.startApplication"); |
308 | 222 |
309 var allDescriptorsByName = {}; | 223 var allDescriptorsByName = {}; |
310 for (var i = 0; Runtime.isReleaseMode() && i < allDescriptors.length; ++i) { | 224 for (var i = 0; Runtime.isReleaseMode() && i < allDescriptors.length; ++i) { |
311 var d = allDescriptors[i]; | 225 var d = allDescriptors[i]; |
312 allDescriptorsByName[d["name"]] = d; | 226 allDescriptorsByName[d["name"]] = d; |
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 | 632 |
719 if (this._pendingLoadPromise) | 633 if (this._pendingLoadPromise) |
720 return this._pendingLoadPromise; | 634 return this._pendingLoadPromise; |
721 | 635 |
722 var dependencies = this._descriptor.dependencies; | 636 var dependencies = this._descriptor.dependencies; |
723 var dependencyPromises = []; | 637 var dependencyPromises = []; |
724 for (var i = 0; dependencies && i < dependencies.length; ++i) | 638 for (var i = 0; dependencies && i < dependencies.length; ++i) |
725 dependencyPromises.push(this._manager._modulesMap[dependencies[i]]._
loadPromise()); | 639 dependencyPromises.push(this._manager._modulesMap[dependencies[i]]._
loadPromise()); |
726 | 640 |
727 this._pendingLoadPromise = Promise.all(dependencyPromises) | 641 this._pendingLoadPromise = Promise.all(dependencyPromises) |
| 642 .then(this._loadStylesheets.bind(this)) |
728 .then(this._loadScripts.bind(this)) | 643 .then(this._loadScripts.bind(this)) |
729 .then(markAsLoaded.bind(this)); | 644 .then(markAsLoaded.bind(this)); |
730 | 645 |
731 return this._pendingLoadPromise; | 646 return this._pendingLoadPromise; |
732 | 647 |
733 /** | 648 /** |
734 * @this {Runtime.Module} | 649 * @this {Runtime.Module} |
735 */ | 650 */ |
736 function markAsLoaded() | 651 function markAsLoaded() |
737 { | 652 { |
738 delete this._pendingLoadPromise; | 653 delete this._pendingLoadPromise; |
739 this._loaded = true; | 654 this._loaded = true; |
740 } | 655 } |
741 }, | 656 }, |
742 | 657 |
743 /** | 658 /** |
744 * @return {!Promise.<undefined>} | 659 * @return {!Promise.<undefined>} |
| 660 * @this {Runtime.Module} |
| 661 */ |
| 662 _loadStylesheets: function() |
| 663 { |
| 664 var stylesheets = this._descriptor["stylesheets"]; |
| 665 if (!stylesheets) |
| 666 return Promise.resolve(); |
| 667 var promises = []; |
| 668 for (var i = 0; i < stylesheets.length; ++i) { |
| 669 var url = this._modularizeURL(stylesheets[i]); |
| 670 promises.push(loadResourcePromise(url).then(cacheStylesheet.bind(thi
s, url), cacheStylesheet.bind(this, url, undefined))); |
| 671 } |
| 672 return Promise.all(promises).then(undefined); |
| 673 |
| 674 /** |
| 675 * @param {string} path |
| 676 * @param {string=} content |
| 677 */ |
| 678 function cacheStylesheet(path, content) |
| 679 { |
| 680 if (!content) { |
| 681 console.error("Failed to load stylesheet: " + path); |
| 682 return; |
| 683 } |
| 684 Runtime.cachedResources[path] = content; |
| 685 } |
| 686 }, |
| 687 |
| 688 /** |
| 689 * @return {!Promise.<undefined>} |
745 */ | 690 */ |
746 _loadScripts: function() | 691 _loadScripts: function() |
747 { | 692 { |
748 if (!this._descriptor.scripts) | 693 if (!this._descriptor.scripts) |
749 return Promise.resolve(undefined); | 694 return Promise.resolve(); |
750 | 695 |
751 if (Runtime.isReleaseMode()) | 696 if (Runtime.isReleaseMode()) |
752 return loadScriptsPromise([this._name + "_module.js"]); | 697 return loadScriptsPromise([this._name + "_module.js"]); |
753 | 698 |
754 return loadScriptsPromise(this._descriptor.scripts.map(modularizeURL, th
is)).catch(Runtime._reportError); | 699 return loadScriptsPromise(this._descriptor.scripts.map(this._modularizeU
RL, this)).catch(Runtime._reportError); |
755 | |
756 /** | |
757 * @param {string} scriptName | |
758 * @this {Runtime.Module} | |
759 */ | |
760 function modularizeURL(scriptName) | |
761 { | |
762 return this._name + "/" + scriptName; | |
763 } | |
764 }, | 700 }, |
765 | 701 |
766 /** | 702 /** |
| 703 * @param {string} resourceName |
| 704 */ |
| 705 _modularizeURL: function(resourceName) |
| 706 { |
| 707 return normalizePath(this._name + "/" + resourceName); |
| 708 }, |
| 709 |
| 710 /** |
767 * @param {string} className | 711 * @param {string} className |
768 * @return {?Object} | 712 * @return {?Object} |
769 */ | 713 */ |
770 _instance: function(className) | 714 _instance: function(className) |
771 { | 715 { |
772 if (className in this._instanceMap) | 716 if (className in this._instanceMap) |
773 return this._instanceMap[className]; | 717 return this._instanceMap[className]; |
774 | 718 |
775 var constructorFunction = window.eval(className); | 719 var constructorFunction = window.eval(className); |
776 if (!(constructorFunction instanceof Function)) { | 720 if (!(constructorFunction instanceof Function)) { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1047 } | 991 } |
1048 } | 992 } |
1049 })();} | 993 })();} |
1050 | 994 |
1051 | 995 |
1052 // This must be constructed after the query parameters have been parsed. | 996 // This must be constructed after the query parameters have been parsed. |
1053 Runtime.experiments = new Runtime.ExperimentsSupport(); | 997 Runtime.experiments = new Runtime.ExperimentsSupport(); |
1054 | 998 |
1055 /** @type {!Runtime} */ | 999 /** @type {!Runtime} */ |
1056 var runtime; | 1000 var runtime; |
OLD | NEW |