Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: google_apis/gcm/engine/gcm_store_impl.cc

Issue 215363007: [GCM] Adding basic G-services handling (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing Jian Li's CR comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "google_apis/gcm/engine/gcm_store_impl.h" 5 #include "google_apis/gcm/engine/gcm_store_impl.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/message_loop/message_loop_proxy.h" 13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
16 #include "base/stl_util.h" 16 #include "base/stl_util.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_piece.h" 18 #include "base/strings/string_piece.h"
19 #include "base/time/time.h"
19 #include "base/tracked_objects.h" 20 #include "base/tracked_objects.h"
20 #include "components/os_crypt/os_crypt.h" 21 #include "components/os_crypt/os_crypt.h"
21 #include "google_apis/gcm/base/mcs_message.h" 22 #include "google_apis/gcm/base/mcs_message.h"
22 #include "google_apis/gcm/base/mcs_util.h" 23 #include "google_apis/gcm/base/mcs_util.h"
23 #include "google_apis/gcm/protocol/mcs.pb.h" 24 #include "google_apis/gcm/protocol/mcs.pb.h"
24 #include "third_party/leveldatabase/src/include/leveldb/db.h" 25 #include "third_party/leveldatabase/src/include/leveldb/db.h"
26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
25 27
26 namespace gcm { 28 namespace gcm {
27 29
28 namespace { 30 namespace {
29 31
30 // Limit to the number of outstanding messages per app. 32 // Limit to the number of outstanding messages per app.
31 const int kMessagesPerAppLimit = 20; 33 const int kMessagesPerAppLimit = 20;
32 34
33 // ---- LevelDB keys. ---- 35 // ---- LevelDB keys. ----
34 // Key for this device's android id. 36 // Key for this device's android id.
(...skipping 11 matching lines...) Expand all
46 const char kIncomingMsgKeyStart[] = "incoming1-"; 48 const char kIncomingMsgKeyStart[] = "incoming1-";
47 // Key guaranteed to be higher than all incoming message keys. 49 // Key guaranteed to be higher than all incoming message keys.
48 // Used for limiting iteration. 50 // Used for limiting iteration.
49 const char kIncomingMsgKeyEnd[] = "incoming2-"; 51 const char kIncomingMsgKeyEnd[] = "incoming2-";
50 // Lowest lexicographically ordered outgoing message key. 52 // Lowest lexicographically ordered outgoing message key.
51 // Used for prefixing outgoing messages. 53 // Used for prefixing outgoing messages.
52 const char kOutgoingMsgKeyStart[] = "outgoing1-"; 54 const char kOutgoingMsgKeyStart[] = "outgoing1-";
53 // Key guaranteed to be higher than all outgoing message keys. 55 // Key guaranteed to be higher than all outgoing message keys.
54 // Used for limiting iteration. 56 // Used for limiting iteration.
55 const char kOutgoingMsgKeyEnd[] = "outgoing2-"; 57 const char kOutgoingMsgKeyEnd[] = "outgoing2-";
58 // Lowest lexicographically ordered G-service settings key.
59 // Used for prefixing G-services settings.
60 const char kGServiceSettingKeyStart[] = "gservice1-";
61 // Key guaranteed to be higher than all G-services settings keys.
62 // Used for limiting iteration.
63 const char kGServiceSettingKeyEnd[] = "gservice2-";
64 // Key for digest of the last G-services settings update.
65 const char kGServiceSettingsDigestKey[] = "gservices_digest";
66 // Key used to timestamp last checkin (marked with G services settings update).
67 const char kLastCheckinTimeKey[] = "last_checkin_time";
56 68
57 std::string MakeRegistrationKey(const std::string& app_id) { 69 std::string MakeRegistrationKey(const std::string& app_id) {
58 return kRegistrationKeyStart + app_id; 70 return kRegistrationKeyStart + app_id;
59 } 71 }
60 72
61 std::string ParseRegistrationKey(const std::string& key) { 73 std::string ParseRegistrationKey(const std::string& key) {
62 return key.substr(arraysize(kRegistrationKeyStart) - 1); 74 return key.substr(arraysize(kRegistrationKeyStart) - 1);
63 } 75 }
64 76
65 std::string MakeIncomingKey(const std::string& persistent_id) { 77 std::string MakeIncomingKey(const std::string& persistent_id) {
66 return kIncomingMsgKeyStart + persistent_id; 78 return kIncomingMsgKeyStart + persistent_id;
67 } 79 }
68 80
69 std::string MakeOutgoingKey(const std::string& persistent_id) { 81 std::string MakeOutgoingKey(const std::string& persistent_id) {
70 return kOutgoingMsgKeyStart + persistent_id; 82 return kOutgoingMsgKeyStart + persistent_id;
71 } 83 }
72 84
73 std::string ParseOutgoingKey(const std::string& key) { 85 std::string ParseOutgoingKey(const std::string& key) {
74 return key.substr(arraysize(kOutgoingMsgKeyStart) - 1); 86 return key.substr(arraysize(kOutgoingMsgKeyStart) - 1);
75 } 87 }
76 88
89 std::string MakeGServiceSettingKey(const std::string& setting_name) {
90 return kGServiceSettingKeyStart + setting_name;
91 }
92
93 std::string ParseGServiceSettingKey(const std::string& key) {
94 return key.substr(arraysize(kGServiceSettingKeyStart) - 1);
95 }
96
77 // Note: leveldb::Slice keeps a pointer to the data in |s|, which must therefore 97 // Note: leveldb::Slice keeps a pointer to the data in |s|, which must therefore
78 // outlive the slice. 98 // outlive the slice.
79 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid. 99 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid.
80 leveldb::Slice MakeSlice(const base::StringPiece& s) { 100 leveldb::Slice MakeSlice(const base::StringPiece& s) {
81 return leveldb::Slice(s.begin(), s.size()); 101 return leveldb::Slice(s.begin(), s.size());
82 } 102 }
83 103
84 } // namespace 104 } // namespace
85 105
86 class GCMStoreImpl::Backend 106 class GCMStoreImpl::Backend
(...skipping 24 matching lines...) Expand all
111 void RemoveOutgoingMessages( 131 void RemoveOutgoingMessages(
112 const PersistentIdList& persistent_ids, 132 const PersistentIdList& persistent_ids,
113 const base::Callback<void(bool, const AppIdToMessageCountMap&)> 133 const base::Callback<void(bool, const AppIdToMessageCountMap&)>
114 callback); 134 callback);
115 void AddUserSerialNumber(const std::string& username, 135 void AddUserSerialNumber(const std::string& username,
116 int64 serial_number, 136 int64 serial_number,
117 const UpdateCallback& callback); 137 const UpdateCallback& callback);
118 void RemoveUserSerialNumber(const std::string& username, 138 void RemoveUserSerialNumber(const std::string& username,
119 const UpdateCallback& callback); 139 const UpdateCallback& callback);
120 void SetNextSerialNumber(int64 serial_number, const UpdateCallback& callback); 140 void SetNextSerialNumber(int64 serial_number, const UpdateCallback& callback);
141 void SetGServicesSettings(
142 const std::map<std::string, std::string>& settings,
143 const std::string& digest,
144 const base::Time& last_checkin_time,
145 const UpdateCallback& callback);
121 146
122 private: 147 private:
123 friend class base::RefCountedThreadSafe<Backend>; 148 friend class base::RefCountedThreadSafe<Backend>;
124 ~Backend(); 149 ~Backend();
125 150
126 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); 151 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token);
127 bool LoadRegistrations(RegistrationInfoMap* registrations); 152 bool LoadRegistrations(RegistrationInfoMap* registrations);
128 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); 153 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages);
129 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); 154 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages);
155 bool LoadGServicesSettings(std::map<std::string, std::string>* settings,
156 std::string* digest,
157 base::Time* last_checkin_time);
130 158
131 const base::FilePath path_; 159 const base::FilePath path_;
132 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; 160 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_;
133 161
134 scoped_ptr<leveldb::DB> db_; 162 scoped_ptr<leveldb::DB> db_;
135 }; 163 };
136 164
137 GCMStoreImpl::Backend::Backend( 165 GCMStoreImpl::Backend::Backend(
138 const base::FilePath& path, 166 const base::FilePath& path,
139 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner) 167 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner)
(...skipping 24 matching lines...) Expand all
164 base::Bind(callback, 192 base::Bind(callback,
165 base::Passed(&result))); 193 base::Passed(&result)));
166 return; 194 return;
167 } 195 }
168 db_.reset(db); 196 db_.reset(db);
169 197
170 if (!LoadDeviceCredentials(&result->device_android_id, 198 if (!LoadDeviceCredentials(&result->device_android_id,
171 &result->device_security_token) || 199 &result->device_security_token) ||
172 !LoadRegistrations(&result->registrations) || 200 !LoadRegistrations(&result->registrations) ||
173 !LoadIncomingMessages(&result->incoming_messages) || 201 !LoadIncomingMessages(&result->incoming_messages) ||
174 !LoadOutgoingMessages(&result->outgoing_messages)) { 202 !LoadOutgoingMessages(&result->outgoing_messages) ||
203 !LoadGServicesSettings(&result->gservices_settings,
204 &result->gservices_digest,
205 &result->last_checkin_time)) {
175 result->device_android_id = 0; 206 result->device_android_id = 0;
176 result->device_security_token = 0; 207 result->device_security_token = 0;
177 result->registrations.clear(); 208 result->registrations.clear();
178 result->incoming_messages.clear(); 209 result->incoming_messages.clear();
179 result->outgoing_messages.clear(); 210 result->outgoing_messages.clear();
211 result->gservices_settings.clear();
212 result->gservices_digest.clear();
213 result->last_checkin_time = base::Time::FromInternalValue(0LL);
180 foreground_task_runner_->PostTask(FROM_HERE, 214 foreground_task_runner_->PostTask(FROM_HERE,
181 base::Bind(callback, 215 base::Bind(callback,
182 base::Passed(&result))); 216 base::Passed(&result)));
183 return; 217 return;
184 } 218 }
185 219
186 // Only record histograms if GCM had already been set up for this device. 220 // Only record histograms if GCM had already been set up for this device.
187 if (result->device_android_id != 0 && result->device_security_token != 0) { 221 if (result->device_android_id != 0 && result->device_security_token != 0) {
188 int64 file_size = 0; 222 int64 file_size = 0;
189 if (base::GetFileSize(path_, &file_size)) { 223 if (base::GetFileSize(path_, &file_size)) {
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 removed_message_counts)); 472 removed_message_counts));
439 return; 473 return;
440 } 474 }
441 LOG(ERROR) << "LevelDB remove failed: " << s.ToString(); 475 LOG(ERROR) << "LevelDB remove failed: " << s.ToString();
442 foreground_task_runner_->PostTask(FROM_HERE, 476 foreground_task_runner_->PostTask(FROM_HERE,
443 base::Bind(callback, 477 base::Bind(callback,
444 false, 478 false,
445 AppIdToMessageCountMap())); 479 AppIdToMessageCountMap()));
446 } 480 }
447 481
482 void GCMStoreImpl::Backend::SetGServicesSettings(
483 const std::map<std::string, std::string>& settings,
484 const std::string& settings_digest,
485 const base::Time& last_checkin_time,
486 const UpdateCallback& callback) {
487 leveldb::WriteBatch write_batch;
488
489 // Remove all existing settings.
490 leveldb::ReadOptions read_options;
491 read_options.verify_checksums = true;
492 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options));
493 for (iter->Seek(MakeSlice(kGServiceSettingKeyStart));
494 iter->Valid() && iter->key().ToString() < kGServiceSettingKeyEnd;
495 iter->Next()) {
496 write_batch.Delete(iter->key());
497 }
498
499 // Add the new settings.
500 for (std::map<std::string, std::string>::const_iterator iter =
501 settings.begin();
502 iter != settings.end(); ++iter) {
503 write_batch.Put(MakeSlice(MakeGServiceSettingKey(iter->first)),
504 MakeSlice(iter->second));
505 }
506
507 // Update the settings digest.
508 write_batch.Put(MakeSlice(kGServiceSettingsDigestKey),
509 MakeSlice(settings_digest));
510
511 // Update last checkin time.
512 int64 last_checkin_time_internal = last_checkin_time.ToInternalValue();
513 write_batch.Put(MakeSlice(kLastCheckinTimeKey),
514 MakeSlice(base::Int64ToString(last_checkin_time_internal)));
515
516 // Write it all in a batch.
517 leveldb::WriteOptions write_options;
518 write_options.sync = true;
519
520 leveldb::Status s = db_->Write(write_options, &write_batch);
521 if (!s.ok())
522 LOG(ERROR) << "LevelDB GService Settings update failed: " << s.ToString();
523 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok()));
524 }
525
448 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id, 526 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id,
449 uint64* security_token) { 527 uint64* security_token) {
450 leveldb::ReadOptions read_options; 528 leveldb::ReadOptions read_options;
451 read_options.verify_checksums = true; 529 read_options.verify_checksums = true;
452 530
453 std::string result; 531 std::string result;
454 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result); 532 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result);
455 if (s.ok()) { 533 if (s.ok()) {
456 if (!base::StringToUint64(result, android_id)) { 534 if (!base::StringToUint64(result, android_id)) {
457 LOG(ERROR) << "Failed to restore device id."; 535 LOG(ERROR) << "Failed to restore device id.";
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 return false; 631 return false;
554 } 632 }
555 DVLOG(1) << "Found outgoing message with id " << id << " of type " 633 DVLOG(1) << "Found outgoing message with id " << id << " of type "
556 << base::IntToString(tag); 634 << base::IntToString(tag);
557 (*outgoing_messages)[id] = make_linked_ptr(message.release()); 635 (*outgoing_messages)[id] = make_linked_ptr(message.release());
558 } 636 }
559 637
560 return true; 638 return true;
561 } 639 }
562 640
641 bool GCMStoreImpl::Backend::LoadGServicesSettings(
642 std::map<std::string, std::string>* settings,
643 std::string* digest,
644 base::Time* last_checkin_time) {
645 leveldb::ReadOptions read_options;
646 read_options.verify_checksums = true;
647
648 // Load all of the GServices settings.
649 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options));
650 for (iter->Seek(MakeSlice(kGServiceSettingKeyStart));
651 iter->Valid() && iter->key().ToString() < kGServiceSettingKeyEnd;
652 iter->Next()) {
653 std::string value = iter->value().ToString();
654 if (value.empty()) {
655 LOG(ERROR) << "Error reading GService Settings " << value;
656 return false;
657 }
658 std::string id = ParseGServiceSettingKey(iter->key().ToString());
659 (*settings)[id] = value;
660 DVLOG(1) << "Found G Service setting with key: " << id
661 << ", and value: " << value;
662 }
663
664 // Load the settings digest. It's ok if it is empty.
665 db_->Get(read_options, MakeSlice(kGServiceSettingsDigestKey), digest);
666
667 // Load the last checkin time.
668 std::string result;
669 leveldb::Status s = db_->Get(read_options,
670 MakeSlice(kLastCheckinTimeKey),
671 &result);
672 int64 time_internal = 0LL;
673 if (s.ok()) {
674 if (!base::StringToInt64(result, &time_internal)) {
675 LOG(ERROR) << "Failed to restore last checkin time.";
676 return false;
677 }
678 }
679
680 // In case we cannot read last checkin time, we default it to 0, as we don't
681 // want that situation to cause the whole load to fail.
682 *last_checkin_time = base::Time::FromInternalValue(time_internal);
683
684 return true;
685 }
686
563 GCMStoreImpl::GCMStoreImpl( 687 GCMStoreImpl::GCMStoreImpl(
564 bool use_mock_keychain, 688 bool use_mock_keychain,
565 const base::FilePath& path, 689 const base::FilePath& path,
566 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) 690 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
567 : backend_(new Backend(path, base::MessageLoopProxy::current())), 691 : backend_(new Backend(path, base::MessageLoopProxy::current())),
568 blocking_task_runner_(blocking_task_runner), 692 blocking_task_runner_(blocking_task_runner),
569 weak_ptr_factory_(this) { 693 weak_ptr_factory_(this) {
570 // On OSX, prevent the Keychain permissions popup during unit tests. 694 // On OSX, prevent the Keychain permissions popup during unit tests.
571 #if defined(OS_MACOSX) 695 #if defined(OS_MACOSX)
572 OSCrypt::UseMockKeychain(use_mock_keychain); 696 OSCrypt::UseMockKeychain(use_mock_keychain);
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 blocking_task_runner_->PostTask( 851 blocking_task_runner_->PostTask(
728 FROM_HERE, 852 FROM_HERE,
729 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages, 853 base::Bind(&GCMStoreImpl::Backend::RemoveOutgoingMessages,
730 backend_, 854 backend_,
731 persistent_ids, 855 persistent_ids,
732 base::Bind(&GCMStoreImpl::RemoveOutgoingMessagesContinuation, 856 base::Bind(&GCMStoreImpl::RemoveOutgoingMessagesContinuation,
733 weak_ptr_factory_.GetWeakPtr(), 857 weak_ptr_factory_.GetWeakPtr(),
734 callback))); 858 callback)));
735 } 859 }
736 860
861 void GCMStoreImpl::SetGServicesSettings(
862 const std::map<std::string, std::string>& settings,
863 const std::string& digest,
864 const base::Time& last_checkin_time,
865 const UpdateCallback& callback) {
866 blocking_task_runner_->PostTask(
867 FROM_HERE,
868 base::Bind(&GCMStoreImpl::Backend::SetGServicesSettings,
869 backend_,
870 settings,
871 digest,
872 last_checkin_time,
873 callback));
874 }
875
737 void GCMStoreImpl::LoadContinuation(const LoadCallback& callback, 876 void GCMStoreImpl::LoadContinuation(const LoadCallback& callback,
738 scoped_ptr<LoadResult> result) { 877 scoped_ptr<LoadResult> result) {
739 if (!result->success) { 878 if (!result->success) {
740 callback.Run(result.Pass()); 879 callback.Run(result.Pass());
741 return; 880 return;
742 } 881 }
743 int num_throttled_apps = 0; 882 int num_throttled_apps = 0;
744 for (OutgoingMessageMap::const_iterator 883 for (OutgoingMessageMap::const_iterator
745 iter = result->outgoing_messages.begin(); 884 iter = result->outgoing_messages.begin();
746 iter != result->outgoing_messages.end(); ++iter) { 885 iter != result->outgoing_messages.end(); ++iter) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 removed_message_counts.begin(); 920 removed_message_counts.begin();
782 iter != removed_message_counts.end(); ++iter) { 921 iter != removed_message_counts.end(); ++iter) {
783 DCHECK_NE(app_message_counts_.count(iter->first), 0U); 922 DCHECK_NE(app_message_counts_.count(iter->first), 0U);
784 app_message_counts_[iter->first] -= iter->second; 923 app_message_counts_[iter->first] -= iter->second;
785 DCHECK_GE(app_message_counts_[iter->first], 0); 924 DCHECK_GE(app_message_counts_[iter->first], 0);
786 } 925 }
787 callback.Run(true); 926 callback.Run(true);
788 } 927 }
789 928
790 } // namespace gcm 929 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698