File: kiosk_app_data_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (173 lines) | stat: -rw-r--r-- 5,782 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/app_mode/kiosk_app_data_base.h"

#include <cstddef>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "chrome/browser/ash/app_mode/kiosk_app_icon_loader.h"
#include "chrome/browser/browser_process.h"
#include "components/account_id/account_id.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"

using content::BrowserThread;

namespace ash {

namespace {

// Keys for local state data.
constexpr char kKeyName[] = "name";
constexpr char kKeyIcon[] = "icon";

// Icon file extension.
constexpr char kIconFileExtension[] = ".png";

// Save `raw_icon` for given `app_id`.
void SaveIconToLocalOnBlockingPool(const base::FilePath& icon_path,
                                   std::vector<unsigned char> image_data) {
  base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
                                                base::BlockingType::MAY_BLOCK);
  const base::FilePath dir = icon_path.DirName();
  if (!base::PathExists(dir) && !base::CreateDirectory(dir)) {
    LOG(ERROR) << "Failed to create directory to store kiosk icons";
    return;
  }

  if (!base::WriteFile(icon_path, image_data)) {
    LOG(ERROR) << "Failed to write kiosk icon file";
  }
}

void RemoveDictionaryPath(base::Value::Dict& dict, std::string_view path) {
  std::string_view current_path(path);
  base::Value::Dict* current_dictionary = &dict;
  size_t delimiter_position = current_path.rfind('.');
  if (delimiter_position != std::string_view::npos) {
    current_dictionary =
        dict.FindDictByDottedPath(current_path.substr(0, delimiter_position));
    if (!current_dictionary) {
      return;
    }
    current_path = current_path.substr(delimiter_position + 1);
  }
  current_dictionary->Remove(current_path);
}

}  // namespace

// static
const char KioskAppDataBase::kKeyApps[] = "apps";

KioskAppDataBase::KioskAppDataBase(const std::string& dictionary_name,
                                   const std::string& app_id,
                                   const AccountId& account_id)
    : dictionary_name_(dictionary_name),
      app_id_(app_id),
      account_id_(account_id) {}

KioskAppDataBase::~KioskAppDataBase() = default;

void KioskAppDataBase::SaveToDictionary(ScopedDictPrefUpdate& dict_update) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  const std::string app_key = std::string(kKeyApps) + '.' + app_id_;
  const std::string name_key = app_key + '.' + kKeyName;
  const std::string icon_path_key = app_key + '.' + kKeyIcon;

  dict_update->SetByDottedPath(name_key, name_);
  dict_update->SetByDottedPath(icon_path_key, icon_path_.value());
}

void KioskAppDataBase::SaveIconToDictionary(ScopedDictPrefUpdate& dict_update) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  const std::string app_key = std::string(kKeyApps) + '.' + app_id_;
  const std::string icon_path_key = app_key + '.' + kKeyIcon;

  dict_update->SetByDottedPath(icon_path_key, icon_path_.value());
}

bool KioskAppDataBase::LoadFromDictionary(const base::Value::Dict& dict) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  const std::string app_key =
      std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_;
  const std::string name_key = app_key + '.' + kKeyName;
  const std::string icon_path_key = app_key + '.' + kKeyIcon;

  // If there is no title stored, do not stop, sometimes only icon is cached.
  const std::string* maybe_name = dict.FindStringByDottedPath(name_key);
  if (maybe_name) {
    name_ = *maybe_name;
  }

  const std::string* icon_path_string =
      dict.FindStringByDottedPath(icon_path_key);
  if (!icon_path_string) {
    return false;
  }

  icon_path_ = base::FilePath(*icon_path_string);

  return true;
}

void KioskAppDataBase::DecodeIcon(KioskAppIconLoader::ResultCallback callback) {
  DLOG_IF(ERROR, icon_path_.empty()) << "Icon path is empty";
  kiosk_app_icon_loader_ =
      std::make_unique<KioskAppIconLoader>(std::move(callback));
  kiosk_app_icon_loader_->Start(icon_path_);
}

void KioskAppDataBase::SaveIcon(const SkBitmap& icon,
                                const base::FilePath& cache_dir) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  std::optional<std::vector<uint8_t>> image_data =
      gfx::PNGCodec::EncodeBGRASkBitmap(icon, /*discard_transparency=*/false);
  if (!image_data) {
    LOG(ERROR) << "Failed to encode kiosk icon";
    return;
  }

  const base::FilePath icon_path =
      cache_dir.AppendASCII(app_id_).AddExtension(kIconFileExtension);
  base::ThreadPool::PostTask(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&SaveIconToLocalOnBlockingPool, icon_path,
                     std::move(image_data).value()));

  icon_path_ = icon_path;
}

void KioskAppDataBase::ClearCache() const {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  PrefService* local_state = g_browser_process->local_state();

  ScopedDictPrefUpdate dict_update(local_state, dictionary_name());

  const std::string app_key =
      std::string(KioskAppDataBase::kKeyApps) + '.' + app_id_;
  RemoveDictionaryPath(dict_update.Get(), app_key);

  if (!icon_path_.empty()) {
    base::ThreadPool::PostTask(
        FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
        base::GetDeleteFileCallback(icon_path_));
  }
}

}  // namespace ash