[email protected] | 679facce | 2012-07-25 16:13:12 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_ICON_LOADER_H_ |
| 6 | #define CHROME_BROWSER_ICON_LOADER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <string> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | |
[email protected] | 56d01f6 | 2009-03-12 22:41:54 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | 76ae8a6 | 2013-05-10 05:34:22 | [diff] [blame] | 16 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | f08e051 | 2011-06-13 18:10:44 | [diff] [blame] | 17 | #include "ui/gfx/image/image.h" |
[email protected] | 982735c2 | 2010-10-29 13:58:57 | [diff] [blame] | 18 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 19 | #if defined(OS_WIN) |
| 20 | // On Windows, we group files by their extension, with several exceptions: |
| 21 | // .dll, .exe, .ico. See IconManager.h for explanation. |
| 22 | typedef std::wstring IconGroupID; |
| 23 | #elif defined(OS_POSIX) |
| 24 | // On POSIX, we group files by MIME type. |
| 25 | typedef std::string IconGroupID; |
| 26 | #endif |
| 27 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | //////////////////////////////////////////////////////////////////////////////// |
| 29 | // |
| 30 | // A facility to read a file containing an icon asynchronously in the IO |
[email protected] | 679facce | 2012-07-25 16:13:12 | [diff] [blame] | 31 | // thread. Returns the icon in the form of an ImageSkia. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | // |
| 33 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 34 | class IconLoader : public base::RefCountedThreadSafe<IconLoader> { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | enum IconSize { |
| 37 | SMALL = 0, // 16x16 |
| 38 | NORMAL, // 32x32 |
[email protected] | 955c37b | 2011-06-08 21:26:30 | [diff] [blame] | 39 | LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported |
| 40 | ALL, // All sizes available |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class Delegate { |
| 44 | public: |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 45 | // Invoked when an icon group has been read, but before the icon data |
| 46 | // is read. If the icon is already cached, this method should call and |
| 47 | // return the results of OnImageLoaded with the cached image. |
| 48 | virtual bool OnGroupLoaded(IconLoader* source, |
| 49 | const IconGroupID& group) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | // Invoked when an icon has been read. |source| is the IconLoader. If the |
| 51 | // icon has been successfully loaded, result is non-null. This method must |
[email protected] | 679facce | 2012-07-25 16:13:12 | [diff] [blame] | 52 | // return true if it is taking ownership of the returned image. |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 53 | virtual bool OnImageLoaded(IconLoader* source, |
| 54 | gfx::Image* result, |
| 55 | const IconGroupID& group) = 0; |
[email protected] | 135fd3b6 | 2009-12-16 01:07:08 | [diff] [blame] | 56 | |
| 57 | protected: |
| 58 | virtual ~Delegate() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | }; |
| 60 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 61 | IconLoader(const base::FilePath& file_path, |
| 62 | IconSize size, |
| 63 | Delegate* delegate); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | |
[email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 65 | // Start reading the icon on the file thread. |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 66 | void Start(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | |
| 68 | private: |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 69 | friend class base::RefCountedThreadSafe<IconLoader>; |
| 70 | |
| 71 | virtual ~IconLoader(); |
| 72 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 73 | // Get the identifying string for the given file. The implementation |
| 74 | // is in icon_loader_[platform].cc. |
| 75 | static IconGroupID ReadGroupIDFromFilepath(const base::FilePath& path); |
| 76 | |
[email protected] | 681b4b8 | 2013-04-09 23:34:21 | [diff] [blame] | 77 | // Some icons (exe's on windows) can change as they're loaded. |
| 78 | static bool IsIconMutableFromFilepath(const base::FilePath& path); |
| 79 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 80 | void ReadGroup(); |
| 81 | void OnReadGroup(); |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 82 | void ReadIcon(); |
| 83 | |
| 84 | void NotifyDelegate(); |
| 85 | |
| 86 | // The message loop object of the thread in which we notify the delegate. |
[email protected] | 982735c2 | 2010-10-29 13:58:57 | [diff] [blame] | 87 | scoped_refptr<base::MessageLoopProxy> target_message_loop_; |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 88 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 89 | base::FilePath file_path_; |
| 90 | |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 91 | IconGroupID group_; |
| 92 | |
| 93 | IconSize icon_size_; |
| 94 | |
[email protected] | 66171ab | 2011-03-03 15:50:07 | [diff] [blame] | 95 | scoped_ptr<gfx::Image> image_; |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 96 | |
| 97 | Delegate* delegate_; |
| 98 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 99 | DISALLOW_COPY_AND_ASSIGN(IconLoader); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 100 | }; |
| 101 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 102 | #endif // CHROME_BROWSER_ICON_LOADER_H_ |