[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 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 8 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <string> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 13 | #include "base/macros.h" |
pranay.kumar | 4b8db4d | 2015-04-29 11:12:04 | [diff] [blame] | 14 | #include "base/single_thread_task_runner.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 15 | #include "base/task/task_traits.h" |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 16 | #include "build/build_config.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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | //////////////////////////////////////////////////////////////////////////////// |
| 21 | // |
| 22 | // A facility to read a file containing an icon asynchronously in the IO |
[email protected] | 679facce | 2012-07-25 16:13:12 | [diff] [blame] | 23 | // thread. Returns the icon in the form of an ImageSkia. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | // |
| 25 | //////////////////////////////////////////////////////////////////////////////// |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 26 | class IconLoader { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | public: |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 28 | // An IconGroup is a class of files that all share the same icon. For all |
| 29 | // platforms but Windows, and for most files on Windows, it is the file type |
| 30 | // (e.g. all .mp3 files share an icon, all .html files share an icon). On |
| 31 | // Windows, for certain file types (.exe, .dll, etc), each file of that type |
| 32 | // is assumed to have a unique icon. In that case, each of those files is a |
| 33 | // group to itself. |
| 34 | using IconGroup = base::FilePath::StringType; |
| 35 | |
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 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 43 | // The callback invoked when an icon has been read. The parameters are: |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 44 | // - The icon that was loaded (IsEmpty() will be true on failure to load). |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 45 | // - The determined group from the original requested path. |
| 46 | using IconLoadedCallback = |
Dana Fried | fa14d5f | 2019-04-21 20:49:36 | [diff] [blame] | 47 | base::OnceCallback<void(gfx::Image, const IconGroup&)>; |
[email protected] | 135fd3b6 | 2009-12-16 01:07:08 | [diff] [blame] | 48 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 49 | // Creates an IconLoader, which owns itself. If the IconLoader might outlive |
| 50 | // the caller, be sure to use a weak pointer in the |callback|. |
| 51 | static IconLoader* Create(const base::FilePath& file_path, |
| 52 | IconSize size, |
| 53 | IconLoadedCallback callback); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 55 | // Starts the process of reading the icon. When the reading of the icon is |
| 56 | // complete, the IconLoadedCallback callback will be fulfilled, and the |
| 57 | // IconLoader will delete itself. |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 58 | void Start(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | |
| 60 | private: |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 61 | IconLoader(const base::FilePath& file_path, |
| 62 | IconSize size, |
| 63 | IconLoadedCallback callback); |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 64 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 65 | ~IconLoader(); |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 66 | |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 67 | // Given a file path, get the group for the given file. |
| 68 | static IconGroup GroupForFilepath(const base::FilePath& file_path); |
[email protected] | 681b4b8 | 2013-04-09 23:34:21 | [diff] [blame] | 69 | |
avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 70 | // The TaskRunner that ReadIcon() must be called on. |
| 71 | static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner(); |
[email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 72 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 73 | void ReadGroup(); |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 74 | void ReadIcon(); |
Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 75 | #if defined(OS_WIN) |
| 76 | // Reads an icon in a sandboxed service. Use this when the file itself must |
| 77 | // be parsed. |
| 78 | void ReadIconInSandbox(); |
| 79 | #endif |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 80 | |
Gabriel Charette | 5338185 | 2020-03-02 15:27:20 | [diff] [blame] | 81 | // The traits of the tasks posted to base::ThreadPool by this class. These |
| 82 | // operations may block, because they are fetching icons from the disk, yet |
| 83 | // the result will be seen by the user so they should be prioritized |
| 84 | // accordingly. |
avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 85 | static constexpr base::TaskTraits traits() { |
Gabriel Charette | 5338185 | 2020-03-02 15:27:20 | [diff] [blame] | 86 | return {base::MayBlock(), base::TaskPriority::USER_VISIBLE}; |
avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 87 | } |
| 88 | |
pranay.kumar | 4b8db4d | 2015-04-29 11:12:04 | [diff] [blame] | 89 | // The task runner object of the thread in which we notify the delegate. |
| 90 | scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_; |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 91 | |
[email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 92 | base::FilePath file_path_; |
| 93 | |
avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 94 | IconGroup group_; |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 95 | |
Daniel Bratell | 71e14370 | 2017-10-11 11:18:26 | [diff] [blame] | 96 | #if !defined(OS_ANDROID) |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 97 | IconSize icon_size_; |
Daniel Bratell | 71e14370 | 2017-10-11 11:18:26 | [diff] [blame] | 98 | #endif // !defined(OS_ANDROID) |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 99 | |
avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 100 | IconLoadedCallback callback_; |
[email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 101 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(IconLoader); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | }; |
| 104 | |
[email protected] | 7ced67af | 2009-04-14 23:25:16 | [diff] [blame] | 105 | #endif // CHROME_BROWSER_ICON_LOADER_H_ |