blob: 8a398e794f48f3a430aad29264c17f7e79820148 [file] [log] [blame]
[email protected]4c03b2e92012-01-03 19:36:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294//
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]0f38ceae2009-05-08 19:01:0228// POSIX files don't have associated icons. We query the OS by the file's
29// mime type.
30//
initial.commit09911bf2008-07-26 23:55:2931// 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//
avi381f719f2016-12-16 00:05:0237// When using the second (asynchronous) method, callers must supply a callback
initial.commit09911bf2008-07-26 23:55:2938// 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]7ced67af2009-04-14 23:25:1645#ifndef CHROME_BROWSER_ICON_MANAGER_H_
46#define CHROME_BROWSER_ICON_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:2947
initial.commit09911bf2008-07-26 23:55:2948#include <map>
avi381f719f2016-12-16 00:05:0249#include <memory>
initial.commit09911bf2008-07-26 23:55:2950
[email protected]bc0147b2013-04-03 20:50:5951#include "base/files/file_path.h"
avif0a7b5b812016-12-17 19:01:3152#include "base/memory/weak_ptr.h"
[email protected]e95b717f2014-02-06 13:47:1353#include "base/task/cancelable_task_tracker.h"
[email protected]0f38ceae2009-05-08 19:01:0254#include "chrome/browser/icon_loader.h"
[email protected]f08e0512011-06-13 18:10:4455#include "ui/gfx/image/image.h"
initial.commit09911bf2008-07-26 23:55:2956
avif0a7b5b812016-12-17 19:01:3157class IconManager {
[email protected]0f38ceae2009-05-08 19:01:0258 public:
initial.commit09911bf2008-07-26 23:55:2959 IconManager();
Peter Boström53c6c5952021-09-17 09:41:2660
61 IconManager(const IconManager&) = delete;
62 IconManager& operator=(const IconManager&) = delete;
63
avif0a7b5b812016-12-17 19:01:3164 ~IconManager();
initial.commit09911bf2008-07-26 23:55:2965
66 // Synchronous call to examine the internal caches for the icon. Returns the
avi381f719f2016-12-16 00:05:0267 // 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 Zhirovf51bebd2021-04-13 09:52:0472 IconLoader::IconSize size,
73 float scale);
initial.commit09911bf2008-07-26 23:55:2974
Dana Friedfa14d5f2019-04-21 20:49:3675 using IconRequestCallback = base::OnceCallback<void(gfx::Image)>;
initial.commit09911bf2008-07-26 23:55:2976
[email protected]4118ab92009-06-17 00:43:4377 // Asynchronous call to lookup and return the icon associated with file. The
[email protected]c1896982012-12-05 20:26:1778 // work is done on the file thread, with the callbacks running on the thread
79 // this function is called.
[email protected]4118ab92009-06-17 00:43:4380 //
[email protected]c1896982012-12-05 20:26:1781 // 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.
avi381f719f2016-12-16 00:05:0285 // 3. The gfx::Image pointer passed to the callback will be null if decoding
[email protected]c1896982012-12-05 20:26:1786 // failed.
[email protected]e95b717f2014-02-06 13:47:1387 base::CancelableTaskTracker::TaskId LoadIcon(
88 const base::FilePath& file_name,
89 IconLoader::IconSize size,
Alexander Zhirovf51bebd2021-04-13 09:52:0490 float scale,
Avi Drissmanefe4dc82018-02-23 17:55:3991 IconRequestCallback callback,
[email protected]e95b717f2014-02-06 13:47:1392 base::CancelableTaskTracker* tracker);
initial.commit09911bf2008-07-26 23:55:2993
[email protected]0f38ceae2009-05-08 19:01:0294 private:
Evan Stade91b50f52022-02-14 17:09:5795 gfx::Image* DoLookupIconFromFilepath(const base::FilePath& file_path,
96 IconLoader::IconSize size,
97 float scale);
98
avif0a7b5b812016-12-17 19:01:3199 void OnIconLoaded(IconRequestCallback callback,
100 base::FilePath file_path,
101 IconLoader::IconSize size,
Alexander Zhirovf51bebd2021-04-13 09:52:04102 float scale,
Dana Friedfa14d5f2019-04-21 20:49:36103 gfx::Image result,
avif0a7b5b812016-12-17 19:01:31104 const IconLoader::IconGroup& group);
avi381f719f2016-12-16 00:05:02105
initial.commit09911bf2008-07-26 23:55:29106 struct CacheKey {
Alexander Zhirovf51bebd2021-04-13 09:52:04107 CacheKey(const IconLoader::IconGroup& group,
108 IconLoader::IconSize size,
109 float scale);
initial.commit09911bf2008-07-26 23:55:29110
111 // Used as a key in the map below, so we need this comparator.
112 bool operator<(const CacheKey &other) const;
113
avi381f719f2016-12-16 00:05:02114 IconLoader::IconGroup group;
initial.commit09911bf2008-07-26 23:55:29115 IconLoader::IconSize size;
Alexander Zhirovf51bebd2021-04-13 09:52:04116 float scale;
initial.commit09911bf2008-07-26 23:55:29117 };
118
avi381f719f2016-12-16 00:05:02119 std::map<base::FilePath, IconLoader::IconGroup> group_cache_;
Dana Friedfa14d5f2019-04-21 20:49:36120 std::map<CacheKey, gfx::Image> icon_cache_;
[email protected]bc0147b2013-04-03 20:50:59121
Jeremy Roman495db682019-07-12 16:03:24122 base::WeakPtrFactory<IconManager> weak_factory_{this};
initial.commit09911bf2008-07-26 23:55:29123};
124
[email protected]11f4857282009-11-13 19:56:17125#endif // CHROME_BROWSER_ICON_MANAGER_H_