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"
16 class SequencedTaskRunner;
21 // An implementation of GCM Store that uses LevelDB for persistence.
22 // It performs all blocking operations on the blocking task runner, and posts
23 // all callbacks to the thread on which the GCMStoreImpl is created.
24 class GCM_EXPORT GCMStoreImpl : public GCMStore {
26 GCMStoreImpl(const base::FilePath& path,
27 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
28 virtual ~GCMStoreImpl();
30 // Load the directory and pass the initial state back to caller.
31 virtual void Load(const LoadCallback& callback) OVERRIDE;
33 // Closes the GCM store.
34 virtual void Close() OVERRIDE;
36 // Clears the GCM store of all data and destroys any LevelDB files associated
38 // WARNING: this will permanently destroy any pending outgoing messages
39 // and require the device to re-create credentials and serial number mapping
41 virtual void Destroy(const UpdateCallback& callback) OVERRIDE;
43 // Sets this device's messaging credentials.
44 virtual void SetDeviceCredentials(uint64 device_android_id,
45 uint64 device_security_token,
46 const UpdateCallback& callback) OVERRIDE;
49 virtual void AddRegistration(const std::string& app_id,
50 const linked_ptr<RegistrationInfo>& registration,
51 const UpdateCallback& callback) OVERRIDE;
52 virtual void RemoveRegistration(const std::string& app_id,
53 const UpdateCallback& callback) OVERRIDE;
55 // Unacknowledged incoming message handling.
56 virtual void AddIncomingMessage(const std::string& persistent_id,
57 const UpdateCallback& callback) OVERRIDE;
58 virtual void RemoveIncomingMessage(const std::string& persistent_id,
59 const UpdateCallback& callback) OVERRIDE;
60 virtual void RemoveIncomingMessages(const PersistentIdList& persistent_ids,
61 const UpdateCallback& callback) OVERRIDE;
63 // Unacknowledged outgoing messages handling.
64 virtual bool AddOutgoingMessage(const std::string& persistent_id,
65 const MCSMessage& message,
66 const UpdateCallback& callback) OVERRIDE;
67 virtual void OverwriteOutgoingMessage(const std::string& persistent_id,
68 const MCSMessage& message,
69 const UpdateCallback& callback)
71 virtual void RemoveOutgoingMessage(const std::string& persistent_id,
72 const UpdateCallback& callback) OVERRIDE;
73 virtual void RemoveOutgoingMessages(const PersistentIdList& persistent_ids,
74 const UpdateCallback& callback) OVERRIDE;
76 // Sets last device's checkin time.
77 virtual void SetLastCheckinTime(const base::Time& last_checkin_time,
78 const UpdateCallback& callback) OVERRIDE;
80 // G-service settings handling.
81 virtual void SetGServicesSettings(
82 const std::map<std::string, std::string>& settings,
83 const std::string& settings_digest,
84 const UpdateCallback& callback) OVERRIDE;
87 typedef std::map<std::string, int> AppIdToMessageCountMap;
89 // Continuation to update the per-app message counts after a load.
90 void LoadContinuation(const LoadCallback& callback,
91 scoped_ptr<LoadResult> result);
93 // Continuation to update the per-app message counts when adding messages.
94 // In particular, if a message fails to add, the message count is decremented.
95 void AddOutgoingMessageContinuation(const UpdateCallback& callback,
96 const std::string& app_id,
99 // Continuation to update the per-app message counts when removing messages.
100 // Note: if doing a read-then-write when removing messages proves expensive,
101 // an in-memory mapping of persisted message id to app could be maintained
103 void RemoveOutgoingMessagesContinuation(
104 const UpdateCallback& callback,
106 const std::map<std::string, int>& removed_message_counts);
110 // Map of App ids to their message counts.
111 AppIdToMessageCountMap app_message_counts_;
113 scoped_refptr<Backend> backend_;
114 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
116 base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
118 DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
123 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_