[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 | ffaaf85f | 2018-08-03 19:20:47 | [diff] [blame] | 10 | #include "base/task/post_task_forward.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 12 | #include "chrome/common/pref_names.h" |
[email protected] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 13 | #include "chromeos/system/statistics_provider.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 14 | #include "components/prefs/pref_service.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 15 | #include "content/public/browser/browser_thread.h" |
| 16 | |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 17 | namespace google_brand { |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 18 | namespace chromeos { |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // Path to file that stores the RLZ brand code on ChromeOS. |
[email protected] | a7e1dc0 | 2013-02-14 15:00:47 | [diff] [blame] | 23 | const base::FilePath::CharType kRLZBrandFilePath[] = |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 24 | FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE"); |
| 25 | |
| 26 | // Reads the brand code from file |kRLZBrandFilePath|. |
| 27 | std::string ReadBrandFromFile() { |
| 28 | std::string brand; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 29 | base::FilePath brand_file_path(kRLZBrandFilePath); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 30 | if (!base::ReadFileToString(brand_file_path, &brand)) |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 31 | LOG(WARNING) << "Brand code file missing: " << brand_file_path.value(); |
tfarina | 023b1dcc | 2015-12-06 13:25:41 | [diff] [blame] | 32 | base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 33 | return brand; |
| 34 | } |
| 35 | |
| 36 | // Sets the brand code to |brand|. |
| 37 | void SetBrand(const base::Closure& callback, const std::string& brand) { |
| 38 | g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand); |
| 39 | callback.Run(); |
| 40 | } |
| 41 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 42 | // True if brand code has been cleared for the current session. |
| 43 | bool g_brand_empty = false; |
| 44 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 45 | } // namespace |
| 46 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 47 | void ClearBrandForCurrentSession() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 48 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 49 | content::BrowserThread::UI) || |
| 50 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 51 | g_brand_empty = true; |
| 52 | } |
| 53 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 54 | std::string GetBrand() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 55 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 56 | content::BrowserThread::UI) || |
| 57 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 58 | if (g_brand_empty) |
| 59 | return std::string(); |
jamescook | 8747d18f | 2014-11-05 01:29:06 | [diff] [blame] | 60 | // Unit tests do not have prefs. |
| 61 | if (!g_browser_process->local_state()) |
| 62 | return std::string(); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 63 | return g_browser_process->local_state()->GetString(prefs::kRLZBrand); |
| 64 | } |
| 65 | |
[email protected] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 66 | void InitBrand(const base::Closure& callback) { |
| 67 | ::chromeos::system::StatisticsProvider* provider = |
| 68 | ::chromeos::system::StatisticsProvider::GetInstance(); |
| 69 | std::string brand; |
| 70 | const bool found = provider->GetMachineStatistic( |
| 71 | ::chromeos::system::kRlzBrandCodeKey, &brand); |
| 72 | if (found && !brand.empty()) { |
| 73 | SetBrand(callback, brand); |
| 74 | return; |
| 75 | } |
| 76 | |
fdoray | 6c00462 | 2016-12-02 23:08:38 | [diff] [blame] | 77 | base::PostTaskWithTraitsAndReplyWithResult( |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 78 | FROM_HERE, |
Gabriel Charette | b10aeebc | 2018-07-26 20:15:00 | [diff] [blame] | 79 | {base::MayBlock(), base::TaskPriority::BEST_EFFORT, |
fdoray | 4ad1493 | 2017-05-03 21:21:11 | [diff] [blame] | 80 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
fdoray | 6c00462 | 2016-12-02 23:08:38 | [diff] [blame] | 81 | base::Bind(&ReadBrandFromFile), base::Bind(&SetBrand, callback)); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace chromeos |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 85 | } // namespace google_brand |