blob: c0eb9641356953265b82efc6395b535faa8265fe [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
[email protected]427e48f2012-12-05 16:02:245#include "base/bind.h"
[email protected]57999812013-02-24 05:40:526#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:047#include "base/files/file_util.h"
[email protected]427e48f2012-12-05 16:02:248#include "base/logging.h"
[email protected]d8830562013-06-10 22:01:549#include "base/strings/string_util.h"
Gabriel Charetteffaaf85f2018-08-03 19:20:4710#include "base/task/post_task_forward.h"
[email protected]427e48f2012-12-05 16:02:2411#include "chrome/browser/browser_process.h"
[email protected]427e48f2012-12-05 16:02:2412#include "chrome/common/pref_names.h"
[email protected]1a32f302014-03-05 14:20:2613#include "chromeos/system/statistics_provider.h"
brettwb1fc1b82016-02-02 00:19:0814#include "components/prefs/pref_service.h"
[email protected]427e48f2012-12-05 16:02:2415#include "content/public/browser/browser_thread.h"
16
[email protected]dd3e528a2014-06-06 11:41:1217namespace google_brand {
[email protected]427e48f2012-12-05 16:02:2418namespace chromeos {
19
20namespace {
21
22// Path to file that stores the RLZ brand code on ChromeOS.
[email protected]a7e1dc02013-02-14 15:00:4723const base::FilePath::CharType kRLZBrandFilePath[] =
[email protected]427e48f2012-12-05 16:02:2424 FILE_PATH_LITERAL("/opt/oem/etc/BRAND_CODE");
25
26// Reads the brand code from file |kRLZBrandFilePath|.
27std::string ReadBrandFromFile() {
28 std::string brand;
[email protected]650b2d52013-02-10 03:41:4529 base::FilePath brand_file_path(kRLZBrandFilePath);
[email protected]82f84b92013-08-30 18:23:5030 if (!base::ReadFileToString(brand_file_path, &brand))
[email protected]427e48f2012-12-05 16:02:2431 LOG(WARNING) << "Brand code file missing: " << brand_file_path.value();
tfarina023b1dcc2015-12-06 13:25:4132 base::TrimWhitespaceASCII(brand, base::TRIM_ALL, &brand);
[email protected]427e48f2012-12-05 16:02:2433 return brand;
34}
35
36// Sets the brand code to |brand|.
37void SetBrand(const base::Closure& callback, const std::string& brand) {
38 g_browser_process->local_state()->SetString(prefs::kRLZBrand, brand);
39 callback.Run();
40}
41
[email protected]b2574e82012-12-17 15:58:2642// True if brand code has been cleared for the current session.
43bool g_brand_empty = false;
44
[email protected]427e48f2012-12-05 16:02:2445} // namespace
46
[email protected]b2574e82012-12-17 15:58:2647void ClearBrandForCurrentSession() {
[email protected]33d06242013-08-12 05:20:3048 DCHECK(!content::BrowserThread::IsThreadInitialized(
49 content::BrowserThread::UI) ||
50 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2651 g_brand_empty = true;
52}
53
[email protected]427e48f2012-12-05 16:02:2454std::string GetBrand() {
[email protected]33d06242013-08-12 05:20:3055 DCHECK(!content::BrowserThread::IsThreadInitialized(
56 content::BrowserThread::UI) ||
57 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]b2574e82012-12-17 15:58:2658 if (g_brand_empty)
59 return std::string();
jamescook8747d18f2014-11-05 01:29:0660 // Unit tests do not have prefs.
61 if (!g_browser_process->local_state())
62 return std::string();
[email protected]427e48f2012-12-05 16:02:2463 return g_browser_process->local_state()->GetString(prefs::kRLZBrand);
64}
65
[email protected]1a32f302014-03-05 14:20:2666void 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
fdoray6c004622016-12-02 23:08:3877 base::PostTaskWithTraitsAndReplyWithResult(
fdoray4ad14932017-05-03 21:21:1178 FROM_HERE,
Gabriel Charetteb10aeebc2018-07-26 20:15:0079 {base::MayBlock(), base::TaskPriority::BEST_EFFORT,
fdoray4ad14932017-05-03 21:21:1180 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN},
fdoray6c004622016-12-02 23:08:3881 base::Bind(&ReadBrandFromFile), base::Bind(&SetBrand, callback));
[email protected]427e48f2012-12-05 16:02:2482}
83
84} // namespace chromeos
[email protected]dd3e528a2014-06-06 11:41:1285} // namespace google_brand