blob: 3505aae720fcc1ba787a6dc25ba83dcd38fb2d5a [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#ifndef CHROME_BROWSER_ICON_LOADER_H__
6#define CHROME_BROWSER_ICON_LOADER_H__
7
initial.commit09911bf2008-07-26 23:55:298#include <string>
9#include <windows.h>
10
[email protected]56d01f62009-03-12 22:41:5411#include "base/basictypes.h"
12
initial.commit09911bf2008-07-26 23:55:2913namespace {
14class IconLoaderProcessor;
15}
16
17class SkBitmap;
18
19////////////////////////////////////////////////////////////////////////////////
20//
21// A facility to read a file containing an icon asynchronously in the IO
22// thread. Clients has the option to get the icon in the form of two HICON
23// handles (one for a small icon size and one for a large icon) or in the form
24// of an SkBitmap.
25//
26// This class currently only supports reading .ico files. Please extend as
27// needed.
28//
29////////////////////////////////////////////////////////////////////////////////
30class IconLoader {
31 public:
32 enum IconType {
33 SK_BITMAP = 0,
34 WINDOWS_HICON
35 };
36
37 enum IconSize {
38 SMALL = 0, // 16x16
39 NORMAL, // 32x32
40 LARGE
41 };
42
43 class Delegate {
44 public:
45 // Invoked when an icon has been read. |source| is the IconLoader. If the
46 // icon has been successfully loaded, result is non-null. This method must
47 // return true if it is taking ownership of the returned bitmap.
48 //
49 // This method is only called when GetIconType() above returns SK_BITMAP.
50 virtual bool OnSkBitmapLoaded(IconLoader* source, SkBitmap* result) = 0;
51
52 // Invoked when the small and the large HICONS have been read. |source| is
53 // the IconLoader. If the small icon has been successfully loaded,
54 // small_icon is non-null. The same applies to the large_icon. This method
55 // must return true if it is taking ownership of the returned icon handles.
56 //
57 // This method is only called when GetIconType() above returns
58 // WINDOWS_HICON.
59 virtual bool OnHICONLoaded(IconLoader* source,
60 HICON small_icon,
61 HICON large_icon) = 0;
62 };
63
64 // Create a new IconLoader that loads the icon from the data at contained in
65 // the file at |path|. |icon_type| specifies which format to generate and
66 // which method is invoked on the |delegate| once the icon was loaded.
67 static IconLoader* CreateIconLoaderForFile(const std::wstring& path,
68 IconType icon_type,
69 Delegate* delegate);
70
71 // Create a new IconLoader that loads the icon in the resource of the file
72 // at |path|. This is used with .exe/.dll files.
73 // Note that this generates a SkBitmap (and consequently OnSkBitmapLoaded is
74 // invoked on the delegate once the load has completed).
75 static IconLoader* CreateIconLoaderForFileResource(const std::wstring& path,
76 IconSize size,
77 Delegate* delegate);
78
79 ~IconLoader();
80
81 // Start the read operation
82 void Start();
83
84 // Cancel the read operation. The delegate will no longer be contacted. Call
85 // this method if you need to delete the delegate.
86 void Cancel();
87
88 private:
89 friend class IconLoaderProcessor;
90
91 // Use the factory methods CreateIconLoader* instead of using this constructor
92 IconLoader(const std::wstring& path,
93 IconType type,
94 bool from_resource,
95 IconSize size,
96 Delegate* delegate);
97
98 // Invoked by the processor when the file has been read and an SkBitmap
99 // object was requested.
100 bool OnLoadComplete(SkBitmap* result);
101
102 // Invoked by the processor when the file has been read and HICON handles
103 // (small and large) were requested.
104 bool OnLoadComplete(HICON small_icon, HICON large_icon);
105
106 // The path.
107 std::wstring path_;
108
109 // The delegate.
110 Delegate* delegate_;
111
112 // Whether we are loading the icon from the resource in the file (if false,
113 // the icon is simply loaded from the file content).
114 bool loading_from_resource_;
115
116 // The size of the icon that should be loaded from the file resource.
117 // Not used if loading_from_resource_ is false.
118 IconSize icon_size_;
119
120 // The type of icon that should be generated.
121 // Not used if loading_from_resource_ is true.
122 IconType icon_type_;
123
124 // The underlying object performing the read.
125 IconLoaderProcessor* processor_;
126
127 DISALLOW_EVIL_CONSTRUCTORS(IconLoader);
128};
129
130#endif // CHROME_BROWSER_ICON_LOADER_H__