| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/app_notification_storage.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/json/json_reader.h" | |
| 10 #include "base/json/json_writer.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/sys_string_conversions.h" | |
| 13 #include "base/values.h" | |
| 14 #include "base/version.h" | |
| 15 #include "chrome/common/extensions/extension.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "third_party/leveldatabase/src/include/leveldb/db.h" | |
| 18 | |
| 19 using base::JSONReader; | |
| 20 using base::JSONWriter; | |
| 21 using content::BrowserThread; | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 // A concrete implementation of the AppNotificationStorage interface, using | |
| 26 // LevelDb for backing storage. | |
| 27 class LevelDbAppNotificationStorage : public AppNotificationStorage { | |
| 28 public: | |
| 29 explicit LevelDbAppNotificationStorage(const base::FilePath& path); | |
| 30 virtual ~LevelDbAppNotificationStorage(); | |
| 31 | |
| 32 // Implementing the AppNotificationStorage interface. | |
| 33 virtual bool GetExtensionIds(std::set<std::string>* result) OVERRIDE; | |
| 34 virtual bool Get(const std::string& extension_id, | |
| 35 AppNotificationList* result) OVERRIDE; | |
| 36 virtual bool Set(const std::string& extension_id, | |
| 37 const AppNotificationList& list) OVERRIDE; | |
| 38 virtual bool Delete(const std::string& extension_id) OVERRIDE; | |
| 39 | |
| 40 private: | |
| 41 // If |db_| is NULL, attempt to open it. You should pass true for | |
| 42 // |create_if_missing| if you need to write data into the database and want to | |
| 43 // ensure it has been created after this call. | |
| 44 bool OpenDbIfNeeded(bool create_if_missing); | |
| 45 | |
| 46 // The path where the database will reside. | |
| 47 base::FilePath path_; | |
| 48 | |
| 49 // This should be used for all read operations on the db. | |
| 50 leveldb::ReadOptions read_options_; | |
| 51 | |
| 52 // The leveldb database object - might be NULL if there was nothing found on | |
| 53 // disk at |path_| and OpenDbIfNeeded hasn't been called with | |
| 54 // create_if_missing=true yet. | |
| 55 scoped_ptr<leveldb::DB> db_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(LevelDbAppNotificationStorage); | |
| 58 }; | |
| 59 | |
| 60 // static | |
| 61 AppNotificationStorage* AppNotificationStorage::Create( | |
| 62 const base::FilePath& path) { | |
| 63 return new LevelDbAppNotificationStorage(path); | |
| 64 } | |
| 65 | |
| 66 AppNotificationStorage::~AppNotificationStorage() {} | |
| 67 | |
| 68 namespace { | |
| 69 | |
| 70 void AppNotificationListToJSON(const AppNotificationList& list, | |
| 71 std::string* result) { | |
| 72 ListValue list_value; | |
| 73 AppNotificationList::const_iterator i; | |
| 74 for (i = list.begin(); i != list.end(); ++i) { | |
| 75 DictionaryValue* dictionary = new DictionaryValue(); | |
| 76 (*i)->ToDictionaryValue(dictionary); | |
| 77 list_value.Append(dictionary); | |
| 78 } | |
| 79 JSONWriter::Write(&list_value, result); | |
| 80 } | |
| 81 | |
| 82 bool JSONToAppNotificationList(const std::string& json, | |
| 83 AppNotificationList* list) { | |
| 84 CHECK(list); | |
| 85 scoped_ptr<Value> value(JSONReader::Read(json)); | |
| 86 if (!value.get() || value->GetType() != Value::TYPE_LIST) | |
| 87 return false; | |
| 88 | |
| 89 ListValue* list_value = static_cast<ListValue*>(value.get()); | |
| 90 for (size_t i = 0; i < list_value->GetSize(); i++) { | |
| 91 Value* item = NULL; | |
| 92 if (!list_value->Get(i, &item) || !item || | |
| 93 item->GetType() != Value::TYPE_DICTIONARY) | |
| 94 return false; | |
| 95 DictionaryValue* dictionary = static_cast<DictionaryValue*>(item); | |
| 96 AppNotification* notification = | |
| 97 AppNotification::FromDictionaryValue(*dictionary); | |
| 98 if (!notification) | |
| 99 return false; | |
| 100 list->push_back(linked_ptr<AppNotification>(notification)); | |
| 101 } | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 void LogLevelDbError(const tracked_objects::Location& location, | |
| 106 const leveldb::Status& status) { | |
| 107 LOG(ERROR) << "AppNotificationStorage database error at " | |
| 108 << location.ToString() << " status:" << status.ToString(); | |
| 109 } | |
| 110 | |
| 111 } // namespace | |
| 112 | |
| 113 | |
| 114 LevelDbAppNotificationStorage::LevelDbAppNotificationStorage( | |
| 115 const base::FilePath& path) : path_(path) { | |
| 116 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 117 read_options_.verify_checksums = true; | |
| 118 } | |
| 119 | |
| 120 LevelDbAppNotificationStorage::~LevelDbAppNotificationStorage() { | |
| 121 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 122 } | |
| 123 | |
| 124 bool LevelDbAppNotificationStorage::GetExtensionIds( | |
| 125 std::set<std::string>* result) { | |
| 126 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 127 CHECK(result); | |
| 128 if (!OpenDbIfNeeded(false)) | |
| 129 return false; | |
| 130 if (!db_.get()) | |
| 131 return true; | |
| 132 | |
| 133 scoped_ptr<leveldb::Iterator> iter(db_->NewIterator(read_options_)); | |
| 134 for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { | |
| 135 std::string key = iter->key().ToString(); | |
| 136 if (Extension::IdIsValid(key)) | |
| 137 result->insert(key); | |
| 138 } | |
| 139 | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 bool LevelDbAppNotificationStorage::Get(const std::string& extension_id, | |
| 144 AppNotificationList* result) { | |
| 145 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 146 CHECK(result); | |
| 147 if (!OpenDbIfNeeded(false)) | |
| 148 return false; | |
| 149 if (!db_.get()) | |
| 150 return true; | |
| 151 | |
| 152 std::string json; | |
| 153 leveldb::Status status = db_->Get(read_options_, extension_id, &json); | |
| 154 if (status.IsNotFound()) { | |
| 155 return true; | |
| 156 } else if (!status.ok()) { | |
| 157 LogLevelDbError(FROM_HERE, status); | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 return JSONToAppNotificationList(json, result); | |
| 162 } | |
| 163 | |
| 164 bool LevelDbAppNotificationStorage::Set(const std::string& extension_id, | |
| 165 const AppNotificationList& list) { | |
| 166 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 167 if (!OpenDbIfNeeded(true)) | |
| 168 return false; | |
| 169 CHECK(db_.get()); | |
| 170 | |
| 171 std::string json; | |
| 172 AppNotificationListToJSON(list, &json); | |
| 173 leveldb::Status status = db_->Put(leveldb::WriteOptions(), | |
| 174 extension_id, | |
| 175 json); | |
| 176 if (!status.ok()) { | |
| 177 LogLevelDbError(FROM_HERE, status); | |
| 178 return false; | |
| 179 } | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 bool LevelDbAppNotificationStorage::Delete(const std::string& extension_id) { | |
| 184 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 185 | |
| 186 if (!OpenDbIfNeeded(false)) | |
| 187 return false; | |
| 188 | |
| 189 if (!db_.get()) | |
| 190 return true; // The database doesn't exist on disk. | |
| 191 | |
| 192 // Leveldb does not consider it an error if the key to delete isn't present, | |
| 193 // so we don't bother checking that first. | |
| 194 leveldb::Status status = db_->Delete(leveldb::WriteOptions(), extension_id); | |
| 195 | |
| 196 if (!status.ok()) { | |
| 197 LogLevelDbError(FROM_HERE, status); | |
| 198 return false; | |
| 199 } | |
| 200 return true; | |
| 201 } | |
| 202 | |
| 203 bool LevelDbAppNotificationStorage::OpenDbIfNeeded(bool create_if_missing) { | |
| 204 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 205 | |
| 206 if (db_.get()) | |
| 207 return true; | |
| 208 | |
| 209 // If the file doesn't exist and the caller doesn't want it created, just | |
| 210 // return early. | |
| 211 if (!create_if_missing && !file_util::PathExists(path_)) | |
| 212 return true; | |
| 213 | |
| 214 #if defined(OS_POSIX) | |
| 215 std::string os_path = path_.value(); | |
| 216 #elif defined(OS_WIN) | |
| 217 std::string os_path = base::SysWideToUTF8(path_.value()); | |
| 218 #endif | |
| 219 | |
| 220 leveldb::Options options; | |
| 221 options.create_if_missing = true; | |
| 222 leveldb::DB* db = NULL; | |
| 223 leveldb::Status status = leveldb::DB::Open(options, os_path, &db); | |
| 224 if (!status.ok()) { | |
| 225 LogLevelDbError(FROM_HERE, status); | |
| 226 return false; | |
| 227 } | |
| 228 db_.reset(db); | |
| 229 return true; | |
| 230 } | |
| 231 | |
| 232 } // namespace extensions | |
| OLD | NEW |