1// Copyright (c) 2011 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_EXTENSION_ICON_MANAGER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_ICON_MANAGER_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/memory/weak_ptr.h"
14#include "third_party/skia/include/core/SkBitmap.h"
15#include "ui/gfx/insets.h"
16
17namespace content {
18class BrowserContext;
19}
20
21namespace extensions {
22class Extension;
23}
24
25namespace gfx {
26class Image;
27}
28
29class ExtensionIconManager {
30 public:
31  ExtensionIconManager();
32  virtual ~ExtensionIconManager();
33
34  // Start loading the icon for the given extension.
35  void LoadIcon(content::BrowserContext* context,
36                const extensions::Extension* extension);
37
38  // This returns a bitmap of width/height kFaviconSize, loaded either from an
39  // entry specified in the extension's 'icon' section of the manifest, or a
40  // default extension icon.
41  const SkBitmap& GetIcon(const std::string& extension_id);
42
43  // Removes the extension's icon from memory.
44  void RemoveIcon(const std::string& extension_id);
45
46  void set_monochrome(bool value) { monochrome_ = value; }
47  void set_padding(const gfx::Insets& value) { padding_ = value; }
48
49 protected:
50  virtual void OnImageLoaded(const std::string& extension_id,
51                             const gfx::Image& image);
52
53 private:
54  // Makes sure we've done one-time initialization of the default extension icon
55  // default_icon_.
56  void EnsureDefaultIcon();
57
58  // Helper function to return a copy of |src| with the proper scaling and
59  // coloring.
60  SkBitmap ApplyTransforms(const SkBitmap& src);
61
62  // Maps extension id to an SkBitmap with the icon for that extension.
63  std::map<std::string, SkBitmap> icons_;
64
65  // Set of extension IDs waiting for icons to load.
66  std::set<std::string> pending_icons_;
67
68  // The default icon we'll use if an extension doesn't have one.
69  SkBitmap default_icon_;
70
71  // If true, we will desaturate the icons to make them monochromatic.
72  bool monochrome_;
73
74  // Specifies the amount of empty padding to place around the icon.
75  gfx::Insets padding_;
76
77  base::WeakPtrFactory<ExtensionIconManager> weak_ptr_factory_;
78
79  DISALLOW_COPY_AND_ASSIGN(ExtensionIconManager);
80};
81
82#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_ICON_MANAGER_H_
83