blob: e7459fb599d5218dbad4566191cce28680adc5f0 [file] [log] [blame]
khmel8c1f6622017-05-11 19:14:501// Copyright 2016 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/extensions/chrome_app_icon_loader.h"
6
7#include "base/stl_util.h"
8#include "chrome/browser/extensions/chrome_app_icon.h"
9#include "chrome/browser/extensions/chrome_app_icon_service.h"
khmel8c1f6622017-05-11 19:14:5010#include "chrome/browser/extensions/extension_util.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/common/extensions/extension_constants.h"
David Bertoni9f897c92019-09-20 17:46:3513#include "extensions/browser/extension_registry.h"
khmel8c1f6622017-05-11 19:14:5014#include "extensions/common/extension.h"
15#include "extensions/common/manifest_handlers/icons_handler.h"
16
17namespace extensions {
18
19namespace {
20
21const Extension* GetExtensionByID(Profile* profile, const std::string& id) {
David Bertoni9f897c92019-09-20 17:46:3522 return ExtensionRegistry::Get(profile)->GetInstalledExtension(id);
khmel8c1f6622017-05-11 19:14:5023}
24
25} // namespace
26
27ChromeAppIconLoader::ChromeAppIconLoader(Profile* profile,
Nigel Tao7cc6f7a2019-03-14 09:13:3828 int icon_size_in_dip,
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1129 const ResizeFunction& resize_function,
khmel8c1f6622017-05-11 19:14:5030 AppIconLoaderDelegate* delegate)
Nigel Tao7cc6f7a2019-03-14 09:13:3831 : AppIconLoader(profile, icon_size_in_dip, delegate),
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1132 resize_function_(resize_function) {}
33
34ChromeAppIconLoader::ChromeAppIconLoader(Profile* profile,
Nigel Tao7cc6f7a2019-03-14 09:13:3835 int icon_size_in_dip,
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1136 AppIconLoaderDelegate* delegate)
37 : ChromeAppIconLoader(profile,
Nigel Tao7cc6f7a2019-03-14 09:13:3838 icon_size_in_dip,
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1139 ResizeFunction(),
40 delegate) {}
khmel8c1f6622017-05-11 19:14:5041
42ChromeAppIconLoader::~ChromeAppIconLoader() {}
43
44bool ChromeAppIconLoader::CanLoadImageForApp(const std::string& id) {
45 if (map_.find(id) != map_.end())
46 return true;
nancy0bd5b1a2020-01-21 23:54:2847
48 const Extension* extension = GetExtensionByID(profile(), id);
49 if (!extension || (extensions_only_ && !extension->is_extension()))
50 return false;
51
52 return true;
khmel8c1f6622017-05-11 19:14:5053}
54
55void ChromeAppIconLoader::FetchImage(const std::string& id) {
56 if (map_.find(id) != map_.end())
57 return; // Already loading the image.
58
59 const Extension* extension = GetExtensionByID(profile(), id);
60 if (!extension)
61 return;
62
63 std::unique_ptr<ChromeAppIcon> icon =
Nigel Tao7cc6f7a2019-03-14 09:13:3864 ChromeAppIconService::Get(profile())->CreateIcon(
65 this, id, icon_size_in_dip(), resize_function_);
khmel8c1f6622017-05-11 19:14:5066 // Triggers image loading now instead of depending on paint message. This
67 // makes the temp blank image be shown for shorter time and improves user
68 // experience. See http://crbug.com/146114.
69 icon->image_skia().EnsureRepsForSupportedScales();
70 map_[id] = std::move(icon);
71}
72
73void ChromeAppIconLoader::ClearImage(const std::string& id) {
74 map_.erase(id);
75}
76
77void ChromeAppIconLoader::UpdateImage(const std::string& id) {
jdoerrie13cd648c82018-10-02 21:21:0278 auto it = map_.find(id);
khmel8c1f6622017-05-11 19:14:5079 if (it == map_.end())
80 return;
81
82 it->second->UpdateIcon();
83}
84
nancy0bd5b1a2020-01-21 23:54:2885void ChromeAppIconLoader::SetExtensionsOnly() {
86 extensions_only_ = true;
87}
88
khmel8c1f6622017-05-11 19:14:5089void ChromeAppIconLoader::OnIconUpdated(ChromeAppIcon* icon) {
90 delegate()->OnAppImageUpdated(icon->app_id(), icon->image_skia());
91}
92
93} // namespace extensions