OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "extensions/browser/api/lock_screen_data/lock_screen_item_storage.h" |
| 6 |
| 7 #include <set> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/guid.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/values.h" |
| 14 #include "components/prefs/pref_registry_simple.h" |
| 15 #include "components/prefs/pref_service.h" |
| 16 #include "components/prefs/scoped_user_pref_update.h" |
| 17 #include "extensions/browser/api/lock_screen_data/data_item.h" |
| 18 #include "extensions/browser/api/lock_screen_data/operation_result.h" |
| 19 #include "extensions/browser/api/storage/backend_task_runner.h" |
| 20 #include "extensions/browser/api/storage/local_value_store_cache.h" |
| 21 #include "extensions/browser/event_router.h" |
| 22 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/browser/extensions_browser_client.h" |
| 24 #include "extensions/browser/value_store/value_store.h" |
| 25 #include "extensions/browser/value_store/value_store_factory_impl.h" |
| 26 #include "extensions/common/api/lock_screen_data.h" |
| 27 |
| 28 namespace extensions { |
| 29 |
| 30 namespace lock_screen_data { |
| 31 |
| 32 namespace { |
| 33 |
| 34 const char kLockScreenDataPrefKey[] = "lockScreenDataItems"; |
| 35 |
| 36 LockScreenItemStorage* g_data_item_storage = nullptr; |
| 37 |
| 38 LockScreenItemStorage::ItemFactoryCallback* g_test_item_factory_callback = |
| 39 nullptr; |
| 40 LockScreenItemStorage::RegisteredItemsGetter* |
| 41 g_test_registered_items_getter_callback = nullptr; |
| 42 LockScreenItemStorage::ItemStoreDeleter* g_test_delete_all_items_callback = |
| 43 nullptr; |
| 44 |
| 45 std::unique_ptr<DataItem> CreateDataItem(const std::string& item_id, |
| 46 const std::string& extension_id, |
| 47 content::BrowserContext* context, |
| 48 ValueStoreCache* value_store_cache, |
| 49 base::SequencedTaskRunner* task_runner, |
| 50 const std::string& crypto_key) { |
| 51 return g_test_item_factory_callback |
| 52 ? g_test_item_factory_callback->Run(item_id, extension_id, |
| 53 crypto_key) |
| 54 : base::MakeUnique<DataItem>(item_id, extension_id, context, |
| 55 value_store_cache, task_runner, |
| 56 crypto_key); |
| 57 } |
| 58 |
| 59 void GetRegisteredItems(const std::string& extension_id, |
| 60 content::BrowserContext* context, |
| 61 ValueStoreCache* value_store_cache, |
| 62 base::SequencedTaskRunner* task_runner, |
| 63 const DataItem::RegisteredValuesCallback& callback) { |
| 64 if (g_test_registered_items_getter_callback) { |
| 65 g_test_registered_items_getter_callback->Run(extension_id, callback); |
| 66 return; |
| 67 } |
| 68 DataItem::GetRegisteredValuesForExtension( |
| 69 context, value_store_cache, task_runner, extension_id, callback); |
| 70 } |
| 71 |
| 72 void DeleteAllItems(const std::string& extension_id, |
| 73 content::BrowserContext* context, |
| 74 ValueStoreCache* value_store_cache, |
| 75 base::SequencedTaskRunner* task_runner, |
| 76 const base::Closure& callback) { |
| 77 if (g_test_delete_all_items_callback) { |
| 78 g_test_delete_all_items_callback->Run(extension_id, callback); |
| 79 return; |
| 80 } |
| 81 DataItem::DeleteAllItemsForExtension(context, value_store_cache, task_runner, |
| 82 extension_id, callback); |
| 83 } |
| 84 |
| 85 void ReleaseValueStoreCache(std::unique_ptr<LocalValueStoreCache> cache) { |
| 86 // Nothing to do. Used only to defer |cache| destruction to the FILE thread. |
| 87 } |
| 88 |
| 89 } // namespace |
| 90 |
| 91 // static |
| 92 LockScreenItemStorage* LockScreenItemStorage::GetIfAllowed( |
| 93 content::BrowserContext* context) { |
| 94 if (g_data_item_storage && !g_data_item_storage->IsContextAllowed(context)) |
| 95 return nullptr; |
| 96 return g_data_item_storage; |
| 97 } |
| 98 |
| 99 // static |
| 100 void LockScreenItemStorage::RegisterLocalState(PrefRegistrySimple* registry) { |
| 101 registry->RegisterDictionaryPref(kLockScreenDataPrefKey); |
| 102 } |
| 103 |
| 104 LockScreenItemStorage::LockScreenItemStorage(content::BrowserContext* context, |
| 105 PrefService* local_state, |
| 106 const std::string& crypto_key, |
| 107 const base::FilePath& storage_root) |
| 108 : context_(context), |
| 109 user_id_( |
| 110 ExtensionsBrowserClient::Get()->GetUserIdHashFromContext(context)), |
| 111 crypto_key_(crypto_key), |
| 112 local_state_(local_state), |
| 113 storage_root_(storage_root.Append(user_id_)), |
| 114 extension_registry_observer_(this), |
| 115 value_store_cache_(base::MakeUnique<LocalValueStoreCache>( |
| 116 new ValueStoreFactoryImpl(storage_root))), |
| 117 weak_ptr_factory_(this) { |
| 118 CHECK(!user_id_.empty()); |
| 119 extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
| 120 task_runner_ = GetBackendTaskRunner(); |
| 121 |
| 122 DCHECK(!g_data_item_storage); |
| 123 g_data_item_storage = this; |
| 124 |
| 125 ClearUninstalledAppData(); |
| 126 } |
| 127 |
| 128 LockScreenItemStorage::~LockScreenItemStorage() { |
| 129 data_item_cache_.clear(); |
| 130 |
| 131 task_runner_->PostTask( |
| 132 FROM_HERE, base::Bind(&ReleaseValueStoreCache, |
| 133 base::Passed(std::move(value_store_cache_)))); |
| 134 |
| 135 DCHECK_EQ(g_data_item_storage, this); |
| 136 g_data_item_storage = nullptr; |
| 137 } |
| 138 |
| 139 // static |
| 140 void LockScreenItemStorage::SetItemProvidersForTesting( |
| 141 RegisteredItemsGetter* items_getter_callback, |
| 142 ItemFactoryCallback* factory_callback, |
| 143 ItemStoreDeleter* deleter_callback) { |
| 144 g_test_registered_items_getter_callback = items_getter_callback; |
| 145 g_test_item_factory_callback = factory_callback; |
| 146 g_test_delete_all_items_callback = deleter_callback; |
| 147 } |
| 148 |
| 149 void LockScreenItemStorage::SetSessionLocked(bool session_locked) { |
| 150 SessionLockedState new_state = session_locked |
| 151 ? SessionLockedState::kLocked |
| 152 : SessionLockedState::kNotLocked; |
| 153 if (new_state == session_locked_state_) |
| 154 return; |
| 155 |
| 156 bool was_locked = session_locked_state_ == SessionLockedState::kLocked; |
| 157 session_locked_state_ = new_state; |
| 158 |
| 159 if (session_locked_state_ != SessionLockedState::kNotLocked) |
| 160 return; |
| 161 |
| 162 EventRouter* event_router = EventRouter::Get(context_); |
| 163 std::set<std::string> extensions = GetExtensionsWithDataItems(false); |
| 164 for (const auto& id : extensions) { |
| 165 // If the session state is unlocked, dispatch Item availability events to |
| 166 // apps with available data items. |
| 167 api::lock_screen_data::DataItemsAvailableEvent event_args; |
| 168 event_args.was_locked = was_locked; |
| 169 |
| 170 std::unique_ptr<Event> event = base::MakeUnique<Event>( |
| 171 events::LOCK_SCREEN_DATA_ON_DATA_ITEMS_AVAILABLE, |
| 172 api::lock_screen_data::OnDataItemsAvailable::kEventName, |
| 173 api::lock_screen_data::OnDataItemsAvailable::Create(event_args)); |
| 174 event_router->DispatchEventToExtension(id, std::move(event)); |
| 175 } |
| 176 } |
| 177 |
| 178 void LockScreenItemStorage::CreateItem(const std::string& extension_id, |
| 179 const CreateCallback& callback) { |
| 180 EnsureCacheForExtensionLoaded( |
| 181 extension_id, |
| 182 base::Bind(&LockScreenItemStorage::CreateItemImpl, |
| 183 weak_ptr_factory_.GetWeakPtr(), extension_id, callback)); |
| 184 } |
| 185 |
| 186 void LockScreenItemStorage::GetAllForExtension( |
| 187 const std::string& extension_id, |
| 188 const DataItemListCallback& callback) { |
| 189 EnsureCacheForExtensionLoaded( |
| 190 extension_id, |
| 191 base::Bind(&LockScreenItemStorage::GetAllForExtensionImpl, |
| 192 weak_ptr_factory_.GetWeakPtr(), extension_id, callback)); |
| 193 } |
| 194 |
| 195 void LockScreenItemStorage::SetItemContent( |
| 196 const std::string& extension_id, |
| 197 const std::string& item_id, |
| 198 const std::vector<char>& data, |
| 199 const LockScreenItemStorage::WriteCallback& callback) { |
| 200 EnsureCacheForExtensionLoaded( |
| 201 extension_id, base::Bind(&LockScreenItemStorage::SetItemContentImpl, |
| 202 weak_ptr_factory_.GetWeakPtr(), extension_id, |
| 203 item_id, data, callback)); |
| 204 } |
| 205 |
| 206 void LockScreenItemStorage::GetItemContent( |
| 207 const std::string& extension_id, |
| 208 const std::string& item_id, |
| 209 const LockScreenItemStorage::ReadCallback& callback) { |
| 210 EnsureCacheForExtensionLoaded( |
| 211 extension_id, base::Bind(&LockScreenItemStorage::GetItemContentImpl, |
| 212 weak_ptr_factory_.GetWeakPtr(), extension_id, |
| 213 item_id, callback)); |
| 214 } |
| 215 |
| 216 void LockScreenItemStorage::DeleteItem(const std::string& extension_id, |
| 217 const std::string& item_id, |
| 218 const WriteCallback& callback) { |
| 219 EnsureCacheForExtensionLoaded( |
| 220 extension_id, base::Bind(&LockScreenItemStorage::DeleteItemImpl, |
| 221 weak_ptr_factory_.GetWeakPtr(), extension_id, |
| 222 item_id, callback)); |
| 223 } |
| 224 |
| 225 void LockScreenItemStorage::OnExtensionUninstalled( |
| 226 content::BrowserContext* browser_context, |
| 227 const Extension* extension, |
| 228 UninstallReason reason) { |
| 229 ClearExtensionData(extension->id()); |
| 230 } |
| 231 |
| 232 LockScreenItemStorage::CachedExtensionData::CachedExtensionData() = default; |
| 233 |
| 234 LockScreenItemStorage::CachedExtensionData::~CachedExtensionData() = default; |
| 235 |
| 236 bool LockScreenItemStorage::IsContextAllowed(content::BrowserContext* context) { |
| 237 switch (session_locked_state_) { |
| 238 case SessionLockedState::kUnknown: |
| 239 return false; |
| 240 case SessionLockedState::kLocked: |
| 241 return ExtensionsBrowserClient::Get()->IsLockScreenContext(context); |
| 242 case SessionLockedState::kNotLocked: |
| 243 return context_ == context; |
| 244 } |
| 245 NOTREACHED() << "Unknown session locked state"; |
| 246 return false; |
| 247 } |
| 248 |
| 249 void LockScreenItemStorage::CreateItemImpl(const std::string& extension_id, |
| 250 const CreateCallback& callback) { |
| 251 std::unique_ptr<DataItem> item = |
| 252 CreateDataItem(base::GenerateGUID(), extension_id, context_, |
| 253 value_store_cache_.get(), task_runner_.get(), crypto_key_); |
| 254 DataItem* item_ptr = item.get(); |
| 255 item_ptr->Register(base::Bind( |
| 256 &LockScreenItemStorage::OnItemRegistered, weak_ptr_factory_.GetWeakPtr(), |
| 257 base::Passed(std::move(item)), extension_id, callback)); |
| 258 } |
| 259 |
| 260 void LockScreenItemStorage::GetAllForExtensionImpl( |
| 261 const std::string& extension_id, |
| 262 const DataItemListCallback& callback) { |
| 263 std::vector<const DataItem*> items; |
| 264 ExtensionDataMap::iterator extension_data = |
| 265 data_item_cache_.find(extension_id); |
| 266 if (extension_data == data_item_cache_.end()) { |
| 267 callback.Run(items); |
| 268 return; |
| 269 } |
| 270 |
| 271 for (const auto& item : extension_data->second.data_items) { |
| 272 if (!item.second) |
| 273 continue; |
| 274 items.push_back(item.second.get()); |
| 275 } |
| 276 |
| 277 callback.Run(items); |
| 278 } |
| 279 |
| 280 void LockScreenItemStorage::SetItemContentImpl( |
| 281 const std::string& extension_id, |
| 282 const std::string& item_id, |
| 283 const std::vector<char>& data, |
| 284 const LockScreenItemStorage::WriteCallback& callback) { |
| 285 DataItem* item = FindItem(extension_id, item_id); |
| 286 if (!item) { |
| 287 callback.Run(OperationResult::kNotFound); |
| 288 return; |
| 289 } |
| 290 |
| 291 item->Write(data, callback); |
| 292 } |
| 293 |
| 294 void LockScreenItemStorage::GetItemContentImpl(const std::string& extension_id, |
| 295 const std::string& item_id, |
| 296 const ReadCallback& callback) { |
| 297 DataItem* item = FindItem(extension_id, item_id); |
| 298 if (!item) { |
| 299 callback.Run(OperationResult::kNotFound, nullptr); |
| 300 return; |
| 301 } |
| 302 |
| 303 item->Read(callback); |
| 304 } |
| 305 |
| 306 void LockScreenItemStorage::DeleteItemImpl(const std::string& extension_id, |
| 307 const std::string& item_id, |
| 308 const WriteCallback& callback) { |
| 309 DataItem* item = FindItem(extension_id, item_id); |
| 310 if (!item) { |
| 311 callback.Run(OperationResult::kNotFound); |
| 312 return; |
| 313 } |
| 314 |
| 315 item->Delete(base::Bind(&LockScreenItemStorage::OnItemDeleted, |
| 316 base::Unretained(this), extension_id, item_id, |
| 317 callback)); |
| 318 } |
| 319 |
| 320 void LockScreenItemStorage::OnItemRegistered(std::unique_ptr<DataItem> item, |
| 321 const std::string& extension_id, |
| 322 const CreateCallback& callback, |
| 323 OperationResult result) { |
| 324 if (result != OperationResult::kSuccess) { |
| 325 callback.Run(result, nullptr); |
| 326 return; |
| 327 } |
| 328 |
| 329 DataItem* item_ptr = item.get(); |
| 330 data_item_cache_[extension_id].data_items.emplace(item_ptr->id(), |
| 331 std::move(item)); |
| 332 |
| 333 { |
| 334 DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
| 335 update->SetInteger(user_id_ + "." + extension_id, |
| 336 data_item_cache_[extension_id].data_items.size()); |
| 337 } |
| 338 |
| 339 callback.Run(OperationResult::kSuccess, item_ptr); |
| 340 } |
| 341 |
| 342 void LockScreenItemStorage::OnItemDeleted(const std::string& extension_id, |
| 343 const std::string& item_id, |
| 344 const WriteCallback& callback, |
| 345 OperationResult result) { |
| 346 data_item_cache_[extension_id].data_items.erase(item_id); |
| 347 { |
| 348 DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
| 349 update->SetInteger(user_id_ + "." + extension_id, |
| 350 data_item_cache_[extension_id].data_items.size()); |
| 351 } |
| 352 |
| 353 callback.Run(result); |
| 354 } |
| 355 |
| 356 void LockScreenItemStorage::EnsureCacheForExtensionLoaded( |
| 357 const std::string& extension_id, |
| 358 const base::Closure& callback) { |
| 359 CachedExtensionData* data = &data_item_cache_[extension_id]; |
| 360 if (data->state == CachedExtensionData::State::kLoaded) { |
| 361 callback.Run(); |
| 362 return; |
| 363 } |
| 364 |
| 365 data->load_callbacks.push_back(callback); |
| 366 |
| 367 if (data->state == CachedExtensionData::State::kLoading) |
| 368 return; |
| 369 |
| 370 data->state = CachedExtensionData::State::kLoading; |
| 371 |
| 372 GetRegisteredItems(extension_id, context_, value_store_cache_.get(), |
| 373 task_runner_.get(), |
| 374 base::Bind(&LockScreenItemStorage::OnGotExtensionItems, |
| 375 weak_ptr_factory_.GetWeakPtr(), extension_id)); |
| 376 } |
| 377 |
| 378 void LockScreenItemStorage::OnGotExtensionItems( |
| 379 const std::string& extension_id, |
| 380 OperationResult result, |
| 381 std::unique_ptr<base::DictionaryValue> items) { |
| 382 ExtensionDataMap::iterator data = data_item_cache_.find(extension_id); |
| 383 if (data == data_item_cache_.end() || |
| 384 data->second.state != CachedExtensionData::State::kLoading) { |
| 385 return; |
| 386 } |
| 387 |
| 388 if (result == OperationResult::kSuccess) { |
| 389 for (base::DictionaryValue::Iterator item_iter(*items); |
| 390 !item_iter.IsAtEnd(); item_iter.Advance()) { |
| 391 std::unique_ptr<DataItem> item = CreateDataItem( |
| 392 item_iter.key(), extension_id, context_, value_store_cache_.get(), |
| 393 task_runner_.get(), crypto_key_); |
| 394 data->second.data_items.emplace(item_iter.key(), std::move(item)); |
| 395 } |
| 396 } |
| 397 |
| 398 DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
| 399 update->SetInteger(user_id_ + "." + extension_id, |
| 400 data->second.data_items.size()); |
| 401 |
| 402 data->second.state = CachedExtensionData::State::kLoaded; |
| 403 |
| 404 std::vector<base::Closure> callbacks; |
| 405 callbacks.swap(data->second.load_callbacks); |
| 406 for (auto& callback : callbacks) |
| 407 callback.Run(); |
| 408 } |
| 409 |
| 410 DataItem* LockScreenItemStorage::FindItem(const std::string& extension_id, |
| 411 const std::string& item_id) { |
| 412 ExtensionDataMap::iterator extension_data = |
| 413 data_item_cache_.find(extension_id); |
| 414 if (extension_data == data_item_cache_.end()) |
| 415 return nullptr; |
| 416 |
| 417 if (extension_data->second.state != CachedExtensionData::State::kLoaded) |
| 418 return nullptr; |
| 419 DataItemMap::iterator item_it = |
| 420 extension_data->second.data_items.find(item_id); |
| 421 if (item_it == extension_data->second.data_items.end()) |
| 422 return nullptr; |
| 423 |
| 424 if (!item_it->second) |
| 425 return nullptr; |
| 426 return item_it->second.get(); |
| 427 } |
| 428 |
| 429 std::set<std::string> LockScreenItemStorage::GetExtensionsWithDataItems( |
| 430 bool include_empty) { |
| 431 std::set<std::string> result; |
| 432 |
| 433 const base::DictionaryValue* user_data = nullptr; |
| 434 const base::DictionaryValue* items = |
| 435 local_state_->GetDictionary(kLockScreenDataPrefKey); |
| 436 if (!items || !items->GetDictionary(user_id_, &user_data) || !user_data) |
| 437 return result; |
| 438 |
| 439 for (base::DictionaryValue::Iterator extension_iter(*user_data); |
| 440 !extension_iter.IsAtEnd(); extension_iter.Advance()) { |
| 441 if (extension_iter.value().is_int() && |
| 442 (include_empty || extension_iter.value().GetInt() > 0)) { |
| 443 result.insert(extension_iter.key()); |
| 444 } |
| 445 } |
| 446 return result; |
| 447 } |
| 448 |
| 449 void LockScreenItemStorage::ClearUninstalledAppData() { |
| 450 std::set<std::string> extensions = |
| 451 GetExtensionsWithDataItems(true /* include_empty */); |
| 452 for (const auto& id : extensions) { |
| 453 if (!ExtensionRegistry::Get(context_)->GetInstalledExtension(id)) |
| 454 ClearExtensionData(id); |
| 455 } |
| 456 } |
| 457 |
| 458 void LockScreenItemStorage::ClearExtensionData(const std::string& id) { |
| 459 DeleteAllItems( |
| 460 id, context_, value_store_cache_.get(), task_runner_.get(), |
| 461 base::Bind(&LockScreenItemStorage::RemoveExtensionFromLocalState, |
| 462 weak_ptr_factory_.GetWeakPtr(), id)); |
| 463 } |
| 464 |
| 465 void LockScreenItemStorage::RemoveExtensionFromLocalState( |
| 466 const std::string& id) { |
| 467 { |
| 468 DictionaryPrefUpdate update(local_state_, kLockScreenDataPrefKey); |
| 469 update->Remove(user_id_ + "." + id, nullptr); |
| 470 } |
| 471 |
| 472 ExtensionDataMap::iterator it = data_item_cache_.find(id); |
| 473 if (it == data_item_cache_.end()) |
| 474 return; |
| 475 |
| 476 std::vector<base::Closure> callbacks; |
| 477 callbacks.swap(it->second.load_callbacks); |
| 478 |
| 479 data_item_cache_.erase(it); |
| 480 |
| 481 for (auto& callback : callbacks) |
| 482 callback.Run(); |
| 483 } |
| 484 |
| 485 } // namespace lock_screen_data |
| 486 } // namespace extensions |
OLD | NEW |