[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 | |
| 5 | #include "base/basictypes.h" |
| 6 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 10 | #include "base/prefs/pref_service.h" |
[email protected] | d883056 | 2013-06-10 22:01:54 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 12 | #include "base/task_runner_util.h" |
| 13 | #include "base/threading/worker_pool.h" |
| 14 | #include "chrome/browser/browser_process.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" |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 17 | #include "content/public/browser/browser_thread.h" |
| 18 | |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 19 | namespace google_brand { |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 20 | namespace chromeos { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | // Path to file that stores the RLZ brand code on ChromeOS. |
[email protected] | a7e1dc0 | 2013-02-14 15:00:47 | [diff] [blame] | 25 | const base::FilePath::CharType kRLZBrandFilePath[] = |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 26 | FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE"); |
| 27 | |
| 28 | // Reads the brand code from file |kRLZBrandFilePath|. |
| 29 | std::string ReadBrandFromFile() { |
| 30 | std::string brand; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 31 | base::FilePath brand_file_path(kRLZBrandFilePath); |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 32 | if (!base::ReadFileToString(brand_file_path, &brand)) |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 33 | LOG(WARNING) << "Brand code file missing: " << brand_file_path.value(); |
[email protected] | 8af69c6c | 2014-03-03 19:05:31 | [diff] [blame] | 34 | base::TrimWhitespace(brand, base::TRIM_ALL, &brand); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 35 | return brand; |
| 36 | } |
| 37 | |
| 38 | // Sets the brand code to |brand|. |
| 39 | void SetBrand(const base::Closure& callback, const std::string& brand) { |
| 40 | g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand); |
| 41 | callback.Run(); |
| 42 | } |
| 43 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 44 | // True if brand code has been cleared for the current session. |
| 45 | bool g_brand_empty = false; |
| 46 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 47 | } // namespace |
| 48 | |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 49 | void ClearBrandForCurrentSession() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 50 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 51 | content::BrowserThread::UI) || |
| 52 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 53 | g_brand_empty = true; |
| 54 | } |
| 55 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 56 | std::string GetBrand() { |
[email protected] | 33d0624 | 2013-08-12 05:20:30 | [diff] [blame] | 57 | DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 58 | content::BrowserThread::UI) || |
| 59 | content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
[email protected] | b2574e8 | 2012-12-17 15:58:26 | [diff] [blame] | 60 | if (g_brand_empty) |
| 61 | return std::string(); |
jamescook | 8747d18f | 2014-11-05 01:29:06 | [diff] [blame^] | 62 | // Unit tests do not have prefs. |
| 63 | if (!g_browser_process->local_state()) |
| 64 | return std::string(); |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 65 | return g_browser_process->local_state()->GetString(prefs::kRLZBrand); |
| 66 | } |
| 67 | |
[email protected] | 1a32f30 | 2014-03-05 14:20:26 | [diff] [blame] | 68 | void InitBrand(const base::Closure& callback) { |
| 69 | ::chromeos::system::StatisticsProvider* provider = |
| 70 | ::chromeos::system::StatisticsProvider::GetInstance(); |
| 71 | std::string brand; |
| 72 | const bool found = provider->GetMachineStatistic( |
| 73 | ::chromeos::system::kRlzBrandCodeKey, &brand); |
| 74 | if (found && !brand.empty()) { |
| 75 | SetBrand(callback, brand); |
| 76 | return; |
| 77 | } |
| 78 | |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 79 | base::PostTaskAndReplyWithResult( |
[email protected] | 144b6c4 | 2013-06-14 07:30:38 | [diff] [blame] | 80 | base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), |
[email protected] | 427e48f | 2012-12-05 16:02:24 | [diff] [blame] | 81 | FROM_HERE, |
| 82 | base::Bind(&ReadBrandFromFile), |
| 83 | base::Bind(&SetBrand, callback)); |
| 84 | } |
| 85 | |
| 86 | } // namespace chromeos |
[email protected] | dd3e528a | 2014-06-06 11:41:12 | [diff] [blame] | 87 | } // namespace google_brand |