Home | History | Annotate | Download | only in engine
      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 time.
     80   virtual void SetLastCheckinTime(const base::Time& last_checkin_time,
     81                                   const UpdateCallback& callback) OVERRIDE;
     82 
     83   // G-service settings handling.
     84   virtual void SetGServicesSettings(
     85       const std::map<std::string, std::string>& settings,
     86       const std::string& settings_digest,
     87       const UpdateCallback& callback) OVERRIDE;
     88 
     89  private:
     90   typedef std::map<std::string, int> AppIdToMessageCountMap;
     91 
     92   // Continuation to update the per-app message counts after a load.
     93   void LoadContinuation(const LoadCallback& callback,
     94                         scoped_ptr<LoadResult> result);
     95 
     96   // Continuation to update the per-app message counts when adding messages.
     97   // In particular, if a message fails to add, the message count is decremented.
     98   void AddOutgoingMessageContinuation(const UpdateCallback& callback,
     99                                       const std::string& app_id,
    100                                       bool success);
    101 
    102   // Continuation to update the per-app message counts when removing messages.
    103   // Note: if doing a read-then-write when removing messages proves expensive,
    104   // an in-memory mapping of persisted message id to app could be maintained
    105   // instead.
    106   void RemoveOutgoingMessagesContinuation(
    107       const UpdateCallback& callback,
    108       bool success,
    109       const std::map<std::string, int>& removed_message_counts);
    110 
    111   class Backend;
    112 
    113   // Map of App ids to their message counts.
    114   AppIdToMessageCountMap app_message_counts_;
    115 
    116   scoped_refptr<Backend> backend_;
    117   scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
    118 
    119   base::WeakPtrFactory<GCMStoreImpl> weak_ptr_factory_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl);
    122 };
    123 
    124 }  // namespace gcm
    125 
    126 #endif  // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
    127