[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / google / google_util_chromeos.cc
blob5783846ac5be61093ddb5ab4735f1a4b674640ea
1 // Copyright (c) 2012 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"
6 #include "base/bind.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.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_util {
20 namespace chromeos {
22 namespace {
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() {
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;
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();
44 // True if brand code has been cleared for the current session.
45 bool g_brand_empty = false;
47 } // namespace
49 void ClearBrandForCurrentSession() {
50 DCHECK(!content::BrowserThread::IsThreadInitialized(
51 content::BrowserThread::UI) ||
52 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
53 g_brand_empty = true;
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);
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;
77 base::PostTaskAndReplyWithResult(
78 base::WorkerPool::GetTaskRunner(false /* task_is_slow */).get(),
79 FROM_HERE,
80 base::Bind(&ReadBrandFromFile),
81 base::Bind(&SetBrand, callback));
84 } // namespace chromeos
85 } // namespace google_util