Chromium Code Reviews| Index: components/wallpaper/pref_based_wallpaper_color_cache.cc |
| diff --git a/components/wallpaper/pref_based_wallpaper_color_cache.cc b/components/wallpaper/pref_based_wallpaper_color_cache.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4aad98564df080e8b411eb62ca42020026d4395b |
| --- /dev/null |
| +++ b/components/wallpaper/pref_based_wallpaper_color_cache.cc |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/wallpaper/pref_based_wallpaper_color_cache.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/values.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/prefs/scoped_user_pref_update.h" |
| +#include "ui/gfx/color_utils.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace wallpaper { |
| + |
| +const char kWallpaperCache[] = "wallpaper_cache"; |
| +const char kWallpaperImagesNodeKey[] = "images"; |
| + |
| +namespace { |
| + |
| +const char kColorNodeKey[] = "color"; |
| +const char kLastAccessTimeNodeKey[] = "last_access_time"; |
| + |
| +void UpdateAccessTime(base::DictionaryValue* images_dict, uint32_t image_id) { |
| + const std::string key = |
| + base::StringPrintf("%u.%s", image_id, kLastAccessTimeNodeKey); |
| + images_dict->SetString( |
| + key, base::Int64ToString(base::Time::Now().ToInternalValue())); |
| +} |
| + |
| +void UpdateColor(base::DictionaryValue* images_dict, |
| + uint32_t image_id, |
| + SkColor color) { |
| + const std::string key = base::StringPrintf("%u.%s", image_id, kColorNodeKey); |
| + images_dict->SetString(key, base::UintToString(color)); |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +void PrefBasedWallpaperColorCache::RegisterPrefs(PrefRegistrySimple* registry) { |
| + registry->RegisterDictionaryPref(kWallpaperCache); |
| +} |
| + |
| +PrefBasedWallpaperColorCache::PrefBasedWallpaperColorCache( |
| + PrefService* pref_service) |
| + : pref_service_(pref_service) { |
| + CHECK(pref_service_); |
| +} |
| + |
| +PrefBasedWallpaperColorCache::~PrefBasedWallpaperColorCache() {} |
| + |
| +bool PrefBasedWallpaperColorCache::GetColor(uint32_t image_id, |
| + SkColor* color) const { |
| + const base::DictionaryValue* info_dict = |
| + pref_service_->GetDictionary(kWallpaperCache); |
| + LOG(ERROR) << *info_dict; |
| + |
| + const std::string color_key = base::StringPrintf( |
| + "%s.%u.%s", kWallpaperImagesNodeKey, image_id, kColorNodeKey); |
|
jonross
2017/03/28 14:33:28
This key seems more complex than is required.
Wou
|
| + |
| + // TODO(bruthig): Add UMA stat for hits/misses. |
| + std::string color_str; |
| + if (info_dict->GetString(color_key, &color_str)) { |
|
jonross
2017/03/28 14:33:28
You can store / extract integers directly as keys
|
| + CHECK(base::StringToUint(color_str, color)); |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +void PrefBasedWallpaperColorCache::SetColor(uint32_t image_id, SkColor color) { |
| + DictionaryPrefUpdate wallpaper_update(pref_service_, kWallpaperCache); |
| + base::DictionaryValue* info_dict = wallpaper_update.Get(); |
| + if (!info_dict->HasKey(kWallpaperImagesNodeKey)) { |
| + info_dict->Set(kWallpaperImagesNodeKey, |
| + base::MakeUnique<base::DictionaryValue>()); |
| + } |
| + base::DictionaryValue* images_dict = nullptr; |
| + info_dict->GetDictionary(kWallpaperImagesNodeKey, &images_dict); |
| + UpdateColor(images_dict, image_id, color); |
| + UpdateAccessTime(images_dict, image_id); |
| + |
| + PurgeOldRecords(); |
| + |
| + LOG(ERROR) << *info_dict; |
| +} |
| + |
| +void PrefBasedWallpaperColorCache::PurgeOldRecords() { |
| + // TODO(bruthig): Implement me! |
| +} |
| + |
| +} // namespace wallpaper |