system_salt_getter.h revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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.
4
5#ifndef CHROMEOS_CRYPTOHOME_SYSTEM_SALT_GETTER_H_
6#define CHROMEOS_CRYPTOHOME_SYSTEM_SALT_GETTER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/callback_forward.h"
13#include "base/memory/weak_ptr.h"
14#include "chromeos/chromeos_export.h"
15
16namespace chromeos {
17
18// This class is used to get the system salt from cryptohome and cache it.
19class CHROMEOS_EXPORT SystemSaltGetter {
20 public:
21  typedef base::Callback<void(const std::string& system_salt)>
22      GetSystemSaltCallback;
23
24  // Manage an explicitly initialized global instance.
25  static void Initialize();
26  static bool IsInitialized();
27  static void Shutdown();
28  static SystemSaltGetter* Get();
29
30  // Converts |salt| to a hex encoded string.
31  static std::string ConvertRawSaltToHexString(const std::vector<uint8>& salt);
32
33  // Returns system hash in hex encoded ascii format. Note: this may return
34  // an empty string (e.g. if cryptohome is not running). It is up to the
35  // calling function to try again after a delay if desired.
36  void GetSystemSalt(const GetSystemSaltCallback& callback);
37
38  // Synchronous version of GetSystemSalt().
39  // Blocks the UI thread until the Cryptohome service returns the result.
40  // DEPRECATED: DO NOT USE.
41  std::string GetSystemSaltSync();
42
43  // Returns system hash in hex encoded ascii format, cached by a prior call
44  // to GetSystemSalt(). Note: this may return an empty string (e.g. if
45  // GetSystemSalt() is not yet called).
46  std::string GetCachedSystemSalt();
47
48 protected:
49  SystemSaltGetter();
50  ~SystemSaltGetter();
51
52 private:
53  // Used to implement GetSystemSalt().
54  void GetSystemSaltInternal(const GetSystemSaltCallback& callback,
55                             bool service_is_available);
56
57  // Loads the system salt from cryptohome and caches it.
58  void LoadSystemSalt();
59
60  std::string system_salt_;
61
62  base::WeakPtrFactory<SystemSaltGetter> weak_ptr_factory_;
63
64  DISALLOW_COPY_AND_ASSIGN(SystemSaltGetter);
65};
66
67}  // namespace chromeos
68
69#endif  // CHROMEOS_CRYPTOHOME_SYSTEM_SALT_GETTER_H_
70