1/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {globalConfig} from 'common/global_config';
18
19export class OriginAllowList {
20  private static readonly ALLOW_LIST_PROD = [
21    new RegExp('^https://([^\\/]*\\.)*googleplex\\.com$'),
22    new RegExp('^https://([^\\/]*\\.)*google\\.com$'),
23    new RegExp('^https://([^\\/]*\\.)*perfetto\\.dev$'),
24  ];
25
26  private static readonly ALLOW_LIST_DEV = [
27    ...OriginAllowList.ALLOW_LIST_PROD,
28    new RegExp('^(http|https)://localhost:8081$'), // remote tool mock
29  ];
30
31  static isAllowed(originUrl: string, mode = globalConfig.MODE): boolean {
32    const list = OriginAllowList.getList(mode);
33
34    for (const regex of list) {
35      if (regex.test(originUrl)) {
36        return true;
37      }
38    }
39
40    return false;
41  }
42
43  private static getList(mode: typeof globalConfig.MODE): RegExp[] {
44    switch (mode) {
45      case 'DEV':
46        return OriginAllowList.ALLOW_LIST_DEV;
47      case 'KARMA_TEST':
48        return OriginAllowList.ALLOW_LIST_DEV;
49      case 'PROD':
50        return OriginAllowList.ALLOW_LIST_PROD;
51      default:
52        throw new Error(`Unhandled mode: ${globalConfig.MODE}`);
53    }
54  }
55}
56