Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / google_apis / gcm / engine / gcm_store_impl.h
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 GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
6 #define GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
7
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"
13
14 namespace base {
15 class FilePath;
16 class SequencedTaskRunner;
17 }  // namespace base
18
19 namespace gcm {
20
21 class Encryptor;
22
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;
32
33   // Load the directory and pass the initial state back to caller.
34   void Load(const LoadCallback& callback) override;
35
36   // Closes the GCM store.
37   void Close() override;
38
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;
45
46   // Sets this device's messaging credentials.
47   void SetDeviceCredentials(uint64 device_android_id,
48                             uint64 device_security_token,
49                             const UpdateCallback& callback) override;
50
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;
57
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;
65
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;
77
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;
82
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;
87
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;
93
94   // Sets last token fetch time.
95   void SetLastTokenFetchTime(const base::Time& time,
96                              const UpdateCallback& callback) override;
97
98  private:
99   typedef std::map<std::string, int> AppIdToMessageCountMap;
100
101   // Continuation to update the per-app message counts after a load.
102   void LoadContinuation(const LoadCallback& callback,
103                         scoped_ptr<LoadResult> result);
104
105   // Continuation to update the per-app message counts when adding messages.
106   // In particular, if a message fails to add, the message count is decremented.
107   void AddOutgoingMessageContinuation(const UpdateCallback& callback,
108                                       const std::string& app_id,
109                                       bool success);
110
111   // Continuation to update the per-app message counts when removing messages.
112   // Note: if doing a read-then-write when removing messages proves expensive,
113   // an in-memory mapping of persisted message id to app could be maintained
114   // instead.
115   void RemoveOutgoingMessagesContinuation(
116       const UpdateCallback& callback,
117       bool success,
118       const std::map<std::string, int>& removed_message_counts);
119
120   class Backend;
121
122   // Map of App ids to their message counts.
123   AppIdToMessageCountMap app_message_counts_;
124
125   scoped_refptr<Backend> backend_;
126   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
127
128   base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
129
130   DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
131 };
132
133 }  // namespace gcm
134
135 #endif  // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_