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