license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | // |
| 28 | // The IconManager can be queried in two ways: |
| 29 | // 1. A quick, synchronous check of its caches which does not touch the disk: |
| 30 | // IconManager::LookupIcon() |
| 31 | // 2. An asynchronous icon load from a file on the file thread: |
| 32 | // IconManager::LoadIcon() |
| 33 | // |
| 34 | // When using the second (asychronous) method, callers must supply a callback |
| 35 | // which will be run once the icon has been extracted. The icon manager will |
| 36 | // cache the results of the icon extraction so that subsequent lookups will be |
| 37 | // fast. |
| 38 | // |
| 39 | // Icon bitmaps returned should be treated as const since they may be referenced |
| 40 | // by other clients. Make a copy of the icon if you need to modify it. |
| 41 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 42 | #ifndef CHROME_BROWSER_ICON_MANAGER_H_ |
| 43 | #define CHROME_BROWSER_ICON_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | #include <map> |
| 46 | #include <set> |
| 47 | #include <string> |
| 48 | |
[email protected] | 2314403 | 2008-09-08 20:51:30 | [diff] [blame] | 49 | #include "base/hash_tables.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | #include "chrome/browser/icon_loader.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | #include "chrome/browser/cancelable_request.h" |
| 52 | |
| 53 | class SkBitmap; |
| 54 | |
| 55 | class IconManager : public IconLoader::Delegate, |
| 56 | public CancelableRequestProvider { |
| 57 | public: |
| 58 | IconManager(); |
| 59 | ~IconManager(); |
| 60 | |
| 61 | // 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 | // it via 'LoadIcon'. The returned bitmap is owned by the IconManager and must |
| 64 | // not be free'd by the caller. If the caller needs to modify the icon, it |
| 65 | // must make a copy and modify the copy. |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 66 | SkBitmap* LookupIcon(const FilePath& file_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | IconLoader::IconSize size); |
| 68 | |
| 69 | // Asynchronous call to lookup and return the icon associated with file. The |
| 70 | // work is done on the file thread, with the callbacks running on the UI |
| 71 | // thread. The return value is the 'request_id' that will be passed to the |
| 72 | // client in the callback. |
[email protected] | e79f10f3 | 2008-11-07 16:13:31 | [diff] [blame] | 73 | // |
| 74 | // WATCH OUT: The returned bitmap pointer may be NULL if decoding failed. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | typedef CancelableRequestProvider::Handle Handle; |
| 76 | typedef Callback2<Handle, SkBitmap*>::Type IconRequestCallback; |
| 77 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 78 | Handle LoadIcon(const FilePath& file_name, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | IconLoader::IconSize size, |
| 80 | CancelableRequestConsumerBase* consumer, |
| 81 | IconRequestCallback* callback); |
| 82 | |
| 83 | // IconLoader::Delegate interface. |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 84 | virtual bool OnBitmapLoaded(IconLoader* source, SkBitmap* result); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | struct CacheKey { |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 88 | CacheKey(const FilePath& file_name, IconLoader::IconSize size); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | |
| 90 | // Used as a key in the map below, so we need this comparator. |
| 91 | bool operator<(const CacheKey &other) const; |
| 92 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 93 | FilePath file_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | IconLoader::IconSize size; |
| 95 | }; |
| 96 | |
| 97 | typedef std::map<CacheKey, SkBitmap*> IconMap; |
| 98 | IconMap icon_cache_; |
| 99 | |
| 100 | typedef CancelableRequest<IconRequestCallback> IconRequest; |
| 101 | typedef struct { |
| 102 | scoped_refptr<IconRequest> request; |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 103 | FilePath file_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | IconLoader::IconSize size; |
| 105 | } ClientRequest; |
| 106 | |
| 107 | // Asynchronous requests that have not yet been completed. |
[email protected] | 2314403 | 2008-09-08 20:51:30 | [diff] [blame] | 108 | typedef base::hash_map<IconLoader*, ClientRequest> ClientRequests; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | ClientRequests requests_; |
| 110 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 111 | DISALLOW_COPY_AND_ASSIGN(IconManager); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | }; |
| 113 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 114 | #endif // #ifndef CHROME_BROWSER_ICON_MANAGER_H_ |