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

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

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

Powered by Google App Engine
This is Rietveld 408576698