| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 return key.substr(arraysize(kInstanceIDKeyStart) - 1); | 171 return key.substr(arraysize(kInstanceIDKeyStart) - 1); |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Note: leveldb::Slice keeps a pointer to the data in |s|, which must therefore | 174 // Note: leveldb::Slice keeps a pointer to the data in |s|, which must therefore |
| 175 // outlive the slice. | 175 // outlive the slice. |
| 176 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid. | 176 // For example: MakeSlice(MakeOutgoingKey(x)) is invalid. |
| 177 leveldb::Slice MakeSlice(const base::StringPiece& s) { | 177 leveldb::Slice MakeSlice(const base::StringPiece& s) { |
| 178 return leveldb::Slice(s.begin(), s.size()); | 178 return leveldb::Slice(s.begin(), s.size()); |
| 179 } | 179 } |
| 180 | 180 |
| 181 bool DatabaseExists(const base::FilePath& path) { |
| 182 // It's not enough to check that the directory exists, since DestroyDB |
| 183 // sometimes leaves behind an empty directory |
| 184 // (https://github.com/google/leveldb/issues/215). |
| 185 return base::PathExists(path.Append(FILE_PATH_LITERAL("CURRENT"))); |
| 186 } |
| 187 |
| 181 } // namespace | 188 } // namespace |
| 182 | 189 |
| 183 class GCMStoreImpl::Backend | 190 class GCMStoreImpl::Backend |
| 184 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { | 191 : public base::RefCountedThreadSafe<GCMStoreImpl::Backend> { |
| 185 public: | 192 public: |
| 186 Backend(const base::FilePath& path, | 193 Backend(const base::FilePath& path, |
| 187 scoped_refptr<base::SequencedTaskRunner> foreground_runner, | 194 scoped_refptr<base::SequencedTaskRunner> foreground_runner, |
| 188 std::unique_ptr<Encryptor> encryptor); | 195 std::unique_ptr<Encryptor> encryptor); |
| 189 | 196 |
| 190 // Blocking implementations of GCMStoreImpl methods. | 197 // Blocking implementations of GCMStoreImpl methods. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 LoadStatus GCMStoreImpl::Backend::OpenStoreAndLoadData(StoreOpenMode open_mode, | 287 LoadStatus GCMStoreImpl::Backend::OpenStoreAndLoadData(StoreOpenMode open_mode, |
| 281 LoadResult* result) { | 288 LoadResult* result) { |
| 282 LoadStatus load_status; | 289 LoadStatus load_status; |
| 283 if (db_.get()) { | 290 if (db_.get()) { |
| 284 LOG(ERROR) << "Attempting to reload open database."; | 291 LOG(ERROR) << "Attempting to reload open database."; |
| 285 return RELOADING_OPEN_STORE; | 292 return RELOADING_OPEN_STORE; |
| 286 } | 293 } |
| 287 | 294 |
| 288 // Checks if the store exists or not. Calling DB::Open with create_if_missing | 295 // Checks if the store exists or not. Calling DB::Open with create_if_missing |
| 289 // not set will still create a new directory if the store does not exist. | 296 // not set will still create a new directory if the store does not exist. |
| 290 if (open_mode == DO_NOT_CREATE && !base::DirectoryExists(path_)) { | 297 if (open_mode == DO_NOT_CREATE && !DatabaseExists(path_)) { |
| 291 DVLOG(2) << "Database " << path_.value() << " does not exist"; | 298 DVLOG(2) << "Database " << path_.value() << " does not exist"; |
| 292 return STORE_DOES_NOT_EXIST; | 299 return STORE_DOES_NOT_EXIST; |
| 293 } | 300 } |
| 294 | 301 |
| 295 leveldb::Options options; | 302 leveldb::Options options; |
| 296 options.create_if_missing = open_mode == CREATE_IF_MISSING; | 303 options.create_if_missing = open_mode == CREATE_IF_MISSING; |
| 297 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 304 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
| 298 options.paranoid_checks = true; | 305 options.paranoid_checks = true; |
| 299 leveldb::DB* db; | 306 leveldb::DB* db; |
| 300 leveldb::Status status = | 307 leveldb::Status status = |
| (...skipping 1173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1474 removed_message_counts.begin(); | 1481 removed_message_counts.begin(); |
| 1475 iter != removed_message_counts.end(); ++iter) { | 1482 iter != removed_message_counts.end(); ++iter) { |
| 1476 DCHECK_NE(app_message_counts_.count(iter->first), 0U); | 1483 DCHECK_NE(app_message_counts_.count(iter->first), 0U); |
| 1477 app_message_counts_[iter->first] -= iter->second; | 1484 app_message_counts_[iter->first] -= iter->second; |
| 1478 DCHECK_GE(app_message_counts_[iter->first], 0); | 1485 DCHECK_GE(app_message_counts_[iter->first], 0); |
| 1479 } | 1486 } |
| 1480 callback.Run(true); | 1487 callback.Run(true); |
| 1481 } | 1488 } |
| 1482 | 1489 |
| 1483 } // namespace gcm | 1490 } // namespace gcm |
| OLD | NEW |