blob: d6e17d43c29700e46038658152a70c7dd1ef540c [file] [log] [blame]
[email protected]4c03b2e92012-01-03 19:36:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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//
[email protected]0f38ceae2009-05-08 19:01:0228// POSIX files don't have associated icons. We query the OS by the file's
29// mime type.
30//
initial.commit09911bf2008-07-26 23:55:2931// The IconManager can be queried in two ways:
32// 1. A quick, synchronous check of its caches which does not touch the disk:
33// IconManager::LookupIcon()
34// 2. An asynchronous icon load from a file on the file thread:
35// IconManager::LoadIcon()
36//
37// When using the second (asychronous) method, callers must supply a callback
38// which will be run once the icon has been extracted. The icon manager will
39// cache the results of the icon extraction so that subsequent lookups will be
40// fast.
41//
42// Icon bitmaps returned should be treated as const since they may be referenced
43// by other clients. Make a copy of the icon if you need to modify it.
44
[email protected]7ced67af2009-04-14 23:25:1645#ifndef CHROME_BROWSER_ICON_MANAGER_H_
46#define CHROME_BROWSER_ICON_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:2947
initial.commit09911bf2008-07-26 23:55:2948#include <map>
initial.commit09911bf2008-07-26 23:55:2949
[email protected]0f38ceae2009-05-08 19:01:0250#include "chrome/browser/icon_loader.h"
[email protected]c1896982012-12-05 20:26:1751#include "chrome/common/cancelable_task_tracker.h"
[email protected]f08e0512011-06-13 18:10:4452#include "ui/gfx/image/image.h"
initial.commit09911bf2008-07-26 23:55:2953
[email protected]0f38ceae2009-05-08 19:01:0254class FilePath;
initial.commit09911bf2008-07-26 23:55:2955
[email protected]c1896982012-12-05 20:26:1756class IconManager : public IconLoader::Delegate {
[email protected]0f38ceae2009-05-08 19:01:0257 public:
initial.commit09911bf2008-07-26 23:55:2958 IconManager();
[email protected]3690ebe02011-05-25 09:08:1959 virtual ~IconManager();
initial.commit09911bf2008-07-26 23:55:2960
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]c1896982012-12-05 20:26:1766 gfx::Image* LookupIcon(const FilePath& file_name, IconLoader::IconSize size);
initial.commit09911bf2008-07-26 23:55:2967
[email protected]c1896982012-12-05 20:26:1768 typedef base::Callback<void(gfx::Image*)> IconRequestCallback;
initial.commit09911bf2008-07-26 23:55:2969
[email protected]4118ab92009-06-17 00:43:4370 // Asynchronous call to lookup and return the icon associated with file. The
[email protected]c1896982012-12-05 20:26:1771 // work is done on the file thread, with the callbacks running on the thread
72 // this function is called.
[email protected]4118ab92009-06-17 00:43:4373 //
[email protected]c1896982012-12-05 20:26:1774 // Note:
75 // 1. This does *not* check the cache.
76 // 2. The returned bitmap pointer is *not* owned by callback. So callback
77 // should never keep it or delete it.
78 // 3. The gfx::Image pointer passed to the callback may be NULL if decoding
79 // failed.
80 CancelableTaskTracker::TaskId LoadIcon(const FilePath& file_name,
81 IconLoader::IconSize size,
82 const IconRequestCallback& callback,
83 CancelableTaskTracker* tracker);
initial.commit09911bf2008-07-26 23:55:2984
85 // IconLoader::Delegate interface.
[email protected]c1896982012-12-05 20:26:1786 virtual bool OnImageLoaded(IconLoader* loader, gfx::Image* result) OVERRIDE;
initial.commit09911bf2008-07-26 23:55:2987
[email protected]0f38ceae2009-05-08 19:01:0288 // Get the identifying string for the given file. The implementation
89 // is in icon_manager_[platform].cc.
90 static IconGroupID GetGroupIDFromFilepath(const FilePath& path);
91
92 private:
initial.commit09911bf2008-07-26 23:55:2993 struct CacheKey {
[email protected]0f38ceae2009-05-08 19:01:0294 CacheKey(const IconGroupID& group, IconLoader::IconSize size);
initial.commit09911bf2008-07-26 23:55:2995
96 // Used as a key in the map below, so we need this comparator.
97 bool operator<(const CacheKey &other) const;
98
[email protected]0f38ceae2009-05-08 19:01:0299 IconGroupID group;
initial.commit09911bf2008-07-26 23:55:29100 IconLoader::IconSize size;
101 };
102
[email protected]66171ab2011-03-03 15:50:07103 typedef std::map<CacheKey, gfx::Image*> IconMap;
initial.commit09911bf2008-07-26 23:55:29104 IconMap icon_cache_;
105
initial.commit09911bf2008-07-26 23:55:29106 // Asynchronous requests that have not yet been completed.
[email protected]38e08982010-10-22 17:28:43107 struct ClientRequest;
[email protected]0f38ceae2009-05-08 19:01:02108 typedef std::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]11f4857282009-11-13 19:56:17114#endif // CHROME_BROWSER_ICON_MANAGER_H_