khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 1 | // 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" |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_util.h" |
| 11 | #include "chrome/browser/profiles/profile.h" |
| 12 | #include "chrome/common/extensions/extension_constants.h" |
David Bertoni | 9f897c9 | 2019-09-20 17:46:35 | [diff] [blame] | 13 | #include "extensions/browser/extension_registry.h" |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 14 | #include "extensions/common/extension.h" |
| 15 | #include "extensions/common/manifest_handlers/icons_handler.h" |
| 16 | |
| 17 | namespace extensions { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | const Extension* GetExtensionByID(Profile* profile, const std::string& id) { |
David Bertoni | 9f897c9 | 2019-09-20 17:46:35 | [diff] [blame] | 22 | return ExtensionRegistry::Get(profile)->GetInstalledExtension(id); |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | ChromeAppIconLoader::ChromeAppIconLoader(Profile* profile, |
Nigel Tao | 7cc6f7a | 2019-03-14 09:13:38 | [diff] [blame] | 28 | int icon_size_in_dip, |
Vladislav Kaznacheev | 6ae79b4 | 2018-09-13 06:11:11 | [diff] [blame] | 29 | const ResizeFunction& resize_function, |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 30 | AppIconLoaderDelegate* delegate) |
Nigel Tao | 7cc6f7a | 2019-03-14 09:13:38 | [diff] [blame] | 31 | : AppIconLoader(profile, icon_size_in_dip, delegate), |
Vladislav Kaznacheev | 6ae79b4 | 2018-09-13 06:11:11 | [diff] [blame] | 32 | resize_function_(resize_function) {} |
| 33 | |
| 34 | ChromeAppIconLoader::ChromeAppIconLoader(Profile* profile, |
Nigel Tao | 7cc6f7a | 2019-03-14 09:13:38 | [diff] [blame] | 35 | int icon_size_in_dip, |
Vladislav Kaznacheev | 6ae79b4 | 2018-09-13 06:11:11 | [diff] [blame] | 36 | AppIconLoaderDelegate* delegate) |
| 37 | : ChromeAppIconLoader(profile, |
Nigel Tao | 7cc6f7a | 2019-03-14 09:13:38 | [diff] [blame] | 38 | icon_size_in_dip, |
Vladislav Kaznacheev | 6ae79b4 | 2018-09-13 06:11:11 | [diff] [blame] | 39 | ResizeFunction(), |
| 40 | delegate) {} |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 41 | |
| 42 | ChromeAppIconLoader::~ChromeAppIconLoader() {} |
| 43 | |
| 44 | bool ChromeAppIconLoader::CanLoadImageForApp(const std::string& id) { |
| 45 | if (map_.find(id) != map_.end()) |
| 46 | return true; |
nancy | 0bd5b1a | 2020-01-21 23:54:28 | [diff] [blame] | 47 | |
| 48 | const Extension* extension = GetExtensionByID(profile(), id); |
| 49 | if (!extension || (extensions_only_ && !extension->is_extension())) |
| 50 | return false; |
| 51 | |
| 52 | return true; |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void 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 Tao | 7cc6f7a | 2019-03-14 09:13:38 | [diff] [blame] | 64 | ChromeAppIconService::Get(profile())->CreateIcon( |
| 65 | this, id, icon_size_in_dip(), resize_function_); |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 66 | // 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 | |
| 73 | void ChromeAppIconLoader::ClearImage(const std::string& id) { |
| 74 | map_.erase(id); |
| 75 | } |
| 76 | |
| 77 | void ChromeAppIconLoader::UpdateImage(const std::string& id) { |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 78 | auto it = map_.find(id); |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 79 | if (it == map_.end()) |
| 80 | return; |
| 81 | |
| 82 | it->second->UpdateIcon(); |
| 83 | } |
| 84 | |
nancy | 0bd5b1a | 2020-01-21 23:54:28 | [diff] [blame] | 85 | void ChromeAppIconLoader::SetExtensionsOnly() { |
| 86 | extensions_only_ = true; |
| 87 | } |
| 88 | |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 89 | void ChromeAppIconLoader::OnIconUpdated(ChromeAppIcon* icon) { |
| 90 | delegate()->OnAppImageUpdated(icon->app_id(), icon->image_skia()); |
| 91 | } |
| 92 | |
| 93 | } // namespace extensions |