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 namespace chromeos { | |
19 | |
20 namespace { | |
21 | |
22 // Keys for local state data. | |
23 constexpr char kKeyName[] = "name"; | |
24 constexpr char kKeyIcon[] = "icon"; | |
25 | |
26 // Icon file extension. | |
27 const char kIconFileExtension[] = ".png"; | |
Luis Héctor Chávez
2017/03/30 16:49:54
nit: constexpr?
Sergey Poromov
2017/03/30 18:22:07
Done.
| |
28 | |
29 // Save |raw_icon| for given |app_id|. | |
30 void SaveIconToLocalOnBlockingPool( | |
31 const base::FilePath& icon_path, | |
32 scoped_refptr<base::RefCountedString> raw_icon) { | |
33 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
34 | |
35 const base::FilePath dir = icon_path.DirName(); | |
36 if (!base::PathExists(dir)) | |
37 CHECK(base::CreateDirectory(dir)); | |
Luis Héctor Chávez
2017/03/30 16:49:54
don't use CHECK: https://chromium.googlesource.com
Sergey Poromov
2017/03/30 18:22:07
Done.
| |
38 | |
39 CHECK_EQ( | |
40 static_cast<int>(raw_icon->size()), | |
41 base::WriteFile(icon_path, raw_icon->data().c_str(), raw_icon->size())); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 // static | |
47 const char KioskAppDataBase::kKeyApps[] = "apps"; | |
48 | |
49 KioskAppDataBase::KioskAppDataBase(const std::string& app_id, | |
50 const AccountId& account_id) | |
51 : app_id_(app_id), account_id_(account_id) {} | |
52 | |
53 KioskAppDataBase::~KioskAppDataBase() = default; | |
54 | |
55 void KioskAppDataBase::SaveToDictionary(DictionaryPrefUpdate& dict_update) { | |
Luis Héctor Chávez
2017/03/30 16:49:54
Can you add DCHECK_CURRENTLY_ON to all methods?
Sergey Poromov
2017/03/30 18:22:07
Done.
| |
56 const std::string app_key = std::string(kKeyApps) + '.' + app_id_; | |
Luis Héctor Chávez
2017/03/30 16:49:54
It's probably better to use base::StringPrintf ins
Sergey Poromov
2017/03/30 18:22:07
I copied the code from original functions, prefer
| |
57 const std::string name_key = app_key + '.' + kKeyName; | |
58 const std::string icon_path_key = app_key + '.' + kKeyIcon; | |
59 | |
60 dict_update->SetString(name_key, name_); | |
61 dict_update->SetString(icon_path_key, icon_path_.value()); | |
62 } | |
63 | |
64 bool KioskAppDataBase::LoadFromDictionary(const base::DictionaryValue* dict) { | |
65 const std::string app_key = | |
66 std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_; | |
67 const std::string name_key = app_key + '.' + kKeyName; | |
68 const std::string icon_path_key = app_key + '.' + kKeyIcon; | |
69 | |
70 std::string icon_path_string; | |
71 if (!dict->GetString(name_key, &name_) || | |
72 !dict->GetString(icon_path_key, &icon_path_string)) { | |
73 return false; | |
74 } | |
75 icon_path_ = base::FilePath(icon_path_string); | |
76 | |
77 // KioskAppIconLoader deletes itself when done. | |
78 (new KioskAppIconLoader(AsWeakPtr()))->Start(icon_path_); | |
Luis Héctor Chávez
2017/03/30 16:49:54
Is this the only caller? (Seems like it)
If so, c
Sergey Poromov
2017/03/30 18:22:07
What if KioskAppIconLoader will live longer than K
Luis Héctor Chávez
2017/03/30 19:57:22
That's fine, you still _need_ the weak ptr in Kios
Sergey Poromov
2017/03/31 12:56:24
Acknowledged.
| |
79 return true; | |
80 } | |
81 | |
82 void KioskAppDataBase::SaveIcon(const SkBitmap& icon, | |
83 const base::FilePath& cache_dir) { | |
84 std::vector<unsigned char> image_data; | |
85 CHECK(gfx::PNGCodec::EncodeBGRASkBitmap(icon, false, &image_data)); | |
86 scoped_refptr<base::RefCountedString> raw_icon(new base::RefCountedString); | |
Luis Héctor Chávez
2017/03/30 16:49:54
nit: Avoid parenthesisless ctors https://sites.goo
Sergey Poromov
2017/03/30 18:22:07
Done.
| |
87 raw_icon->data().assign(image_data.begin(), image_data.end()); | |
88 | |
89 base::FilePath icon_path = | |
90 cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension); | |
91 content::BrowserThread::GetBlockingPool()->PostTask( | |
92 FROM_HERE, | |
93 base::Bind(&SaveIconToLocalOnBlockingPool, icon_path, raw_icon)); | |
Luis Héctor Chávez
2017/03/30 16:49:54
Why are you wrapping image_data in a scoped_refptr
Sergey Poromov
2017/03/30 18:22:07
Done.
| |
94 | |
95 icon_path_ = icon_path; | |
96 } | |
97 | |
98 void KioskAppDataBase::ClearCache() { | |
99 PrefService* local_state = g_browser_process->local_state(); | |
100 | |
101 DictionaryPrefUpdate dict_update(local_state, kiosk_dictionary_name()); | |
102 | |
103 std::string app_key = std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_; | |
104 dict_update->Remove(app_key, NULL); | |
Luis Héctor Chávez
2017/03/30 16:49:54
nit: nullptr
Sergey Poromov
2017/03/30 18:22:07
Done.
| |
105 | |
106 if (!icon_path_.empty()) { | |
107 base::PostTaskWithTraits( | |
108 FROM_HERE, | |
109 base::TaskTraits().MayBlock().WithPriority( | |
110 base::TaskPriority::BACKGROUND), | |
111 base::Bind(base::IgnoreResult(&base::DeleteFile), icon_path_, false)); | |
112 } | |
113 } | |
114 | |
115 } // namespace chromeos | |
OLD | NEW |