blob: 0a55afd2cb2fa47ea2cae06259d77e982404ee51 [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
[email protected]57999812013-02-24 05:40:525#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:046#include "base/files/file_util.h"
Avi Drissman02e49e582023-01-07 01:23:187#include "base/functional/bind.h"
[email protected]427e48f2012-12-05 16:02:248#include "base/logging.h"
Artem Sumaneev4b8d65f2022-11-21 19:24:519#include "base/strings/string_piece.h"
[email protected]d8830562013-06-10 22:01:5410#include "base/strings/string_util.h"
Gabriel Charette055039132020-02-26 23:02:0611#include "base/task/thread_pool.h"
Yeunjoo Choi7227dba2021-08-04 06:35:4712#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
[email protected]427e48f2012-12-05 16:02:2413#include "chrome/browser/browser_process.h"
Hans Wennborg63344452019-10-15 10:15:2114#include "chrome/browser/browser_process_platform_part.h"
Wenzhao Zang20153792018-09-29 00:37:1015#include "chrome/browser/google/google_brand_code_map_chromeos.h"
[email protected]427e48f2012-12-05 16:02:2416#include "chrome/common/pref_names.h"
Yeunjoo Choia10bd86e2022-12-15 02:34:5117#include "chromeos/ash/components/system/statistics_provider.h"
Wenzhao Zang20153792018-09-29 00:37:1018#include "components/policy/core/common/cloud/cloud_policy_constants.h"
brettwb1fc1b82016-02-02 00:19:0819#include "components/prefs/pref_service.h"
[email protected]427e48f2012-12-05 16:02:2420#include "content/public/browser/browser_thread.h"
21
[email protected]dd3e528a2014-06-06 11:41:1222namespace google_brand {
[email protected]427e48f2012-12-05 16:02:2423namespace chromeos {
24
25namespace {
26
27// Path to file that stores the RLZ brand code on ChromeOS.
[email protected]a7e1dc02013-02-14 15:00:4728const base::FilePath::CharType kRLZBrandFilePath[] =
[email protected]427e48f2012-12-05 16:02:2429 FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE");
30
Artem Sumaneev4b8d65f2022-11-21 19:24:5131bool IsBrandValid(base::StringPiece brand) {
Wenzhao Zang5f5b2b52018-10-09 19:05:5532 return !brand.empty();
33}
34
[email protected]427e48f2012-12-05 16:02:2435// Reads the brand code from file |kRLZBrandFilePath|.
36std::string ReadBrandFromFile() {
37 std::string brand;
[email protected]650b2d52013-02-10 03:41:4538 base::FilePath brand_file_path(kRLZBrandFilePath);
[email protected]82f84b92013-08-30 18:23:5039 if (!base::ReadFileToString(brand_file_path, &brand))
[email protected]427e48f2012-12-05 16:02:2440 LOG(WARNING) << "Brand code file missing: " << brand_file_path.value();
tfarina023b1dcc2015-12-06 13:25:4141 base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand);
[email protected]427e48f2012-12-05 16:02:2442 return brand;
43}
44
Wenzhao Zang5f5b2b52018-10-09 19:05:5545// For a valid |brand|, sets the brand code and runs |callback|.
Xida Chen338c95c2020-07-07 16:50:4746void SetBrand(base::OnceClosure callback, const std::string& brand) {
Wenzhao Zang5f5b2b52018-10-09 19:05:5547 if (!IsBrandValid(brand))
48 return;
[email protected]427e48f2012-12-05 16:02:2449 g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand);
Xida Chen338c95c2020-07-07 16:50:4750 std::move(callback).Run();
[email protected]427e48f2012-12-05 16:02:2451}
52
[email protected]b2574e82012-12-17 15:58:2653// True if brand code has been cleared for the current session.
54bool g_brand_empty = false;
55
[email protected]427e48f2012-12-05 16:02:2456} // namespace
57
[email protected]b2574e82012-12-17 15:58:2658void ClearBrandForCurrentSession() {
[email protected]33d06242013-08-12 05:20:3059 DCHECK(!content::BrowserThread::IsThreadInitialized(
60 content::BrowserThread::UI) ||
61 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2662 g_brand_empty = true;
63}
64
[email protected]427e48f2012-12-05 16:02:2465std::string GetBrand() {
[email protected]33d06242013-08-12 05:20:3066 DCHECK(!content::BrowserThread::IsThreadInitialized(
67 content::BrowserThread::UI) ||
68 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2669 if (g_brand_empty)
70 return std::string();
jamescook8747d18f2014-11-05 01:29:0671 // Unit tests do not have prefs.
72 if (!g_browser_process->local_state())
73 return std::string();
[email protected]427e48f2012-12-05 16:02:2474 return g_browser_process->local_state()->GetString(prefs::kRLZBrand);
75}
76
Wenzhao Zang20153792018-09-29 00:37:1077std::string GetRlzBrand() {
Yeunjoo Choi7227dba2021-08-04 06:35:4778 policy::BrowserPolicyConnectorAsh* connector =
79 g_browser_process->platform_part()->browser_policy_connector_ash();
Arthur Sonzognife132ee2024-01-15 11:01:0480 std::optional<policy::MarketSegment> market_segment;
Roland Bock4ce23a922021-06-11 13:23:1181 if (connector->IsDeviceEnterpriseManaged())
Wenzhao Zang20153792018-09-29 00:37:1082 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 Chen338c95c2020-07-07 16:50:4789void InitBrand(base::OnceClosure callback) {
Yeunjoo Choi462896712022-12-20 01:15:4090 ::ash::system::StatisticsProvider* provider =
91 ::ash::system::StatisticsProvider::GetInstance();
Arthur Sonzognife132ee2024-01-15 11:01:0492 const std::optional<base::StringPiece> brand =
Yeunjoo Choi462896712022-12-20 01:15:4093 provider->GetMachineStatistic(::ash::system::kRlzBrandCodeKey);
Artem Sumaneev4b8d65f2022-11-21 19:24:5194 if (brand && IsBrandValid(brand.value())) {
95 SetBrand(std::move(callback), std::string(brand.value()));
[email protected]1a32f302014-03-05 14:20:2696 return;
97 }
98
Gabriel Charette055039132020-02-26 23:02:0699 base::ThreadPool::PostTaskAndReplyWithResult(
fdoray4ad14932017-05-03 21:21:11100 FROM_HERE,
Gabriel Charette055039132020-02-26 23:02:06101 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray4ad14932017-05-03 21:21:11102 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
Xida Chen338c95c2020-07-07 16:50:47103 base::BindOnce(&ReadBrandFromFile),
104 base::BindOnce(&SetBrand, std::move(callback)));
[email protected]427e48f2012-12-05 16:02:24105}
106
107} // namespace chromeos
[email protected]dd3e528a2014-06-06 11:41:12108} // namespace google_brand