OLD | NEW |
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 Loading... |
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"; |
56 // Key used to timestamp last checkin (marked with G services settings update). | 66 // Key used to timestamp last checkin (marked with G services settings update). |
57 const char kLastCheckinTimeKey[] = "last_checkin_time"; | 67 const char kLastCheckinTimeKey[] = "last_checkin_time"; |
58 | 68 |
59 std::string MakeRegistrationKey(const std::string& app_id) { | 69 std::string MakeRegistrationKey(const std::string& app_id) { |
60 return kRegistrationKeyStart + app_id; | 70 return kRegistrationKeyStart + app_id; |
61 } | 71 } |
62 | 72 |
63 std::string ParseRegistrationKey(const std::string& key) { | 73 std::string ParseRegistrationKey(const std::string& key) { |
64 return key.substr(arraysize(kRegistrationKeyStart) - 1); | 74 return key.substr(arraysize(kRegistrationKeyStart) - 1); |
65 } | 75 } |
66 | 76 |
67 std::string MakeIncomingKey(const std::string& persistent_id) { | 77 std::string MakeIncomingKey(const std::string& persistent_id) { |
68 return kIncomingMsgKeyStart + persistent_id; | 78 return kIncomingMsgKeyStart + persistent_id; |
69 } | 79 } |
70 | 80 |
71 std::string MakeOutgoingKey(const std::string& persistent_id) { | 81 std::string MakeOutgoingKey(const std::string& persistent_id) { |
72 return kOutgoingMsgKeyStart + persistent_id; | 82 return kOutgoingMsgKeyStart + persistent_id; |
73 } | 83 } |
74 | 84 |
75 std::string ParseOutgoingKey(const std::string& key) { | 85 std::string ParseOutgoingKey(const std::string& key) { |
76 return key.substr(arraysize(kOutgoingMsgKeyStart) - 1); | 86 return key.substr(arraysize(kOutgoingMsgKeyStart) - 1); |
77 } | 87 } |
78 | 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 |
79 // 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 |
80 // outlive the slice. | 98 // outlive the slice. |
81 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid. | 99 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid. |
82 leveldb::Slice MakeSlice(const base::StringPiece& s) { | 100 leveldb::Slice MakeSlice(const base::StringPiece& s) { |
83 return leveldb::Slice(s.begin(), s.size()); | 101 return leveldb::Slice(s.begin(), s.size()); |
84 } | 102 } |
85 | 103 |
86 } // namespace | 104 } // namespace |
87 | 105 |
88 class GCMStoreImpl::Backend | 106 class GCMStoreImpl::Backend |
(...skipping 25 matching lines...) Expand all Loading... |
114 const PersistentIdList& persistent_ids, | 132 const PersistentIdList& persistent_ids, |
115 const base::Callback<void(bool, const AppIdToMessageCountMap&)> | 133 const base::Callback<void(bool, const AppIdToMessageCountMap&)> |
116 callback); | 134 callback); |
117 void AddUserSerialNumber(const std::string& username, | 135 void AddUserSerialNumber(const std::string& username, |
118 int64 serial_number, | 136 int64 serial_number, |
119 const UpdateCallback& callback); | 137 const UpdateCallback& callback); |
120 void RemoveUserSerialNumber(const std::string& username, | 138 void RemoveUserSerialNumber(const std::string& username, |
121 const UpdateCallback& callback); | 139 const UpdateCallback& callback); |
122 void SetLastCheckinTime(const base::Time& last_checkin_time, | 140 void SetLastCheckinTime(const base::Time& last_checkin_time, |
123 const UpdateCallback& callback); | 141 const UpdateCallback& callback); |
| 142 void SetGServicesSettings( |
| 143 const std::map<std::string, std::string>& settings, |
| 144 const std::string& digest, |
| 145 const UpdateCallback& callback); |
124 | 146 |
125 private: | 147 private: |
126 friend class base::RefCountedThreadSafe<Backend>; | 148 friend class base::RefCountedThreadSafe<Backend>; |
127 ~Backend(); | 149 ~Backend(); |
128 | 150 |
129 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); | 151 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); |
130 bool LoadRegistrations(RegistrationInfoMap* registrations); | 152 bool LoadRegistrations(RegistrationInfoMap* registrations); |
131 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); | 153 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); |
132 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); | 154 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); |
133 bool LoadLastCheckinTime(base::Time* last_checkin_time); | 155 bool LoadLastCheckinTime(base::Time* last_checkin_time); |
| 156 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, |
| 157 std::string* digest); |
134 | 158 |
135 const base::FilePath path_; | 159 const base::FilePath path_; |
136 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; | 160 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; |
137 | 161 |
138 scoped_ptr<leveldb::DB> db_; | 162 scoped_ptr<leveldb::DB> db_; |
139 }; | 163 }; |
140 | 164 |
141 GCMStoreImpl::Backend::Backend( | 165 GCMStoreImpl::Backend::Backend( |
142 const base::FilePath& path, | 166 const base::FilePath& path, |
143 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner) | 167 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner) |
(...skipping 25 matching lines...) Expand all Loading... |
169 base::Passed(&result))); | 193 base::Passed(&result))); |
170 return; | 194 return; |
171 } | 195 } |
172 db_.reset(db); | 196 db_.reset(db); |
173 | 197 |
174 if (!LoadDeviceCredentials(&result->device_android_id, | 198 if (!LoadDeviceCredentials(&result->device_android_id, |
175 &result->device_security_token) || | 199 &result->device_security_token) || |
176 !LoadRegistrations(&result->registrations) || | 200 !LoadRegistrations(&result->registrations) || |
177 !LoadIncomingMessages(&result->incoming_messages) || | 201 !LoadIncomingMessages(&result->incoming_messages) || |
178 !LoadOutgoingMessages(&result->outgoing_messages) || | 202 !LoadOutgoingMessages(&result->outgoing_messages) || |
179 !LoadLastCheckinTime(&result->last_checkin_time)) { | 203 !LoadLastCheckinTime(&result->last_checkin_time) || |
| 204 !LoadGServicesSettings(&result->gservices_settings, |
| 205 &result->gservices_digest)) { |
180 result->device_android_id = 0; | 206 result->device_android_id = 0; |
181 result->device_security_token = 0; | 207 result->device_security_token = 0; |
182 result->registrations.clear(); | 208 result->registrations.clear(); |
183 result->incoming_messages.clear(); | 209 result->incoming_messages.clear(); |
184 result->outgoing_messages.clear(); | 210 result->outgoing_messages.clear(); |
| 211 result->gservices_settings.clear(); |
| 212 result->gservices_digest.clear(); |
185 result->last_checkin_time = base::Time::FromInternalValue(0LL); | 213 result->last_checkin_time = base::Time::FromInternalValue(0LL); |
186 foreground_task_runner_->PostTask(FROM_HERE, | 214 foreground_task_runner_->PostTask(FROM_HERE, |
187 base::Bind(callback, | 215 base::Bind(callback, |
188 base::Passed(&result))); | 216 base::Passed(&result))); |
189 return; | 217 return; |
190 } | 218 } |
191 | 219 |
192 // 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. |
193 if (result->device_android_id != 0 && result->device_security_token != 0) { | 221 if (result->device_android_id != 0 && result->device_security_token != 0) { |
194 int64 file_size = 0; | 222 int64 file_size = 0; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 write_options.sync = true; | 486 write_options.sync = true; |
459 | 487 |
460 int64 last_checkin_time_internal = last_checkin_time.ToInternalValue(); | 488 int64 last_checkin_time_internal = last_checkin_time.ToInternalValue(); |
461 const leveldb::Status s = | 489 const leveldb::Status s = |
462 db_->Put(write_options, | 490 db_->Put(write_options, |
463 MakeSlice(kLastCheckinTimeKey), | 491 MakeSlice(kLastCheckinTimeKey), |
464 MakeSlice(base::Int64ToString(last_checkin_time_internal))); | 492 MakeSlice(base::Int64ToString(last_checkin_time_internal))); |
465 | 493 |
466 if (!s.ok()) | 494 if (!s.ok()) |
467 LOG(ERROR) << "LevelDB set last checkin time failed: " << s.ToString(); | 495 LOG(ERROR) << "LevelDB set last checkin time failed: " << s.ToString(); |
468 | |
469 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); | 496 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); |
470 } | 497 } |
471 | 498 |
| 499 void GCMStoreImpl::Backend::SetGServicesSettings( |
| 500 const std::map<std::string, std::string>& settings, |
| 501 const std::string& settings_digest, |
| 502 const UpdateCallback& callback) { |
| 503 leveldb::WriteBatch write_batch; |
| 504 |
| 505 // Remove all existing settings. |
| 506 leveldb::ReadOptions read_options; |
| 507 read_options.verify_checksums = true; |
| 508 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
| 509 for (iter->Seek(MakeSlice(kGServiceSettingKeyStart)); |
| 510 iter->Valid() && iter->key().ToString() < kGServiceSettingKeyEnd; |
| 511 iter->Next()) { |
| 512 write_batch.Delete(iter->key()); |
| 513 } |
| 514 |
| 515 // Add the new settings. |
| 516 for (std::map<std::string, std::string>::const_iterator iter = |
| 517 settings.begin(); |
| 518 iter != settings.end(); ++iter) { |
| 519 write_batch.Put(MakeSlice(MakeGServiceSettingKey(iter->first)), |
| 520 MakeSlice(iter->second)); |
| 521 } |
| 522 |
| 523 // Update the settings digest. |
| 524 write_batch.Put(MakeSlice(kGServiceSettingsDigestKey), |
| 525 MakeSlice(settings_digest)); |
| 526 |
| 527 // Write it all in a batch. |
| 528 leveldb::WriteOptions write_options; |
| 529 write_options.sync = true; |
| 530 |
| 531 leveldb::Status s = db_->Write(write_options, &write_batch); |
| 532 if (!s.ok()) |
| 533 LOG(ERROR) << "LevelDB GService Settings update failed: " << s.ToString(); |
| 534 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, s.ok())); |
| 535 } |
| 536 |
472 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id, | 537 bool GCMStoreImpl::Backend::LoadDeviceCredentials(uint64* android_id, |
473 uint64* security_token) { | 538 uint64* security_token) { |
474 leveldb::ReadOptions read_options; | 539 leveldb::ReadOptions read_options; |
475 read_options.verify_checksums = true; | 540 read_options.verify_checksums = true; |
476 | 541 |
477 std::string result; | 542 std::string result; |
478 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result); | 543 leveldb::Status s = db_->Get(read_options, MakeSlice(kDeviceAIDKey), &result); |
479 if (s.ok()) { | 544 if (s.ok()) { |
480 if (!base::StringToUint64(result, android_id)) { | 545 if (!base::StringToUint64(result, android_id)) { |
481 LOG(ERROR) << "Failed to restore device id."; | 546 LOG(ERROR) << "Failed to restore device id."; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 if (s.ok() && !base::StringToInt64(result, &time_internal)) | 662 if (s.ok() && !base::StringToInt64(result, &time_internal)) |
598 LOG(ERROR) << "Failed to restore last checkin time. Using default = 0."; | 663 LOG(ERROR) << "Failed to restore last checkin time. Using default = 0."; |
599 | 664 |
600 // In case we cannot read last checkin time, we default it to 0, as we don't | 665 // In case we cannot read last checkin time, we default it to 0, as we don't |
601 // want that situation to cause the whole load to fail. | 666 // want that situation to cause the whole load to fail. |
602 *last_checkin_time = base::Time::FromInternalValue(time_internal); | 667 *last_checkin_time = base::Time::FromInternalValue(time_internal); |
603 | 668 |
604 return true; | 669 return true; |
605 } | 670 } |
606 | 671 |
| 672 bool GCMStoreImpl::Backend::LoadGServicesSettings( |
| 673 std::map<std::string, std::string>* settings, |
| 674 std::string* digest) { |
| 675 leveldb::ReadOptions read_options; |
| 676 read_options.verify_checksums = true; |
| 677 |
| 678 // Load all of the GServices settings. |
| 679 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options)); |
| 680 for (iter->Seek(MakeSlice(kGServiceSettingKeyStart)); |
| 681 iter->Valid() && iter->key().ToString() < kGServiceSettingKeyEnd; |
| 682 iter->Next()) { |
| 683 std::string value = iter->value().ToString(); |
| 684 if (value.empty()) { |
| 685 LOG(ERROR) << "Error reading GService Settings " << value; |
| 686 return false; |
| 687 } |
| 688 std::string id = ParseGServiceSettingKey(iter->key().ToString()); |
| 689 (*settings)[id] = value; |
| 690 DVLOG(1) << "Found G Service setting with key: " << id |
| 691 << ", and value: " << value; |
| 692 } |
| 693 |
| 694 // Load the settings digest. It's ok if it is empty. |
| 695 db_->Get(read_options, MakeSlice(kGServiceSettingsDigestKey), digest); |
| 696 |
| 697 return true; |
| 698 } |
| 699 |
607 GCMStoreImpl::GCMStoreImpl( | 700 GCMStoreImpl::GCMStoreImpl( |
608 const base::FilePath& path, | 701 const base::FilePath& path, |
609 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 702 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
610 : backend_(new Backend(path, base::MessageLoopProxy::current())), | 703 : backend_(new Backend(path, base::MessageLoopProxy::current())), |
611 blocking_task_runner_(blocking_task_runner), | 704 blocking_task_runner_(blocking_task_runner), |
612 weak_ptr_factory_(this) { | 705 weak_ptr_factory_(this) { |
613 } | 706 } |
614 | 707 |
615 GCMStoreImpl::~GCMStoreImpl() {} | 708 GCMStoreImpl::~GCMStoreImpl() {} |
616 | 709 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 void GCMStoreImpl::SetLastCheckinTime(const base::Time& last_checkin_time, | 869 void GCMStoreImpl::SetLastCheckinTime(const base::Time& last_checkin_time, |
777 const UpdateCallback& callback) { | 870 const UpdateCallback& callback) { |
778 blocking_task_runner_->PostTask( | 871 blocking_task_runner_->PostTask( |
779 FROM_HERE, | 872 FROM_HERE, |
780 base::Bind(&GCMStoreImpl::Backend::SetLastCheckinTime, | 873 base::Bind(&GCMStoreImpl::Backend::SetLastCheckinTime, |
781 backend_, | 874 backend_, |
782 last_checkin_time, | 875 last_checkin_time, |
783 callback)); | 876 callback)); |
784 } | 877 } |
785 | 878 |
| 879 void GCMStoreImpl::SetGServicesSettings( |
| 880 const std::map<std::string, std::string>& settings, |
| 881 const std::string& digest, |
| 882 const UpdateCallback& callback) { |
| 883 blocking_task_runner_->PostTask( |
| 884 FROM_HERE, |
| 885 base::Bind(&GCMStoreImpl::Backend::SetGServicesSettings, |
| 886 backend_, |
| 887 settings, |
| 888 digest, |
| 889 callback)); |
| 890 } |
| 891 |
786 void GCMStoreImpl::LoadContinuation(const LoadCallback& callback, | 892 void GCMStoreImpl::LoadContinuation(const LoadCallback& callback, |
787 scoped_ptr<LoadResult> result) { | 893 scoped_ptr<LoadResult> result) { |
788 if (!result->success) { | 894 if (!result->success) { |
789 callback.Run(result.Pass()); | 895 callback.Run(result.Pass()); |
790 return; | 896 return; |
791 } | 897 } |
792 int num_throttled_apps = 0; | 898 int num_throttled_apps = 0; |
793 for (OutgoingMessageMap::const_iterator | 899 for (OutgoingMessageMap::const_iterator |
794 iter = result->outgoing_messages.begin(); | 900 iter = result->outgoing_messages.begin(); |
795 iter != result->outgoing_messages.end(); ++iter) { | 901 iter != result->outgoing_messages.end(); ++iter) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
830 removed_message_counts.begin(); | 936 removed_message_counts.begin(); |
831 iter != removed_message_counts.end(); ++iter) { | 937 iter != removed_message_counts.end(); ++iter) { |
832 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 938 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
833 app_message_counts_[iter->first] -= iter->second; | 939 app_message_counts_[iter->first] -= iter->second; |
834 DCHECK_GE(app_message_counts_[iter->first], 0); | 940 DCHECK_GE(app_message_counts_[iter->first], 0); |
835 } | 941 } |
836 callback.Run(true); | 942 callback.Run(true); |
837 } | 943 } |
838 | 944 |
839 } // namespace gcm | 945 } // namespace gcm |
OLD | NEW |