File: browser_switcher_policy_migrator.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (91 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download | duplicates (7)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/browser_switcher/browser_switcher_policy_migrator.h"

#include <string>
#include <utility>

#include "base/functional/bind.h"
#include "base/logging.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_namespace.h"
#include "components/policy/policy_constants.h"
#include "extensions/common/hashed_extension_id.h"

namespace browser_switcher {

namespace {

void SecondsToMilliseconds(base::Value* val) {
  const int ms_per_second = 1000;
  *val = base::Value(val->GetInt() * ms_per_second);
}

// Transforms the string policy to a list policy (containing 1 string).
//
// The LBS extension's command-line parameter policies are single strings,
// because on Windows the command-line parameters are passed as a single string
// to the program. The parameters are parsed by the program, not the shell.
//
// On other platforms though, parameter parsing is done by the shell, not the
// program. So the new policies are string-lists that are given to the program
// pre-parsed. This is why we need to convert the string to a list, when
// migrating from the old policy.
void StringToList(base::Value* val) {
  base::Value::List list;
  list.Append(val->GetString());
  *val = base::Value(std::move(list));
}

}  // namespace

const char kLBSExtensionId[] = "heildphpnddilhkemkielfhnkaagiabh";

BrowserSwitcherPolicyMigrator::BrowserSwitcherPolicyMigrator() = default;
BrowserSwitcherPolicyMigrator::~BrowserSwitcherPolicyMigrator() = default;

void BrowserSwitcherPolicyMigrator::Migrate(policy::PolicyBundle* bundle) {
  VLOG(3) << "Migrating Legacy Browser Support extension policies...";

  policy::PolicyMap& extension_map = bundle->Get(policy::PolicyNamespace(
      policy::POLICY_DOMAIN_EXTENSIONS, kLBSExtensionId));
  policy::PolicyMap& chrome_map =
      bundle->Get(policy::PolicyNamespace(policy::POLICY_DOMAIN_CHROME, ""));

  const auto* entry = chrome_map.Get("BrowserSwitcherEnabled");
  if (!entry || !entry->value(base::Value::Type::BOOLEAN) ||
      !entry->value(base::Value::Type::BOOLEAN)->GetBool()) {
    VLOG(3) << "BrowserSwitcherEnabled is false, aborting policy migration.";
    return;
  }
  extension_map.Set("browser_switcher_enabled", entry->DeepCopy());

  using Migration = policy::PolicyMigrator::Migration;
  const Migration migrations[] = {
      Migration("alternative_browser_path",
                policy::key::kAlternativeBrowserPath),
      Migration("chrome_path", policy::key::kBrowserSwitcherChromePath),
      Migration("url_list", policy::key::kBrowserSwitcherUrlList),
      Migration("url_greylist", policy::key::kBrowserSwitcherUrlGreylist),
      Migration("keep_last_chrome_tab",
                policy::key::kBrowserSwitcherKeepLastChromeTab),
      Migration("use_ie_site_list", policy::key::kBrowserSwitcherUseIeSitelist),
      Migration("show_transition_screen", policy::key::kBrowserSwitcherDelay,
                base::BindRepeating(&SecondsToMilliseconds)),
      Migration("chrome_arguments",
                policy::key::kBrowserSwitcherChromeParameters,
                base::BindRepeating(&StringToList)),
      Migration("alternative_browser_arguments",
                policy::key::kAlternativeBrowserParameters,
                base::BindRepeating(&StringToList)),
  };

  CopyPoliciesIfUnset(bundle,
                      extensions::HashedExtensionId(kLBSExtensionId).value(),
                      migrations);
}

}  // namespace browser_switcher