Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/icon_manager.h

Issue 2440273002: Clean up the IconLoader. (Closed)
Patch Set: nits Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/icon_loader_win.cc ('k') | chrome/browser/icon_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Class for finding and caching Windows explorer icons. The IconManager 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 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 7 // to avoid blocking the UI thread with potentially expensive COM and disk
8 // operations. 8 // operations.
9 // 9 //
10 // Terminology 10 // Terminology
(...skipping 16 matching lines...) Expand all
27 // 27 //
28 // POSIX files don't have associated icons. We query the OS by the file's 28 // POSIX files don't have associated icons. We query the OS by the file's
29 // mime type. 29 // mime type.
30 // 30 //
31 // The IconManager can be queried in two ways: 31 // The IconManager can be queried in two ways:
32 // 1. A quick, synchronous check of its caches which does not touch the disk: 32 // 1. A quick, synchronous check of its caches which does not touch the disk:
33 // IconManager::LookupIcon() 33 // IconManager::LookupIcon()
34 // 2. An asynchronous icon load from a file on the file thread: 34 // 2. An asynchronous icon load from a file on the file thread:
35 // IconManager::LoadIcon() 35 // IconManager::LoadIcon()
36 // 36 //
37 // When using the second (asychronous) method, callers must supply a callback 37 // When using the second (asynchronous) method, callers must supply a callback
38 // which will be run once the icon has been extracted. The icon manager will 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 39 // cache the results of the icon extraction so that subsequent lookups will be
40 // fast. 40 // fast.
41 // 41 //
42 // Icon bitmaps returned should be treated as const since they may be referenced 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. 43 // by other clients. Make a copy of the icon if you need to modify it.
44 44
45 #ifndef CHROME_BROWSER_ICON_MANAGER_H_ 45 #ifndef CHROME_BROWSER_ICON_MANAGER_H_
46 #define CHROME_BROWSER_ICON_MANAGER_H_ 46 #define CHROME_BROWSER_ICON_MANAGER_H_
47 47
48 #include <map> 48 #include <map>
49 #include <memory>
49 50
50 #include "base/files/file_path.h" 51 #include "base/files/file_path.h"
51 #include "base/macros.h" 52 #include "base/macros.h"
52 #include "base/task/cancelable_task_tracker.h" 53 #include "base/task/cancelable_task_tracker.h"
53 #include "chrome/browser/icon_loader.h" 54 #include "chrome/browser/icon_loader.h"
54 #include "ui/gfx/image/image.h" 55 #include "ui/gfx/image/image.h"
55 56
56 class IconManager : public IconLoader::Delegate { 57 class IconManager : public IconLoader::Delegate {
57 public: 58 public:
58 IconManager(); 59 IconManager();
59 ~IconManager() override; 60 ~IconManager() override;
60 61
61 // Synchronous call to examine the internal caches for the icon. Returns the 62 // Synchronous call to examine the internal caches for the icon. Returns the
62 // icon if we have already loaded it, NULL if we don't have it and must load 63 // icon if we have already loaded it, or null if we don't have it and must
63 // it via 'LoadIcon'. The returned bitmap is owned by the IconManager and must 64 // load it via LoadIcon(). The returned bitmap is owned by the IconManager and
64 // not be free'd by the caller. If the caller needs to modify the icon, it 65 // must not be free'd by the caller. If the caller needs to modify the icon,
65 // must make a copy and modify the copy. 66 // it must make a copy and modify the copy.
66 gfx::Image* LookupIconFromFilepath(const base::FilePath& file_name, 67 gfx::Image* LookupIconFromFilepath(const base::FilePath& file_path,
67 IconLoader::IconSize size); 68 IconLoader::IconSize size);
68 69
69 typedef base::Callback<void(gfx::Image*)> IconRequestCallback; 70 using IconRequestCallback = base::Callback<void(gfx::Image*)>;
70 71
71 // Asynchronous call to lookup and return the icon associated with file. The 72 // Asynchronous call to lookup and return the icon associated with file. The
72 // work is done on the file thread, with the callbacks running on the thread 73 // work is done on the file thread, with the callbacks running on the thread
73 // this function is called. 74 // this function is called.
74 // 75 //
75 // Note: 76 // Note:
76 // 1. This does *not* check the cache. 77 // 1. This does *not* check the cache.
77 // 2. The returned bitmap pointer is *not* owned by callback. So callback 78 // 2. The returned bitmap pointer is *not* owned by callback. So callback
78 // should never keep it or delete it. 79 // should never keep it or delete it.
79 // 3. The gfx::Image pointer passed to the callback may be NULL if decoding 80 // 3. The gfx::Image pointer passed to the callback will be null if decoding
80 // failed. 81 // failed.
81 base::CancelableTaskTracker::TaskId LoadIcon( 82 base::CancelableTaskTracker::TaskId LoadIcon(
82 const base::FilePath& file_name, 83 const base::FilePath& file_name,
83 IconLoader::IconSize size, 84 IconLoader::IconSize size,
84 const IconRequestCallback& callback, 85 const IconRequestCallback& callback,
85 base::CancelableTaskTracker* tracker); 86 base::CancelableTaskTracker* tracker);
86 87
88 private:
87 // IconLoader::Delegate interface. 89 // IconLoader::Delegate interface.
88 bool OnGroupLoaded(IconLoader* loader, const IconGroupID& group) override; 90 void OnImageLoaded(IconLoader* loader,
89 bool OnImageLoaded(IconLoader* loader, 91 std::unique_ptr<gfx::Image> result,
90 gfx::Image* result, 92 const IconLoader::IconGroup& group) override;
91 const IconGroupID& group) override;
92 93
93 private:
94 struct CacheKey { 94 struct CacheKey {
95 CacheKey(const IconGroupID& group, IconLoader::IconSize size); 95 CacheKey(const IconLoader::IconGroup& group, IconLoader::IconSize size);
96 96
97 // Used as a key in the map below, so we need this comparator. 97 // Used as a key in the map below, so we need this comparator.
98 bool operator<(const CacheKey &other) const; 98 bool operator<(const CacheKey &other) const;
99 99
100 IconGroupID group; 100 IconLoader::IconGroup group;
101 IconLoader::IconSize size; 101 IconLoader::IconSize size;
102 }; 102 };
103 103
104 gfx::Image* LookupIconFromGroup(const IconGroupID& group, 104 std::map<base::FilePath, IconLoader::IconGroup> group_cache_;
105 IconLoader::IconSize size); 105 std::map<CacheKey, std::unique_ptr<gfx::Image>> icon_cache_;
106
107 typedef std::map<CacheKey, gfx::Image*> IconMap;
108 IconMap icon_cache_;
109
110 typedef std::map<base::FilePath, IconGroupID> GroupMap;
111 GroupMap group_cache_;
112 106
113 // Asynchronous requests that have not yet been completed. 107 // Asynchronous requests that have not yet been completed.
114 struct ClientRequest; 108 struct ClientRequest;
115 typedef std::map<IconLoader*, ClientRequest> ClientRequests; 109 std::map<IconLoader*, ClientRequest> requests_;
116 ClientRequests requests_;
117 110
118 DISALLOW_COPY_AND_ASSIGN(IconManager); 111 DISALLOW_COPY_AND_ASSIGN(IconManager);
119 }; 112 };
120 113
121 #endif // CHROME_BROWSER_ICON_MANAGER_H_ 114 #endif // CHROME_BROWSER_ICON_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/icon_loader_win.cc ('k') | chrome/browser/icon_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698