blob: bd832593c644233ce48a9f4066d8392bb94baa83 [file] [log] [blame]
Tim van der Lippe6d51bf02020-03-18 12:15:141/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31// Make sure that global references to Runtime still exist, which is
32// necessary for entrypoints such as the workers.
33import './root/root-legacy.js';
34
35import * as RootModule from './root/root.js';
36
37self.Runtime = self.Runtime || {};
38Runtime = Runtime || {};
39
40/**
41 * @type {!Object.<string, string>}
42 */
43self.Runtime.cachedResources = {
44 __proto__: null
45};
46
47self.Root = self.Root || {};
48Root = Root || {};
49
50// The following two variables are initialized in `build_release_applications`
51Root.allDescriptors = Root.allDescriptors || [];
52Root.applicationDescriptor = Root.applicationDescriptor || undefined;
53
54/** @type {Function} */
55let appStartedPromiseCallback;
56Runtime.appStarted = new Promise(fulfil => appStartedPromiseCallback = fulfil);
57
58/**
59 * @param {string} appName
60 * @return {!Promise.<void>}
61 */
62export async function startApplication(appName) {
63 console.timeStamp('Root.Runtime.startApplication');
64
65 /** @type {!Object<string, RootModule.Runtime.ModuleDescriptor>} */
66 const allDescriptorsByName = {};
67 for (let i = 0; i < Root.allDescriptors.length; ++i) {
68 const d = Root.allDescriptors[i];
69 allDescriptorsByName[d['name']] = d;
70 }
71
72 if (!Root.applicationDescriptor) {
73 let data = await RootModule.Runtime.loadResourcePromise(appName + '.json');
74 Root.applicationDescriptor = JSON.parse(data);
75 let descriptor = Root.applicationDescriptor;
76 while (descriptor.extends) {
77 data = await RootModule.Runtime.loadResourcePromise(descriptor.extends + '.json');
78 descriptor = JSON.parse(data);
79 Root.applicationDescriptor.modules = descriptor.modules.concat(Root.applicationDescriptor.modules);
80 }
81 }
82
83 const configuration = Root.applicationDescriptor.modules;
84 const moduleJSONPromises = [];
85 const coreModuleNames = [];
86 for (let i = 0; i < configuration.length; ++i) {
87 const descriptor = configuration[i];
88 const name = descriptor['name'];
89 const moduleJSON = allDescriptorsByName[name];
90 if (moduleJSON) {
91 moduleJSONPromises.push(Promise.resolve(moduleJSON));
92 } else {
93 moduleJSONPromises.push(
94 RootModule.Runtime.loadResourcePromise(name + '/module.json').then(JSON.parse.bind(JSON)));
95 }
96 if (descriptor['type'] === 'autostart') {
97 coreModuleNames.push(name);
98 }
99 }
100
101 const moduleDescriptors = await Promise.all(moduleJSONPromises);
102
103 for (let i = 0; i < moduleDescriptors.length; ++i) {
104 moduleDescriptors[i].name = configuration[i]['name'];
105 moduleDescriptors[i].condition = configuration[i]['condition'];
106 moduleDescriptors[i].remote = configuration[i]['type'] === 'remote';
107 }
108 self.runtime = RootModule.Runtime.Runtime.instance({forceNew: true, moduleDescriptors});
109 if (coreModuleNames) {
110 await self.runtime.loadAutoStartModules(coreModuleNames);
111 }
112 appStartedPromiseCallback();
113}
114
115/**
116 * @param {string} appName
117 * @return {!Promise.<void>}
118 */
119export async function startWorker(appName) {
120 return startApplication(appName).then(sendWorkerReady);
121
122 function sendWorkerReady() {
123 self.postMessage('workerReady');
124 }
125}