blob: 366d941f634e5d4bbd4c33f97588371edc0bef53 [file] [log] [blame]
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:271// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/managed_ui.h"
6
Nicolas Ouellet-payeur2bacf202019-05-10 20:20:227#include "base/strings/utf_string_conversions.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:278#include "chrome/browser/browser_process.h"
9#include "chrome/browser/browser_process_platform_part.h"
10#include "chrome/browser/policy/chrome_browser_policy_connector.h"
11#include "chrome/browser/policy/profile_policy_connector.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2712#include "chrome/browser/profiles/profile.h"
Nicolas Ouellet-payeur2bacf202019-05-10 20:20:2213#include "chrome/browser/ui/webui/management_ui_handler.h"
Nicolas Ouellet-payeur2bacf202019-05-10 20:20:2214#include "chrome/grit/generated_resources.h"
15#include "ui/base/l10n/l10n_util.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2716
17#if defined(OS_CHROMEOS)
Nicolas Ouellet-payeur81bdcc52019-01-24 21:20:4218#include "chrome/browser/chromeos/login/demo_mode/demo_session.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2719#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
20#include "chrome/browser/chromeos/profiles/profile_helper.h"
21#include "components/user_manager/user_manager.h"
Nicolas Ouellet-payeur2bacf202019-05-10 20:20:2222#include "ui/chromeos/devicetype_utils.h"
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2723#endif
24
25namespace chrome {
26
27bool ShouldDisplayManagedUi(Profile* profile) {
Nicolas Ouellet-payeur81bdcc52019-01-24 21:20:4228#if defined(OS_CHROMEOS)
29 // Don't show the UI in demo mode.
30 if (chromeos::DemoSession::IsDeviceInDemoMode())
31 return false;
Nicolas Ouellet-payeura445df3d2019-02-05 19:37:2632
33 // Don't show the UI for Unicorn accounts.
34 if (profile->IsSupervised())
35 return false;
Nicolas Ouellet-payeur81bdcc52019-01-24 21:20:4236#endif
37
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2738 // This profile may have policies configured.
Xi Hanfe39afc62019-04-29 14:50:1439 auto* profile_connector = profile->GetProfilePolicyConnector();
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2740 if (profile_connector->IsManaged())
41 return true;
42
43#if defined(OS_CHROMEOS)
44 // This session's primary user may also have policies, and those policies may
45 // not have per-profile support.
46 auto* primary_user = user_manager::UserManager::Get()->GetPrimaryUser();
47 if (primary_user) {
48 auto* primary_profile =
49 chromeos::ProfileHelper::Get()->GetProfileByUser(primary_user);
50 if (primary_profile) {
51 auto* primary_profile_connector =
Xi Hanfe39afc62019-04-29 14:50:1452 primary_profile->GetProfilePolicyConnector();
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:2753 if (primary_profile_connector->IsManaged())
54 return true;
55 }
56 }
57
58 // The machine may be enrolled, via Google Cloud or Active Directory.
59 auto* browser_connector =
60 g_browser_process->platform_part()->browser_policy_connector_chromeos();
61 if (browser_connector->IsEnterpriseManaged())
62 return true;
63#else
64 // There may be policies set in a platform-specific way (e.g. Windows
65 // Registry), or with machine level user cloud policies.
66 auto* browser_connector = g_browser_process->browser_policy_connector();
67 if (browser_connector->HasMachineLevelPolicies())
68 return true;
69#endif
70
71 return false;
72}
73
Nicolas Ouellet-payeur2bacf202019-05-10 20:20:2274base::string16 GetManagedUiMenuItemLabel(Profile* profile) {
75 std::string management_domain =
76 ManagementUIHandler::GetAccountDomain(profile);
77
78 int string_id = IDS_MANAGED;
79 std::vector<base::string16> replacements;
80 if (!management_domain.empty()) {
81 string_id = IDS_MANAGED_BY;
82 replacements.push_back(base::UTF8ToUTF16(management_domain));
83 }
84
85 return l10n_util::GetStringFUTF16(string_id, replacements, nullptr);
86}
87
88base::string16 GetManagedUiWebUILabel(Profile* profile) {
89 std::string management_domain =
90 ManagementUIHandler::GetAccountDomain(profile);
91
92 int string_id = IDS_MANAGED_WITH_HYPERLINK;
93
94 std::vector<base::string16> replacements;
95 replacements.push_back(base::UTF8ToUTF16(chrome::kChromeUIManagementURL));
Nicolas Ouellet-payeur2bacf202019-05-10 20:20:2296 if (!management_domain.empty()) {
97 string_id = IDS_MANAGED_BY_WITH_HYPERLINK;
98 replacements.push_back(base::UTF8ToUTF16(management_domain));
99 }
100
101 return l10n_util::GetStringFUTF16(string_id, replacements, nullptr);
102}
103
Bailey Berro6ee1e0f22019-08-26 17:41:53104#if defined(OS_CHROMEOS)
105base::string16 GetDeviceManagedUiWebUILabel(Profile* profile) {
106 std::string management_domain =
107 ManagementUIHandler::GetAccountDomain(profile);
108
109 int string_id = IDS_DEVICE_MANAGED_WITH_HYPERLINK;
110
111 std::vector<base::string16> replacements;
112 replacements.push_back(base::UTF8ToUTF16(chrome::kChromeUIManagementURL));
113 replacements.push_back(ui::GetChromeOSDeviceName());
114 if (!management_domain.empty()) {
115 string_id = IDS_DEVICE_MANAGED_BY_WITH_HYPERLINK;
116 replacements.push_back(base::UTF8ToUTF16(management_domain));
117 }
118
119 return l10n_util::GetStringFUTF16(string_id, replacements, nullptr);
120}
121#endif
122
Nicolas Ouellet-payeur82eb65b2018-11-05 16:49:27123} // namespace chrome