summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/dev_ui_loader/dev_ui_loader.js
blob: 61bc4c25be8509b78ac1bf7d04312c0275b90e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function() {
'use strict';

/** @return {!URL} */
function getRedirectUrl() {
  try {  // For potential TypeError from new URL().
    const urlString = new URL(location).searchParams.get('url');
    if (urlString) {
      const url = new URL(decodeURIComponent(urlString));
      // Perform basic filtering. Also skip 'dev-ui-loader' to avoid redirect
      // cycle (benign but bizarre). Note that |url.host| is always lowercase.
      if (url.protocol === 'chrome:' && url.host.match(/[a-z0-9_\-]+/) &&
          url.host !== 'dev-ui-loader') {
        return url;
      }
    }
  } catch (e) {
  }
  return new URL('chrome://chrome-urls');
}

function redirectToChromePage() {
  // Use replace() so the current page disappears from history.
  location.replace(getRedirectUrl());
}

function doInstall() {
  cr.sendWithPromise('installAndLoadDevUiDfm').then((response) => {
    const status = response[0];
    if (status === 'success' || status === 'noop') {
      redirectToChromePage();
    } else if (status === 'failure') {
      document.querySelector('#failure-message').hidden = false;
    }
  });
}

document.addEventListener('DOMContentLoaded', () => {
  cr.sendWithPromise('getDevUiDfmState').then((state) => {
    if (state === 'ready') {
      redirectToChromePage();
    } else if (state === 'not-installed' || 'not-loaded') {
      doInstall();
    }
  });
});
})();