• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15import {RECORDING_V2_FLAG} from '../common/feature_flags';
16import {globals} from '../frontend/globals';
17
18import {Child, Controller, ControllerInitializerAny} from './controller';
19import {PermalinkController} from './permalink_controller';
20import {RecordController} from './record_controller';
21import {TraceController} from './trace_controller';
22
23// The root controller for the entire app. It handles the lifetime of all
24// the other controllers (e.g., track and query controllers) according to the
25// global state.
26export class AppController extends Controller<'main'> {
27  // extensionPort is needed for the RecordController to communicate with the
28  // extension through the frontend. This is because the controller is running
29  // on a worker, and isn't able to directly send messages to the extension.
30  private extensionPort: MessagePort;
31
32  constructor(extensionPort: MessagePort) {
33    super('main');
34    this.extensionPort = extensionPort;
35  }
36
37  // This is the root method that is called every time the controller tree is
38  // re-triggered. This can happen due to:
39  // - An action received from the frontend.
40  // - An internal promise of a nested controller being resolved and manually
41  //   re-triggering the controllers.
42  run() {
43    const childControllers: ControllerInitializerAny[] =
44        [Child('permalink', PermalinkController, {})];
45    if (!RECORDING_V2_FLAG.get()) {
46      childControllers.push(Child(
47          'record', RecordController, {extensionPort: this.extensionPort}));
48    }
49    if (globals.state.engine !== undefined) {
50      const engineCfg = globals.state.engine;
51      childControllers.push(Child(engineCfg.id, TraceController, engineCfg.id));
52    }
53    return childControllers;
54  }
55}
56