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