[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[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] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 5 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 6 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 7 | #include "base/files/file_util.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 10 | #include "base/task/post_task.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
Hans Wennborg | 6334445 | 2019-10-15 10:15:21 | [diff] [blame^] | 12 | #include "chrome/browser/browser_process_platform_part.h" |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 13 | #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| 14 | #include "chrome/browser/google/google_brand_code_map_chromeos.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 15 | #include "chrome/common/pref_names.h" |
[email protected] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 16 | #include "chromeos/system/statistics_provider.h" |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 17 | #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 18 | #include "components/prefs/pref_service.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
| 20 | |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 21 | namespace google_brand { |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 22 | namespace chromeos { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // Path to file that stores the RLZ brand code on ChromeOS. |
[email protected] | a7e1dc0 | 2013-02-14 15:00:47 | [diff] [blame] | 27 | const base::FilePath::CharType kRLZBrandFilePath[] = |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 28 | FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE"); |
| 29 | |
Wenzhao Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 30 | bool IsBrandValid(const std::string& brand) { |
| 31 | return !brand.empty(); |
| 32 | } |
| 33 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 34 | // Reads the brand code from file |kRLZBrandFilePath|. |
| 35 | std::string ReadBrandFromFile() { |
| 36 | std::string brand; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 37 | base::FilePath brand_file_path(kRLZBrandFilePath); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 38 | if (!base::ReadFileToString(brand_file_path, &brand)) |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 39 | LOG(WARNING) << "Brand code file missing: " << brand_file_path.value(); |
tfarina | 023b1dcc | 2015-12-06 13:25:41 | [diff] [blame] | 40 | base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 41 | return brand; |
| 42 | } |
| 43 | |
Wenzhao Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 44 | // For a valid |brand|, sets the brand code and runs |callback|. |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 45 | void SetBrand(const base::Closure& callback, const std::string& brand) { |
Wenzhao Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 46 | if (!IsBrandValid(brand)) |
| 47 | return; |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 48 | g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand); |
| 49 | callback.Run(); |
| 50 | } |
| 51 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 52 | // True if brand code has been cleared for the current session. |
| 53 | bool g_brand_empty = false; |
| 54 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 55 | } // namespace |
| 56 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 57 | void ClearBrandForCurrentSession() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 58 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 59 | content::BrowserThread::UI) || |
| 60 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 61 | g_brand_empty = true; |
| 62 | } |
| 63 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 64 | std::string GetBrand() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 65 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 66 | content::BrowserThread::UI) || |
| 67 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 68 | if (g_brand_empty) |
| 69 | return std::string(); |
jamescook | 8747d18f | 2014-11-05 01:29:06 | [diff] [blame] | 70 | // Unit tests do not have prefs. |
| 71 | if (!g_browser_process->local_state()) |
| 72 | return std::string(); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 73 | return g_browser_process->local_state()->GetString(prefs::kRLZBrand); |
| 74 | } |
| 75 | |
Wenzhao Zang | 2015379 | 2018-09-29 00:37:10 | [diff] [blame] | 76 | std::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] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 88 | void 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 Zang | 5f5b2b5 | 2018-10-09 19:05:55 | [diff] [blame] | 94 | if (found && IsBrandValid(brand)) { |
[email protected] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 95 | SetBrand(callback, brand); |
| 96 | return; |
| 97 | } |
| 98 | |
Sami Kyostila | 4dbac1c9 | 2019-08-12 18:39:13 | [diff] [blame] | 99 | base::PostTaskAndReplyWithResult( |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 100 | FROM_HERE, |
Sami Kyostila | 4dbac1c9 | 2019-08-12 18:39:13 | [diff] [blame] | 101 | {base::ThreadPool(), base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 102 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
fdoray | 6c00462 | 2016-12-02 23:08:38 | [diff] [blame] | 103 | base::Bind(&ReadBrandFromFile), base::Bind(&SetBrand, callback)); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } // namespace chromeos |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 107 | } // namespace google_brand |