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"
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"
19 SystemSaltGetter
* g_system_salt_getter
= NULL
;
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_
));
37 DBusThreadManager::Get()->GetCryptohomeClient()->WaitForServiceToBeAvailable(
38 base::Bind(&SystemSaltGetter::DidWaitForServiceToBeAvailable
,
39 weak_ptr_factory_
.GetWeakPtr(),
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());
51 DBusThreadManager::Get()->GetCryptohomeClient()->GetSystemSalt(
52 base::Bind(&SystemSaltGetter::DidGetSystemSalt
,
53 weak_ptr_factory_
.GetWeakPtr(),
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
);
65 LOG(WARNING
) << "System salt not available";
67 callback
.Run(system_salt_
);
71 void SystemSaltGetter::Initialize() {
72 CHECK(!g_system_salt_getter
);
73 g_system_salt_getter
= new SystemSaltGetter();
77 bool SystemSaltGetter::IsInitialized() {
78 return g_system_salt_getter
;
82 void SystemSaltGetter::Shutdown() {
83 CHECK(g_system_salt_getter
);
84 delete g_system_salt_getter
;
85 g_system_salt_getter
= NULL
;
89 SystemSaltGetter
* SystemSaltGetter::Get() {
90 CHECK(g_system_salt_getter
)
91 << "SystemSaltGetter::Get() called before Initialize()";
92 return g_system_salt_getter
;
96 std::string
SystemSaltGetter::ConvertRawSaltToHexString(
97 const std::vector
<uint8
>& salt
) {
98 return base::ToLowerASCII(
99 base::HexEncode(reinterpret_cast<const void*>(salt
.data()), salt
.size()));
102 } // namespace chromeos