Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 1 | // 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-payeur | 2bacf20 | 2019-05-10 20:20:22 | [diff] [blame] | 7 | #include "base/strings/utf_string_conversions.h" |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 8 | #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-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
Nicolas Ouellet-payeur | 2bacf20 | 2019-05-10 20:20:22 | [diff] [blame] | 13 | #include "chrome/browser/ui/webui/management_ui_handler.h" |
Nicolas Ouellet-payeur | 2bacf20 | 2019-05-10 20:20:22 | [diff] [blame] | 14 | #include "chrome/grit/generated_resources.h" |
| 15 | #include "ui/base/l10n/l10n_util.h" |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 16 | |
| 17 | #if defined(OS_CHROMEOS) |
Nicolas Ouellet-payeur | 81bdcc5 | 2019-01-24 21:20:42 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/login/demo_mode/demo_session.h" |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 19 | #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-payeur | 2bacf20 | 2019-05-10 20:20:22 | [diff] [blame] | 22 | #include "ui/chromeos/devicetype_utils.h" |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | namespace chrome { |
| 26 | |
| 27 | bool ShouldDisplayManagedUi(Profile* profile) { |
Nicolas Ouellet-payeur | 81bdcc5 | 2019-01-24 21:20:42 | [diff] [blame] | 28 | #if defined(OS_CHROMEOS) |
| 29 | // Don't show the UI in demo mode. |
| 30 | if (chromeos::DemoSession::IsDeviceInDemoMode()) |
| 31 | return false; |
Nicolas Ouellet-payeur | a445df3d | 2019-02-05 19:37:26 | [diff] [blame] | 32 | |
| 33 | // Don't show the UI for Unicorn accounts. |
| 34 | if (profile->IsSupervised()) |
| 35 | return false; |
Nicolas Ouellet-payeur | 81bdcc5 | 2019-01-24 21:20:42 | [diff] [blame] | 36 | #endif |
| 37 | |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 38 | // This profile may have policies configured. |
Xi Han | fe39afc6 | 2019-04-29 14:50:14 | [diff] [blame] | 39 | auto* profile_connector = profile->GetProfilePolicyConnector(); |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 40 | 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 Han | fe39afc6 | 2019-04-29 14:50:14 | [diff] [blame] | 52 | primary_profile->GetProfilePolicyConnector(); |
Nicolas Ouellet-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 53 | 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-payeur | 2bacf20 | 2019-05-10 20:20:22 | [diff] [blame] | 74 | base::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 | |
| 88 | base::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-payeur | 2bacf20 | 2019-05-10 20:20:22 | [diff] [blame] | 96 | 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 Berro | 6ee1e0f2 | 2019-08-26 17:41:53 | [diff] [blame] | 104 | #if defined(OS_CHROMEOS) |
| 105 | base::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-payeur | 82eb65b | 2018-11-05 16:49:27 | [diff] [blame] | 123 | } // namespace chrome |