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_H_
6 #define GOOGLE_APIS_GCM_ENGINE_GCM_STORE_H_
7 
8 #include <google/protobuf/message_lite.h>
9 #include <stdint.h>
10 
11 #include <map>
12 #include <memory>
13 #include <set>
14 #include <string>
15 #include <vector>
16 
17 #include "base/callback_forward.h"
18 #include "base/macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/time/time.h"
21 #include "google_apis/gaia/core_account_id.h"
22 #include "google_apis/gcm/base/gcm_export.h"
23 #include "google_apis/gcm/engine/account_mapping.h"
24 
25 namespace gcm {
26 
27 class MCSMessage;
28 
29 // A GCM data store interface. GCM Store will handle persistence portion of RMQ,
30 // as well as store device and user checkin information.
31 class GCM_EXPORT GCMStore {
32  public:
33   enum StoreOpenMode {
34     DO_NOT_CREATE,
35     CREATE_IF_MISSING
36   };
37 
38   // Map of message id to message data for outgoing messages.
39   using OutgoingMessageMap =
40       std::map<std::string, std::unique_ptr<google::protobuf::MessageLite>>;
41 
42   // List of account mappings.
43   using AccountMappings = std::vector<AccountMapping>;
44 
45   // Container for Load(..) results.
46   struct GCM_EXPORT LoadResult {
47     LoadResult();
48     ~LoadResult();
49 
50     void Reset();
51 
52     bool success;
53     bool store_does_not_exist;
54     uint64_t device_android_id;
55     uint64_t device_security_token;
56     std::map<std::string, std::string> registrations;
57     std::vector<std::string> incoming_messages;
58     OutgoingMessageMap outgoing_messages;
59     std::map<std::string, std::string> gservices_settings;
60     std::string gservices_digest;
61     base::Time last_checkin_time;
62     std::set<std::string> last_checkin_accounts;
63     AccountMappings account_mappings;
64     base::Time last_token_fetch_time;
65     std::map<std::string, int> heartbeat_intervals;
66     std::map<std::string, std::string> instance_id_data;
67   };
68 
69   using PersistentIdList = std::vector<std::string>;
70   using LoadCallback =
71       base::OnceCallback<void(std::unique_ptr<LoadResult> result)>;
72   using UpdateCallback = base::OnceCallback<void(bool success)>;
73 
74   GCMStore();
75   virtual ~GCMStore();
76 
77   // Load the data from persistent store and pass the initial state back to
78   // caller.
79   virtual void Load(StoreOpenMode open_mode, LoadCallback callback) = 0;
80 
81   // Close the persistent store.
82   virtual void Close() = 0;
83 
84   // Clears the GCM store of all data.
85   virtual void Destroy(UpdateCallback callback) = 0;
86 
87   // Sets this device's messaging credentials.
88   virtual void SetDeviceCredentials(uint64_t device_android_id,
89                                     uint64_t device_security_token,
90                                     UpdateCallback callback) = 0;
91 
92   // Registration info for both GCM registrations and InstanceID tokens.
93   // For GCM, |serialized_key| is app_id and |serialized_value| is
94   // serialization of (senders, registration_id). For InstanceID,
95   // |serialized_key| is serialization of (app_id, authorized_entity, scope)
96   // and |serialized_value| is token.
97   virtual void AddRegistration(const std::string& serialized_key,
98                                const std::string& serialized_value,
99                                UpdateCallback callback) = 0;
100   virtual void RemoveRegistration(const std::string& serialized_key,
101                                   UpdateCallback callback) = 0;
102 
103   // Unacknowledged incoming message handling.
104   virtual void AddIncomingMessage(const std::string& persistent_id,
105                                   UpdateCallback callback) = 0;
106   virtual void RemoveIncomingMessage(const std::string& persistent_id,
107                                      UpdateCallback callback) = 0;
108   virtual void RemoveIncomingMessages(const PersistentIdList& persistent_ids,
109                                       UpdateCallback callback) = 0;
110 
111   // Unacknowledged outgoing messages handling.
112   // Returns false if app has surpassed message limits, else returns true. Note
113   // that the message isn't persisted until |callback| is invoked with
114   // |success| == true.
115   virtual bool AddOutgoingMessage(const std::string& persistent_id,
116                                   const MCSMessage& message,
117                                   UpdateCallback callback) = 0;
118   virtual void OverwriteOutgoingMessage(const std::string& persistent_id,
119                                         const MCSMessage& message,
120                                         UpdateCallback callback) = 0;
121   virtual void RemoveOutgoingMessage(const std::string& persistent_id,
122                                      UpdateCallback callback) = 0;
123   virtual void RemoveOutgoingMessages(const PersistentIdList& persistent_ids,
124                                       UpdateCallback callback) = 0;
125 
126   // Sets last device's checkin information.
127   virtual void SetLastCheckinInfo(const base::Time& time,
128                                   const std::set<std::string>& accounts,
129                                   UpdateCallback callback) = 0;
130 
131   // G-service settings handling.
132   // Persists |settings| and |settings_digest|. It completely replaces the
133   // existing data.
134   virtual void SetGServicesSettings(
135       const std::map<std::string, std::string>& settings,
136       const std::string& settings_digest,
137       UpdateCallback callback) = 0;
138 
139   // Sets the account information related to device to account mapping.
140   virtual void AddAccountMapping(const AccountMapping& account_mapping,
141                                  UpdateCallback callback) = 0;
142   virtual void RemoveAccountMapping(const CoreAccountId& account_id,
143                                     UpdateCallback callback) = 0;
144 
145   // Sets last token fetch time.
146   virtual void SetLastTokenFetchTime(const base::Time& time,
147                                      UpdateCallback callback) = 0;
148 
149   // Sets the custom client heartbeat interval for a specified scope.
150   virtual void AddHeartbeatInterval(const std::string& scope,
151                                     int interval_ms,
152                                     UpdateCallback callback) = 0;
153   virtual void RemoveHeartbeatInterval(const std::string& scope,
154                                        UpdateCallback callback) = 0;
155 
156   // Instance ID data.
157   virtual void AddInstanceIDData(const std::string& app_id,
158                                  const std::string& instance_id_data,
159                                  UpdateCallback callback) = 0;
160   virtual void RemoveInstanceIDData(const std::string& app_id,
161                                     UpdateCallback callback) = 0;
162 
163  private:
164   DISALLOW_COPY_AND_ASSIGN(GCMStore);
165 };
166 
167 }  // namespace gcm
168 
169 #endif  // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_H_
170