blob: e93b06fde51ffe9b5e6280f6f706119fb1e1ba6d [file] [log] [blame]
[email protected]dd3e528a2014-06-06 11:41:121// Copyright 2014 The Chromium Authors. All rights reserved.
[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]427e48f2012-12-05 16:02:245#include "base/bind.h"
[email protected]57999812013-02-24 05:40:526#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:047#include "base/files/file_util.h"
[email protected]427e48f2012-12-05 16:02:248#include "base/logging.h"
[email protected]d8830562013-06-10 22:01:549#include "base/strings/string_util.h"
Gabriel Charette44db1422018-08-06 11:19:3310#include "base/task/post_task.h"
Gabriel Charette055039132020-02-26 23:02:0611#include "base/task/thread_pool.h"
[email protected]427e48f2012-12-05 16:02:2412#include "chrome/browser/browser_process.h"
Hans Wennborg63344452019-10-15 10:15:2113#include "chrome/browser/browser_process_platform_part.h"
phweiss5e0dc2c752021-06-10 15:39:3814#include "chrome/browser/chromeos/policy/core/browser_policy_connector_chromeos.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"
[email protected]1a32f302014-03-05 14:20:2617#include "chromeos/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
Wenzhao Zang5f5b2b52018-10-09 19:05:5531bool IsBrandValid(const std::string& brand) {
32 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() {
78 policy::BrowserPolicyConnectorChromeOS* connector =
79 g_browser_process->platform_part()->browser_policy_connector_chromeos();
Anton Bikineev46bbb972021-05-15 17:53:5380 absl::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) {
[email protected]1a32f302014-03-05 14:20:2690 ::chromeos::system::StatisticsProvider* provider =
91 ::chromeos::system::StatisticsProvider::GetInstance();
92 std::string brand;
93 const bool found = provider->GetMachineStatistic(
94 ::chromeos::system::kRlzBrandCodeKey, &brand);
Wenzhao Zang5f5b2b52018-10-09 19:05:5595 if (found && IsBrandValid(brand)) {
Xida Chen338c95c2020-07-07 16:50:4796 SetBrand(std::move(callback), brand);
[email protected]1a32f302014-03-05 14:20:2697 return;
98 }
99
Gabriel Charette055039132020-02-26 23:02:06100 base::ThreadPool::PostTaskAndReplyWithResult(
fdoray4ad14932017-05-03 21:21:11101 FROM_HERE,
Gabriel Charette055039132020-02-26 23:02:06102 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray4ad14932017-05-03 21:21:11103 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
Xida Chen338c95c2020-07-07 16:50:47104 base::BindOnce(&ReadBrandFromFile),
105 base::BindOnce(&SetBrand, std::move(callback)));
[email protected]427e48f2012-12-05 16:02:24106}
107
108} // namespace chromeos
[email protected]dd3e528a2014-06-06 11:41:12109} // namespace google_brand