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.
5 #ifndef GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
6 #define GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_
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"
16 class SequencedTaskRunner
;
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
{
28 GCMStoreImpl(const base::FilePath
& path
,
29 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
,
30 scoped_ptr
<Encryptor
> encryptor
);
31 virtual ~GCMStoreImpl();
33 // Load the directory and pass the initial state back to caller.
34 virtual void Load(const LoadCallback
& callback
) OVERRIDE
;
36 // Closes the GCM store.
37 virtual void Close() OVERRIDE
;
39 // Clears the GCM store of all data and destroys any LevelDB files associated
41 // WARNING: this will permanently destroy any pending outgoing messages
42 // and require the device to re-create credentials and serial number mapping
44 virtual void Destroy(const UpdateCallback
& callback
) OVERRIDE
;
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
;
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
;
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
;
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
)
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
;
79 // Sets last device's checkin time.
80 virtual void SetLastCheckinTime(const base::Time
& last_checkin_time
,
81 const UpdateCallback
& callback
) OVERRIDE
;
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
;
90 typedef std::map
<std::string
, int> AppIdToMessageCountMap
;
92 // Continuation to update the per-app message counts after a load.
93 void LoadContinuation(const LoadCallback
& callback
,
94 scoped_ptr
<LoadResult
> result
);
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
,
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
106 void RemoveOutgoingMessagesContinuation(
107 const UpdateCallback
& callback
,
109 const std::map
<std::string
, int>& removed_message_counts
);
113 // Map of App ids to their message counts.
114 AppIdToMessageCountMap app_message_counts_
;
116 scoped_refptr
<Backend
> backend_
;
117 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
119 base::WeakPtrFactory
<GCMStoreImpl
> weak_ptr_factory_
;
121 DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl
);
126 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_