cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / gcm_driver / crypto / gcm_key_store.h
blob41de906d05c658697c443f5b8d66fc51a77ff478
1 // Copyright 2015 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 #ifndef COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_
6 #define COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback_forward.h"
13 #include "base/files/file_path.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/gcm_driver/crypto/proto/gcm_encryption_data.pb.h"
19 #include "components/gcm_driver/gcm_delayed_task_controller.h"
21 namespace base {
22 class SequencedTaskRunner;
25 namespace leveldb_proto {
26 template <typename T>
27 class ProtoDatabase;
30 namespace gcm {
32 // Key storage for use with encrypted messages received from Google Cloud
33 // Messaging. It provides the ability to create and store a key-pair for a given
34 // app id, as well as retrieving and deleting key-pairs.
36 // This class is backed by a proto database and might end up doing file I/O on
37 // a background task runner. For this reason, all public APIs take a callback
38 // rather than returning the result. Do not rely on the timing of the callbacks.
39 class GCMKeyStore {
40 public:
41 using KeysCallback = base::Callback<void(const KeyPair& pair)>;
42 using DeleteCallback = base::Callback<void(bool success)>;
44 GCMKeyStore(
45 const base::FilePath& key_store_path,
46 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner);
47 ~GCMKeyStore();
49 // Retrieves the public/private key-pair associated with |app_id|, and
50 // invokes |callback| when they are available, or when an error occurred.
51 void GetKeys(const std::string& app_id, const KeysCallback& callback);
53 // Creates a new public/private key-pair for |app_id|, and invokes
54 // |callback| when they are available, or when an error occurred.
55 void CreateKeys(const std::string& app_id, const KeysCallback& callback);
57 // Deletes the keys associated with |app_id|, and invokes |callback| when
58 // the deletion has finished, or when an error occurred.
59 void DeleteKeys(const std::string& app_id, const DeleteCallback& callback);
61 private:
62 // Initializes the database if necessary, and runs |done_closure| when done.
63 void LazyInitialize(const base::Closure& done_closure);
65 void DidInitialize(bool success);
66 void DidLoadKeys(bool success,
67 scoped_ptr<std::vector<EncryptionData>> entries);
69 void DidStoreKeys(const std::string& app_id,
70 const KeyPair& pair,
71 const KeysCallback& callback,
72 bool success);
74 void DidDeleteKeys(const std::string& app_id,
75 const DeleteCallback& callback,
76 bool success);
78 // Private implementations of the API that will be executed when the database
79 // has either been successfully loaded, or failed to load.
81 void GetKeysAfterInitialize(const std::string& app_id,
82 const KeysCallback& callback);
83 void CreateKeysAfterInitialize(const std::string& app_id,
84 const KeysCallback& callback);
85 void DeleteKeysAfterInitialize(const std::string& app_id,
86 const DeleteCallback& callback);
88 // Path in which the key store database will be saved.
89 base::FilePath key_store_path_;
91 // Blocking task runner which the database will do I/O operations on.
92 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
94 // Instance of the ProtoDatabase backing the key store.
95 scoped_ptr<leveldb_proto::ProtoDatabase<EncryptionData>> database_;
97 enum class State;
99 // The current state of the database. It has to be initialized before use.
100 State state_;
102 // Controller for tasks that should be executed once the key store has
103 // finished initializing.
104 GCMDelayedTaskController delayed_task_controller_;
106 // Mapping of an app id to the loaded EncryptedData structure.
107 std::map<std::string, KeyPair> key_pairs_;
109 base::WeakPtrFactory<GCMKeyStore> weak_factory_;
111 DISALLOW_COPY_AND_ASSIGN(GCMKeyStore);
114 } // namespace gcm
116 #endif // COMPONENTS_GCM_DRIVER_CRYPTO_GCM_KEY_STORE_H_