blob: a4b3ad2b533efab18520c018b2f2a83bfe77c12d [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2014 The Chromium Authors
[email protected]427e48f2012-12-05 16:02:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Helmut Januschka7290d132024-03-28 09:15:105#include <string_view>
6
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
Avi Drissman02e49e582023-01-07 01:23:189#include "base/functional/bind.h"
[email protected]427e48f2012-12-05 16:02:2410#include "base/logging.h"
[email protected]d8830562013-06-10 22:01:5411#include "base/strings/string_util.h"
Gabriel Charette055039132020-02-26 23:02:0612#include "base/task/thread_pool.h"
Yeunjoo Choi7227dba2021-08-04 06:35:4713#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
[email protected]427e48f2012-12-05 16:02:2414#include "chrome/browser/browser_process.h"
Hans Wennborg63344452019-10-15 10:15:2115#include "chrome/browser/browser_process_platform_part.h"
Wenzhao Zang20153792018-09-29 00:37:1016#include "chrome/browser/google/google_brand_code_map_chromeos.h"
[email protected]427e48f2012-12-05 16:02:2417#include "chrome/common/pref_names.h"
Yeunjoo Choia10bd86e2022-12-15 02:34:5118#include "chromeos/ash/components/system/statistics_provider.h"
Wenzhao Zang20153792018-09-29 00:37:1019#include "components/policy/core/common/cloud/cloud_policy_constants.h"
brettwb1fc1b82016-02-02 00:19:0820#include "components/prefs/pref_service.h"
[email protected]427e48f2012-12-05 16:02:2421#include "content/public/browser/browser_thread.h"
22
[email protected]dd3e528a2014-06-06 11:41:1223namespace google_brand {
[email protected]427e48f2012-12-05 16:02:2424namespace chromeos {
25
26namespace {
27
28// Path to file that stores the RLZ brand code on ChromeOS.
[email protected]a7e1dc02013-02-14 15:00:4729const base::FilePath::CharType kRLZBrandFilePath[] =
[email protected]427e48f2012-12-05 16:02:2430 FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE");
31
Helmut Januschka7290d132024-03-28 09:15:1032bool IsBrandValid(std::string_view brand) {
Wenzhao Zang5f5b2b52018-10-09 19:05:5533 return !brand.empty();
34}
35
[email protected]427e48f2012-12-05 16:02:2436// Reads the brand code from file |kRLZBrandFilePath|.
37std::string ReadBrandFromFile() {
38 std::string brand;
[email protected]650b2d52013-02-10 03:41:4539 base::FilePath brand_file_path(kRLZBrandFilePath);
Peter Kastingde196f32024-12-23 00:06:3340 if (!base::ReadFileToString(brand_file_path, &brand)) {
[email protected]427e48f2012-12-05 16:02:2441 LOG(WARNING) << "Brand code file missing: " << brand_file_path.value();
Peter Kastingde196f32024-12-23 00:06:3342 }
tfarina023b1dcc2015-12-06 13:25:4143 base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand);
[email protected]427e48f2012-12-05 16:02:2444 return brand;
45}
46
Wenzhao Zang5f5b2b52018-10-09 19:05:5547// For a valid |brand|, sets the brand code and runs |callback|.
Xida Chen338c95c2020-07-07 16:50:4748void SetBrand(base::OnceClosure callback, const std::string& brand) {
Peter Kastingde196f32024-12-23 00:06:3349 if (!IsBrandValid(brand)) {
Wenzhao Zang5f5b2b52018-10-09 19:05:5550 return;
Peter Kastingde196f32024-12-23 00:06:3351 }
[email protected]427e48f2012-12-05 16:02:2452 g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand);
Xida Chen338c95c2020-07-07 16:50:4753 std::move(callback).Run();
[email protected]427e48f2012-12-05 16:02:2454}
55
[email protected]b2574e82012-12-17 15:58:2656// True if brand code has been cleared for the current session.
57bool g_brand_empty = false;
58
[email protected]427e48f2012-12-05 16:02:2459} // namespace
60
[email protected]b2574e82012-12-17 15:58:2661void ClearBrandForCurrentSession() {
[email protected]33d06242013-08-12 05:20:3062 DCHECK(!content::BrowserThread::IsThreadInitialized(
63 content::BrowserThread::UI) ||
64 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2665 g_brand_empty = true;
66}
67
[email protected]427e48f2012-12-05 16:02:2468std::string GetBrand() {
[email protected]33d06242013-08-12 05:20:3069 DCHECK(!content::BrowserThread::IsThreadInitialized(
70 content::BrowserThread::UI) ||
71 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
Peter Kastingde196f32024-12-23 00:06:3372 if (g_brand_empty) {
[email protected]b2574e82012-12-17 15:58:2673 return std::string();
Peter Kastingde196f32024-12-23 00:06:3374 }
jamescook8747d18f2014-11-05 01:29:0675 // Unit tests do not have prefs.
Peter Kastingde196f32024-12-23 00:06:3376 if (!g_browser_process->local_state()) {
jamescook8747d18f2014-11-05 01:29:0677 return std::string();
Peter Kastingde196f32024-12-23 00:06:3378 }
[email protected]427e48f2012-12-05 16:02:2479 return g_browser_process->local_state()->GetString(prefs::kRLZBrand);
80}
81
Wenzhao Zang20153792018-09-29 00:37:1082std::string GetRlzBrand() {
Yeunjoo Choi7227dba2021-08-04 06:35:4783 policy::BrowserPolicyConnectorAsh* connector =
84 g_browser_process->platform_part()->browser_policy_connector_ash();
Arthur Sonzognife132ee2024-01-15 11:01:0485 std::optional<policy::MarketSegment> market_segment;
Peter Kastingde196f32024-12-23 00:06:3386 if (connector->IsDeviceEnterpriseManaged()) {
Wenzhao Zang20153792018-09-29 00:37:1087 market_segment = connector->GetEnterpriseMarketSegment();
Peter Kastingde196f32024-12-23 00:06:3388 }
Wenzhao Zang20153792018-09-29 00:37:1089 // 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 Uekawa05769ac92024-07-18 05:41:1792 return std::string(GetRlzBrandCode(GetBrand(), market_segment));
Wenzhao Zang20153792018-09-29 00:37:1093}
94
Xida Chen338c95c2020-07-07 16:50:4795void InitBrand(base::OnceClosure callback) {
Yeunjoo Choi462896712022-12-20 01:15:4096 ::ash::system::StatisticsProvider* provider =
97 ::ash::system::StatisticsProvider::GetInstance();
Helmut Januschka7290d132024-03-28 09:15:1098 const std::optional<std::string_view> brand =
Yeunjoo Choi462896712022-12-20 01:15:4099 provider->GetMachineStatistic(::ash::system::kRlzBrandCodeKey);
Artem Sumaneev4b8d65f2022-11-21 19:24:51100 if (brand && IsBrandValid(brand.value())) {
101 SetBrand(std::move(callback), std::string(brand.value()));
[email protected]1a32f302014-03-05 14:20:26102 return;
103 }
104
Gabriel Charette055039132020-02-26 23:02:06105 base::ThreadPool::PostTaskAndReplyWithResult(
fdoray4ad14932017-05-03 21:21:11106 FROM_HERE,
Gabriel Charette055039132020-02-26 23:02:06107 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray4ad14932017-05-03 21:21:11108 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
Xida Chen338c95c2020-07-07 16:50:47109 base::BindOnce(&ReadBrandFromFile),
110 base::BindOnce(&SetBrand, std::move(callback)));
[email protected]427e48f2012-12-05 16:02:24111}
112
113} // namespace chromeos
[email protected]dd3e528a2014-06-06 11:41:12114} // namespace google_brand