blob: 0f11d0f4cba3c34d1423e051351669e112d348c0 [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
Tim van der Lippe3387bd42020-10-20 11:46:3131import * as RootModule from '../root/root.js';
Tim van der Lippe6d51bf02020-03-18 12:15:1432
Tim van der Lippe3387bd42020-10-20 11:46:3133// Legacy runtime namespace definitions
34// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1435self.Runtime = self.Runtime || {};
Tim van der Lippe3387bd42020-10-20 11:46:3136// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1437Runtime = Runtime || {};
Tim van der Lippe6d51bf02020-03-18 12:15:1438// The following two variables are initialized in `build_release_applications`
Tim van der Lippe3387bd42020-10-20 11:46:3139// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1440Root.allDescriptors = Root.allDescriptors || [];
Tim van der Lippe3387bd42020-10-20 11:46:3141// @ts-ignore
Tim van der Lippe6d51bf02020-03-18 12:15:1442Root.applicationDescriptor = Root.applicationDescriptor || undefined;
43
Tim van der Lippe6d51bf02020-03-18 12:15:1444/**
45 * @param {string} appName
46 * @return {!Promise.<void>}
47 */
48export async function startApplication(appName) {
49 console.timeStamp('Root.Runtime.startApplication');
50
51 /** @type {!Object<string, RootModule.Runtime.ModuleDescriptor>} */
52 const allDescriptorsByName = {};
53 for (let i = 0; i < Root.allDescriptors.length; ++i) {
54 const d = Root.allDescriptors[i];
55 allDescriptorsByName[d['name']] = d;
56 }
57
Tim van der Lippe6d51bf02020-03-18 12:15:1458 const configuration = Root.applicationDescriptor.modules;
Tim van der Lippef866e2b2020-09-14 14:49:1359 const moduleDescriptors = [];
Tim van der Lippe6d51bf02020-03-18 12:15:1460 const coreModuleNames = [];
61 for (let i = 0; i < configuration.length; ++i) {
62 const descriptor = configuration[i];
63 const name = descriptor['name'];
Tim van der Lippef866e2b2020-09-14 14:49:1364 moduleDescriptors.push(allDescriptorsByName[name]);
Tim van der Lippe6d51bf02020-03-18 12:15:1465 if (descriptor['type'] === 'autostart') {
66 coreModuleNames.push(name);
67 }
68 }
69
Tim van der Lippe6d51bf02020-03-18 12:15:1470 for (let i = 0; i < moduleDescriptors.length; ++i) {
71 moduleDescriptors[i].name = configuration[i]['name'];
72 moduleDescriptors[i].condition = configuration[i]['condition'];
Tim van der Lippe6d51bf02020-03-18 12:15:1473 }
Tim van der Lippe7f2002c2020-09-28 14:54:0174 const runtimeInstance = RootModule.Runtime.Runtime.instance({forceNew: true, moduleDescriptors});
Tim van der Lippe3387bd42020-10-20 11:46:3175 // @ts-ignore Exposed for legacy layout tests
Tim van der Lippe7f2002c2020-09-28 14:54:0176 self.runtime = runtimeInstance;
Tim van der Lippe6d51bf02020-03-18 12:15:1477 if (coreModuleNames) {
Tim van der Lippe7f2002c2020-09-28 14:54:0178 await runtimeInstance.loadAutoStartModules(coreModuleNames);
Tim van der Lippe6d51bf02020-03-18 12:15:1479 }
Tim van der Lippe1ad9ada2020-11-04 17:08:4280 RootModule.Runtime.appStartedPromiseCallback();
Tim van der Lippe6d51bf02020-03-18 12:15:1481}
82
83/**
84 * @param {string} appName
85 * @return {!Promise.<void>}
86 */
87export async function startWorker(appName) {
88 return startApplication(appName).then(sendWorkerReady);
89
90 function sendWorkerReady() {
91 self.postMessage('workerReady');
92 }
93}