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 #include "google_apis/gcm/engine/gcm_store_impl.h"
7 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/metrics/histogram_macros.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_piece.h"
19 #include "base/strings/string_tokenizer.h"
20 #include "base/time/time.h"
21 #include "base/tracked_objects.h"
22 #include "google_apis/gcm/base/encryptor.h"
23 #include "google_apis/gcm/base/mcs_message.h"
24 #include "google_apis/gcm/base/mcs_util.h"
25 #include "google_apis/gcm/protocol/mcs.pb.h"
26 #include "third_party/leveldatabase/src/include/leveldb/db.h"
27 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
33 // This enum is used in an UMA histogram (GCMLoadStatus enum defined in
34 // tools/metrics/histograms/histogram.xml). Hence the entries here shouldn't
35 // be deleted or re-ordered and new ones should be added to the end.
40 LOADING_DEVICE_CREDENTIALS_FAILED
,
41 LOADING_REGISTRATION_FAILED
,
42 LOADING_INCOMING_MESSAGES_FAILED
,
43 LOADING_OUTGOING_MESSAGES_FAILED
,
44 LOADING_LAST_CHECKIN_INFO_FAILED
,
45 LOADING_GSERVICE_SETTINGS_FAILED
,
46 LOADING_ACCOUNT_MAPPING_FAILED
,
47 LOADING_LAST_TOKEN_TIME_FAILED
,
49 // NOTE: always keep this entry at the end. Add new status types only
50 // immediately above this line. Make sure to update the corresponding
51 // histogram enum accordingly.
55 // Limit to the number of outstanding messages per app.
56 const int kMessagesPerAppLimit
= 20;
58 // ---- LevelDB keys. ----
59 // Key for this device's android id.
60 const char kDeviceAIDKey
[] = "device_aid_key";
61 // Key for this device's android security token.
62 const char kDeviceTokenKey
[] = "device_token_key";
63 // Lowest lexicographically ordered app ids.
64 // Used for prefixing app id.
65 const char kRegistrationKeyStart
[] = "reg1-";
66 // Key guaranteed to be higher than all app ids.
67 // Used for limiting iteration.
68 const char kRegistrationKeyEnd
[] = "reg2-";
69 // Lowest lexicographically ordered incoming message key.
70 // Used for prefixing messages.
71 const char kIncomingMsgKeyStart
[] = "incoming1-";
72 // Key guaranteed to be higher than all incoming message keys.
73 // Used for limiting iteration.
74 const char kIncomingMsgKeyEnd
[] = "incoming2-";
75 // Lowest lexicographically ordered outgoing message key.
76 // Used for prefixing outgoing messages.
77 const char kOutgoingMsgKeyStart
[] = "outgoing1-";
78 // Key guaranteed to be higher than all outgoing message keys.
79 // Used for limiting iteration.
80 const char kOutgoingMsgKeyEnd
[] = "outgoing2-";
81 // Lowest lexicographically ordered G-service settings key.
82 // Used for prefixing G-services settings.
83 const char kGServiceSettingKeyStart
[] = "gservice1-";
84 // Key guaranteed to be higher than all G-services settings keys.
85 // Used for limiting iteration.
86 const char kGServiceSettingKeyEnd
[] = "gservice2-";
87 // Key for digest of the last G-services settings update.
88 const char kGServiceSettingsDigestKey
[] = "gservices_digest";
89 // Key used to indicate how many accounts were last checked in with this device.
90 const char kLastCheckinAccountsKey
[] = "last_checkin_accounts_count";
91 // Key used to timestamp last checkin (marked with G services settings update).
92 const char kLastCheckinTimeKey
[] = "last_checkin_time";
93 // Lowest lexicographically ordered account key.
94 // Used for prefixing account information.
95 const char kAccountKeyStart
[] = "account1-";
96 // Key guaranteed to be higher than all account keys.
97 // Used for limiting iteration.
98 const char kAccountKeyEnd
[] = "account2-";
99 // Key used for last token fetch time.
100 const char kLastTokenFetchTimeKey
[] = "last_token_fetch_time";
102 std::string
MakeRegistrationKey(const std::string
& app_id
) {
103 return kRegistrationKeyStart
+ app_id
;
106 std::string
ParseRegistrationKey(const std::string
& key
) {
107 return key
.substr(arraysize(kRegistrationKeyStart
) - 1);
110 std::string
MakeIncomingKey(const std::string
& persistent_id
) {
111 return kIncomingMsgKeyStart
+ persistent_id
;
114 std::string
MakeOutgoingKey(const std::string
& persistent_id
) {
115 return kOutgoingMsgKeyStart
+ persistent_id
;
118 std::string
ParseOutgoingKey(const std::string
& key
) {
119 return key
.substr(arraysize(kOutgoingMsgKeyStart
) - 1);
122 std::string
MakeGServiceSettingKey(const std::string
& setting_name
) {
123 return kGServiceSettingKeyStart
+ setting_name
;
126 std::string
ParseGServiceSettingKey(const std::string
& key
) {
127 return key
.substr(arraysize(kGServiceSettingKeyStart
) - 1);
130 std::string
MakeAccountKey(const std::string
& account_id
) {
131 return kAccountKeyStart
+ account_id
;
134 std::string
ParseAccountKey(const std::string
& key
) {
135 return key
.substr(arraysize(kAccountKeyStart
) - 1);
138 // Note: leveldb::Slice keeps a pointer to the data in |s|, which must therefore
139 // outlive the slice.
140 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid.
141 leveldb::Slice
MakeSlice(const base::StringPiece
& s
) {
142 return leveldb::Slice(s
.begin(), s
.size());
147 class GCMStoreImpl::Backend
148 : public base::RefCountedThreadSafe
<GCMStoreImpl::Backend
> {
150 Backend(const base::FilePath
& path
,
151 scoped_refptr
<base::SequencedTaskRunner
> foreground_runner
,
152 scoped_ptr
<Encryptor
> encryptor
);
154 // Blocking implementations of GCMStoreImpl methods.
155 void Load(const LoadCallback
& callback
);
157 void Destroy(const UpdateCallback
& callback
);
158 void SetDeviceCredentials(uint64 device_android_id
,
159 uint64 device_security_token
,
160 const UpdateCallback
& callback
);
161 void AddRegistration(const std::string
& app_id
,
162 const linked_ptr
<RegistrationInfo
>& registration
,
163 const UpdateCallback
& callback
);
164 void RemoveRegistration(const std::string
& app_id
,
165 const UpdateCallback
& callback
);
166 void AddIncomingMessage(const std::string
& persistent_id
,
167 const UpdateCallback
& callback
);
168 void RemoveIncomingMessages(const PersistentIdList
& persistent_ids
,
169 const UpdateCallback
& callback
);
170 void AddOutgoingMessage(const std::string
& persistent_id
,
171 const MCSMessage
& message
,
172 const UpdateCallback
& callback
);
173 void RemoveOutgoingMessages(
174 const PersistentIdList
& persistent_ids
,
175 const base::Callback
<void(bool, const AppIdToMessageCountMap
&)>
177 void AddUserSerialNumber(const std::string
& username
,
179 const UpdateCallback
& callback
);
180 void RemoveUserSerialNumber(const std::string
& username
,
181 const UpdateCallback
& callback
);
182 void SetLastCheckinInfo(const base::Time
& time
,
183 const std::set
<std::string
>& accounts
,
184 const UpdateCallback
& callback
);
185 void SetGServicesSettings(
186 const std::map
<std::string
, std::string
>& settings
,
187 const std::string
& digest
,
188 const UpdateCallback
& callback
);
189 void AddAccountMapping(const AccountMapping
& account_mapping
,
190 const UpdateCallback
& callback
);
191 void RemoveAccountMapping(const std::string
& account_id
,
192 const UpdateCallback
& callback
);
193 void SetLastTokenFetchTime(const base::Time
& time
,
194 const UpdateCallback
& callback
);
195 void SetValue(const std::string
& key
,
196 const std::string
& value
,
197 const UpdateCallback
& callback
);
200 friend class base::RefCountedThreadSafe
<Backend
>;
203 LoadStatus
OpenStoreAndLoadData(LoadResult
* result
);
204 bool LoadDeviceCredentials(uint64
* android_id
, uint64
* security_token
);
205 bool LoadRegistrations(RegistrationInfoMap
* registrations
);
206 bool LoadIncomingMessages(std::vector
<std::string
>* incoming_messages
);
207 bool LoadOutgoingMessages(OutgoingMessageMap
* outgoing_messages
);
208 bool LoadLastCheckinInfo(base::Time
* last_checkin_time
,
209 std::set
<std::string
>* accounts
);
210 bool LoadGServicesSettings(std::map
<std::string
, std::string
>* settings
,
211 std::string
* digest
);
212 bool LoadAccountMappingInfo(AccountMappings
* account_mappings
);
213 bool LoadLastTokenFetchTime(base::Time
* last_token_fetch_time
);
215 const base::FilePath path_
;
216 scoped_refptr
<base::SequencedTaskRunner
> foreground_task_runner_
;
217 scoped_ptr
<Encryptor
> encryptor_
;
219 scoped_ptr
<leveldb::DB
> db_
;
222 GCMStoreImpl::Backend::Backend(
223 const base::FilePath
& path
,
224 scoped_refptr
<base::SequencedTaskRunner
> foreground_task_runner
,
225 scoped_ptr
<Encryptor
> encryptor
)
227 foreground_task_runner_(foreground_task_runner
),
228 encryptor_(encryptor
.Pass()) {
231 GCMStoreImpl::Backend::~Backend() {}
233 LoadStatus
GCMStoreImpl::Backend::OpenStoreAndLoadData(LoadResult
* result
) {
234 LoadStatus load_status
;
236 LOG(ERROR
) << "Attempting to reload open database.";
237 return RELOADING_OPEN_STORE
;
240 leveldb::Options options
;
241 options
.create_if_missing
= true;
243 leveldb::Status status
=
244 leveldb::DB::Open(options
, path_
.AsUTF8Unsafe(), &db
);
246 LOG(ERROR
) << "Failed to open database " << path_
.value() << ": "
247 << status
.ToString();
248 return OPENING_STORE_FAILED
;
252 if (!LoadDeviceCredentials(&result
->device_android_id
,
253 &result
->device_security_token
)) {
254 return LOADING_DEVICE_CREDENTIALS_FAILED
;
256 if (!LoadRegistrations(&result
->registrations
))
257 return LOADING_REGISTRATION_FAILED
;
258 if (!LoadIncomingMessages(&result
->incoming_messages
))
259 return LOADING_INCOMING_MESSAGES_FAILED
;
260 if (!LoadOutgoingMessages(&result
->outgoing_messages
))
261 return LOADING_OUTGOING_MESSAGES_FAILED
;
262 if (!LoadLastCheckinInfo(&result
->last_checkin_time
,
263 &result
->last_checkin_accounts
)) {
264 return LOADING_LAST_CHECKIN_INFO_FAILED
;
266 if (!LoadGServicesSettings(&result
->gservices_settings
,
267 &result
->gservices_digest
)) {
268 return load_status
= LOADING_GSERVICE_SETTINGS_FAILED
;
270 if (!LoadAccountMappingInfo(&result
->account_mappings
))
271 return LOADING_ACCOUNT_MAPPING_FAILED
;
272 if (!LoadLastTokenFetchTime(&result
->last_token_fetch_time
))
273 return LOADING_LAST_TOKEN_TIME_FAILED
;
275 return LOADING_SUCCEEDED
;
278 void GCMStoreImpl::Backend::Load(const LoadCallback
& callback
) {
279 scoped_ptr
<LoadResult
> result(new LoadResult());
280 LoadStatus load_status
= OpenStoreAndLoadData(result
.get());
281 UMA_HISTOGRAM_ENUMERATION("GCM.LoadStatus", load_status
, LOAD_STATUS_COUNT
);
282 if (load_status
!= LOADING_SUCCEEDED
) {
284 foreground_task_runner_
->PostTask(FROM_HERE
,
286 base::Passed(&result
)));
290 // Only record histograms if GCM had already been set up for this device.
291 if (result
->device_android_id
!= 0 && result
->device_security_token
!= 0) {
293 if (base::GetFileSize(path_
, &file_size
)) {
294 UMA_HISTOGRAM_COUNTS("GCM.StoreSizeKB",
295 static_cast<int>(file_size
/ 1024));
297 UMA_HISTOGRAM_COUNTS("GCM.RestoredRegistrations",
298 result
->registrations
.size());
299 UMA_HISTOGRAM_COUNTS("GCM.RestoredOutgoingMessages",
300 result
->outgoing_messages
.size());
301 UMA_HISTOGRAM_COUNTS("GCM.RestoredIncomingMessages",
302 result
->incoming_messages
.size());
305 DVLOG(1) << "Succeeded in loading " << result
->registrations
.size()
306 << " registrations, "
307 << result
->incoming_messages
.size()
308 << " unacknowledged incoming messages and "
309 << result
->outgoing_messages
.size()
310 << " unacknowledged outgoing messages.";
311 result
->success
= true;
312 foreground_task_runner_
->PostTask(FROM_HERE
,
314 base::Passed(&result
)));
318 void GCMStoreImpl::Backend::Close() {
319 DVLOG(1) << "Closing GCM store.";
323 void GCMStoreImpl::Backend::Destroy(const UpdateCallback
& callback
) {
324 DVLOG(1) << "Destroying GCM store.";
326 const leveldb::Status s
=
327 leveldb::DestroyDB(path_
.AsUTF8Unsafe(), leveldb::Options());
329 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
332 LOG(ERROR
) << "Destroy failed: " << s
.ToString();
333 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
336 void GCMStoreImpl::Backend::SetDeviceCredentials(
337 uint64 device_android_id
,
338 uint64 device_security_token
,
339 const UpdateCallback
& callback
) {
340 DVLOG(1) << "Saving device credentials with AID " << device_android_id
;
342 LOG(ERROR
) << "GCMStore db doesn't exist.";
343 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
347 leveldb::WriteOptions write_options
;
348 write_options
.sync
= true;
350 std::string encrypted_token
;
351 encryptor_
->EncryptString(base::Uint64ToString(device_security_token
),
353 std::string android_id_str
= base::Uint64ToString(device_android_id
);
355 db_
->Put(write_options
,
356 MakeSlice(kDeviceAIDKey
),
357 MakeSlice(android_id_str
));
360 write_options
, MakeSlice(kDeviceTokenKey
), MakeSlice(encrypted_token
));
363 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
366 LOG(ERROR
) << "LevelDB put failed: " << s
.ToString();
367 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
370 void GCMStoreImpl::Backend::AddRegistration(
371 const std::string
& app_id
,
372 const linked_ptr
<RegistrationInfo
>& registration
,
373 const UpdateCallback
& callback
) {
374 DVLOG(1) << "Saving registration info for app: " << app_id
;
376 LOG(ERROR
) << "GCMStore db doesn't exist.";
377 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
380 leveldb::WriteOptions write_options
;
381 write_options
.sync
= true;
383 std::string key
= MakeRegistrationKey(app_id
);
384 std::string value
= registration
->SerializeAsString();
385 const leveldb::Status status
= db_
->Put(write_options
,
389 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
392 LOG(ERROR
) << "LevelDB put failed: " << status
.ToString();
393 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
396 void GCMStoreImpl::Backend::RemoveRegistration(const std::string
& app_id
,
397 const UpdateCallback
& callback
) {
399 LOG(ERROR
) << "GCMStore db doesn't exist.";
400 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
403 leveldb::WriteOptions write_options
;
404 write_options
.sync
= true;
406 leveldb::Status status
=
407 db_
->Delete(write_options
, MakeSlice(MakeRegistrationKey(app_id
)));
409 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
412 LOG(ERROR
) << "LevelDB remove failed: " << status
.ToString();
413 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
416 void GCMStoreImpl::Backend::AddIncomingMessage(const std::string
& persistent_id
,
417 const UpdateCallback
& callback
) {
418 DVLOG(1) << "Saving incoming message with id " << persistent_id
;
420 LOG(ERROR
) << "GCMStore db doesn't exist.";
421 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
425 leveldb::WriteOptions write_options
;
426 write_options
.sync
= true;
428 std::string key
= MakeIncomingKey(persistent_id
);
429 const leveldb::Status s
= db_
->Put(write_options
,
431 MakeSlice(persistent_id
));
433 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
436 LOG(ERROR
) << "LevelDB put failed: " << s
.ToString();
437 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
440 void GCMStoreImpl::Backend::RemoveIncomingMessages(
441 const PersistentIdList
& persistent_ids
,
442 const UpdateCallback
& callback
) {
444 LOG(ERROR
) << "GCMStore db doesn't exist.";
445 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
448 leveldb::WriteOptions write_options
;
449 write_options
.sync
= true;
452 for (PersistentIdList::const_iterator iter
= persistent_ids
.begin();
453 iter
!= persistent_ids
.end();
455 DVLOG(1) << "Removing incoming message with id " << *iter
;
456 std::string key
= MakeIncomingKey(*iter
);
457 s
= db_
->Delete(write_options
, MakeSlice(key
));
462 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
465 LOG(ERROR
) << "LevelDB remove failed: " << s
.ToString();
466 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
469 void GCMStoreImpl::Backend::AddOutgoingMessage(const std::string
& persistent_id
,
470 const MCSMessage
& message
,
471 const UpdateCallback
& callback
) {
472 DVLOG(1) << "Saving outgoing message with id " << persistent_id
;
474 LOG(ERROR
) << "GCMStore db doesn't exist.";
475 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
478 leveldb::WriteOptions write_options
;
479 write_options
.sync
= true;
482 static_cast<char>(message
.tag()) + message
.SerializeAsString();
483 std::string key
= MakeOutgoingKey(persistent_id
);
484 const leveldb::Status s
= db_
->Put(write_options
,
488 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, true));
491 LOG(ERROR
) << "LevelDB put failed: " << s
.ToString();
492 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
495 void GCMStoreImpl::Backend::RemoveOutgoingMessages(
496 const PersistentIdList
& persistent_ids
,
497 const base::Callback
<void(bool, const AppIdToMessageCountMap
&)>
500 LOG(ERROR
) << "GCMStore db doesn't exist.";
501 foreground_task_runner_
->PostTask(FROM_HERE
,
504 AppIdToMessageCountMap()));
507 leveldb::ReadOptions read_options
;
508 leveldb::WriteOptions write_options
;
509 write_options
.sync
= true;
511 AppIdToMessageCountMap removed_message_counts
;
514 for (PersistentIdList::const_iterator iter
= persistent_ids
.begin();
515 iter
!= persistent_ids
.end();
517 DVLOG(1) << "Removing outgoing message with id " << *iter
;
518 std::string outgoing_message
;
519 std::string key
= MakeOutgoingKey(*iter
);
520 s
= db_
->Get(read_options
,
525 mcs_proto::DataMessageStanza data_message
;
526 // Skip the initial tag byte and parse the rest to extract the message.
527 if (data_message
.ParseFromString(outgoing_message
.substr(1))) {
528 DCHECK(!data_message
.category().empty());
529 if (removed_message_counts
.count(data_message
.category()) != 0)
530 removed_message_counts
[data_message
.category()]++;
532 removed_message_counts
[data_message
.category()] = 1;
534 DVLOG(1) << "Removing outgoing message with id " << *iter
;
535 s
= db_
->Delete(write_options
, MakeSlice(key
));
540 foreground_task_runner_
->PostTask(FROM_HERE
,
543 removed_message_counts
));
546 LOG(ERROR
) << "LevelDB remove failed: " << s
.ToString();
547 foreground_task_runner_
->PostTask(FROM_HERE
,
550 AppIdToMessageCountMap()));
553 void GCMStoreImpl::Backend::SetLastCheckinInfo(
554 const base::Time
& time
,
555 const std::set
<std::string
>& accounts
,
556 const UpdateCallback
& callback
) {
557 leveldb::WriteBatch write_batch
;
559 int64 last_checkin_time_internal
= time
.ToInternalValue();
560 write_batch
.Put(MakeSlice(kLastCheckinTimeKey
),
561 MakeSlice(base::Int64ToString(last_checkin_time_internal
)));
563 std::string serialized_accounts
;
564 for (std::set
<std::string
>::iterator iter
= accounts
.begin();
565 iter
!= accounts
.end();
567 serialized_accounts
+= *iter
;
568 serialized_accounts
+= ",";
570 if (!serialized_accounts
.empty())
571 serialized_accounts
.erase(serialized_accounts
.length() - 1);
573 write_batch
.Put(MakeSlice(kLastCheckinAccountsKey
),
574 MakeSlice(serialized_accounts
));
576 leveldb::WriteOptions write_options
;
577 write_options
.sync
= true;
578 const leveldb::Status s
= db_
->Write(write_options
, &write_batch
);
581 LOG(ERROR
) << "LevelDB set last checkin info failed: " << s
.ToString();
582 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, s
.ok()));
585 void GCMStoreImpl::Backend::SetGServicesSettings(
586 const std::map
<std::string
, std::string
>& settings
,
587 const std::string
& settings_digest
,
588 const UpdateCallback
& callback
) {
589 leveldb::WriteBatch write_batch
;
591 // Remove all existing settings.
592 leveldb::ReadOptions read_options
;
593 read_options
.verify_checksums
= true;
594 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(read_options
));
595 for (iter
->Seek(MakeSlice(kGServiceSettingKeyStart
));
596 iter
->Valid() && iter
->key().ToString() < kGServiceSettingKeyEnd
;
598 write_batch
.Delete(iter
->key());
601 // Add the new settings.
602 for (std::map
<std::string
, std::string
>::const_iterator iter
=
604 iter
!= settings
.end(); ++iter
) {
605 write_batch
.Put(MakeSlice(MakeGServiceSettingKey(iter
->first
)),
606 MakeSlice(iter
->second
));
609 // Update the settings digest.
610 write_batch
.Put(MakeSlice(kGServiceSettingsDigestKey
),
611 MakeSlice(settings_digest
));
613 // Write it all in a batch.
614 leveldb::WriteOptions write_options
;
615 write_options
.sync
= true;
617 leveldb::Status s
= db_
->Write(write_options
, &write_batch
);
619 LOG(ERROR
) << "LevelDB GService Settings update failed: " << s
.ToString();
620 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, s
.ok()));
623 void GCMStoreImpl::Backend::AddAccountMapping(
624 const AccountMapping
& account_mapping
,
625 const UpdateCallback
& callback
) {
626 DVLOG(1) << "Saving account info for account with email: "
627 << account_mapping
.email
;
629 LOG(ERROR
) << "GCMStore db doesn't exist.";
630 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
634 leveldb::WriteOptions write_options
;
635 write_options
.sync
= true;
637 std::string data
= account_mapping
.SerializeAsString();
638 std::string key
= MakeAccountKey(account_mapping
.account_id
);
639 const leveldb::Status s
=
640 db_
->Put(write_options
, MakeSlice(key
), MakeSlice(data
));
642 LOG(ERROR
) << "LevelDB adding account mapping failed: " << s
.ToString();
643 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, s
.ok()));
646 void GCMStoreImpl::Backend::RemoveAccountMapping(
647 const std::string
& account_id
,
648 const UpdateCallback
& callback
) {
650 LOG(ERROR
) << "GCMStore db doesn't exist.";
651 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
655 leveldb::WriteOptions write_options
;
656 write_options
.sync
= true;
659 db_
->Delete(write_options
, MakeSlice(MakeAccountKey(account_id
)));
662 LOG(ERROR
) << "LevelDB removal of account mapping failed: " << s
.ToString();
663 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, s
.ok()));
666 void GCMStoreImpl::Backend::SetLastTokenFetchTime(
667 const base::Time
& time
,
668 const UpdateCallback
& callback
) {
669 DVLOG(1) << "Setting last token fetching time.";
671 LOG(ERROR
) << "GCMStore db doesn't exist.";
672 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
676 leveldb::WriteOptions write_options
;
677 write_options
.sync
= true;
679 const leveldb::Status s
=
680 db_
->Put(write_options
,
681 MakeSlice(kLastTokenFetchTimeKey
),
682 MakeSlice(base::Int64ToString(time
.ToInternalValue())));
685 LOG(ERROR
) << "LevelDB setting last token fetching time: " << s
.ToString();
686 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, s
.ok()));
689 void GCMStoreImpl::Backend::SetValue(const std::string
& key
,
690 const std::string
& value
,
691 const UpdateCallback
& callback
) {
692 DVLOG(1) << "Injecting a value to GCM Store for testing. Key: "
693 << key
<< ", Value: " << value
;
695 LOG(ERROR
) << "GCMStore db doesn't exist.";
696 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, false));
700 leveldb::WriteOptions write_options
;
701 write_options
.sync
= true;
703 const leveldb::Status s
=
704 db_
->Put(write_options
, MakeSlice(key
), MakeSlice(value
));
707 LOG(ERROR
) << "LevelDB had problems injecting a value: " << s
.ToString();
708 foreground_task_runner_
->PostTask(FROM_HERE
, base::Bind(callback
, s
.ok()));
711 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64
* android_id
,
712 uint64
* security_token
) {
713 leveldb::ReadOptions read_options
;
714 read_options
.verify_checksums
= true;
717 leveldb::Status s
= db_
->Get(read_options
, MakeSlice(kDeviceAIDKey
), &result
);
719 if (!base::StringToUint64(result
, android_id
)) {
720 LOG(ERROR
) << "Failed to restore device id.";
724 s
= db_
->Get(read_options
, MakeSlice(kDeviceTokenKey
), &result
);
727 std::string decrypted_token
;
728 encryptor_
->DecryptString(result
, &decrypted_token
);
729 if (!base::StringToUint64(decrypted_token
, security_token
)) {
730 LOG(ERROR
) << "Failed to restore security token.";
736 if (s
.IsNotFound()) {
737 DVLOG(1) << "No credentials found.";
741 LOG(ERROR
) << "Error reading credentials from store.";
745 bool GCMStoreImpl::Backend::LoadRegistrations(
746 RegistrationInfoMap
* registrations
) {
747 leveldb::ReadOptions read_options
;
748 read_options
.verify_checksums
= true;
750 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(read_options
));
751 for (iter
->Seek(MakeSlice(kRegistrationKeyStart
));
752 iter
->Valid() && iter
->key().ToString() < kRegistrationKeyEnd
;
754 leveldb::Slice s
= iter
->value();
756 LOG(ERROR
) << "Error reading registration with key " << s
.ToString();
759 std::string app_id
= ParseRegistrationKey(iter
->key().ToString());
760 linked_ptr
<RegistrationInfo
> registration(new RegistrationInfo
);
761 if (!registration
->ParseFromString(iter
->value().ToString())) {
762 LOG(ERROR
) << "Failed to parse registration with app id " << app_id
;
765 DVLOG(1) << "Found registration with app id " << app_id
;
766 (*registrations
)[app_id
] = registration
;
772 bool GCMStoreImpl::Backend::LoadIncomingMessages(
773 std::vector
<std::string
>* incoming_messages
) {
774 leveldb::ReadOptions read_options
;
775 read_options
.verify_checksums
= true;
777 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(read_options
));
778 for (iter
->Seek(MakeSlice(kIncomingMsgKeyStart
));
779 iter
->Valid() && iter
->key().ToString() < kIncomingMsgKeyEnd
;
781 leveldb::Slice s
= iter
->value();
783 LOG(ERROR
) << "Error reading incoming message with key "
784 << iter
->key().ToString();
787 DVLOG(1) << "Found incoming message with id " << s
.ToString();
788 incoming_messages
->push_back(s
.ToString());
794 bool GCMStoreImpl::Backend::LoadOutgoingMessages(
795 OutgoingMessageMap
* outgoing_messages
) {
796 leveldb::ReadOptions read_options
;
797 read_options
.verify_checksums
= true;
799 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(read_options
));
800 for (iter
->Seek(MakeSlice(kOutgoingMsgKeyStart
));
801 iter
->Valid() && iter
->key().ToString() < kOutgoingMsgKeyEnd
;
803 leveldb::Slice s
= iter
->value();
805 LOG(ERROR
) << "Error reading incoming message with key " << s
.ToString();
808 uint8 tag
= iter
->value().data()[0];
809 std::string id
= ParseOutgoingKey(iter
->key().ToString());
810 scoped_ptr
<google::protobuf::MessageLite
> message(
811 BuildProtobufFromTag(tag
));
812 if (!message
.get() ||
813 !message
->ParseFromString(iter
->value().ToString().substr(1))) {
814 LOG(ERROR
) << "Failed to parse outgoing message with id " << id
815 << " and tag " << tag
;
818 DVLOG(1) << "Found outgoing message with id " << id
<< " of type "
819 << base::IntToString(tag
);
820 (*outgoing_messages
)[id
] = make_linked_ptr(message
.release());
826 bool GCMStoreImpl::Backend::LoadLastCheckinInfo(
827 base::Time
* last_checkin_time
,
828 std::set
<std::string
>* accounts
) {
829 leveldb::ReadOptions read_options
;
830 read_options
.verify_checksums
= true;
833 leveldb::Status s
= db_
->Get(read_options
,
834 MakeSlice(kLastCheckinTimeKey
),
836 int64 time_internal
= 0LL;
837 if (s
.ok() && !base::StringToInt64(result
, &time_internal
)) {
838 LOG(ERROR
) << "Failed to restore last checkin time. Using default = 0.";
842 // In case we cannot read last checkin time, we default it to 0, as we don't
843 // want that situation to cause the whole load to fail.
844 *last_checkin_time
= base::Time::FromInternalValue(time_internal
);
847 s
= db_
->Get(read_options
, MakeSlice(kLastCheckinAccountsKey
), &result
);
849 DVLOG(1) << "No accounts where stored during last run.";
851 base::StringTokenizer
t(result
, ",");
853 accounts
->insert(t
.token());
858 bool GCMStoreImpl::Backend::LoadGServicesSettings(
859 std::map
<std::string
, std::string
>* settings
,
860 std::string
* digest
) {
861 leveldb::ReadOptions read_options
;
862 read_options
.verify_checksums
= true;
864 // Load all of the GServices settings.
865 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(read_options
));
866 for (iter
->Seek(MakeSlice(kGServiceSettingKeyStart
));
867 iter
->Valid() && iter
->key().ToString() < kGServiceSettingKeyEnd
;
869 std::string value
= iter
->value().ToString();
871 LOG(ERROR
) << "Error reading GService Settings " << value
;
874 std::string id
= ParseGServiceSettingKey(iter
->key().ToString());
875 (*settings
)[id
] = value
;
876 DVLOG(1) << "Found G Service setting with key: " << id
877 << ", and value: " << value
;
880 // Load the settings digest. It's ok if it is empty.
881 db_
->Get(read_options
, MakeSlice(kGServiceSettingsDigestKey
), digest
);
886 bool GCMStoreImpl::Backend::LoadAccountMappingInfo(
887 AccountMappings
* account_mappings
) {
888 leveldb::ReadOptions read_options
;
889 read_options
.verify_checksums
= true;
891 scoped_ptr
<leveldb::Iterator
> iter(db_
->NewIterator(read_options
));
892 for (iter
->Seek(MakeSlice(kAccountKeyStart
));
893 iter
->Valid() && iter
->key().ToString() < kAccountKeyEnd
;
895 AccountMapping account_mapping
;
896 account_mapping
.account_id
= ParseAccountKey(iter
->key().ToString());
897 if (!account_mapping
.ParseFromString(iter
->value().ToString())) {
898 DVLOG(1) << "Failed to parse account info with ID: "
899 << account_mapping
.account_id
;
902 DVLOG(1) << "Found account mapping with ID: " << account_mapping
.account_id
;
903 account_mappings
->push_back(account_mapping
);
909 bool GCMStoreImpl::Backend::LoadLastTokenFetchTime(
910 base::Time
* last_token_fetch_time
) {
911 leveldb::ReadOptions read_options
;
912 read_options
.verify_checksums
= true;
916 db_
->Get(read_options
, MakeSlice(kLastTokenFetchTimeKey
), &result
);
917 int64 time_internal
= 0LL;
918 if (s
.ok() && !base::StringToInt64(result
, &time_internal
)) {
920 "Failed to restore last token fetching time. Using default = 0.";
924 // In case we cannot read last token fetching time, we default it to 0.
925 *last_token_fetch_time
= base::Time::FromInternalValue(time_internal
);
930 GCMStoreImpl::GCMStoreImpl(
931 const base::FilePath
& path
,
932 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
,
933 scoped_ptr
<Encryptor
> encryptor
)
934 : backend_(new Backend(path
,
935 base::MessageLoopProxy::current(),
937 blocking_task_runner_(blocking_task_runner
),
938 weak_ptr_factory_(this) {
941 GCMStoreImpl::~GCMStoreImpl() {}
943 void GCMStoreImpl::Load(const LoadCallback
& callback
) {
944 blocking_task_runner_
->PostTask(
946 base::Bind(&GCMStoreImpl::Backend::Load
,
948 base::Bind(&GCMStoreImpl::LoadContinuation
,
949 weak_ptr_factory_
.GetWeakPtr(),
953 void GCMStoreImpl::Close() {
954 weak_ptr_factory_
.InvalidateWeakPtrs();
955 app_message_counts_
.clear();
956 blocking_task_runner_
->PostTask(
958 base::Bind(&GCMStoreImpl::Backend::Close
, backend_
));
961 void GCMStoreImpl::Destroy(const UpdateCallback
& callback
) {
962 blocking_task_runner_
->PostTask(
964 base::Bind(&GCMStoreImpl::Backend::Destroy
, backend_
, callback
));
967 void GCMStoreImpl::SetDeviceCredentials(uint64 device_android_id
,
968 uint64 device_security_token
,
969 const UpdateCallback
& callback
) {
970 blocking_task_runner_
->PostTask(
972 base::Bind(&GCMStoreImpl::Backend::SetDeviceCredentials
,
975 device_security_token
,
979 void GCMStoreImpl::AddRegistration(
980 const std::string
& app_id
,
981 const linked_ptr
<RegistrationInfo
>& registration
,
982 const UpdateCallback
& callback
) {
983 blocking_task_runner_
->PostTask(
985 base::Bind(&GCMStoreImpl::Backend::AddRegistration
,
992 void GCMStoreImpl::RemoveRegistration(const std::string
& app_id
,
993 const UpdateCallback
& callback
) {
994 blocking_task_runner_
->PostTask(
996 base::Bind(&GCMStoreImpl::Backend::RemoveRegistration
,
1002 void GCMStoreImpl::AddIncomingMessage(const std::string
& persistent_id
,
1003 const UpdateCallback
& callback
) {
1004 blocking_task_runner_
->PostTask(
1006 base::Bind(&GCMStoreImpl::Backend::AddIncomingMessage
,
1012 void GCMStoreImpl::RemoveIncomingMessage(const std::string
& persistent_id
,
1013 const UpdateCallback
& callback
) {
1014 blocking_task_runner_
->PostTask(
1016 base::Bind(&GCMStoreImpl::Backend::RemoveIncomingMessages
,
1018 PersistentIdList(1, persistent_id
),
1022 void GCMStoreImpl::RemoveIncomingMessages(
1023 const PersistentIdList
& persistent_ids
,
1024 const UpdateCallback
& callback
) {
1025 blocking_task_runner_
->PostTask(
1027 base::Bind(&GCMStoreImpl::Backend::RemoveIncomingMessages
,
1033 bool GCMStoreImpl::AddOutgoingMessage(const std::string
& persistent_id
,
1034 const MCSMessage
& message
,
1035 const UpdateCallback
& callback
) {
1036 DCHECK_EQ(message
.tag(), kDataMessageStanzaTag
);
1037 std::string app_id
= reinterpret_cast<const mcs_proto::DataMessageStanza
*>(
1038 &message
.GetProtobuf())->category();
1039 DCHECK(!app_id
.empty());
1040 if (app_message_counts_
.count(app_id
) == 0)
1041 app_message_counts_
[app_id
] = 0;
1042 if (app_message_counts_
[app_id
] < kMessagesPerAppLimit
) {
1043 app_message_counts_
[app_id
]++;
1045 blocking_task_runner_
->PostTask(
1047 base::Bind(&GCMStoreImpl::Backend::AddOutgoingMessage
,
1051 base::Bind(&GCMStoreImpl::AddOutgoingMessageContinuation
,
1052 weak_ptr_factory_
.GetWeakPtr(),
1060 void GCMStoreImpl::OverwriteOutgoingMessage(const std::string
& persistent_id
,
1061 const MCSMessage
& message
,
1062 const UpdateCallback
& callback
) {
1063 DCHECK_EQ(message
.tag(), kDataMessageStanzaTag
);
1064 std::string app_id
= reinterpret_cast<const mcs_proto::DataMessageStanza
*>(
1065 &message
.GetProtobuf())->category();
1066 DCHECK(!app_id
.empty());
1067 // There should already be pending messages for this app.
1068 DCHECK(app_message_counts_
.count(app_id
));
1069 // TODO(zea): consider verifying the specific message already exists.
1070 blocking_task_runner_
->PostTask(
1072 base::Bind(&GCMStoreImpl::Backend::AddOutgoingMessage
,
1079 void GCMStoreImpl::RemoveOutgoingMessage(const std::string
& persistent_id
,
1080 const UpdateCallback
& callback
) {
1081 blocking_task_runner_
->PostTask(
1083 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages
,
1085 PersistentIdList(1, persistent_id
),
1086 base::Bind(&GCMStoreImpl::RemoveOutgoingMessagesContinuation
,
1087 weak_ptr_factory_
.GetWeakPtr(),
1091 void GCMStoreImpl::RemoveOutgoingMessages(
1092 const PersistentIdList
& persistent_ids
,
1093 const UpdateCallback
& callback
) {
1094 blocking_task_runner_
->PostTask(
1096 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages
,
1099 base::Bind(&GCMStoreImpl::RemoveOutgoingMessagesContinuation
,
1100 weak_ptr_factory_
.GetWeakPtr(),
1104 void GCMStoreImpl::SetLastCheckinInfo(const base::Time
& time
,
1105 const std::set
<std::string
>& accounts
,
1106 const UpdateCallback
& callback
) {
1107 blocking_task_runner_
->PostTask(
1109 base::Bind(&GCMStoreImpl::Backend::SetLastCheckinInfo
,
1116 void GCMStoreImpl::SetGServicesSettings(
1117 const std::map
<std::string
, std::string
>& settings
,
1118 const std::string
& digest
,
1119 const UpdateCallback
& callback
) {
1120 blocking_task_runner_
->PostTask(
1122 base::Bind(&GCMStoreImpl::Backend::SetGServicesSettings
,
1129 void GCMStoreImpl::AddAccountMapping(const AccountMapping
& account_mapping
,
1130 const UpdateCallback
& callback
) {
1131 blocking_task_runner_
->PostTask(
1133 base::Bind(&GCMStoreImpl::Backend::AddAccountMapping
,
1139 void GCMStoreImpl::RemoveAccountMapping(const std::string
& account_id
,
1140 const UpdateCallback
& callback
) {
1141 blocking_task_runner_
->PostTask(
1143 base::Bind(&GCMStoreImpl::Backend::RemoveAccountMapping
,
1149 void GCMStoreImpl::SetLastTokenFetchTime(const base::Time
& time
,
1150 const UpdateCallback
& callback
) {
1151 blocking_task_runner_
->PostTask(
1153 base::Bind(&GCMStoreImpl::Backend::SetLastTokenFetchTime
,
1159 void GCMStoreImpl::SetValueForTesting(const std::string
& key
,
1160 const std::string
& value
,
1161 const UpdateCallback
& callback
) {
1162 blocking_task_runner_
->PostTask(
1164 base::Bind(&GCMStoreImpl::Backend::SetValue
,
1171 void GCMStoreImpl::LoadContinuation(const LoadCallback
& callback
,
1172 scoped_ptr
<LoadResult
> result
) {
1173 if (!result
->success
) {
1174 callback
.Run(result
.Pass());
1177 int num_throttled_apps
= 0;
1178 for (OutgoingMessageMap::const_iterator
1179 iter
= result
->outgoing_messages
.begin();
1180 iter
!= result
->outgoing_messages
.end(); ++iter
) {
1181 const mcs_proto::DataMessageStanza
* data_message
=
1182 reinterpret_cast<mcs_proto::DataMessageStanza
*>(iter
->second
.get());
1183 DCHECK(!data_message
->category().empty());
1184 if (app_message_counts_
.count(data_message
->category()) == 0)
1185 app_message_counts_
[data_message
->category()] = 1;
1187 app_message_counts_
[data_message
->category()]++;
1188 if (app_message_counts_
[data_message
->category()] == kMessagesPerAppLimit
)
1189 num_throttled_apps
++;
1191 UMA_HISTOGRAM_COUNTS("GCM.NumThrottledApps", num_throttled_apps
);
1192 callback
.Run(result
.Pass());
1195 void GCMStoreImpl::AddOutgoingMessageContinuation(
1196 const UpdateCallback
& callback
,
1197 const std::string
& app_id
,
1200 DCHECK(app_message_counts_
[app_id
] > 0);
1201 app_message_counts_
[app_id
]--;
1203 callback
.Run(success
);
1206 void GCMStoreImpl::RemoveOutgoingMessagesContinuation(
1207 const UpdateCallback
& callback
,
1209 const AppIdToMessageCountMap
& removed_message_counts
) {
1211 callback
.Run(false);
1214 for (AppIdToMessageCountMap::const_iterator iter
=
1215 removed_message_counts
.begin();
1216 iter
!= removed_message_counts
.end(); ++iter
) {
1217 DCHECK_NE(app_message_counts_
.count(iter
->first
), 0U);
1218 app_message_counts_
[iter
->first
] -= iter
->second
;
1219 DCHECK_GE(app_message_counts_
[iter
->first
], 0);