Upstream version 5.34.104.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(bool use_mock_keychain,
27                const base::FilePath& path,
28                scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
29   virtual ~GCMStoreImpl();
30
31   // Load the directory and pass the initial state back to caller.
32   virtual void Load(const LoadCallback& callback) OVERRIDE;
33
34   // Clears the GCM store of all data and destroys any LevelDB files associated
35   // with this store.
36   // WARNING: this will permanently destroy any pending outgoing messages
37   // and require the device to re-create credentials and serial number mapping
38   // tables.
39   virtual void Destroy(const UpdateCallback& callback) OVERRIDE;
40
41   // Sets this device's messaging credentials.
42   virtual void SetDeviceCredentials(uint64 device_android_id,
43                                     uint64 device_security_token,
44                                     const UpdateCallback& callback) OVERRIDE;
45
46   // Unacknowledged incoming message handling.
47   virtual void AddIncomingMessage(const std::string& persistent_id,
48                                   const UpdateCallback& callback) OVERRIDE;
49   virtual void RemoveIncomingMessage(const std::string& persistent_id,
50                                      const UpdateCallback& callback) OVERRIDE;
51   virtual void RemoveIncomingMessages(const PersistentIdList& persistent_ids,
52                                       const UpdateCallback& callback) OVERRIDE;
53
54   // Unacknowledged outgoing messages handling.
55   virtual bool AddOutgoingMessage(const std::string& persistent_id,
56                                   const MCSMessage& message,
57                                   const UpdateCallback& callback) OVERRIDE;
58   virtual void OverwriteOutgoingMessage(const std::string& persistent_id,
59                                         const MCSMessage& message,
60                                         const UpdateCallback& callback)
61       OVERRIDE;
62   virtual void RemoveOutgoingMessage(const std::string& persistent_id,
63                                      const UpdateCallback& callback) OVERRIDE;
64   virtual void RemoveOutgoingMessages(const PersistentIdList& persistent_ids,
65                                       const UpdateCallback& callback) OVERRIDE;
66
67   // User serial number handling.
68   virtual void SetNextSerialNumber(int64 next_serial_number,
69                                    const UpdateCallback& callback) OVERRIDE;
70   virtual void AddUserSerialNumber(const std::string& username,
71                                    int64 serial_number,
72                                    const UpdateCallback& callback) OVERRIDE;
73   virtual void RemoveUserSerialNumber(const std::string& username,
74                                       const UpdateCallback& callback) OVERRIDE;
75
76  private:
77   typedef std::map<std::string, int> AppIdToMessageCountMap;
78
79   // Continuation to update the per-app message counts after a load.
80   void LoadContinuation(const LoadCallback& callback,
81                         scoped_ptr<LoadResult> result);
82
83   // Continuation to update the per-app message counts when adding messages.
84   // In particular, if a message fails to add, the message count is decremented.
85   void AddOutgoingMessageContinuation(const UpdateCallback& callback,
86                                       const std::string& app_id,
87                                       bool success);
88
89   // Continuation to update the per-app message counts when removing messages.
90   // Note: if doing a read-then-write when removing messages proves expensive,
91   // an in-memory mapping of persisted message id to app could be maintained
92   // instead.
93   void RemoveOutgoingMessagesContinuation(
94       const UpdateCallback& callback,
95       bool success,
96       const std::map<std::string, int>& removed_message_counts);
97
98   class Backend;
99
100   // Map of App ids to their message counts.
101   AppIdToMessageCountMap app_message_counts_;
102
103   scoped_refptr<Backend> backend_;
104   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
105
106   base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
107
108   DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
109 };
110
111 }  // namespace gcm
112
113 #endif  // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_