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/time/time.h" |
20 #include "base/tracked_objects.h" | 20 #include "base/tracked_objects.h" |
21 #include "google_apis/gcm/base/encryptor.h" | 21 #include "components/os_crypt/os_crypt.h" |
22 #include "google_apis/gcm/base/mcs_message.h" | 22 #include "google_apis/gcm/base/mcs_message.h" |
23 #include "google_apis/gcm/base/mcs_util.h" | 23 #include "google_apis/gcm/base/mcs_util.h" |
24 #include "google_apis/gcm/protocol/mcs.pb.h" | 24 #include "google_apis/gcm/protocol/mcs.pb.h" |
25 #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" | 26 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
27 | 27 |
28 namespace gcm { | 28 namespace gcm { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 leveldb::Slice MakeSlice(const base::StringPiece& s) { | 100 leveldb::Slice MakeSlice(const base::StringPiece& s) { |
101 return leveldb::Slice(s.begin(), s.size()); | 101 return leveldb::Slice(s.begin(), s.size()); |
102 } | 102 } |
103 | 103 |
104 } // namespace | 104 } // namespace |
105 | 105 |
106 class GCMStoreImpl::Backend | 106 class GCMStoreImpl::Backend |
107 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { | 107 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { |
108 public: | 108 public: |
109 Backend(const base::FilePath& path, | 109 Backend(const base::FilePath& path, |
110 scoped_refptr<base::SequencedTaskRunner> foreground_runner, | 110 scoped_refptr<base::SequencedTaskRunner> foreground_runner); |
111 scoped_ptr<Encryptor> encryptor); | |
112 | 111 |
113 // Blocking implementations of GCMStoreImpl methods. | 112 // Blocking implementations of GCMStoreImpl methods. |
114 void Load(const LoadCallback& callback); | 113 void Load(const LoadCallback& callback); |
115 void Close(); | 114 void Close(); |
116 void Destroy(const UpdateCallback& callback); | 115 void Destroy(const UpdateCallback& callback); |
117 void SetDeviceCredentials(uint64 device_android_id, | 116 void SetDeviceCredentials(uint64 device_android_id, |
118 uint64 device_security_token, | 117 uint64 device_security_token, |
119 const UpdateCallback& callback); | 118 const UpdateCallback& callback); |
120 void AddRegistration(const std::string& app_id, | 119 void AddRegistration(const std::string& app_id, |
121 const linked_ptr<RegistrationInfo>& registration, | 120 const linked_ptr<RegistrationInfo>& registration, |
(...skipping 30 matching lines...) Expand all Loading... |
152 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); | 151 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); |
153 bool LoadRegistrations(RegistrationInfoMap* registrations); | 152 bool LoadRegistrations(RegistrationInfoMap* registrations); |
154 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); | 153 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); |
155 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); | 154 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); |
156 bool LoadLastCheckinTime(base::Time* last_checkin_time); | 155 bool LoadLastCheckinTime(base::Time* last_checkin_time); |
157 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, | 156 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, |
158 std::string* digest); | 157 std::string* digest); |
159 | 158 |
160 const base::FilePath path_; | 159 const base::FilePath path_; |
161 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; | 160 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner_; |
162 scoped_ptr<Encryptor> encryptor_; | |
163 | 161 |
164 scoped_ptr<leveldb::DB> db_; | 162 scoped_ptr<leveldb::DB> db_; |
165 }; | 163 }; |
166 | 164 |
167 GCMStoreImpl::Backend::Backend( | 165 GCMStoreImpl::Backend::Backend( |
168 const base::FilePath& path, | 166 const base::FilePath& path, |
169 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner, | 167 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner) |
170 scoped_ptr<Encryptor> encryptor) | 168 : path_(path), foreground_task_runner_(foreground_task_runner) {} |
171 : path_(path), | |
172 foreground_task_runner_(foreground_task_runner), | |
173 encryptor_(encryptor.Pass()) { | |
174 } | |
175 | 169 |
176 GCMStoreImpl::Backend::~Backend() {} | 170 GCMStoreImpl::Backend::~Backend() {} |
177 | 171 |
178 void GCMStoreImpl::Backend::Load(const LoadCallback& callback) { | 172 void GCMStoreImpl::Backend::Load(const LoadCallback& callback) { |
179 scoped_ptr<LoadResult> result(new LoadResult()); | 173 scoped_ptr<LoadResult> result(new LoadResult()); |
180 if (db_.get()) { | 174 if (db_.get()) { |
181 LOG(ERROR) << "Attempting to reload open database."; | 175 LOG(ERROR) << "Attempting to reload open database."; |
182 foreground_task_runner_->PostTask(FROM_HERE, | 176 foreground_task_runner_->PostTask(FROM_HERE, |
183 base::Bind(callback, | 177 base::Bind(callback, |
184 base::Passed(&result))); | 178 base::Passed(&result))); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 if (!db_.get()) { | 271 if (!db_.get()) { |
278 LOG(ERROR) << "GCMStore db doesn't exist."; | 272 LOG(ERROR) << "GCMStore db doesn't exist."; |
279 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); | 273 foreground_task_runner_->PostTask(FROM_HERE, base::Bind(callback, false)); |
280 return; | 274 return; |
281 } | 275 } |
282 | 276 |
283 leveldb::WriteOptions write_options; | 277 leveldb::WriteOptions write_options; |
284 write_options.sync = true; | 278 write_options.sync = true; |
285 | 279 |
286 std::string encrypted_token; | 280 std::string encrypted_token; |
287 encryptor_->EncryptString(base::Uint64ToString(device_security_token), | 281 OSCrypt::EncryptString(base::Uint64ToString(device_security_token), |
288 &encrypted_token); | 282 &encrypted_token); |
289 std::string android_id_str = base::Uint64ToString(device_android_id); | 283 std::string android_id_str = base::Uint64ToString(device_android_id); |
290 leveldb::Status s = | 284 leveldb::Status s = |
291 db_->Put(write_options, | 285 db_->Put(write_options, |
292 MakeSlice(kDeviceAIDKey), | 286 MakeSlice(kDeviceAIDKey), |
293 MakeSlice(android_id_str)); | 287 MakeSlice(android_id_str)); |
294 if (s.ok()) { | 288 if (s.ok()) { |
295 s = db_->Put( | 289 s = db_->Put( |
296 write_options, MakeSlice(kDeviceTokenKey), MakeSlice(encrypted_token)); | 290 write_options, MakeSlice(kDeviceTokenKey), MakeSlice(encrypted_token)); |
297 } | 291 } |
298 if (s.ok()) { | 292 if (s.ok()) { |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 if (s.ok()) { | 544 if (s.ok()) { |
551 if (!base::StringToUint64(result, android_id)) { | 545 if (!base::StringToUint64(result, android_id)) { |
552 LOG(ERROR) << "Failed to restore device id."; | 546 LOG(ERROR) << "Failed to restore device id."; |
553 return false; | 547 return false; |
554 } | 548 } |
555 result.clear(); | 549 result.clear(); |
556 s = db_->Get(read_options, MakeSlice(kDeviceTokenKey), &result); | 550 s = db_->Get(read_options, MakeSlice(kDeviceTokenKey), &result); |
557 } | 551 } |
558 if (s.ok()) { | 552 if (s.ok()) { |
559 std::string decrypted_token; | 553 std::string decrypted_token; |
560 encryptor_->DecryptString(result, &decrypted_token); | 554 OSCrypt::DecryptString(result, &decrypted_token); |
561 if (!base::StringToUint64(decrypted_token, security_token)) { | 555 if (!base::StringToUint64(decrypted_token, security_token)) { |
562 LOG(ERROR) << "Failed to restore security token."; | 556 LOG(ERROR) << "Failed to restore security token."; |
563 return false; | 557 return false; |
564 } | 558 } |
565 return true; | 559 return true; |
566 } | 560 } |
567 | 561 |
568 if (s.IsNotFound()) { | 562 if (s.IsNotFound()) { |
569 DVLOG(1) << "No credentials found."; | 563 DVLOG(1) << "No credentials found."; |
570 return true; | 564 return true; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 } | 692 } |
699 | 693 |
700 // Load the settings digest. It's ok if it is empty. | 694 // Load the settings digest. It's ok if it is empty. |
701 db_->Get(read_options, MakeSlice(kGServiceSettingsDigestKey), digest); | 695 db_->Get(read_options, MakeSlice(kGServiceSettingsDigestKey), digest); |
702 | 696 |
703 return true; | 697 return true; |
704 } | 698 } |
705 | 699 |
706 GCMStoreImpl::GCMStoreImpl( | 700 GCMStoreImpl::GCMStoreImpl( |
707 const base::FilePath& path, | 701 const base::FilePath& path, |
708 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | 702 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
709 scoped_ptr<Encryptor> encryptor) | 703 : backend_(new Backend(path, base::MessageLoopProxy::current())), |
710 : backend_(new Backend(path, | |
711 base::MessageLoopProxy::current(), | |
712 encryptor.Pass())), | |
713 blocking_task_runner_(blocking_task_runner), | 704 blocking_task_runner_(blocking_task_runner), |
714 weak_ptr_factory_(this) { | 705 weak_ptr_factory_(this) { |
715 } | 706 } |
716 | 707 |
717 GCMStoreImpl::~GCMStoreImpl() {} | 708 GCMStoreImpl::~GCMStoreImpl() {} |
718 | 709 |
719 void GCMStoreImpl::Load(const LoadCallback& callback) { | 710 void GCMStoreImpl::Load(const LoadCallback& callback) { |
720 blocking_task_runner_->PostTask( | 711 blocking_task_runner_->PostTask( |
721 FROM_HERE, | 712 FROM_HERE, |
722 base::Bind(&GCMStoreImpl::Backend::Load, | 713 base::Bind(&GCMStoreImpl::Backend::Load, |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 removed_message_counts.begin(); | 936 removed_message_counts.begin(); |
946 iter != removed_message_counts.end(); ++iter) { | 937 iter != removed_message_counts.end(); ++iter) { |
947 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 938 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
948 app_message_counts_[iter->first] -= iter->second; | 939 app_message_counts_[iter->first] -= iter->second; |
949 DCHECK_GE(app_message_counts_[iter->first], 0); | 940 DCHECK_GE(app_message_counts_[iter->first], 0); |
950 } | 941 } |
951 callback.Run(true); | 942 callback.Run(true); |
952 } | 943 } |
953 | 944 |
954 } // namespace gcm | 945 } // namespace gcm |
OLD | NEW |