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.
5 #include "base/basictypes.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"
19 namespace google_brand
{
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");
28 // Reads the brand code from file |kRLZBrandFilePath|.
29 std::string
ReadBrandFromFile() {
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
);
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
);
44 // True if brand code has been cleared for the current session.
45 bool g_brand_empty
= false;
49 void ClearBrandForCurrentSession() {
50 DCHECK(!content::BrowserThread::IsThreadInitialized(
51 content::BrowserThread::UI
) ||
52 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
56 std::string
GetBrand() {
57 DCHECK(!content::BrowserThread::IsThreadInitialized(
58 content::BrowserThread::UI
) ||
59 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
62 // Unit tests do not have prefs.
63 if (!g_browser_process
->local_state())
65 return g_browser_process
->local_state()->GetString(prefs::kRLZBrand
);
68 void InitBrand(const base::Closure
& callback
) {
69 ::chromeos::system::StatisticsProvider
* provider
=
70 ::chromeos::system::StatisticsProvider::GetInstance();
72 const bool found
= provider
->GetMachineStatistic(
73 ::chromeos::system::kRlzBrandCodeKey
, &brand
);
74 if (found
&& !brand
.empty()) {
75 SetBrand(callback
, brand
);
79 base::PostTaskAndReplyWithResult(
80 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(),
82 base::Bind(&ReadBrandFromFile
),
83 base::Bind(&SetBrand
, callback
));
86 } // namespace chromeos
87 } // namespace google_brand