Kristian Monsen | dc0f95d | 2011-06-09 11:47:42 +0100 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_ICON_LOADER_H_ |
| 6 | #define CHROME_BROWSER_ICON_LOADER_H_ |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 7 | #pragma once |
| 8 | |
| 9 | #include "build/build_config.h" |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | |
| 13 | #include "base/basictypes.h" |
Kristian Monsen | ddb351d | 2011-06-28 21:49:31 +0100 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
Ben Murdoch | 513209b | 2010-11-18 18:32:45 +0000 | [diff] [blame] | 16 | #include "base/message_loop_proxy.h" |
Kristian Monsen | dc0f95d | 2011-06-09 11:47:42 +0100 | [diff] [blame] | 17 | #include "ui/gfx/image.h" |
Ben Murdoch | 513209b | 2010-11-18 18:32:45 +0000 | [diff] [blame] | 18 | |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 19 | #if defined(TOOLKIT_USES_GTK) |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 20 | #include "base/file_path.h" |
Iain Merrick | 3345a68 | 2010-10-19 14:37:37 +0100 | [diff] [blame] | 21 | #endif |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 22 | |
| 23 | #if defined(OS_WIN) |
| 24 | // On Windows, we group files by their extension, with several exceptions: |
| 25 | // .dll, .exe, .ico. See IconManager.h for explanation. |
| 26 | typedef std::wstring IconGroupID; |
| 27 | #elif defined(OS_POSIX) |
| 28 | // On POSIX, we group files by MIME type. |
| 29 | typedef std::string IconGroupID; |
| 30 | #endif |
| 31 | |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 32 | //////////////////////////////////////////////////////////////////////////////// |
| 33 | // |
| 34 | // A facility to read a file containing an icon asynchronously in the IO |
| 35 | // thread. Returns the icon in the form of an SkBitmap. |
| 36 | // |
| 37 | //////////////////////////////////////////////////////////////////////////////// |
| 38 | class IconLoader : public base::RefCountedThreadSafe<IconLoader> { |
| 39 | public: |
| 40 | enum IconSize { |
| 41 | SMALL = 0, // 16x16 |
| 42 | NORMAL, // 32x32 |
| 43 | LARGE |
| 44 | }; |
| 45 | |
| 46 | class Delegate { |
| 47 | public: |
| 48 | // Invoked when an icon has been read. |source| is the IconLoader. If the |
| 49 | // icon has been successfully loaded, result is non-null. This method must |
| 50 | // return true if it is taking ownership of the returned bitmap. |
Kristian Monsen | dc0f95d | 2011-06-09 11:47:42 +0100 | [diff] [blame] | 51 | virtual bool OnImageLoaded(IconLoader* source, gfx::Image* result) = 0; |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 52 | |
| 53 | protected: |
| 54 | virtual ~Delegate() {} |
| 55 | }; |
| 56 | |
| 57 | IconLoader(const IconGroupID& group, IconSize size, Delegate* delegate); |
| 58 | |
| 59 | // Start reading the icon on the file thread. |
| 60 | void Start(); |
| 61 | |
| 62 | private: |
| 63 | friend class base::RefCountedThreadSafe<IconLoader>; |
| 64 | |
| 65 | virtual ~IconLoader(); |
| 66 | |
| 67 | void ReadIcon(); |
| 68 | |
| 69 | void NotifyDelegate(); |
| 70 | |
| 71 | // The message loop object of the thread in which we notify the delegate. |
Ben Murdoch | 513209b | 2010-11-18 18:32:45 +0000 | [diff] [blame] | 72 | scoped_refptr<base::MessageLoopProxy> target_message_loop_; |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 73 | |
| 74 | IconGroupID group_; |
| 75 | |
| 76 | IconSize icon_size_; |
| 77 | |
Kristian Monsen | dc0f95d | 2011-06-09 11:47:42 +0100 | [diff] [blame] | 78 | scoped_ptr<gfx::Image> image_; |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 79 | |
| 80 | Delegate* delegate_; |
| 81 | |
| 82 | #if defined(TOOLKIT_USES_GTK) |
| 83 | // On X11 we use gdk's pixbuf loader, which has to execute on the UI |
| 84 | // thread, so we only read the file on the background thread and parse it |
| 85 | // on the main thread. |
| 86 | void ParseIcon(); |
| 87 | FilePath filename_; |
| 88 | std::string icon_data_; |
| 89 | #endif |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(IconLoader); |
| 92 | }; |
| 93 | |
| 94 | #endif // CHROME_BROWSER_ICON_LOADER_H_ |