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/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 LOADING_DEVICE_CREDENTIALS_FAILED, | 43 LOADING_DEVICE_CREDENTIALS_FAILED, |
44 LOADING_REGISTRATION_FAILED, | 44 LOADING_REGISTRATION_FAILED, |
45 LOADING_INCOMING_MESSAGES_FAILED, | 45 LOADING_INCOMING_MESSAGES_FAILED, |
46 LOADING_OUTGOING_MESSAGES_FAILED, | 46 LOADING_OUTGOING_MESSAGES_FAILED, |
47 LOADING_LAST_CHECKIN_INFO_FAILED, | 47 LOADING_LAST_CHECKIN_INFO_FAILED, |
48 LOADING_GSERVICE_SETTINGS_FAILED, | 48 LOADING_GSERVICE_SETTINGS_FAILED, |
49 LOADING_ACCOUNT_MAPPING_FAILED, | 49 LOADING_ACCOUNT_MAPPING_FAILED, |
50 LOADING_LAST_TOKEN_TIME_FAILED, | 50 LOADING_LAST_TOKEN_TIME_FAILED, |
51 LOADING_HEARTBEAT_INTERVALS_FAILED, | 51 LOADING_HEARTBEAT_INTERVALS_FAILED, |
52 LOADING_INSTANCE_ID_DATA_FAILED, | 52 LOADING_INSTANCE_ID_DATA_FAILED, |
| 53 STORE_DOES_NOT_EXIST, |
53 | 54 |
54 // NOTE: always keep this entry at the end. Add new status types only | 55 // NOTE: always keep this entry at the end. Add new status types only |
55 // immediately above this line. Make sure to update the corresponding | 56 // immediately above this line. Make sure to update the corresponding |
56 // histogram enum accordingly. | 57 // histogram enum accordingly. |
57 LOAD_STATUS_COUNT | 58 LOAD_STATUS_COUNT |
58 }; | 59 }; |
59 | 60 |
60 // Limit to the number of outstanding messages per app. | 61 // Limit to the number of outstanding messages per app. |
61 const int kMessagesPerAppLimit = 20; | 62 const int kMessagesPerAppLimit = 20; |
62 | 63 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 } // namespace | 179 } // namespace |
179 | 180 |
180 class GCMStoreImpl::Backend | 181 class GCMStoreImpl::Backend |
181 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { | 182 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { |
182 public: | 183 public: |
183 Backend(const base::FilePath& path, | 184 Backend(const base::FilePath& path, |
184 scoped_refptr<base::SequencedTaskRunner> foreground_runner, | 185 scoped_refptr<base::SequencedTaskRunner> foreground_runner, |
185 scoped_ptr<Encryptor> encryptor); | 186 scoped_ptr<Encryptor> encryptor); |
186 | 187 |
187 // Blocking implementations of GCMStoreImpl methods. | 188 // Blocking implementations of GCMStoreImpl methods. |
188 void Load(const LoadCallback& callback); | 189 void Load(StoreOpenMode open_mode, const LoadCallback& callback); |
189 void Close(); | 190 void Close(); |
190 void Destroy(const UpdateCallback& callback); | 191 void Destroy(const UpdateCallback& callback); |
191 void SetDeviceCredentials(uint64 device_android_id, | 192 void SetDeviceCredentials(uint64 device_android_id, |
192 uint64 device_security_token, | 193 uint64 device_security_token, |
193 const UpdateCallback& callback); | 194 const UpdateCallback& callback); |
194 void AddRegistration(const std::string& serialized_key, | 195 void AddRegistration(const std::string& serialized_key, |
195 const std::string& serialized_value, | 196 const std::string& serialized_value, |
196 const UpdateCallback& callback); | 197 const UpdateCallback& callback); |
197 void RemoveRegistration(const std::string& serialized_key, | 198 void RemoveRegistration(const std::string& serialized_key, |
198 const UpdateCallback& callback); | 199 const UpdateCallback& callback); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 void RemoveInstanceIDData(const std::string& app_id, | 237 void RemoveInstanceIDData(const std::string& app_id, |
237 const UpdateCallback& callback); | 238 const UpdateCallback& callback); |
238 void SetValue(const std::string& key, | 239 void SetValue(const std::string& key, |
239 const std::string& value, | 240 const std::string& value, |
240 const UpdateCallback& callback); | 241 const UpdateCallback& callback); |
241 | 242 |
242 private: | 243 private: |
243 friend class base::RefCountedThreadSafe<Backend>; | 244 friend class base::RefCountedThreadSafe<Backend>; |
244 ~Backend(); | 245 ~Backend(); |
245 | 246 |
246 LoadStatus OpenStoreAndLoadData(LoadResult* result); | 247 LoadStatus OpenStoreAndLoadData(StoreOpenMode open_mode, LoadResult* result); |
247 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); | 248 bool LoadDeviceCredentials(uint64* android_id, uint64* security_token); |
248 bool LoadRegistrations(std::map<std::string, std::string>* registrations); | 249 bool LoadRegistrations(std::map<std::string, std::string>* registrations); |
249 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); | 250 bool LoadIncomingMessages(std::vector<std::string>* incoming_messages); |
250 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); | 251 bool LoadOutgoingMessages(OutgoingMessageMap* outgoing_messages); |
251 bool LoadLastCheckinInfo(base::Time* last_checkin_time, | 252 bool LoadLastCheckinInfo(base::Time* last_checkin_time, |
252 std::set<std::string>* accounts); | 253 std::set<std::string>* accounts); |
253 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, | 254 bool LoadGServicesSettings(std::map<std::string, std::string>* settings, |
254 std::string* digest); | 255 std::string* digest); |
255 bool LoadAccountMappingInfo(AccountMappings* account_mappings); | 256 bool LoadAccountMappingInfo(AccountMappings* account_mappings); |
256 bool LoadLastTokenFetchTime(base::Time* last_token_fetch_time); | 257 bool LoadLastTokenFetchTime(base::Time* last_token_fetch_time); |
(...skipping 11 matching lines...) Expand all Loading... |
268 const base::FilePath& path, | 269 const base::FilePath& path, |
269 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner, | 270 scoped_refptr<base::SequencedTaskRunner> foreground_task_runner, |
270 scoped_ptr<Encryptor> encryptor) | 271 scoped_ptr<Encryptor> encryptor) |
271 : path_(path), | 272 : path_(path), |
272 foreground_task_runner_(foreground_task_runner), | 273 foreground_task_runner_(foreground_task_runner), |
273 encryptor_(encryptor.Pass()) { | 274 encryptor_(encryptor.Pass()) { |
274 } | 275 } |
275 | 276 |
276 GCMStoreImpl::Backend::~Backend() {} | 277 GCMStoreImpl::Backend::~Backend() {} |
277 | 278 |
278 LoadStatus GCMStoreImpl::Backend::OpenStoreAndLoadData(LoadResult* result) { | 279 LoadStatus GCMStoreImpl::Backend::OpenStoreAndLoadData(StoreOpenMode open_mode, |
| 280 LoadResult* result) { |
279 LoadStatus load_status; | 281 LoadStatus load_status; |
280 if (db_.get()) { | 282 if (db_.get()) { |
281 LOG(ERROR) << "Attempting to reload open database."; | 283 LOG(ERROR) << "Attempting to reload open database."; |
282 return RELOADING_OPEN_STORE; | 284 return RELOADING_OPEN_STORE; |
283 } | 285 } |
284 | 286 |
| 287 // Checks if the store exists or not. Calling DB::Open with create_if_missing |
| 288 // not set will still create a new directory if the store does not exist. |
| 289 if (open_mode == DO_NOT_CREATE && !base::DirectoryExists(path_)) { |
| 290 DVLOG(2) << "Database " << path_.value() << " does not exist"; |
| 291 return STORE_DOES_NOT_EXIST; |
| 292 } |
| 293 |
285 leveldb::Options options; | 294 leveldb::Options options; |
286 options.create_if_missing = true; | 295 options.create_if_missing = open_mode == CREATE_IF_MISSING; |
287 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 296 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
288 leveldb::DB* db; | 297 leveldb::DB* db; |
289 leveldb::Status status = | 298 leveldb::Status status = |
290 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db); | 299 leveldb::DB::Open(options, path_.AsUTF8Unsafe(), &db); |
291 if (!status.ok()) { | 300 if (!status.ok()) { |
292 LOG(ERROR) << "Failed to open database " << path_.value() << ": " | 301 LOG(ERROR) << "Failed to open database " << path_.value() << ": " |
293 << status.ToString(); | 302 << status.ToString(); |
294 return OPENING_STORE_FAILED; | 303 return OPENING_STORE_FAILED; |
295 } | 304 } |
296 | 305 |
(...skipping 21 matching lines...) Expand all Loading... |
318 if (!LoadLastTokenFetchTime(&result->last_token_fetch_time)) | 327 if (!LoadLastTokenFetchTime(&result->last_token_fetch_time)) |
319 return LOADING_LAST_TOKEN_TIME_FAILED; | 328 return LOADING_LAST_TOKEN_TIME_FAILED; |
320 if (!LoadHeartbeatIntervals(&result->heartbeat_intervals)) | 329 if (!LoadHeartbeatIntervals(&result->heartbeat_intervals)) |
321 return LOADING_HEARTBEAT_INTERVALS_FAILED; | 330 return LOADING_HEARTBEAT_INTERVALS_FAILED; |
322 if (!LoadInstanceIDData(&result->instance_id_data)) | 331 if (!LoadInstanceIDData(&result->instance_id_data)) |
323 return LOADING_INSTANCE_ID_DATA_FAILED; | 332 return LOADING_INSTANCE_ID_DATA_FAILED; |
324 | 333 |
325 return LOADING_SUCCEEDED; | 334 return LOADING_SUCCEEDED; |
326 } | 335 } |
327 | 336 |
328 void GCMStoreImpl::Backend::Load(const LoadCallback& callback) { | 337 void GCMStoreImpl::Backend::Load(StoreOpenMode open_mode, |
| 338 const LoadCallback& callback) { |
329 scoped_ptr<LoadResult> result(new LoadResult()); | 339 scoped_ptr<LoadResult> result(new LoadResult()); |
330 LoadStatus load_status = OpenStoreAndLoadData(result.get()); | 340 LoadStatus load_status = OpenStoreAndLoadData(open_mode, result.get()); |
331 UMA_HISTOGRAM_ENUMERATION("GCM.LoadStatus", load_status, LOAD_STATUS_COUNT); | 341 UMA_HISTOGRAM_ENUMERATION("GCM.LoadStatus", load_status, LOAD_STATUS_COUNT); |
332 if (load_status != LOADING_SUCCEEDED) { | 342 if (load_status != LOADING_SUCCEEDED) { |
333 result->Reset(); | 343 result->Reset(); |
| 344 result->store_does_not_exist = (load_status == STORE_DOES_NOT_EXIST); |
334 foreground_task_runner_->PostTask(FROM_HERE, | 345 foreground_task_runner_->PostTask(FROM_HERE, |
335 base::Bind(callback, | 346 base::Bind(callback, |
336 base::Passed(&result))); | 347 base::Passed(&result))); |
337 return; | 348 return; |
338 } | 349 } |
339 | 350 |
340 // |result->registrations| contains both GCM registrations and InstanceID | 351 // |result->registrations| contains both GCM registrations and InstanceID |
341 // tokens. Count them separately. | 352 // tokens. Count them separately. |
342 int gcm_registration_count = 0; | 353 int gcm_registration_count = 0; |
343 int instance_id_token_count = 0; | 354 int instance_id_token_count = 0; |
(...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1149 scoped_ptr<Encryptor> encryptor) | 1160 scoped_ptr<Encryptor> encryptor) |
1150 : backend_(new Backend(path, | 1161 : backend_(new Backend(path, |
1151 base::ThreadTaskRunnerHandle::Get(), | 1162 base::ThreadTaskRunnerHandle::Get(), |
1152 encryptor.Pass())), | 1163 encryptor.Pass())), |
1153 blocking_task_runner_(blocking_task_runner), | 1164 blocking_task_runner_(blocking_task_runner), |
1154 weak_ptr_factory_(this) { | 1165 weak_ptr_factory_(this) { |
1155 } | 1166 } |
1156 | 1167 |
1157 GCMStoreImpl::~GCMStoreImpl() {} | 1168 GCMStoreImpl::~GCMStoreImpl() {} |
1158 | 1169 |
1159 void GCMStoreImpl::Load(const LoadCallback& callback) { | 1170 void GCMStoreImpl::Load(StoreOpenMode open_mode, const LoadCallback& callback) { |
1160 blocking_task_runner_->PostTask( | 1171 blocking_task_runner_->PostTask( |
1161 FROM_HERE, | 1172 FROM_HERE, |
1162 base::Bind(&GCMStoreImpl::Backend::Load, | 1173 base::Bind(&GCMStoreImpl::Backend::Load, |
1163 backend_, | 1174 backend_, |
| 1175 open_mode, |
1164 base::Bind(&GCMStoreImpl::LoadContinuation, | 1176 base::Bind(&GCMStoreImpl::LoadContinuation, |
1165 weak_ptr_factory_.GetWeakPtr(), | 1177 weak_ptr_factory_.GetWeakPtr(), |
1166 callback))); | 1178 callback))); |
1167 } | 1179 } |
1168 | 1180 |
1169 void GCMStoreImpl::Close() { | 1181 void GCMStoreImpl::Close() { |
1170 weak_ptr_factory_.InvalidateWeakPtrs(); | 1182 weak_ptr_factory_.InvalidateWeakPtrs(); |
1171 app_message_counts_.clear(); | 1183 app_message_counts_.clear(); |
1172 blocking_task_runner_->PostTask( | 1184 blocking_task_runner_->PostTask( |
1173 FROM_HERE, | 1185 FROM_HERE, |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1457 removed_message_counts.begin(); | 1469 removed_message_counts.begin(); |
1458 iter != removed_message_counts.end(); ++iter) { | 1470 iter != removed_message_counts.end(); ++iter) { |
1459 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 1471 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
1460 app_message_counts_[iter->first] -= iter->second; | 1472 app_message_counts_[iter->first] -= iter->second; |
1461 DCHECK_GE(app_message_counts_[iter->first], 0); | 1473 DCHECK_GE(app_message_counts_[iter->first], 0); |
1462 } | 1474 } |
1463 callback.Run(true); | 1475 callback.Run(true); |
1464 } | 1476 } |
1465 | 1477 |
1466 } // namespace gcm | 1478 } // namespace gcm |
OLD | NEW |