Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Helmut Januschka | 7290d13 | 2024-03-28 09:15:10 | [diff] [blame] | 5 | #include <string_view> |
| 6 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
Avi Drissman | 02e49e58 | 2023-01-07 01:23:18 | [diff] [blame] | 9 | #include "base/functional/bind.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 12 | #include "base/task/thread_pool.h" |
Yeunjoo Choi | 7227dba | 2021-08-04 06:35:47 | [diff] [blame] | 13 | #include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 14 | #include "chrome/browser/browser_process.h" |
Hans Wennborg | 6334445 | 2019-10-15 10:15:21 | [diff] [blame] | 15 | #include "chrome/browser/browser_process_platform_part.h" |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 16 | #include "chrome/browser/google/google_brand_code_map_chromeos.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 17 | #include "chrome/common/pref_names.h" |
Yeunjoo Choi | a10bd86e | 2022-12-15 02:34:51 | [diff] [blame] | 18 | #include "chromeos/ash/components/system/statistics_provider.h" |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 19 | #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 20 | #include "components/prefs/pref_service.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
| 22 | |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 23 | namespace google_brand { |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 24 | namespace chromeos { |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // Path to file that stores the RLZ brand code on ChromeOS. |
[email protected] | a7e1dc0 | 2013-02-14 15:00:47 | [diff] [blame] | 29 | const base::FilePath::CharType kRLZBrandFilePath[] = |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 30 | FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE"); |
| 31 | |
Helmut Januschka | 7290d13 | 2024-03-28 09:15:10 | [diff] [blame] | 32 | bool IsBrandValid(std::string_view brand) { |
Wenzhao Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 33 | return !brand.empty(); |
| 34 | } |
| 35 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 36 | // Reads the brand code from file |kRLZBrandFilePath|. |
| 37 | std::string ReadBrandFromFile() { |
| 38 | std::string brand; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 39 | base::FilePath brand_file_path(kRLZBrandFilePath); |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 40 | if (!base::ReadFileToString(brand_file_path, &brand)) { |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 41 | LOG(WARNING) << "Brand code file missing: " << brand_file_path.value(); |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 42 | } |
tfarina | 023b1dcc | 2015-12-06 13:25:41 | [diff] [blame] | 43 | base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 44 | return brand; |
| 45 | } |
| 46 | |
Wenzhao Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 47 | // For a valid |brand|, sets the brand code and runs |callback|. |
Xida Chen | 338c95c | 2020-07-07 16:50:47 | [diff] [blame] | 48 | void SetBrand(base::OnceClosure callback, const std::string& brand) { |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 49 | if (!IsBrandValid(brand)) { |
Wenzhao Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 50 | return; |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 51 | } |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 52 | g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand); |
Xida Chen | 338c95c | 2020-07-07 16:50:47 | [diff] [blame] | 53 | std::move(callback).Run(); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 56 | // True if brand code has been cleared for the current session. |
| 57 | bool g_brand_empty = false; |
| 58 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 59 | } // namespace |
| 60 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 61 | void ClearBrandForCurrentSession() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 62 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 63 | content::BrowserThread::UI) || |
| 64 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 65 | g_brand_empty = true; |
| 66 | } |
| 67 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 68 | std::string GetBrand() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 69 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 70 | content::BrowserThread::UI) || |
| 71 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 72 | if (g_brand_empty) { |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 73 | return std::string(); |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 74 | } |
jamescook | 8747d18f | 2014-11-05 01:29:06 | [diff] [blame] | 75 | // Unit tests do not have prefs. |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 76 | if (!g_browser_process->local_state()) { |
jamescook | 8747d18f | 2014-11-05 01:29:06 | [diff] [blame] | 77 | return std::string(); |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 78 | } |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 79 | return g_browser_process->local_state()->GetString(prefs::kRLZBrand); |
| 80 | } |
| 81 | |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 82 | std::string GetRlzBrand() { |
Yeunjoo Choi | 7227dba | 2021-08-04 06:35:47 | [diff] [blame] | 83 | policy::BrowserPolicyConnectorAsh* connector = |
| 84 | g_browser_process->platform_part()->browser_policy_connector_ash(); |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 85 | std::optional<policy::MarketSegment> market_segment; |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 86 | if (connector->IsDeviceEnterpriseManaged()) { |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 87 | market_segment = connector->GetEnterpriseMarketSegment(); |
Peter Kasting | de196f3 | 2024-12-23 00:06:33 | [diff] [blame^] | 88 | } |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 89 | // The rlz brand code may change over time (e.g. when device goes from |
| 90 | // unenrolled to enrolled status in OOBE). Prefer not to save it in pref to |
| 91 | // avoid using outdated value. |
Junichi Uekawa | 05769ac9 | 2024-07-18 05:41:17 | [diff] [blame] | 92 | return std::string(GetRlzBrandCode(GetBrand(), market_segment)); |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 93 | } |
| 94 | |
Xida Chen | 338c95c | 2020-07-07 16:50:47 | [diff] [blame] | 95 | void InitBrand(base::OnceClosure callback) { |
Yeunjoo Choi | 46289671 | 2022-12-20 01:15:40 | [diff] [blame] | 96 | ::ash::system::StatisticsProvider* provider = |
| 97 | ::ash::system::StatisticsProvider::GetInstance(); |
Helmut Januschka | 7290d13 | 2024-03-28 09:15:10 | [diff] [blame] | 98 | const std::optional<std::string_view> brand = |
Yeunjoo Choi | 46289671 | 2022-12-20 01:15:40 | [diff] [blame] | 99 | provider->GetMachineStatistic(::ash::system::kRlzBrandCodeKey); |
Artem Sumaneev | 4b8d65f | 2022-11-21 19:24:51 | [diff] [blame] | 100 | if (brand && IsBrandValid(brand.value())) { |
| 101 | SetBrand(std::move(callback), std::string(brand.value())); |
[email protected] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 105 | base::ThreadPool::PostTaskAndReplyWithResult( |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 106 | FROM_HERE, |
Gabriel Charette | 05503913 | 2020-02-26 23:02:06 | [diff] [blame] | 107 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 108 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
Xida Chen | 338c95c | 2020-07-07 16:50:47 | [diff] [blame] | 109 | base::BindOnce(&ReadBrandFromFile), |
| 110 | base::BindOnce(&SetBrand, std::move(callback))); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // namespace chromeos |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 114 | } // namespace google_brand |