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 <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include <google/protobuf/message_lite.h>
14
15#include "base/basictypes.h"
16#include "base/callback_forward.h"
17#include "base/memory/linked_ptr.h"
18#include "base/memory/ref_counted.h"
19#include "base/memory/scoped_ptr.h"
20#include "base/time/time.h"
21#include "google_apis/gcm/base/gcm_export.h"
22#include "google_apis/gcm/engine/account_mapping.h"
23#include "google_apis/gcm/engine/registration_info.h"
24
25namespace gcm {
26
27class 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.
31class GCM_EXPORT GCMStore {
32 public:
33  // Map of message id to message data for outgoing messages.
34  typedef std::map<std::string, linked_ptr<google::protobuf::MessageLite> >
35      OutgoingMessageMap;
36
37  // List of account mappings.
38  typedef std::vector<AccountMapping> AccountMappings;
39
40  // Container for Load(..) results.
41  struct GCM_EXPORT LoadResult {
42    LoadResult();
43    ~LoadResult();
44
45    void Reset();
46
47    bool success;
48    uint64 device_android_id;
49    uint64 device_security_token;
50    RegistrationInfoMap registrations;
51    std::vector<std::string> incoming_messages;
52    OutgoingMessageMap outgoing_messages;
53    std::map<std::string, std::string> gservices_settings;
54    std::string gservices_digest;
55    base::Time last_checkin_time;
56    std::set<std::string> last_checkin_accounts;
57    AccountMappings account_mappings;
58  };
59
60  typedef std::vector<std::string> PersistentIdList;
61  typedef base::Callback<void(scoped_ptr<LoadResult> result)> LoadCallback;
62  typedef base::Callback<void(bool success)> UpdateCallback;
63
64  GCMStore();
65  virtual ~GCMStore();
66
67  // Load the data from persistent store and pass the initial state back to
68  // caller.
69  virtual void Load(const LoadCallback& callback) = 0;
70
71  // Close the persistent store.
72  virtual void Close() = 0;
73
74  // Clears the GCM store of all data.
75  virtual void Destroy(const UpdateCallback& callback) = 0;
76
77  // Sets this device's messaging credentials.
78  virtual void SetDeviceCredentials(uint64 device_android_id,
79                                    uint64 device_security_token,
80                                    const UpdateCallback& callback) = 0;
81
82  // Registration info.
83  virtual void AddRegistration(const std::string& app_id,
84                               const linked_ptr<RegistrationInfo>& registration,
85                               const UpdateCallback& callback) = 0;
86  virtual void RemoveRegistration(const std::string& app_id,
87                                  const UpdateCallback& callback) = 0;
88
89  // Unacknowledged incoming message handling.
90  virtual void AddIncomingMessage(const std::string& persistent_id,
91                                  const UpdateCallback& callback) = 0;
92  virtual void RemoveIncomingMessage(const std::string& persistent_id,
93                                     const UpdateCallback& callback) = 0;
94  virtual void RemoveIncomingMessages(const PersistentIdList& persistent_ids,
95                                      const UpdateCallback& callback) = 0;
96
97  // Unacknowledged outgoing messages handling.
98  // Returns false if app has surpassed message limits, else returns true. Note
99  // that the message isn't persisted until |callback| is invoked with
100  // |success| == true.
101  virtual bool AddOutgoingMessage(const std::string& persistent_id,
102                                  const MCSMessage& message,
103                                  const UpdateCallback& callback) = 0;
104  virtual void OverwriteOutgoingMessage(const std::string& persistent_id,
105                                        const MCSMessage& message,
106                                        const UpdateCallback& callback) = 0;
107  virtual void RemoveOutgoingMessage(const std::string& persistent_id,
108                                     const UpdateCallback& callback) = 0;
109  virtual void RemoveOutgoingMessages(const PersistentIdList& persistent_ids,
110                                      const UpdateCallback& callback) = 0;
111
112  // Sets last device's checkin information.
113  virtual void SetLastCheckinInfo(const base::Time& time,
114                                  const std::set<std::string>& accounts,
115                                  const UpdateCallback& callback) = 0;
116
117  // G-service settings handling.
118  // Persists |settings| and |settings_digest|. It completely replaces the
119  // existing data.
120  virtual void SetGServicesSettings(
121      const std::map<std::string, std::string>& settings,
122      const std::string& settings_digest,
123      const UpdateCallback& callback) = 0;
124
125  // Sets the account information related to device to account mapping.
126  virtual void AddAccountMapping(const AccountMapping& account_mapping,
127                                 const UpdateCallback& callback) = 0;
128  virtual void RemoveAccountMapping(const std::string& account_id,
129                                    const UpdateCallback& callback) = 0;
130
131 private:
132  DISALLOW_COPY_AND_ASSIGN(GCMStore);
133};
134
135}  // namespace gcm
136
137#endif  // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_H_
138