Re-land: C++ readability review
[chromium-blink-merge.git] / google_apis / gcm / engine / gcm_store_impl.h
blob636b4becea51e33286a180f96c410688915a4e89
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.
5 #ifndef GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
6 #define GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "google_apis/gcm/base/gcm_export.h"
12 #include "google_apis/gcm/engine/gcm_store.h"
14 namespace base {
15 class FilePath;
16 class SequencedTaskRunner;
17 } // namespace base
19 namespace gcm {
21 class Encryptor;
23 // An implementation of GCM Store that uses LevelDB for persistence.
24 // It performs all blocking operations on the blocking task runner, and posts
25 // all callbacks to the thread on which the GCMStoreImpl is created.
26 class GCM_EXPORT GCMStoreImpl : public GCMStore {
27 public:
28 GCMStoreImpl(const base::FilePath& path,
29 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner,
30 scoped_ptr<Encryptor> encryptor);
31 ~GCMStoreImpl() override;
33 // Load the directory and pass the initial state back to caller.
34 void Load(const LoadCallback& callback) override;
36 // Closes the GCM store.
37 void Close() override;
39 // Clears the GCM store of all data and destroys any LevelDB files associated
40 // with this store.
41 // WARNING: this will permanently destroy any pending outgoing messages
42 // and require the device to re-create credentials and serial number mapping
43 // tables.
44 void Destroy(const UpdateCallback& callback) override;
46 // Sets this device's messaging credentials.
47 void SetDeviceCredentials(uint64 device_android_id,
48 uint64 device_security_token,
49 const UpdateCallback& callback) override;
51 // Registration info.
52 void AddRegistration(const std::string& app_id,
53 const linked_ptr<RegistrationInfo>& registration,
54 const UpdateCallback& callback) override;
55 void RemoveRegistration(const std::string& app_id,
56 const UpdateCallback& callback) override;
58 // Unacknowledged incoming message handling.
59 void AddIncomingMessage(const std::string& persistent_id,
60 const UpdateCallback& callback) override;
61 void RemoveIncomingMessage(const std::string& persistent_id,
62 const UpdateCallback& callback) override;
63 void RemoveIncomingMessages(const PersistentIdList& persistent_ids,
64 const UpdateCallback& callback) override;
66 // Unacknowledged outgoing messages handling.
67 bool AddOutgoingMessage(const std::string& persistent_id,
68 const MCSMessage& message,
69 const UpdateCallback& callback) override;
70 void OverwriteOutgoingMessage(const std::string& persistent_id,
71 const MCSMessage& message,
72 const UpdateCallback& callback) override;
73 void RemoveOutgoingMessage(const std::string& persistent_id,
74 const UpdateCallback& callback) override;
75 void RemoveOutgoingMessages(const PersistentIdList& persistent_ids,
76 const UpdateCallback& callback) override;
78 // Sets last device's checkin information.
79 void SetLastCheckinInfo(const base::Time& time,
80 const std::set<std::string>& accounts,
81 const UpdateCallback& callback) override;
83 // G-service settings handling.
84 void SetGServicesSettings(const std::map<std::string, std::string>& settings,
85 const std::string& settings_digest,
86 const UpdateCallback& callback) override;
88 // Sets the account information related to device to account mapping.
89 void AddAccountMapping(const AccountMapping& account_mapping,
90 const UpdateCallback& callback) override;
91 void RemoveAccountMapping(const std::string& account_id,
92 const UpdateCallback& callback) override;
94 // Sets last token fetch time.
95 void SetLastTokenFetchTime(const base::Time& time,
96 const UpdateCallback& callback) override;
98 // Injects a value to database. Only to be used for testing.
99 void SetValueForTesting(const std::string& key,
100 const std::string& value,
101 const UpdateCallback& callback);
103 private:
104 typedef std::map<std::string, int> AppIdToMessageCountMap;
106 // Continuation to update the per-app message counts after a load.
107 void LoadContinuation(const LoadCallback& callback,
108 scoped_ptr<LoadResult> result);
110 // Continuation to update the per-app message counts when adding messages.
111 // In particular, if a message fails to add, the message count is decremented.
112 void AddOutgoingMessageContinuation(const UpdateCallback& callback,
113 const std::string& app_id,
114 bool success);
116 // Continuation to update the per-app message counts when removing messages.
117 // Note: if doing a read-then-write when removing messages proves expensive,
118 // an in-memory mapping of persisted message id to app could be maintained
119 // instead.
120 void RemoveOutgoingMessagesContinuation(
121 const UpdateCallback& callback,
122 bool success,
123 const std::map<std::string, int>& removed_message_counts);
125 class Backend;
127 // Map of App ids to their message counts.
128 AppIdToMessageCountMap app_message_counts_;
130 scoped_refptr<Backend> backend_;
131 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
133 base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
135 DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
138 } // namespace gcm
140 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_