Upstream version 7.36.149.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 // 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 {
25  public:
26   GCMStoreImpl(const base::FilePath& path,
27                scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
28   virtual ~GCMStoreImpl();
29
30   // Load the directory and pass the initial state back to caller.
31   virtual void Load(const LoadCallback& callback) OVERRIDE;
32
33   // Closes the GCM store.
34   virtual void Close() OVERRIDE;
35
36   // Clears the GCM store of all data and destroys any LevelDB files associated
37   // with this store.
38   // WARNING: this will permanently destroy any pending outgoing messages
39   // and require the device to re-create credentials and serial number mapping
40   // tables.
41   virtual void Destroy(const UpdateCallback& callback) OVERRIDE;
42
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;
47
48   // Registration info.
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;
54
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;
62
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)
70       OVERRIDE;
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;
75
76   // Sets last device's checkin time.
77   virtual void SetLastCheckinTime(const base::Time& last_checkin_time,
78                                   const UpdateCallback& callback) OVERRIDE;
79
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;
85
86  private:
87   typedef std::map<std::string, int> AppIdToMessageCountMap;
88
89   // Continuation to update the per-app message counts after a load.
90   void LoadContinuation(const LoadCallback& callback,
91                         scoped_ptr<LoadResult> result);
92
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,
97                                       bool success);
98
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
102   // instead.
103   void RemoveOutgoingMessagesContinuation(
104       const UpdateCallback& callback,
105       bool success,
106       const std::map<std::string, int>& removed_message_counts);
107
108   class Backend;
109
110   // Map of App ids to their message counts.
111   AppIdToMessageCountMap app_message_counts_;
112
113   scoped_refptr<Backend> backend_;
114   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
115
116   base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
117
118   DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
119 };
120
121 }  // namespace gcm
122
123 #endif  // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_