blob: 94b74ad1c70486f67535db01bc623e9b519e1502 [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"
10#include "chrome/browser/extensions/extension_service.h"
11#include "chrome/browser/extensions/extension_util.h"
12#include "chrome/browser/profiles/profile.h"
13#include "chrome/common/extensions/extension_constants.h"
14#include "extensions/browser/extension_system.h"
15#include "extensions/common/extension.h"
16#include "extensions/common/manifest_handlers/icons_handler.h"
17
18namespace extensions {
19
20namespace {
21
22const Extension* GetExtensionByID(Profile* profile, const std::string& id) {
23 ExtensionService* service =
24 ExtensionSystem::Get(profile)->extension_service();
25 if (!service)
26 return nullptr;
27 return service->GetInstalledExtension(id);
28}
29
30} // namespace
31
32ChromeAppIconLoader::ChromeAppIconLoader(Profile* profile,
33 int icon_size_in_dips,
34 AppIconLoaderDelegate* delegate)
35 : AppIconLoader(profile, icon_size_in_dips, delegate) {}
36
37ChromeAppIconLoader::~ChromeAppIconLoader() {}
38
39bool ChromeAppIconLoader::CanLoadImageForApp(const std::string& id) {
40 if (map_.find(id) != map_.end())
41 return true;
42 return GetExtensionByID(profile(), id) != nullptr;
43}
44
45void ChromeAppIconLoader::FetchImage(const std::string& id) {
46 if (map_.find(id) != map_.end())
47 return; // Already loading the image.
48
49 const Extension* extension = GetExtensionByID(profile(), id);
50 if (!extension)
51 return;
52
53 std::unique_ptr<ChromeAppIcon> icon =
54 ChromeAppIconService::Get(profile())->CreateIcon(this, id, icon_size());
55
56 // Triggers image loading now instead of depending on paint message. This
57 // makes the temp blank image be shown for shorter time and improves user
58 // experience. See http://crbug.com/146114.
59 icon->image_skia().EnsureRepsForSupportedScales();
60 map_[id] = std::move(icon);
61}
62
63void ChromeAppIconLoader::ClearImage(const std::string& id) {
64 map_.erase(id);
65}
66
67void ChromeAppIconLoader::UpdateImage(const std::string& id) {
68 ExtensionIDToChromeAppIconMap::iterator it = map_.find(id);
69 if (it == map_.end())
70 return;
71
72 it->second->UpdateIcon();
73}
74
75void ChromeAppIconLoader::OnIconUpdated(ChromeAppIcon* icon) {
76 delegate()->OnAppImageUpdated(icon->app_id(), icon->image_skia());
77}
78
79} // namespace extensions