blob: a083bf3997483674206992ba8a273e20dc13fe17 [file] [log] [blame]
khmel8c1f6622017-05-11 19:14:501// 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#ifndef CHROME_BROWSER_EXTENSIONS_CHROME_APP_ICON_H_
6#define CHROME_BROWSER_EXTENSIONS_CHROME_APP_ICON_H_
7
8#include <memory>
9#include <string>
10
11#include "base/callback.h"
12#include "base/macros.h"
13#include "extensions/browser/extension_icon_image.h"
14#include "ui/gfx/image/image_skia.h"
15
16namespace content {
17class BrowserContext;
18}
19
20namespace extensions {
21
22class Extension;
23class ChromeAppIconDelegate;
24
25// This represents how an extension app icon should finally look. As a base,
26// extension icon is used and effects that depend on extension type, state and
27// some external conditions are applied. Resulting image is sent via
28// ChromeAppIconDelegate. Several updates are expected in case extension
29// state or some external conditions are changed.
30class ChromeAppIcon : public IconImage::Observer {
31 public:
32 using DestroyedCallback = base::OnceCallback<void(ChromeAppIcon*)>;
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1133 using ResizeFunction =
34 base::RepeatingCallback<void(const gfx::Size&, gfx::ImageSkia*)>;
khmel8c1f6622017-05-11 19:14:5035
Nigel Tao22ba05a2018-12-12 01:32:5136 // Applies image processing effects to |image_skia|, such as resizing, adding
37 // badges, converting to gray and rounding corners.
38 static void ApplyEffects(int resource_size_in_dip,
39 const ResizeFunction& resize_function,
40 bool apply_chrome_badge,
41 bool app_launchable,
42 bool from_bookmark,
43 gfx::ImageSkia* image_skia);
44
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1145 // |resize_function| overrides icon resizing behavior if non-null. Otherwise
46 // IconLoader with perform the resizing. In both cases |resource_size_in_dip|
47 // is used to pick the correct icon representation from resources.
khmel8c1f6622017-05-11 19:14:5048 ChromeAppIcon(ChromeAppIconDelegate* delegate,
49 content::BrowserContext* browser_context,
50 DestroyedCallback destroyed_callback,
51 const std::string& app_id,
Vladislav Kaznacheev6ae79b42018-09-13 06:11:1152 int resource_size_in_dip,
53 const ResizeFunction& resize_function);
khmel8c1f6622017-05-11 19:14:5054 ~ChromeAppIcon() override;
55
56 // Reloads icon.
57 void Reload();
58
59 // Returns true if the icon still refers to existing extension. Once extension
60 // is disabled it is discarded from the icon.
61 bool IsValid() const;
62
63 // Re-applies app effects over the current extension icon and dispatches the
64 // result via |delegate_|.
65 void UpdateIcon();
66
67 const gfx::ImageSkia& image_skia() const { return image_skia_; }
68 const std::string& app_id() const { return app_id_; }
Vadim Trysheva5d129fa2018-01-18 04:18:3869#if defined(OS_CHROMEOS)
70 // Returns whether the icon is badged because it's an extension app that has
71 // its Android analog installed.
72 bool icon_is_badged() const { return icon_is_badged_; }
73#endif
khmel8c1f6622017-05-11 19:14:5074
75 private:
76 const Extension* GetExtension();
77
78 // IconImage::Observer:
79 void OnExtensionIconImageChanged(IconImage* image) override;
80
81 // Unowned pointers.
82 ChromeAppIconDelegate* const delegate_;
83 content::BrowserContext* const browser_context_;
84
85 // Called when this instance of ChromeAppIcon is destroyed.
86 DestroyedCallback destroyed_callback_;
87
88 const std::string app_id_;
89
90 // Contains current icon image. This is static image with applied effects and
91 // it is updated each time when |icon_| is updated.
92 gfx::ImageSkia image_skia_;
93
Vadim Trysheva5d129fa2018-01-18 04:18:3894#if defined(OS_CHROMEOS)
95 // Whether the icon got badged because it's an extension app that has its
96 // Android analog installed.
97 bool icon_is_badged_ = false;
98#endif
99
khmel8c1f6622017-05-11 19:14:50100 const int resource_size_in_dip_;
101
Vladislav Kaznacheev6ae79b42018-09-13 06:11:11102 // Function to be used to resize the image loaded from a resource. If null,
103 // resize will be performed by ImageLoader.
104 const ResizeFunction resize_function_;
105
khmel8c1f6622017-05-11 19:14:50106 std::unique_ptr<IconImage> icon_;
107
108 DISALLOW_COPY_AND_ASSIGN(ChromeAppIcon);
109};
110
111} // namespace extensions
112
113#endif // CHROME_BROWSER_EXTENSIONS_CHROME_APP_ICON_H_