Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 1 | // Copyright (C) 2018 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | import {globals} from '../controller/globals'; |
Hector Dearman | 9565d12 | 2018-10-11 11:30:50 +0100 | [diff] [blame] | 16 | |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 17 | import {Child, Controller, ControllerInitializerAny} from './controller'; |
| 18 | import {PermalinkController} from './permalink_controller'; |
Hector Dearman | 9565d12 | 2018-10-11 11:30:50 +0100 | [diff] [blame] | 19 | import {RecordController} from './record_controller'; |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 20 | import {TraceController} from './trace_controller'; |
| 21 | |
| 22 | // The root controller for the entire app. It handles the lifetime of all |
| 23 | // the other controllers (e.g., track and query controllers) according to the |
| 24 | // global state. |
| 25 | export class AppController extends Controller<'main'> { |
Nicolò Mazzucato | 1ed43cb | 2019-07-30 16:25:17 +0100 | [diff] [blame] | 26 | // extensionPort is needed for the RecordController to communicate with the |
| 27 | // extension through the frontend. This is because the controller is running |
| 28 | // on a worker, and isn't able to directly send messages to the extension. |
| 29 | private extensionPort: MessagePort; |
| 30 | |
| 31 | constructor(extensionPort: MessagePort) { |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 32 | super('main'); |
Nicolò Mazzucato | 1ed43cb | 2019-07-30 16:25:17 +0100 | [diff] [blame] | 33 | this.extensionPort = extensionPort; |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // This is the root method that is called every time the controller tree is |
| 37 | // re-triggered. This can happen due to: |
| 38 | // - An action received from the frontend. |
| 39 | // - An internal promise of a nested controller being resolved and manually |
| 40 | // re-triggering the controllers. |
| 41 | run() { |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 42 | const childControllers: ControllerInitializerAny[] = [ |
| 43 | Child('permalink', PermalinkController, {}), |
Nicolò Mazzucato | 1ed43cb | 2019-07-30 16:25:17 +0100 | [diff] [blame] | 44 | Child( |
| 45 | 'record', |
| 46 | RecordController, |
| 47 | {app: globals, extensionPort: this.extensionPort}), |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 48 | ]; |
Deepanjan Roy | 435835d | 2018-09-25 15:24:59 -0400 | [diff] [blame] | 49 | for (const engineCfg of Object.values(globals.state.engines)) { |
| 50 | childControllers.push(Child(engineCfg.id, TraceController, engineCfg.id)); |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 51 | } |
| 52 | return childControllers; |
| 53 | } |
| 54 | } |