[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | // Class for finding and caching Windows explorer icons. The IconManager |
| 6 | // lives on the UI thread but performs icon extraction work on the file thread |
| 7 | // to avoid blocking the UI thread with potentially expensive COM and disk |
| 8 | // operations. |
| 9 | // |
| 10 | // Terminology |
| 11 | // |
| 12 | // Windows files have icons associated with them that can be of two types: |
| 13 | // 1. "Per class": the icon used for this file is used for all files with the |
| 14 | // same file extension or class. Examples are PDF or MP3 files, which use |
| 15 | // the same icon for all files of that type. |
| 16 | // 2. "Per instance": the icon used for this file is embedded in the file |
| 17 | // itself and is unique. Executable files are typically "per instance". |
| 18 | // |
| 19 | // Files that end in the following extensions are considered "per instance": |
| 20 | // .exe |
| 21 | // .dll |
| 22 | // .ico |
| 23 | // The IconManager will do explicit icon loads on the full path of these files |
| 24 | // and cache the results per file. All other file types will be looked up by |
| 25 | // file extension and the results will be cached per extension. That way, all |
| 26 | // .mp3 files will share one icon, but all .exe files will have their own icon. |
| 27 | // |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 28 | // POSIX files don't have associated icons. We query the OS by the file's |
| 29 | // mime type. |
| 30 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | // The IconManager can be queried in two ways: |
| 32 | // 1. A quick, synchronous check of its caches which does not touch the disk: |
| 33 | // IconManager::LookupIcon() |
| 34 | // 2. An asynchronous icon load from a file on the file thread: |
| 35 | // IconManager::LoadIcon() |
| 36 | // |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 37 | // When using the second (asynchronous) method, callers must supply a callback |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | // which will be run once the icon has been extracted. The icon manager will |
| 39 | // cache the results of the icon extraction so that subsequent lookups will be |
| 40 | // fast. |
| 41 | // |
| 42 | // Icon bitmaps returned should be treated as const since they may be referenced |
| 43 | // by other clients. Make a copy of the icon if you need to modify it. |
| 44 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 45 | #ifndef CHROME_BROWSER_ICON_MANAGER_H_ |
| 46 | #define CHROME_BROWSER_ICON_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | #include <map> |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 49 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 51 | #include "base/files/file_path.h" |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 52 | #include "base/memory/weak_ptr.h" |
[email protected] | e95b717f | 2014-02-06 13:47:13 | [diff] [blame] | 53 | #include "base/task/cancelable_task_tracker.h" |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 54 | #include "chrome/browser/icon_loader.h" |
[email protected] | f08e051 | 2011-06-13 18:10:44 | [diff] [blame] | 55 | #include "ui/gfx/image/image.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 57 | class IconManager { |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 58 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | IconManager(); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 60 | |
| 61 | IconManager(const IconManager&) = delete; |
| 62 | IconManager& operator=(const IconManager&) = delete; |
| 63 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 64 | ~IconManager(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
| 66 | // Synchronous call to examine the internal caches for the icon. Returns the |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 67 | // icon if we have already loaded it, or null if we don't have it and must |
| 68 | // load it via LoadIcon(). The returned bitmap is owned by the IconManager and |
| 69 | // must not be free'd by the caller. If the caller needs to modify the icon, |
| 70 | // it must make a copy and modify the copy. |
| 71 | gfx::Image* LookupIconFromFilepath(const base::FilePath& file_path, |
Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 72 | IconLoader::IconSize size, |
| 73 | float scale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 74 | |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 75 | using IconRequestCallback = base::OnceCallback<void(gfx::Image)>; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
[email protected] | 4118ab9 | 2009-06-17 00:43:43 | [diff] [blame] | 77 | // Asynchronous call to lookup and return the icon associated with file. The |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 78 | // work is done on the file thread, with the callbacks running on the thread |
| 79 | // this function is called. |
[email protected] | 4118ab9 | 2009-06-17 00:43:43 | [diff] [blame] | 80 | // |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 81 | // Note: |
| 82 | // 1. This does *not* check the cache. |
| 83 | // 2. The returned bitmap pointer is *not* owned by callback. So callback |
| 84 | // should never keep it or delete it. |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 85 | // 3. The gfx::Image pointer passed to the callback will be null if decoding |
[email protected] | c189698 | 2012-12-05 20:26:17 | [diff] [blame] | 86 | // failed. |
[email protected] | e95b717f | 2014-02-06 13:47:13 | [diff] [blame] | 87 | base::CancelableTaskTracker::TaskId LoadIcon( |
| 88 | const base::FilePath& file_name, |
| 89 | IconLoader::IconSize size, |
Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 90 | float scale, |
Avi Drissman | efe4dc8 | 2018-02-23 17:55:39 | [diff] [blame] | 91 | IconRequestCallback callback, |
[email protected] | e95b717f | 2014-02-06 13:47:13 | [diff] [blame] | 92 | base::CancelableTaskTracker* tracker); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 94 | private: |
Evan Stade | 91b50f5 | 2022-02-14 17:09:57 | [diff] [blame] | 95 | gfx::Image* DoLookupIconFromFilepath(const base::FilePath& file_path, |
| 96 | IconLoader::IconSize size, |
| 97 | float scale); |
| 98 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 99 | void OnIconLoaded(IconRequestCallback callback, |
| 100 | base::FilePath file_path, |
| 101 | IconLoader::IconSize size, |
Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 102 | float scale, |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 103 | gfx::Image result, |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 104 | const IconLoader::IconGroup& group); |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | struct CacheKey { |
Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 107 | CacheKey(const IconLoader::IconGroup& group, |
| 108 | IconLoader::IconSize size, |
| 109 | float scale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | |
| 111 | // Used as a key in the map below, so we need this comparator. |
| 112 | bool operator<(const CacheKey &other) const; |
| 113 | |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 114 | IconLoader::IconGroup group; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | IconLoader::IconSize size; |
Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 116 | float scale; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | }; |
| 118 | |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 119 | std::map<base::FilePath, IconLoader::IconGroup> group_cache_; |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 120 | std::map<CacheKey, gfx::Image> icon_cache_; |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 121 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 122 | base::WeakPtrFactory<IconManager> weak_factory_{this}; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | }; |
| 124 | |
[email protected] | 11f485728 | 2009-11-13 19:56:17 | [diff] [blame] | 125 | #endif // CHROME_BROWSER_ICON_MANAGER_H_ |