| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/settings/settings_leveldb_storage.h" | 5 #include "chrome/browser/extensions/settings/settings_leveldb_storage.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 leveldb::DB* db_; | 42 leveldb::DB* db_; |
| 43 const leveldb::Snapshot* snapshot_; | 43 const leveldb::Snapshot* snapshot_; |
| 44 | 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(ScopedSnapshot); | 45 DISALLOW_COPY_AND_ASSIGN(ScopedSnapshot); |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 /* static */ | 50 // SettingsLeveldbStorage::Factory |
| 51 SettingsLeveldbStorage* SettingsLeveldbStorage::Create( | 51 |
| 52 const FilePath& base_path, | 52 SettingsStorage* SettingsLeveldbStorage::Factory::Create( |
| 53 const std::string& extension_id) { | 53 const FilePath& base_path, const std::string& extension_id) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 55 FilePath path = base_path.AppendASCII(extension_id); | 55 FilePath path = base_path.AppendASCII(extension_id); |
| 56 | 56 |
| 57 #if defined(OS_POSIX) | 57 #if defined(OS_POSIX) |
| 58 std::string os_path(path.value()); | 58 std::string os_path(path.value()); |
| 59 #elif defined(OS_WIN) | 59 #elif defined(OS_WIN) |
| 60 std::string os_path = base::SysWideToUTF8(path.value()); | 60 std::string os_path = base::SysWideToUTF8(path.value()); |
| 61 #endif | 61 #endif |
| 62 | 62 |
| 63 leveldb::Options options; | 63 leveldb::Options options; |
| 64 options.create_if_missing = true; | 64 options.create_if_missing = true; |
| 65 leveldb::DB* db; | 65 leveldb::DB* db; |
| 66 leveldb::Status status = leveldb::DB::Open(options, os_path, &db); | 66 leveldb::Status status = leveldb::DB::Open(options, os_path, &db); |
| 67 if (!status.ok()) { | 67 if (!status.ok()) { |
| 68 LOG(WARNING) << "Failed to create leveldb at " << path.value() << | 68 LOG(WARNING) << "Failed to create leveldb at " << path.value() << |
| 69 ": " << status.ToString(); | 69 ": " << status.ToString(); |
| 70 return NULL; | 70 return NULL; |
| 71 } | 71 } |
| 72 return new SettingsLeveldbStorage(path, db); | 72 return new SettingsLeveldbStorage(path, db); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // SettingsLeveldbStorage |
| 76 |
| 75 SettingsLeveldbStorage::SettingsLeveldbStorage( | 77 SettingsLeveldbStorage::SettingsLeveldbStorage( |
| 76 const FilePath& db_path, leveldb::DB* db) | 78 const FilePath& db_path, leveldb::DB* db) |
| 77 : db_path_(db_path), db_(db) { | 79 : db_path_(db_path), db_(db) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 79 } | 81 } |
| 80 | 82 |
| 81 SettingsLeveldbStorage::~SettingsLeveldbStorage() { | 83 SettingsLeveldbStorage::~SettingsLeveldbStorage() { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 83 | 85 |
| 84 // Delete the database from disk if it's empty. This is safe on destruction, | 86 // Delete the database from disk if it's empty. This is safe on destruction, |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 it->SeekToFirst(); | 320 it->SeekToFirst(); |
| 319 bool is_empty = !it->Valid(); | 321 bool is_empty = !it->Valid(); |
| 320 if (!it->status().ok()) { | 322 if (!it->status().ok()) { |
| 321 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); | 323 LOG(ERROR) << "Checking DB emptiness failed: " << it->status().ToString(); |
| 322 return false; | 324 return false; |
| 323 } | 325 } |
| 324 return is_empty; | 326 return is_empty; |
| 325 } | 327 } |
| 326 | 328 |
| 327 } // namespace extensions | 329 } // namespace extensions |
| OLD | NEW |