blob: 8ccec25a6a292428af1c466227b473f48c4ad1aa [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
5#include "base/basictypes.h"
6#include "base/bind.h"
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]427e48f2012-12-05 16:02:249#include "base/logging.h"
[email protected]3853a4c2013-02-11 17:15:5710#include "base/prefs/pref_service.h"
[email protected]d8830562013-06-10 22:01:5411#include "base/strings/string_util.h"
[email protected]427e48f2012-12-05 16:02:2412#include "base/task_runner_util.h"
13#include "base/threading/worker_pool.h"
14#include "chrome/browser/browser_process.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"
[email protected]427e48f2012-12-05 16:02:2417#include "content/public/browser/browser_thread.h"
18
[email protected]dd3e528a2014-06-06 11:41:1219namespace google_brand {
[email protected]427e48f2012-12-05 16:02:2420namespace chromeos {
21
22namespace {
23
24// Path to file that stores the RLZ brand code on ChromeOS.
[email protected]a7e1dc02013-02-14 15:00:4725const base::FilePath::CharType kRLZBrandFilePath[] =
[email protected]427e48f2012-12-05 16:02:2426 FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE");
27
28// Reads the brand code from file |kRLZBrandFilePath|.
29std::string ReadBrandFromFile() {
30 std::string brand;
[email protected]650b2d52013-02-10 03:41:4531 base::FilePath brand_file_path(kRLZBrandFilePath);
[email protected]82f84b92013-08-30 18:23:5032 if (!base::ReadFileToString(brand_file_path, &brand))
[email protected]427e48f2012-12-05 16:02:2433 LOG(WARNING) << "Brand code file missing: " << brand_file_path.value();
[email protected]8af69c6c2014-03-03 19:05:3134 base::TrimWhitespace(brand, base::TRIM_ALL, &brand);
[email protected]427e48f2012-12-05 16:02:2435 return brand;
36}
37
38// Sets the brand code to |brand|.
39void 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]b2574e82012-12-17 15:58:2644// True if brand code has been cleared for the current session.
45bool g_brand_empty = false;
46
[email protected]427e48f2012-12-05 16:02:2447} // namespace
48
[email protected]b2574e82012-12-17 15:58:2649void ClearBrandForCurrentSession() {
[email protected]33d06242013-08-12 05:20:3050 DCHECK(!content::BrowserThread::IsThreadInitialized(
51 content::BrowserThread::UI) ||
52 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2653 g_brand_empty = true;
54}
55
[email protected]427e48f2012-12-05 16:02:2456std::string GetBrand() {
[email protected]33d06242013-08-12 05:20:3057 DCHECK(!content::BrowserThread::IsThreadInitialized(
58 content::BrowserThread::UI) ||
59 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2660 if (g_brand_empty)
61 return std::string();
jamescook8747d18f2014-11-05 01:29:0662 // Unit tests do not have prefs.
63 if (!g_browser_process->local_state())
64 return std::string();
[email protected]427e48f2012-12-05 16:02:2465 return g_browser_process->local_state()->GetString(prefs::kRLZBrand);
66}
67
[email protected]1a32f302014-03-05 14:20:2668void 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]427e48f2012-12-05 16:02:2479 base::PostTaskAndReplyWithResult(
[email protected]144b6c42013-06-14 07:30:3880 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(),
[email protected]427e48f2012-12-05 16:02:2481 FROM_HERE,
82 base::Bind(&ReadBrandFromFile),
83 base::Bind(&SetBrand, callback));
84}
85
86} // namespace chromeos
[email protected]dd3e528a2014-06-06 11:41:1287} // namespace google_brand