blob: fbd0cd117813389ee8436f817eade21b67a0a48a [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
[email protected]7ced67af2009-04-14 23:25:1642#ifndef CHROME_BROWSER_ICON_MANAGER_H_
43#define CHROME_BROWSER_ICON_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:2944
initial.commit09911bf2008-07-26 23:55:2945#include <map>
46#include <set>
47#include <string>
48
[email protected]23144032008-09-08 20:51:3049#include "base/hash_tables.h"
initial.commit09911bf2008-07-26 23:55:2950#include "chrome/browser/icon_loader.h"
initial.commit09911bf2008-07-26 23:55:2951#include "chrome/browser/cancelable_request.h"
52
53class SkBitmap;
54
55class IconManager : public IconLoader::Delegate,
56 public CancelableRequestProvider {
57public:
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]7ced67af2009-04-14 23:25:1666 SkBitmap* LookupIcon(const FilePath& file_name,
initial.commit09911bf2008-07-26 23:55:2967 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]e79f10f32008-11-07 16:13:3173 //
74 // WATCH OUT: The returned bitmap pointer may be NULL if decoding failed.
initial.commit09911bf2008-07-26 23:55:2975 typedef CancelableRequestProvider::Handle Handle;
76 typedef Callback2<Handle, SkBitmap*>::Type IconRequestCallback;
77
[email protected]7ced67af2009-04-14 23:25:1678 Handle LoadIcon(const FilePath& file_name,
initial.commit09911bf2008-07-26 23:55:2979 IconLoader::IconSize size,
80 CancelableRequestConsumerBase* consumer,
81 IconRequestCallback* callback);
82
83 // IconLoader::Delegate interface.
[email protected]46728b1f2009-05-07 20:42:2484 virtual bool OnBitmapLoaded(IconLoader* source, SkBitmap* result);
initial.commit09911bf2008-07-26 23:55:2985
86private:
87 struct CacheKey {
[email protected]7ced67af2009-04-14 23:25:1688 CacheKey(const FilePath& file_name, IconLoader::IconSize size);
initial.commit09911bf2008-07-26 23:55:2989
90 // Used as a key in the map below, so we need this comparator.
91 bool operator<(const CacheKey &other) const;
92
[email protected]7ced67af2009-04-14 23:25:1693 FilePath file_name;
initial.commit09911bf2008-07-26 23:55:2994 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]7ced67af2009-04-14 23:25:16103 FilePath file_name;
initial.commit09911bf2008-07-26 23:55:29104 IconLoader::IconSize size;
105 } ClientRequest;
106
107 // Asynchronous requests that have not yet been completed.
[email protected]23144032008-09-08 20:51:30108 typedef base::hash_map<IconLoader*, ClientRequest> ClientRequests;
initial.commit09911bf2008-07-26 23:55:29109 ClientRequests requests_;
110
[email protected]7ced67af2009-04-14 23:25:16111 DISALLOW_COPY_AND_ASSIGN(IconManager);
initial.commit09911bf2008-07-26 23:55:29112};
113
[email protected]7ced67af2009-04-14 23:25:16114#endif // #ifndef CHROME_BROWSER_ICON_MANAGER_H_