| Index: chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_data.cc
|
| diff --git a/chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_data.cc b/chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_data.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c6b14a96bc657022d3e8ee3a48c243a7feacf2b1
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_data.cc
|
| @@ -0,0 +1,145 @@
|
| +// 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 "chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_data.h"
|
| +
|
| +#include <utility>
|
| +
|
| +#include "base/files/file_util.h"
|
| +#include "base/path_service.h"
|
| +#include "base/task_scheduler/post_task.h"
|
| +#include "base/threading/sequenced_worker_pool.h"
|
| +#include "chrome/browser/browser_process.h"
|
| +#include "chrome/browser/chromeos/app_mode/arc/arc_kiosk_app_manager.h"
|
| +#include "chrome/common/chrome_paths.h"
|
| +#include "components/prefs/pref_service.h"
|
| +#include "components/prefs/scoped_user_pref_update.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "ui/gfx/codec/png_codec.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +namespace {
|
| +// Keys for local state data.
|
| +constexpr char kKeyName[] = "name";
|
| +constexpr char kKeyIcon[] = "icon";
|
| +
|
| +constexpr char kIconFileExtension[] = ".png";
|
| +constexpr char kIconCacheDir[] = "arc-kiosk/icon";
|
| +
|
| +// Save |raw_icon| for given |app_id|.
|
| +void SaveIconToLocalOnBlockingPool(
|
| + const base::FilePath& icon_path,
|
| + scoped_refptr<base::RefCountedString> raw_icon) {
|
| + DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
|
| +
|
| + base::FilePath dir = icon_path.DirName();
|
| + if (!base::PathExists(dir))
|
| + CHECK(base::CreateDirectory(dir));
|
| +
|
| + CHECK_EQ(
|
| + static_cast<int>(raw_icon->size()),
|
| + base::WriteFile(icon_path, raw_icon->data().c_str(), raw_icon->size()));
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +ArcKioskAppData::ArcKioskAppData(const std::string& app_id,
|
| + const AccountId& account_id,
|
| + const std::string& name)
|
| + : app_id_(app_id), account_id_(account_id), name_(name) {}
|
| +
|
| +ArcKioskAppData::~ArcKioskAppData() = default;
|
| +
|
| +bool ArcKioskAppData::operator==(const std::string& app_id) const {
|
| + return this->app_id_ == app_id;
|
| +}
|
| +
|
| +bool ArcKioskAppData::LoadFromCache() {
|
| + const std::string app_key =
|
| + std::string(ArcKioskAppManager::kKeyApps) + '.' + app_id_;
|
| + const std::string name_key = app_key + '.' + kKeyName;
|
| + const std::string icon_path_key = app_key + '.' + kKeyIcon;
|
| +
|
| + PrefService* local_state = g_browser_process->local_state();
|
| + const base::DictionaryValue* dict =
|
| + local_state->GetDictionary(ArcKioskAppManager::kArcKioskDictionaryName);
|
| +
|
| + std::string icon_path_string;
|
| + if (!dict->GetString(name_key, &name_) ||
|
| + !dict->GetString(icon_path_key, &icon_path_string)) {
|
| + return false;
|
| + }
|
| + icon_path_ = base::FilePath(icon_path_string);
|
| +
|
| + // KioskAppIconLoader deletes itself when done.
|
| + (new KioskAppIconLoader(AsWeakPtr(), icon_path_))->Start();
|
| + return true;
|
| +}
|
| +
|
| +void ArcKioskAppData::SetCache(const std::string& name,
|
| + const gfx::ImageSkia& icon) {
|
| + DCHECK(!name.empty());
|
| + DCHECK(!icon.isNull());
|
| + name_ = name;
|
| + icon_ = icon;
|
| +
|
| + std::vector<unsigned char> image_data;
|
| + CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(*icon.bitmap(), false, &image_data));
|
| + scoped_refptr<base::RefCountedString> raw_icon(new base::RefCountedString);
|
| + raw_icon->data().assign(image_data.begin(), image_data.end());
|
| +
|
| + base::FilePath user_data_dir;
|
| + CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));
|
| + base::FilePath cache_dir = user_data_dir.AppendASCII(kIconCacheDir);
|
| + base::FilePath icon_path =
|
| + cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension);
|
| + content::BrowserThread::GetBlockingPool()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon));
|
| +
|
| + icon_path_ = icon_path;
|
| +
|
| + const std::string app_key =
|
| + std::string(ArcKioskAppManager::kKeyApps) + '.' + app_id_;
|
| + const std::string name_key = app_key + '.' + kKeyName;
|
| + const std::string icon_path_key = app_key + '.' + kKeyIcon;
|
| +
|
| + PrefService* local_state = g_browser_process->local_state();
|
| + DictionaryPrefUpdate dict_update(local_state,
|
| + ArcKioskAppManager::kArcKioskDictionaryName);
|
| + dict_update->SetString(name_key, name);
|
| + dict_update->SetString(icon_path_key, icon_path.value());
|
| +}
|
| +
|
| +void ArcKioskAppData::ClearCache() {
|
| + PrefService* local_state = g_browser_process->local_state();
|
| +
|
| + DictionaryPrefUpdate dict_update(local_state,
|
| + ArcKioskAppManager::kArcKioskDictionaryName);
|
| +
|
| + std::string app_key =
|
| + std::string(ArcKioskAppManager::kKeyApps) + '.' + app_id_;
|
| + dict_update->Remove(app_key, NULL);
|
| +
|
| + if (!icon_path_.empty()) {
|
| + base::PostTaskWithTraits(
|
| + FROM_HERE,
|
| + base::TaskTraits().MayBlock().WithPriority(
|
| + base::TaskPriority::BACKGROUND),
|
| + base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false));
|
| + }
|
| +}
|
| +
|
| +void ArcKioskAppData::OnIconLoadSuccess(const gfx::ImageSkia& icon) {
|
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
| + icon_ = icon;
|
| +}
|
| +
|
| +void ArcKioskAppData::OnIconLoadFailure() {
|
| + LOG(ERROR) << "Icon Load Failure";
|
| + // Do nothing
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|