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
;
21 // An implementation of GCM Store that uses LevelDB for persistence.
22 // It performs all blocking operations on the blocking task runner, and posts
23 // all callbacks to the thread on which the GCMStoreImpl is created.
24 class GCM_EXPORT GCMStoreImpl
: public GCMStore
{
26 GCMStoreImpl(bool use_mock_keychain
,
27 const base::FilePath
& path
,
28 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
29 virtual ~GCMStoreImpl();
31 // Load the directory and pass the initial state back to caller.
32 virtual void Load(const LoadCallback
& callback
) OVERRIDE
;
34 // Clears the GCM store of all data and destroys any LevelDB files associated
36 // WARNING: this will permanently destroy any pending outgoing messages
37 // and require the device to re-create credentials and serial number mapping
39 virtual void Destroy(const UpdateCallback
& callback
) OVERRIDE
;
41 // Sets this device's messaging credentials.
42 virtual void SetDeviceCredentials(uint64 device_android_id
,
43 uint64 device_security_token
,
44 const UpdateCallback
& callback
) OVERRIDE
;
46 // Unacknowledged incoming message handling.
47 virtual void AddIncomingMessage(const std::string
& persistent_id
,
48 const UpdateCallback
& callback
) OVERRIDE
;
49 virtual void RemoveIncomingMessage(const std::string
& persistent_id
,
50 const UpdateCallback
& callback
) OVERRIDE
;
51 virtual void RemoveIncomingMessages(const PersistentIdList
& persistent_ids
,
52 const UpdateCallback
& callback
) OVERRIDE
;
54 // Unacknowledged outgoing messages handling.
55 virtual bool AddOutgoingMessage(const std::string
& persistent_id
,
56 const MCSMessage
& message
,
57 const UpdateCallback
& callback
) OVERRIDE
;
58 virtual void RemoveOutgoingMessage(const std::string
& persistent_id
,
59 const UpdateCallback
& callback
) OVERRIDE
;
60 virtual void RemoveOutgoingMessages(const PersistentIdList
& persistent_ids
,
61 const UpdateCallback
& callback
) OVERRIDE
;
63 // User serial number handling.
64 virtual void SetNextSerialNumber(int64 next_serial_number
,
65 const UpdateCallback
& callback
) OVERRIDE
;
66 virtual void AddUserSerialNumber(const std::string
& username
,
68 const UpdateCallback
& callback
) OVERRIDE
;
69 virtual void RemoveUserSerialNumber(const std::string
& username
,
70 const UpdateCallback
& callback
) OVERRIDE
;
73 typedef std::map
<std::string
, int> AppIdToMessageCountMap
;
75 // Continuation to update the per-app message counts after a load.
76 void LoadContinuation(const LoadCallback
& callback
,
77 scoped_ptr
<LoadResult
> result
);
79 // Continuation to update the per-app message counts when adding messages.
80 // In particular, if a message fails to add, the message count is decremented.
81 void AddOutgoingMessageContinuation(const UpdateCallback
& callback
,
82 const std::string
& app_id
,
85 // Continuation to update the per-app message counts when removing messages.
86 // Note: if doing a read-then-write when removing messages proves expensive,
87 // an in-memory mapping of persisted message id to app could be maintained
89 void RemoveOutgoingMessagesContinuation(
90 const UpdateCallback
& callback
,
92 const std::map
<std::string
, int>& removed_message_counts
);
96 // Map of App ids to their message counts.
97 AppIdToMessageCountMap app_message_counts_
;
99 scoped_refptr
<Backend
> backend_
;
100 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
102 base::WeakPtrFactory
<GCMStoreImpl
> weak_ptr_factory_
;
104 DISALLOW_COPY_AND_ASSIGN(GCMStoreImpl
);
109 #endif // GOOGLE_APIS_GCM_ENGINE_GCM_STORE_IMPL_H_