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