1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_ICON_SOURCE_H_
6 #define CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_ICON_SOURCE_H_
11 #include "base/basictypes.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/task/cancelable_task_tracker.h"
14 #include "components/favicon/core/favicon_service.h"
15 #include "content/public/browser/url_data_source.h"
16 #include "extensions/common/extension_icon_set.h"
17 #include "extensions/common/extension_resource.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
20 class ExtensionIconSet
;
23 namespace extensions
{
26 // ExtensionIconSource serves extension icons through network level chrome:
27 // requests. Icons can be retrieved for any installed extension or app.
29 // The format for requesting an icon is as follows:
30 // chrome://extension-icon/<extension_id>/<icon_size>/<match_type>?[options]
32 // Parameters (<> required, [] optional):
33 // <extension_id> = the id of the extension
34 // <icon_size> = the size of the icon, as the integer value of the
35 // corresponding Extension:Icons enum.
36 // <match_type> = the fallback matching policy, as the integer value of
37 // the corresponding ExtensionIconSet::MatchType enum.
38 // [options] = Optional transformations to apply. Supported options:
39 // grayscale=true to desaturate the image.
42 // chrome-extension://gbmgkahjioeacddebbnengilkgbkhodg/32/1?grayscale=true
43 // (ICON_SMALL, MATCH_BIGGER, grayscale)
44 // chrome-extension://gbmgkahjioeacddebbnengilkgbkhodg/128/0
45 // (ICON_LARGE, MATCH_EXACTLY)
47 // We attempt to load icons from the following sources in order:
48 // 1) The icons as listed in the extension / app manifests.
49 // 2) If a 16px icon was requested, the favicon for extension's launch URL.
50 // 3) The default extension / application icon if there are still no matches.
52 class ExtensionIconSource
: public content::URLDataSource
,
53 public base::SupportsWeakPtr
<ExtensionIconSource
> {
55 explicit ExtensionIconSource(Profile
* profile
);
57 // Gets the URL of the |extension| icon in the given |icon_size|, falling back
58 // based on the |match| type. If |grayscale|, the URL will be for the
59 // desaturated version of the icon. |exists|, if non-NULL, will be set to true
60 // if the icon exists; false if it will lead to a default or not-present
62 static GURL
GetIconURL(const Extension
* extension
,
64 ExtensionIconSet::MatchType match
,
68 // A public utility function for accessing the bitmap of the image specified
70 static SkBitmap
* LoadImageByResourceId(int resource_id
);
72 // content::URLDataSource implementation.
73 std::string
GetSource() const override
;
74 std::string
GetMimeType(const std::string
&) const override
;
75 void StartDataRequest(
76 const std::string
& path
,
77 int render_process_id
,
79 const content::URLDataSource::GotDataCallback
& callback
) override
;
82 // Encapsulates the request parameters for |request_id|.
83 struct ExtensionIconRequest
;
85 ~ExtensionIconSource() override
;
87 // Returns the bitmap for the default app image.
88 const SkBitmap
* GetDefaultAppImage();
90 // Returns the bitmap for the default extension.
91 const SkBitmap
* GetDefaultExtensionImage();
93 // Performs any remaining transformations (like desaturating the |image|),
94 // then returns the |image| to the client and clears up any temporary data
95 // associated with the |request_id|.
96 void FinalizeImage(const SkBitmap
* image
, int request_id
);
98 // Loads the default image for |request_id| and returns to the client.
99 void LoadDefaultImage(int request_id
);
101 // Loads the extension's |icon| for the given |request_id| and returns the
102 // image to the client.
103 void LoadExtensionImage(const ExtensionResource
& icon
,
106 // Loads the favicon image for the app associated with the |request_id|. If
107 // the image does not exist, we fall back to the default image.
108 void LoadFaviconImage(int request_id
);
110 // FaviconService callback
111 void OnFaviconDataAvailable(
113 const favicon_base::FaviconRawBitmapResult
& bitmap_result
);
115 // ImageLoader callback
116 void OnImageLoaded(int request_id
, const gfx::Image
& image
);
118 // Called when the extension doesn't have an icon. We fall back to multiple
119 // sources, using the following order:
120 // 1) The icons as listed in the extension / app manifests.
121 // 2) If a 16px icon and the extension has a launch URL, see if Chrome
122 // has a corresponding favicon.
123 // 3) If still no matches, load the default extension / application icon.
124 void LoadIconFailed(int request_id
);
126 // Parses and savse an ExtensionIconRequest for the URL |path| for the
127 // specified |request_id|.
128 bool ParseData(const std::string
& path
,
130 const content::URLDataSource::GotDataCallback
& callback
);
132 // Stores the parameters associated with the |request_id|, making them
133 // as an ExtensionIconRequest via GetData.
134 void SetData(int request_id
,
135 const content::URLDataSource::GotDataCallback
& callback
,
136 const Extension
* extension
,
139 ExtensionIconSet::MatchType match
);
141 // Returns the ExtensionIconRequest for the given |request_id|.
142 ExtensionIconRequest
* GetData(int request_id
);
144 // Removes temporary data associated with |request_id|.
145 void ClearData(int request_id
);
149 // Maps tracker ids to request ids.
150 std::map
<int, int> tracker_map_
;
152 // Maps request_ids to ExtensionIconRequests.
153 std::map
<int, ExtensionIconRequest
*> request_map_
;
155 scoped_ptr
<SkBitmap
> default_app_data_
;
157 scoped_ptr
<SkBitmap
> default_extension_data_
;
159 base::CancelableTaskTracker cancelable_task_tracker_
;
161 DISALLOW_COPY_AND_ASSIGN(ExtensionIconSource
);
164 } // namespace extensions
166 #endif // CHROME_BROWSER_UI_WEBUI_EXTENSIONS_EXTENSION_ICON_SOURCE_H_