Chromium Code Reviews| 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/task_scheduler/post_task.h" | |
| 11 #include "base/threading/sequenced_worker_pool.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "components/prefs/pref_service.h" | |
| 14 #include "components/prefs/scoped_user_pref_update.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "ui/gfx/codec/png_codec.h" | |
| 17 | |
| 18 using content::BrowserThread; | |
| 19 | |
| 20 namespace chromeos { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Keys for local state data. | |
| 25 constexpr char kKeyName[] = "name"; | |
| 26 constexpr char kKeyIcon[] = "icon"; | |
| 27 | |
| 28 // Icon file extension. | |
| 29 constexpr char kIconFileExtension[] = ".png"; | |
| 30 | |
| 31 // Save |raw_icon| for given |app_id|. | |
| 32 void SaveIconToLocalOnBlockingPool(const base::FilePath& icon_path, | |
| 33 std::vector<unsigned char> image_data) { | |
| 34 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 35 | |
| 36 const base::FilePath dir = icon_path.DirName(); | |
| 37 if (!base::PathExists(dir)) | |
| 38 DCHECK(base::CreateDirectory(dir)); | |
|
xiyuan
2017/03/30 18:40:08
DCHECK could a removed for release build and cause
khmel
2017/03/30 18:45:47
I think when we work with FS it is better not forc
Sergey Poromov
2017/03/31 12:56:24
Agree about incorrect DCHECK.
I refactored CHECKs
| |
| 39 | |
| 40 CHECK_EQ( | |
|
Luis Héctor Chávez
2017/03/30 19:57:22
Please remove all CHECKs from this change.
Sergey Poromov
2017/03/31 12:56:25
Done. Is changing to LOG(ERROR) and return after s
Luis Héctor Chávez
2017/04/03 22:58:55
I believe so, I don't think we can do anything els
| |
| 41 static_cast<int>(image_data.size()), | |
| 42 base::WriteFile(icon_path, reinterpret_cast<char*>(image_data.data()), | |
| 43 image_data.size())); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 // static | |
| 49 const char KioskAppDataBase::kKeyApps[] = "apps"; | |
| 50 | |
| 51 KioskAppDataBase::KioskAppDataBase(const std::string& dictionary_name, | |
| 52 const std::string& app_id, | |
| 53 const AccountId& account_id) | |
| 54 : dictionary_name_(dictionary_name), | |
| 55 app_id_(app_id), | |
| 56 account_id_(account_id), | |
| 57 weak_factory_(this) {} | |
| 58 | |
| 59 KioskAppDataBase::~KioskAppDataBase() = default; | |
| 60 | |
| 61 void KioskAppDataBase::SaveToDictionary(DictionaryPrefUpdate& dict_update) { | |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 63 const std::string app_key = std::string(kKeyApps) + '.' + app_id_; | |
| 64 const std::string name_key = app_key + '.' + kKeyName; | |
| 65 const std::string icon_path_key = app_key + '.' + kKeyIcon; | |
| 66 | |
| 67 dict_update->SetString(name_key, name_); | |
| 68 dict_update->SetString(icon_path_key, icon_path_.value()); | |
| 69 } | |
| 70 | |
| 71 bool KioskAppDataBase::LoadFromDictionary(const base::DictionaryValue* dict) { | |
| 72 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 73 const std::string app_key = | |
| 74 std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_; | |
| 75 const std::string name_key = app_key + '.' + kKeyName; | |
| 76 const std::string icon_path_key = app_key + '.' + kKeyIcon; | |
| 77 | |
| 78 std::string icon_path_string; | |
| 79 if (!dict->GetString(name_key, &name_) || | |
| 80 !dict->GetString(icon_path_key, &icon_path_string)) { | |
| 81 return false; | |
| 82 } | |
| 83 icon_path_ = base::FilePath(icon_path_string); | |
| 84 | |
| 85 // KioskAppIconLoader deletes itself when done. | |
| 86 (new KioskAppIconLoader(weak_factory_.GetWeakPtr()))->Start(icon_path_); | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 void KioskAppDataBase::SaveIcon(const SkBitmap& icon, | |
| 91 const base::FilePath& cache_dir) { | |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 93 std::vector<unsigned char> image_data; | |
| 94 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)); | |
| 95 | |
| 96 base::FilePath icon_path = | |
| 97 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension); | |
| 98 BrowserThread::GetBlockingPool()->PostTask( | |
| 99 FROM_HERE, base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, | |
| 100 base::Passed(std::move(image_data)))); | |
| 101 | |
| 102 icon_path_ = icon_path; | |
| 103 } | |
| 104 | |
| 105 void KioskAppDataBase::ClearCache() { | |
| 106 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 107 PrefService* local_state = g_browser_process->local_state(); | |
| 108 | |
| 109 DictionaryPrefUpdate dict_update(local_state, dictionary_name()); | |
| 110 | |
| 111 std::string app_key = std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_; | |
| 112 dict_update->Remove(app_key, nullptr); | |
| 113 | |
| 114 if (!icon_path_.empty()) { | |
| 115 base::PostTaskWithTraits( | |
| 116 FROM_HERE, | |
| 117 base::TaskTraits().MayBlock().WithPriority( | |
| 118 base::TaskPriority::BACKGROUND), | |
| 119 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false)); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 } // namespace chromeos | |
| OLD | NEW |