| 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 "chrome/browser/chromeos/app_mode/kiosk_app_data_base.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/task_scheduler/post_task.h" | |
| 12 #include "base/threading/sequenced_worker_pool.h" | |
| 13 #include "chrome/browser/browser_process.h" | |
| 14 #include "components/prefs/pref_service.h" | |
| 15 #include "components/prefs/scoped_user_pref_update.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "ui/gfx/codec/png_codec.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Keys for local state data. | |
| 26 constexpr char kKeyName[] = "name"; | |
| 27 constexpr char kKeyIcon[] = "icon"; | |
| 28 | |
| 29 // Icon file extension. | |
| 30 constexpr char kIconFileExtension[] = ".png"; | |
| 31 | |
| 32 // Save |raw_icon| for given |app_id|. | |
| 33 void SaveIconToLocalOnBlockingPool(const base::FilePath& icon_path, | |
| 34 std::vector<unsigned char> image_data) { | |
| 35 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 36 | |
| 37 const base::FilePath dir = icon_path.DirName(); | |
| 38 if (!base::PathExists(dir) && !base::CreateDirectory(dir)) { | |
| 39 LOG(ERROR) << "Failed to create directory to store kiosk icons"; | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 const int wrote = base::WriteFile( | |
| 44 icon_path, reinterpret_cast<char*>(image_data.data()), image_data.size()); | |
| 45 if (wrote != static_cast<int>(image_data.size())) { | |
| 46 LOG(ERROR) << "Failed to write kiosk icon file"; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 // static | |
| 53 const char KioskAppDataBase::kKeyApps[] = "apps"; | |
| 54 | |
| 55 KioskAppDataBase::KioskAppDataBase(const std::string& dictionary_name, | |
| 56 const std::string& app_id, | |
| 57 const AccountId& account_id) | |
| 58 : dictionary_name_(dictionary_name), | |
| 59 app_id_(app_id), | |
| 60 account_id_(account_id) {} | |
| 61 | |
| 62 KioskAppDataBase::~KioskAppDataBase() = default; | |
| 63 | |
| 64 void KioskAppDataBase::SaveToDictionary(DictionaryPrefUpdate& dict_update) { | |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 66 const std::string app_key = std::string(kKeyApps) + '.' + app_id_; | |
| 67 const std::string name_key = app_key + '.' + kKeyName; | |
| 68 const std::string icon_path_key = app_key + '.' + kKeyIcon; | |
| 69 | |
| 70 dict_update->SetString(name_key, name_); | |
| 71 dict_update->SetString(icon_path_key, icon_path_.value()); | |
| 72 } | |
| 73 | |
| 74 bool KioskAppDataBase::LoadFromDictionary(const base::DictionaryValue& dict) { | |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 76 const std::string app_key = | |
| 77 std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_; | |
| 78 const std::string name_key = app_key + '.' + kKeyName; | |
| 79 const std::string icon_path_key = app_key + '.' + kKeyIcon; | |
| 80 | |
| 81 std::string icon_path_string; | |
| 82 if (!dict.GetString(name_key, &name_) || | |
| 83 !dict.GetString(icon_path_key, &icon_path_string)) { | |
| 84 return false; | |
| 85 } | |
| 86 icon_path_ = base::FilePath(icon_path_string); | |
| 87 | |
| 88 kiosk_app_icon_loader_ = base::MakeUnique<KioskAppIconLoader>(this); | |
| 89 kiosk_app_icon_loader_->Start(icon_path_); | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 void KioskAppDataBase::SaveIcon(const SkBitmap& icon, | |
| 94 const base::FilePath& cache_dir) { | |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 96 std::vector<unsigned char> image_data; | |
| 97 if (!gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)) { | |
| 98 LOG(ERROR) << "Failed to encode kiosk icon"; | |
| 99 return; | |
| 100 } | |
| 101 | |
| 102 const base::FilePath icon_path = | |
| 103 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension); | |
| 104 BrowserThread::GetBlockingPool()->PostTask( | |
| 105 FROM_HERE, base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, | |
| 106 base::Passed(std::move(image_data)))); | |
| 107 | |
| 108 icon_path_ = icon_path; | |
| 109 } | |
| 110 | |
| 111 void KioskAppDataBase::ClearCache() { | |
| 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 113 PrefService* local_state = g_browser_process->local_state(); | |
| 114 | |
| 115 DictionaryPrefUpdate dict_update(local_state, dictionary_name()); | |
| 116 | |
| 117 const std::string app_key = | |
| 118 std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_; | |
| 119 dict_update->Remove(app_key, nullptr); | |
| 120 | |
| 121 if (!icon_path_.empty()) { | |
| 122 base::PostTaskWithTraits( | |
| 123 FROM_HERE, | |
| 124 base::TaskTraits().MayBlock().WithPriority( | |
| 125 base::TaskPriority::BACKGROUND), | |
| 126 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false)); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 } // namespace chromeos | |
| OLD | NEW |