blob: 8d1cf6fdb206ac024e8d077c461af799c0e9fdf0 [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"
[email protected]427e48f2012-12-05 16:02:2411#include "chrome/browser/browser_process.h"
Hans Wennborg63344452019-10-15 10:15:2112#include "chrome/browser/browser_process_platform_part.h"
Wenzhao Zang20153792018-09-29 00:37:1013#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
14#include "chrome/browser/google/google_brand_code_map_chromeos.h"
[email protected]427e48f2012-12-05 16:02:2415#include "chrome/common/pref_names.h"
[email protected]1a32f302014-03-05 14:20:2616#include "chromeos/system/statistics_provider.h"
Wenzhao Zang20153792018-09-29 00:37:1017#include "components/policy/core/common/cloud/cloud_policy_constants.h"
brettwb1fc1b82016-02-02 00:19:0818#include "components/prefs/pref_service.h"
[email protected]427e48f2012-12-05 16:02:2419#include "content/public/browser/browser_thread.h"
20
[email protected]dd3e528a2014-06-06 11:41:1221namespace google_brand {
[email protected]427e48f2012-12-05 16:02:2422namespace chromeos {
23
24namespace {
25
26// Path to file that stores the RLZ brand code on ChromeOS.
[email protected]a7e1dc02013-02-14 15:00:4727const base::FilePath::CharType kRLZBrandFilePath[] =
[email protected]427e48f2012-12-05 16:02:2428 FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE");
29
Wenzhao Zang5f5b2b52018-10-09 19:05:5530bool IsBrandValid(const std::string& brand) {
31 return !brand.empty();
32}
33
[email protected]427e48f2012-12-05 16:02:2434// Reads the brand code from file |kRLZBrandFilePath|.
35std::string ReadBrandFromFile() {
36 std::string brand;
[email protected]650b2d52013-02-10 03:41:4537 base::FilePath brand_file_path(kRLZBrandFilePath);
[email protected]82f84b92013-08-30 18:23:5038 if (!base::ReadFileToString(brand_file_path, &brand))
[email protected]427e48f2012-12-05 16:02:2439 LOG(WARNING) << "Brand code file missing: " << brand_file_path.value();
tfarina023b1dcc2015-12-06 13:25:4140 base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand);
[email protected]427e48f2012-12-05 16:02:2441 return brand;
42}
43
Wenzhao Zang5f5b2b52018-10-09 19:05:5544// For a valid |brand|, sets the brand code and runs |callback|.
[email protected]427e48f2012-12-05 16:02:2445void SetBrand(const base::Closure& callback, const std::string& brand) {
Wenzhao Zang5f5b2b52018-10-09 19:05:5546 if (!IsBrandValid(brand))
47 return;
[email protected]427e48f2012-12-05 16:02:2448 g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand);
49 callback.Run();
50}
51
[email protected]b2574e82012-12-17 15:58:2652// True if brand code has been cleared for the current session.
53bool g_brand_empty = false;
54
[email protected]427e48f2012-12-05 16:02:2455} // namespace
56
[email protected]b2574e82012-12-17 15:58:2657void ClearBrandForCurrentSession() {
[email protected]33d06242013-08-12 05:20:3058 DCHECK(!content::BrowserThread::IsThreadInitialized(
59 content::BrowserThread::UI) ||
60 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2661 g_brand_empty = true;
62}
63
[email protected]427e48f2012-12-05 16:02:2464std::string GetBrand() {
[email protected]33d06242013-08-12 05:20:3065 DCHECK(!content::BrowserThread::IsThreadInitialized(
66 content::BrowserThread::UI) ||
67 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2668 if (g_brand_empty)
69 return std::string();
jamescook8747d18f2014-11-05 01:29:0670 // Unit tests do not have prefs.
71 if (!g_browser_process->local_state())
72 return std::string();
[email protected]427e48f2012-12-05 16:02:2473 return g_browser_process->local_state()->GetString(prefs::kRLZBrand);
74}
75
Wenzhao Zang20153792018-09-29 00:37:1076std::string GetRlzBrand() {
77 policy::BrowserPolicyConnectorChromeOS* connector =
78 g_browser_process->platform_part()->browser_policy_connector_chromeos();
79 base::Optional<policy::MarketSegment> market_segment;
80 if (connector->IsEnterpriseManaged())
81 market_segment = connector->GetEnterpriseMarketSegment();
82 // The rlz brand code may change over time (e.g. when device goes from
83 // unenrolled to enrolled status in OOBE). Prefer not to save it in pref to
84 // avoid using outdated value.
85 return GetRlzBrandCode(GetBrand(), market_segment);
86}
87
[email protected]1a32f302014-03-05 14:20:2688void InitBrand(const base::Closure& callback) {
89 ::chromeos::system::StatisticsProvider* provider =
90 ::chromeos::system::StatisticsProvider::GetInstance();
91 std::string brand;
92 const bool found = provider->GetMachineStatistic(
93 ::chromeos::system::kRlzBrandCodeKey, &brand);
Wenzhao Zang5f5b2b52018-10-09 19:05:5594 if (found && IsBrandValid(brand)) {
[email protected]1a32f302014-03-05 14:20:2695 SetBrand(callback, brand);
96 return;
97 }
98
Sami Kyostila4dbac1c92019-08-12 18:39:1399 base::PostTaskAndReplyWithResult(
fdoray4ad14932017-05-03 21:21:11100 FROM_HERE,
Sami Kyostila4dbac1c92019-08-12 18:39:13101 {base::ThreadPool(), base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray4ad14932017-05-03 21:21:11102 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
fdoray6c004622016-12-02 23:08:38103 base::Bind(&ReadBrandFromFile), base::Bind(&SetBrand, callback));
[email protected]427e48f2012-12-05 16:02:24104}
105
106} // namespace chromeos
[email protected]dd3e528a2014-06-06 11:41:12107} // namespace google_brand