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 virtual ~GCMStoreImpl(); 32 33 // Load the directory and pass the initial state back to caller. 34 virtual void Load(const LoadCallback& callback) OVERRIDE; 35 36 // Closes the GCM store. 37 virtual 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 virtual void Destroy(const UpdateCallback& callback) OVERRIDE; 45 46 // Sets this device's messaging credentials. 47 virtual void SetDeviceCredentials(uint64 device_android_id, 48 uint64 device_security_token, 49 const UpdateCallback& callback) OVERRIDE; 50 51 // Registration info. 52 virtual void AddRegistration(const std::string& app_id, 53 const linked_ptr<RegistrationInfo>& registration, 54 const UpdateCallback& callback) OVERRIDE; 55 virtual void RemoveRegistration(const std::string& app_id, 56 const UpdateCallback& callback) OVERRIDE; 57 58 // Unacknowledged incoming message handling. 59 virtual void AddIncomingMessage(const std::string& persistent_id, 60 const UpdateCallback& callback) OVERRIDE; 61 virtual void RemoveIncomingMessage(const std::string& persistent_id, 62 const UpdateCallback& callback) OVERRIDE; 63 virtual void RemoveIncomingMessages(const PersistentIdList& persistent_ids, 64 const UpdateCallback& callback) OVERRIDE; 65 66 // Unacknowledged outgoing messages handling. 67 virtual bool AddOutgoingMessage(const std::string& persistent_id, 68 const MCSMessage& message, 69 const UpdateCallback& callback) OVERRIDE; 70 virtual void OverwriteOutgoingMessage(const std::string& persistent_id, 71 const MCSMessage& message, 72 const UpdateCallback& callback) 73 OVERRIDE; 74 virtual void RemoveOutgoingMessage(const std::string& persistent_id, 75 const UpdateCallback& callback) OVERRIDE; 76 virtual void RemoveOutgoingMessages(const PersistentIdList& persistent_ids, 77 const UpdateCallback& callback) OVERRIDE; 78 79 // Sets last device's checkin information. 80 virtual void SetLastCheckinInfo(const base::Time& time, 81 const std::set<std::string>& accounts, 82 const UpdateCallback& callback) OVERRIDE; 83 84 // G-service settings handling. 85 virtual void SetGServicesSettings( 86 const std::map<std::string, std::string>& settings, 87 const std::string& settings_digest, 88 const UpdateCallback& callback) OVERRIDE; 89 90 // Sets the account information related to device to account mapping. 91 virtual void AddAccountMapping(const AccountMapping& account_mapping, 92 const UpdateCallback& callback) OVERRIDE; 93 virtual void RemoveAccountMapping(const std::string& account_id, 94 const UpdateCallback& callback) OVERRIDE; 95 96 private: 97 typedef std::map<std::string, int> AppIdToMessageCountMap; 98 99 // Continuation to update the per-app message counts after a load. 100 void LoadContinuation(const LoadCallback& callback, 101 scoped_ptr<LoadResult> result); 102 103 // Continuation to update the per-app message counts when adding messages. 104 // In particular, if a message fails to add, the message count is decremented. 105 void AddOutgoingMessageContinuation(const UpdateCallback& callback, 106 const std::string& app_id, 107 bool success); 108 109 // Continuation to update the per-app message counts when removing messages. 110 // Note: if doing a read-then-write when removing messages proves expensive, 111 // an in-memory mapping of persisted message id to app could be maintained 112 // instead. 113 void RemoveOutgoingMessagesContinuation( 114 const UpdateCallback& callback, 115 bool success, 116 const std::map<std::string, int>& removed_message_counts); 117 118 class Backend; 119 120 // Map of App ids to their message counts. 121 AppIdToMessageCountMap app_message_counts_; 122 123 scoped_refptr<Backend> backend_; 124 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 125 126 base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_; 127 128 DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl); 129 }; 130 131 } // namespace gcm 132 133 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_ 134