initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef CHROME_BROWSER_ICON_LOADER_H__ |
| 31 | #define CHROME_BROWSER_ICON_LOADER_H__ |
| 32 | |
| 33 | #include "base/logging.h" |
| 34 | #include <string> |
| 35 | #include <windows.h> |
| 36 | |
| 37 | namespace { |
| 38 | class IconLoaderProcessor; |
| 39 | } |
| 40 | |
| 41 | class SkBitmap; |
| 42 | |
| 43 | //////////////////////////////////////////////////////////////////////////////// |
| 44 | // |
| 45 | // A facility to read a file containing an icon asynchronously in the IO |
| 46 | // thread. Clients has the option to get the icon in the form of two HICON |
| 47 | // handles (one for a small icon size and one for a large icon) or in the form |
| 48 | // of an SkBitmap. |
| 49 | // |
| 50 | // This class currently only supports reading .ico files. Please extend as |
| 51 | // needed. |
| 52 | // |
| 53 | //////////////////////////////////////////////////////////////////////////////// |
| 54 | class IconLoader { |
| 55 | public: |
| 56 | enum IconType { |
| 57 | SK_BITMAP = 0, |
| 58 | WINDOWS_HICON |
| 59 | }; |
| 60 | |
| 61 | enum IconSize { |
| 62 | SMALL = 0, // 16x16 |
| 63 | NORMAL, // 32x32 |
| 64 | LARGE |
| 65 | }; |
| 66 | |
| 67 | class Delegate { |
| 68 | public: |
| 69 | // Invoked when an icon has been read. |source| is the IconLoader. If the |
| 70 | // icon has been successfully loaded, result is non-null. This method must |
| 71 | // return true if it is taking ownership of the returned bitmap. |
| 72 | // |
| 73 | // This method is only called when GetIconType() above returns SK_BITMAP. |
| 74 | virtual bool OnSkBitmapLoaded(IconLoader* source, SkBitmap* result) = 0; |
| 75 | |
| 76 | // Invoked when the small and the large HICONS have been read. |source| is |
| 77 | // the IconLoader. If the small icon has been successfully loaded, |
| 78 | // small_icon is non-null. The same applies to the large_icon. This method |
| 79 | // must return true if it is taking ownership of the returned icon handles. |
| 80 | // |
| 81 | // This method is only called when GetIconType() above returns |
| 82 | // WINDOWS_HICON. |
| 83 | virtual bool OnHICONLoaded(IconLoader* source, |
| 84 | HICON small_icon, |
| 85 | HICON large_icon) = 0; |
| 86 | }; |
| 87 | |
| 88 | // Create a new IconLoader that loads the icon from the data at contained in |
| 89 | // the file at |path|. |icon_type| specifies which format to generate and |
| 90 | // which method is invoked on the |delegate| once the icon was loaded. |
| 91 | static IconLoader* CreateIconLoaderForFile(const std::wstring& path, |
| 92 | IconType icon_type, |
| 93 | Delegate* delegate); |
| 94 | |
| 95 | // Create a new IconLoader that loads the icon in the resource of the file |
| 96 | // at |path|. This is used with .exe/.dll files. |
| 97 | // Note that this generates a SkBitmap (and consequently OnSkBitmapLoaded is |
| 98 | // invoked on the delegate once the load has completed). |
| 99 | static IconLoader* CreateIconLoaderForFileResource(const std::wstring& path, |
| 100 | IconSize size, |
| 101 | Delegate* delegate); |
| 102 | |
| 103 | ~IconLoader(); |
| 104 | |
| 105 | // Start the read operation |
| 106 | void Start(); |
| 107 | |
| 108 | // Cancel the read operation. The delegate will no longer be contacted. Call |
| 109 | // this method if you need to delete the delegate. |
| 110 | void Cancel(); |
| 111 | |
| 112 | private: |
| 113 | friend class IconLoaderProcessor; |
| 114 | |
| 115 | // Use the factory methods CreateIconLoader* instead of using this constructor |
| 116 | IconLoader(const std::wstring& path, |
| 117 | IconType type, |
| 118 | bool from_resource, |
| 119 | IconSize size, |
| 120 | Delegate* delegate); |
| 121 | |
| 122 | // Invoked by the processor when the file has been read and an SkBitmap |
| 123 | // object was requested. |
| 124 | bool OnLoadComplete(SkBitmap* result); |
| 125 | |
| 126 | // Invoked by the processor when the file has been read and HICON handles |
| 127 | // (small and large) were requested. |
| 128 | bool OnLoadComplete(HICON small_icon, HICON large_icon); |
| 129 | |
| 130 | // The path. |
| 131 | std::wstring path_; |
| 132 | |
| 133 | // The delegate. |
| 134 | Delegate* delegate_; |
| 135 | |
| 136 | // Whether we are loading the icon from the resource in the file (if false, |
| 137 | // the icon is simply loaded from the file content). |
| 138 | bool loading_from_resource_; |
| 139 | |
| 140 | // The size of the icon that should be loaded from the file resource. |
| 141 | // Not used if loading_from_resource_ is false. |
| 142 | IconSize icon_size_; |
| 143 | |
| 144 | // The type of icon that should be generated. |
| 145 | // Not used if loading_from_resource_ is true. |
| 146 | IconType icon_type_; |
| 147 | |
| 148 | // The underlying object performing the read. |
| 149 | IconLoaderProcessor* processor_; |
| 150 | |
| 151 | DISALLOW_EVIL_CONSTRUCTORS(IconLoader); |
| 152 | }; |
| 153 | |
| 154 | #endif // CHROME_BROWSER_ICON_LOADER_H__ |