OLD | NEW |
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 #include <map> | 48 #include <map> |
49 #include <memory> | 49 #include <memory> |
50 | 50 |
51 #include "base/files/file_path.h" | 51 #include "base/files/file_path.h" |
52 #include "base/macros.h" | 52 #include "base/macros.h" |
53 #include "base/task/cancelable_task_tracker.h" | 53 #include "base/task/cancelable_task_tracker.h" |
54 #include "chrome/browser/icon_loader.h" | 54 #include "chrome/browser/icon_loader.h" |
55 #include "ui/gfx/image/image.h" | 55 #include "ui/gfx/image/image.h" |
56 | 56 |
57 class IconManager : public IconLoader::Delegate { | 57 class IconManager { |
58 public: | 58 public: |
59 IconManager(); | 59 IconManager(); |
60 ~IconManager() override; | 60 ~IconManager(); |
61 | 61 |
62 // 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 |
63 // icon if we have already loaded it, or null if we don't have it and must | 63 // icon if we have already loaded it, or null if we don't have it and must |
64 // load it via LoadIcon(). The returned bitmap is owned by the IconManager and | 64 // load it via LoadIcon(). The returned bitmap is owned by the IconManager and |
65 // must not be free'd by the caller. If the caller needs to modify the icon, | 65 // must not be free'd by the caller. If the caller needs to modify the icon, |
66 // it must make a copy and modify the copy. | 66 // it must make a copy and modify the copy. |
67 gfx::Image* LookupIconFromFilepath(const base::FilePath& file_path, | 67 gfx::Image* LookupIconFromFilepath(const base::FilePath& file_path, |
68 IconLoader::IconSize size); | 68 IconLoader::IconSize size); |
69 | 69 |
70 using IconRequestCallback = base::Callback<void(gfx::Image*)>; | 70 using IconRequestCallback = base::OnceCallback<void(gfx::Image*)>; |
71 | 71 |
72 // 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 |
73 // 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 |
74 // this function is called. | 74 // this function is called. |
75 // | 75 // |
76 // Note: | 76 // Note: |
77 // 1. This does *not* check the cache. | 77 // 1. This does *not* check the cache. |
78 // 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 |
79 // should never keep it or delete it. | 79 // should never keep it or delete it. |
80 // 3. The gfx::Image pointer passed to the callback will be null if decoding | 80 // 3. The gfx::Image pointer passed to the callback will be null if decoding |
81 // failed. | 81 // failed. |
82 base::CancelableTaskTracker::TaskId LoadIcon( | 82 base::CancelableTaskTracker::TaskId LoadIcon( |
83 const base::FilePath& file_name, | 83 const base::FilePath& file_name, |
84 IconLoader::IconSize size, | 84 IconLoader::IconSize size, |
85 const IconRequestCallback& callback, | 85 IconRequestCallback callback, |
86 base::CancelableTaskTracker* tracker); | 86 base::CancelableTaskTracker* tracker); |
87 | 87 |
88 private: | 88 private: |
89 // IconLoader::Delegate interface. | 89 void OnIconLoaded(IconLoader* loader, |
90 void OnImageLoaded(IconLoader* loader, | 90 std::unique_ptr<gfx::Image> result, |
91 std::unique_ptr<gfx::Image> result, | 91 const IconLoader::IconGroup& group); |
92 const IconLoader::IconGroup& group) override; | |
93 | 92 |
94 struct CacheKey { | 93 struct CacheKey { |
95 CacheKey(const IconLoader::IconGroup& group, IconLoader::IconSize size); | 94 CacheKey(const IconLoader::IconGroup& group, IconLoader::IconSize size); |
96 | 95 |
97 // Used as a key in the map below, so we need this comparator. | 96 // Used as a key in the map below, so we need this comparator. |
98 bool operator<(const CacheKey &other) const; | 97 bool operator<(const CacheKey &other) const; |
99 | 98 |
100 IconLoader::IconGroup group; | 99 IconLoader::IconGroup group; |
101 IconLoader::IconSize size; | 100 IconLoader::IconSize size; |
102 }; | 101 }; |
103 | 102 |
104 std::map<base::FilePath, IconLoader::IconGroup> group_cache_; | 103 std::map<base::FilePath, IconLoader::IconGroup> group_cache_; |
105 std::map<CacheKey, std::unique_ptr<gfx::Image>> icon_cache_; | 104 std::map<CacheKey, std::unique_ptr<gfx::Image>> icon_cache_; |
106 | 105 |
107 // Asynchronous requests that have not yet been completed. | 106 // Asynchronous requests that have not yet been completed. |
108 struct ClientRequest; | 107 struct ClientRequest; |
109 std::map<IconLoader*, ClientRequest> requests_; | 108 std::map<IconLoader*, ClientRequest> requests_; |
110 | 109 |
111 DISALLOW_COPY_AND_ASSIGN(IconManager); | 110 DISALLOW_COPY_AND_ASSIGN(IconManager); |
112 }; | 111 }; |
113 | 112 |
114 #endif // CHROME_BROWSER_ICON_MANAGER_H_ | 113 #endif // CHROME_BROWSER_ICON_MANAGER_H_ |
OLD | NEW |