File: management_context_mixin_browser.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 3,986 bytes parent folder | download | duplicates (8)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2023 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/enterprise/test/browser/management_context_mixin_browser.h"

#include "build/branding_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/test/test_constants.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/enterprise/browser/enterprise_switches.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/proto/device_management_backend.pb.h"

namespace enterprise::test {

ManagementContextMixinBrowser::ManagementContextMixinBrowser(
    InProcessBrowserTestMixinHost* host,
    InProcessBrowserTest* test_base,
    ManagementContext management_context)
    : ManagementContextMixin(host, test_base, std::move(management_context)) {
  // Fake the OS' device ID.
  browser_dm_token_storage_.SetClientId(kBrowserClientId);

  if (management_context_.is_cloud_machine_managed) {
    management_context_.is_cloud_machine_managed = true;
    browser_dm_token_storage_.SetEnrollmentToken(kEnrollmentToken);
    browser_dm_token_storage_.SetDMToken(kBrowserDmToken);
  }
}

ManagementContextMixinBrowser::~ManagementContextMixinBrowser() = default;

void ManagementContextMixinBrowser::ManageCloudUser() {
  ManagementContextMixin::ManageCloudUser();

  auto policy_data = GetBaseUserPolicyData();
  policy_data->set_request_token(kProfileDmToken);
  policy_data->set_device_id(kProfileClientId);

  browser()
      ->profile()
      ->GetCloudPolicyManager()
      ->core()
      ->store()
      ->set_policy_data_for_testing(std::move(policy_data));

  auto client = std::make_unique<policy::MockCloudPolicyClient>();
  client->SetDMToken(kProfileDmToken);
  client->SetClientId(kProfileClientId);

  browser()->profile()->GetCloudPolicyManager()->Connect(
      g_browser_process->local_state(), std::move(client));
}

void ManagementContextMixinBrowser::SetUpOnMainThread() {
  ManagementContextMixin::SetUpOnMainThread();

  if (management_context_.is_cloud_machine_managed) {
    auto browser_policy_data =
        std::make_unique<enterprise_management::PolicyData>();
    browser_policy_data->set_obfuscated_customer_id(kFakeCustomerId);
    browser_policy_data->add_device_affiliation_ids(kFakeCustomerId);

    auto* browser_policy_manager =
        g_browser_process->browser_policy_connector()
            ->machine_level_user_cloud_policy_manager();
    browser_policy_manager->core()->store()->set_policy_data_for_testing(
        std::move(browser_policy_data));
  }

  if (management_context_.is_cloud_user_managed) {
    ManageCloudUser();
  }
}

#if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
void ManagementContextMixinBrowser::SetUpDefaultCommandLine(
    base::CommandLine* command_line) {
  ManagementContextMixin::SetUpDefaultCommandLine(command_line);
  command_line->AppendSwitch(::switches::kEnableChromeBrowserCloudManagement);
}
#endif

void ManagementContextMixinBrowser::SetCloudMachinePolicies(
    base::flat_map<std::string, std::optional<base::Value>> policy_entries) {
  CHECK(management_context_.is_cloud_machine_managed);
  policy::PolicyMap policy_map;

  for (auto& policy_entry : policy_entries) {
    policy_map.Set(policy_entry.first, policy::POLICY_LEVEL_MANDATORY,
                   policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_CLOUD,
                   std::move(policy_entry.second), nullptr);
  }

  MergeNewChromePolicies(policy_map);
}

}  // namespace enterprise::test