blob: 9b268544b3249c178bd4669eb594a316c5dbe675 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.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//
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
42#ifndef CHROME_BROWSER_ICON_MANAGER_H__
43#define CHROME_BROWSER_ICON_MANAGER_H__
44
45#include <hash_map>
46#include <map>
47#include <set>
48#include <string>
49
50#include "chrome/browser/icon_loader.h"
51
52#include "chrome/browser/cancelable_request.h"
53
54class SkBitmap;
55
56class IconManager : public IconLoader::Delegate,
57 public CancelableRequestProvider {
58public:
59 IconManager();
60 ~IconManager();
61
62 // Synchronous call to examine the internal caches for the icon. Returns the
63 // icon if we have already loaded it, NULL if we don't have it and must load
64 // it via 'LoadIcon'. The returned bitmap is owned by the IconManager and must
65 // not be free'd by the caller. If the caller needs to modify the icon, it
66 // must make a copy and modify the copy.
67 SkBitmap* LookupIcon(const std::wstring& file_name,
68 IconLoader::IconSize size);
69
70 // Asynchronous call to lookup and return the icon associated with file. The
71 // work is done on the file thread, with the callbacks running on the UI
72 // thread. The return value is the 'request_id' that will be passed to the
73 // client in the callback.
74 typedef CancelableRequestProvider::Handle Handle;
75 typedef Callback2<Handle, SkBitmap*>::Type IconRequestCallback;
76
77 Handle LoadIcon(const std::wstring& file_name,
78 IconLoader::IconSize size,
79 CancelableRequestConsumerBase* consumer,
80 IconRequestCallback* callback);
81
82 // IconLoader::Delegate interface.
83 virtual bool OnSkBitmapLoaded(IconLoader* source, SkBitmap* result);
84 virtual bool OnHICONLoaded(IconLoader* source,
85 HICON small_icon,
86 HICON large_icon);
87
88private:
89 struct CacheKey {
90 CacheKey(std::wstring file_name, IconLoader::IconSize size);
91
92 // Used as a key in the map below, so we need this comparator.
93 bool operator<(const CacheKey &other) const;
94
95 std::wstring file_name;
96 IconLoader::IconSize size;
97 };
98
99 typedef std::map<CacheKey, SkBitmap*> IconMap;
100 IconMap icon_cache_;
101
102 typedef CancelableRequest<IconRequestCallback> IconRequest;
103 typedef struct {
104 scoped_refptr<IconRequest> request;
105 std::wstring file_name;
106 IconLoader::IconSize size;
107 } ClientRequest;
108
109 // Asynchronous requests that have not yet been completed.
110 typedef stdext::hash_map<IconLoader*, ClientRequest> ClientRequests;
111 ClientRequests requests_;
112
113 DISALLOW_EVIL_CONSTRUCTORS(IconManager);
114};
115
license.botbf09a502008-08-24 00:55:55116#endif // #ifndef CHROME_BROWSER_ICON_MANAGER_H__