1 // Copyright 2014 The Chromium Authors. All rights reserved. 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" 7 #include "base/files/file_path.h" 8 #include "base/files/file_util.h" 9 #include "base/logging.h" 10 #include "base/prefs/pref_service.h" 11 #include "base/strings/string_util.h" 12 #include "base/task_runner_util.h" 13 #include "base/threading/worker_pool.h" 14 #include "chrome/browser/browser_process.h" 15 #include "chrome/common/pref_names.h" 16 #include "chromeos/system/statistics_provider.h" 17 #include "content/public/browser/browser_thread.h" 18 19 namespace google_brand { 20 namespace chromeos { 21 22 namespace { 23 24 // Path to file that stores the RLZ brand code on ChromeOS. 25 const base::FilePath::CharType kRLZBrandFilePath[] = 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; 31 base::FilePath brand_file_path(kRLZBrandFilePath); 32 if (!base::ReadFileToString(brand_file_path, &brand)) 33 LOG(WARNING) << "Brand code file missing: " << brand_file_path.value(); 34 base::TrimWhitespace(brand, base::TRIM_ALL, &brand); 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 44 // True if brand code has been cleared for the current session. 45 bool g_brand_empty = false; 46 47 } // namespace 48 49 void ClearBrandForCurrentSession() { 50 DCHECK(!content::BrowserThread::IsThreadInitialized( 51 content::BrowserThread::UI) || 52 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 53 g_brand_empty = true; 54 } 55 56 std::string GetBrand() { 57 DCHECK(!content::BrowserThread::IsThreadInitialized( 58 content::BrowserThread::UI) || 59 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 60 if (g_brand_empty) 61 return std::string(); 62 DCHECK(g_browser_process->local_state()); 63 return g_browser_process->local_state()->GetString(prefs::kRLZBrand); 64 } 65 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 77 base::PostTaskAndReplyWithResult( 78 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(), 79 FROM_HERE, 80 base::Bind(&ReadBrandFromFile), 81 base::Bind(&SetBrand, callback)); 82 } 83 84 } // namespace chromeos 85 } // namespace google_brand 86