[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / chromeos / cryptohome / system_salt_getter.cc
blob9c2e2449eb8327ae492be634d11fa98a57ae1a6b
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 "chromeos/cryptohome/system_salt_getter.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chromeos/dbus/cryptohome_client.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
16 namespace chromeos {
17 namespace {
19 SystemSaltGetter* g_system_salt_getter = NULL;
21 } // namespace
23 SystemSaltGetter::SystemSaltGetter() : weak_ptr_factory_(this) {
26 SystemSaltGetter::~SystemSaltGetter() {
29 void SystemSaltGetter::GetSystemSalt(
30 const GetSystemSaltCallback& callback) {
31 if (!system_salt_.empty()) {
32 base::ThreadTaskRunnerHandle::Get()->PostTask(
33 FROM_HERE, base::Bind(callback, system_salt_));
34 return;
37 DBusThreadManager::Get()->GetCryptohomeClient()->WaitForServiceToBeAvailable(
38 base::Bind(&SystemSaltGetter::DidWaitForServiceToBeAvailable,
39 weak_ptr_factory_.GetWeakPtr(),
40 callback));
43 void SystemSaltGetter::DidWaitForServiceToBeAvailable(
44 const GetSystemSaltCallback& callback,
45 bool service_is_available) {
46 if (!service_is_available) {
47 LOG(ERROR) << "WaitForServiceToBeAvailable failed.";
48 callback.Run(std::string());
49 return;
51 DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt(
52 base::Bind(&SystemSaltGetter::DidGetSystemSalt,
53 weak_ptr_factory_.GetWeakPtr(),
54 callback));
57 void SystemSaltGetter::DidGetSystemSalt(const GetSystemSaltCallback& callback,
58 DBusMethodCallStatus call_status,
59 const std::vector<uint8>& system_salt) {
60 if (call_status == DBUS_METHOD_CALL_SUCCESS &&
61 !system_salt.empty() &&
62 system_salt.size() % 2 == 0U)
63 system_salt_ = ConvertRawSaltToHexString(system_salt);
64 else
65 LOG(WARNING) << "System salt not available";
67 callback.Run(system_salt_);
70 // static
71 void SystemSaltGetter::Initialize() {
72 CHECK(!g_system_salt_getter);
73 g_system_salt_getter = new SystemSaltGetter();
76 // static
77 bool SystemSaltGetter::IsInitialized() {
78 return g_system_salt_getter;
81 // static
82 void SystemSaltGetter::Shutdown() {
83 CHECK(g_system_salt_getter);
84 delete g_system_salt_getter;
85 g_system_salt_getter = NULL;
88 // static
89 SystemSaltGetter* SystemSaltGetter::Get() {
90 CHECK(g_system_salt_getter)
91 << "SystemSaltGetter::Get() called before Initialize()";
92 return g_system_salt_getter;
95 // static
96 std::string SystemSaltGetter::ConvertRawSaltToHexString(
97 const std::vector<uint8>& salt) {
98 return base::StringToLowerASCII(base::HexEncode(
99 reinterpret_cast<const void*>(salt.data()), salt.size()));
102 } // namespace chromeos