Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / google_apis / gcm / engine / gcm_store_impl.h
blobaf89cff4352f2e0c60c8d86e602e81295e8bfa9a
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(StoreOpenMode open_mode, 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& serialized_key,
53 const std::string& serialized_value,
54 const UpdateCallback& callback) override;
55 void RemoveRegistration(const std::string& serialized_key,
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 // Sets the custom client heartbeat interval for the scope.
99 void AddHeartbeatInterval(const std::string& scope,
100 int interval_ms,
101 const UpdateCallback& callback) override;
102 void RemoveHeartbeatInterval(const std::string& scope,
103 const UpdateCallback& callback) override;
105 // Instance ID data.
106 void AddInstanceIDData(const std::string& app_id,
107 const std::string& instance_id_data,
108 const UpdateCallback& callback) override;
109 void RemoveInstanceIDData(const std::string& app_id,
110 const UpdateCallback& callback) override;
112 // Injects a value to database. Only to be used for testing.
113 void SetValueForTesting(const std::string& key,
114 const std::string& value,
115 const UpdateCallback& callback);
117 private:
118 typedef std::map<std::string, int> AppIdToMessageCountMap;
120 // Continuation to update the per-app message counts after a load.
121 void LoadContinuation(const LoadCallback& callback,
122 scoped_ptr<LoadResult> result);
124 // Continuation to update the per-app message counts when adding messages.
125 // In particular, if a message fails to add, the message count is decremented.
126 void AddOutgoingMessageContinuation(const UpdateCallback& callback,
127 const std::string& app_id,
128 bool success);
130 // Continuation to update the per-app message counts when removing messages.
131 // Note: if doing a read-then-write when removing messages proves expensive,
132 // an in-memory mapping of persisted message id to app could be maintained
133 // instead.
134 void RemoveOutgoingMessagesContinuation(
135 const UpdateCallback& callback,
136 bool success,
137 const std::map<std::string, int>& removed_message_counts);
139 class Backend;
141 // Map of App ids to their message counts.
142 AppIdToMessageCountMap app_message_counts_;
144 scoped_refptr<Backend> backend_;
145 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
147 base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
149 DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
152 } // namespace gcm
154 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_