Home | History | Annotate | Download | only in easy_unlock
      1 // Copyright 2014 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 CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_KEY_MANAGER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_KEY_MANAGER_H_
      7 
      8 #include <deque>
      9 #include <map>
     10 #include <string>
     11 
     12 #include "base/callback.h"
     13 #include "base/macros.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_create_keys_operation.h"
     17 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_get_keys_operation.h"
     18 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_remove_keys_operation.h"
     19 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
     20 
     21 namespace base {
     22 class DictionaryValue;
     23 class ListValue;
     24 }
     25 
     26 namespace chromeos {
     27 
     28 class UserContext;
     29 
     30 // A class to manage Easy unlock cryptohome keys.
     31 class EasyUnlockKeyManager {
     32  public:
     33   typedef EasyUnlockCreateKeysOperation::CreateKeysCallback RefreshKeysCallback;
     34   typedef EasyUnlockRemoveKeysOperation::RemoveKeysCallback RemoveKeysCallback;
     35   typedef EasyUnlockGetKeysOperation::GetKeysCallback GetDeviceDataListCallback;
     36 
     37   EasyUnlockKeyManager();
     38   ~EasyUnlockKeyManager();
     39 
     40   // Nukes existing Easy unlock keys and creates new ones for the given
     41   // |remote_devices| and the given |user_context|. |user_context| must have
     42   // secret to allow keys to be created.
     43   void RefreshKeys(const UserContext& user_context,
     44                    const base::ListValue& remote_devices,
     45                    const RefreshKeysCallback& callback);
     46 
     47   // Remove Easy unlock keys starting at the given index for the given
     48   // |user_context|.
     49   void RemoveKeys(const UserContext& user_context,
     50                   size_t start_index,
     51                   const RemoveKeysCallback& callback);
     52 
     53   // Retrieves the remote device data from cryptohome keys for the given
     54   // |user_context|.
     55   void GetDeviceDataList(const UserContext& user_context,
     56                          const GetDeviceDataListCallback& callback);
     57 
     58   // Helpers to convert between DeviceData and remote device dictionary.
     59   // DeviceDataToRemoteDeviceDictionary fills the remote device dictionary and
     60   // always succeeds. RemoteDeviceDictionaryToDeviceData returns false if the
     61   // conversion fails (missing required propery). Note that
     62   // EasyUnlockDeviceKeyData contains a sub set of the remote device dictionary.
     63   static void DeviceDataToRemoteDeviceDictionary(
     64       const std::string& user_id,
     65       const EasyUnlockDeviceKeyData& data,
     66       base::DictionaryValue* dict);
     67   static bool RemoteDeviceDictionaryToDeviceData(
     68       const base::DictionaryValue& dict,
     69       EasyUnlockDeviceKeyData* data);
     70 
     71   // Helpers to convert between EasyUnlockDeviceKeyDataList and remote devices
     72   // ListValue.
     73   static void DeviceDataListToRemoteDeviceList(
     74       const std::string& user_id,
     75       const EasyUnlockDeviceKeyDataList& data_list,
     76       base::ListValue* device_list);
     77   static bool RemoteDeviceListToDeviceDataList(
     78       const base::ListValue& device_list,
     79       EasyUnlockDeviceKeyDataList* data_list);
     80 
     81   // Gets key label for the given key index.
     82   static std::string GetKeyLabel(size_t key_index);
     83 
     84  private:
     85   // Returns true if there are pending operations.
     86   bool HasPendingOperations() const;
     87 
     88   // Returns the next operations id. Currently only used for get keys ops.
     89   int GetNextOperationId();
     90 
     91   // Runs the first pending op in |pending_ops_|. No-op if |pending_ops_| is
     92   // emtpy.
     93   void RunNextPendingOp();
     94 
     95   // Callback invoked after create keys op.
     96   void OnKeysCreated(size_t remove_start_index,
     97                      const RefreshKeysCallback& callback,
     98                      bool create_success);
     99 
    100   // Callback invoked after remove keys op.
    101   void OnKeysRemoved(const RemoveKeysCallback& callback, bool remove_success);
    102 
    103   // Callback invoked after get keys op.
    104   void OnKeysFetched(int op_id,
    105                      const GetDeviceDataListCallback& callback,
    106                      bool fetch_success,
    107                      const EasyUnlockDeviceKeyDataList& fetched_data);
    108 
    109   int operation_id_;
    110 
    111   scoped_ptr<EasyUnlockCreateKeysOperation> create_keys_op_;
    112   scoped_ptr<EasyUnlockRemoveKeysOperation> remove_keys_op_;
    113   std::map<int, EasyUnlockGetKeysOperation*> get_keys_ops_;
    114 
    115   std::deque<base::Closure> pending_ops_;
    116 
    117   base::WeakPtrFactory<EasyUnlockKeyManager> weak_ptr_factory_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(EasyUnlockKeyManager);
    120 };
    121 
    122 }  // namespace chromeos
    123 
    124 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_EASY_UNLOCK_KEY_MANAGER_H_
    125