| Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
| [email protected] | 46728b1f | 2009-05-07 20:42:24 | [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 | |
| [email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 5 | #include "chrome/browser/icon_loader.h" |
| [email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 6 | |
| 7 | #include <windows.h> |
| Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 8 | |
| [email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 9 | #include <shellapi.h> |
| 10 | |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 11 | #include "base/files/file.h" |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
| Avi Drissman | 02e49e58 | 2023-01-07 01:23:18 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
| 14 | #include "base/functional/callback.h" |
| Peter Kasting | 5105d61 | 2021-02-09 22:41:42 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
| Sean Maher | e672a66 | 2023-01-09 21:42:28 | [diff] [blame] | 16 | #include "base/task/single_thread_task_runner.h" |
| Gabriel Charette | 5338185 | 2020-03-02 15:27:20 | [diff] [blame] | 17 | #include "base/task/thread_pool.h" |
| Gabriel Charette | bf383202 | 2021-09-17 22:37:08 | [diff] [blame] | 18 | #include "base/threading/scoped_blocking_call.h" |
| [email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 19 | #include "base/threading/thread.h" |
| Peter Kasting | 9d13b970 | 2025-01-31 15:15:51 | [diff] [blame] | 20 | #include "base/win/scoped_gdi_object.h" |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 21 | #include "chrome/browser/win/icon_reader_service.h" |
| 22 | #include "chrome/services/util_win/public/mojom/util_read_icon.mojom.h" |
| 23 | #include "content/public/browser/browser_task_traits.h" |
| 24 | #include "content/public/browser/browser_thread.h" |
| robliao | 18e220e8 | 2016-04-19 16:47:12 | [diff] [blame] | 25 | #include "ui/display/win/dpi.h" |
| tfarina | ebe974f0 | 2015-01-03 04:25:32 | [diff] [blame] | 26 | #include "ui/gfx/geometry/size.h" |
| [email protected] | 2b8ac34 | 2012-08-29 03:46:27 | [diff] [blame] | 27 | #include "ui/gfx/image/image_skia.h" |
| Gastón Rodríguez | 78055b46 | 2025-10-13 17:05:27 | [diff] [blame] | 28 | #include "ui/gfx/win/icon_util.h" |
| [email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 29 | |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 30 | namespace { |
| 31 | // Helper class to manage lifetime of icon reader service. |
| 32 | class IconLoaderHelper { |
| 33 | public: |
| 34 | static void ExecuteLoadIcon( |
| 35 | base::FilePath filename, |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 36 | base::File file, |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 37 | chrome::mojom::IconSize size, |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 38 | float scale, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 39 | gfx::Image default_icon, |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 40 | scoped_refptr<base::SingleThreadTaskRunner> target_task_runner, |
| 41 | IconLoader::IconLoadedCallback icon_loaded_callback); |
| 42 | |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 43 | IconLoaderHelper(base::FilePath filename, |
| 44 | chrome::mojom::IconSize size, |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 45 | float scale, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 46 | gfx::Image default_icon); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 47 | |
| Peter Boström | fadb175 | 2021-09-30 19:17:01 | [diff] [blame] | 48 | IconLoaderHelper(const IconLoaderHelper&) = delete; |
| 49 | IconLoaderHelper& operator=(const IconLoaderHelper&) = delete; |
| 50 | |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 51 | private: |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 52 | void StartReadIconRequest(base::File file); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 53 | void OnConnectionError(); |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 54 | void OnReadIconExecuted(const gfx::ImageSkia& icon); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 55 | |
| 56 | using IconLoaderHelperCallback = |
| 57 | base::OnceCallback<void(gfx::Image image, |
| 58 | const IconLoader::IconGroup& icon_group)>; |
| 59 | |
| 60 | void set_finally(IconLoaderHelperCallback finally) { |
| 61 | finally_ = std::move(finally); |
| 62 | } |
| 63 | |
| 64 | mojo::Remote<chrome::mojom::UtilReadIcon> remote_read_icon_; |
| 65 | base::FilePath filename_; |
| 66 | chrome::mojom::IconSize size_; |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 67 | const float scale_; |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 68 | // This callback owns the object until work is done. |
| 69 | IconLoaderHelperCallback finally_; |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 70 | gfx::Image default_icon_; |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 71 | |
| 72 | SEQUENCE_CHECKER(sequence_checker_); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | void IconLoaderHelper::ExecuteLoadIcon( |
| 76 | base::FilePath filename, |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 77 | base::File file, |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 78 | chrome::mojom::IconSize size, |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 79 | float scale, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 80 | gfx::Image default_icon, |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 81 | scoped_refptr<base::SingleThreadTaskRunner> target_task_runner, |
| 82 | IconLoader::IconLoadedCallback icon_loaded_callback) { |
| 83 | // Self-deleting helper manages service lifetime. |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 84 | auto helper = std::make_unique<IconLoaderHelper>(filename, size, scale, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 85 | std::move(default_icon)); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 86 | auto* helper_raw = helper.get(); |
| 87 | // This callback owns the helper and extinguishes itself once work is done. |
| 88 | auto finally_callback = base::BindOnce( |
| 89 | [](std::unique_ptr<IconLoaderHelper> helper, |
| 90 | IconLoader::IconLoadedCallback icon_loaded_callback, |
| 91 | scoped_refptr<base::SingleThreadTaskRunner> target_task_runner, |
| 92 | gfx::Image image, const IconLoader::IconGroup& icon_group) { |
| 93 | target_task_runner->PostTask( |
| 94 | FROM_HERE, base::BindOnce(std::move(icon_loaded_callback), |
| 95 | std::move(image), icon_group)); |
| 96 | }, |
| 97 | std::move(helper), std::move(icon_loaded_callback), target_task_runner); |
| 98 | |
| 99 | helper_raw->set_finally(std::move(finally_callback)); |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 100 | helper_raw->StartReadIconRequest(std::move(file)); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | IconLoaderHelper::IconLoaderHelper(base::FilePath filename, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 104 | chrome::mojom::IconSize size, |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 105 | float scale, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 106 | gfx::Image default_icon) |
| Alexander Zhirov | f51bebd | 2021-04-13 09:52:04 | [diff] [blame] | 107 | : filename_(filename), |
| 108 | size_(size), |
| 109 | scale_(scale), |
| 110 | default_icon_(std::move(default_icon)) { |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 111 | remote_read_icon_ = LaunchIconReaderInstance(); |
| 112 | remote_read_icon_.set_disconnect_handler(base::BindOnce( |
| 113 | &IconLoaderHelper::OnConnectionError, base::Unretained(this))); |
| 114 | } |
| 115 | |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 116 | void IconLoaderHelper::StartReadIconRequest(base::File file) { |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 117 | remote_read_icon_->ReadIcon( |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 118 | std::move(file), size_, scale_, |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 119 | base::BindOnce(&IconLoaderHelper::OnReadIconExecuted, |
| 120 | base::Unretained(this))); |
| 121 | } |
| 122 | |
| 123 | void IconLoaderHelper::OnConnectionError() { |
| 124 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 125 | if (finally_.is_null()) |
| 126 | return; |
| 127 | |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 128 | std::move(finally_).Run(std::move(default_icon_), filename_.value()); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 129 | } |
| 130 | |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 131 | void IconLoaderHelper::OnReadIconExecuted(const gfx::ImageSkia& icon) { |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 132 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 133 | |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 134 | std::wstring icon_group = filename_.value(); |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 135 | if (icon.isNull()) { |
| Peter Kasting | 5105d61 | 2021-02-09 22:41:42 | [diff] [blame] | 136 | std::move(finally_).Run(std::move(default_icon_), icon_group); |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 137 | } else { |
| 138 | gfx::Image image(icon); |
| Peter Kasting | 5105d61 | 2021-02-09 22:41:42 | [diff] [blame] | 139 | std::move(finally_).Run(std::move(image), icon_group); |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | // Must be called in a COM context. |group| should be a file extension. |
| Peter Kasting | 5105d61 | 2021-02-09 22:41:42 | [diff] [blame] | 144 | gfx::Image GetIconForFileExtension(const std::wstring& group, |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 145 | IconLoader::IconSize icon_size) { |
| 146 | int size = 0; |
| 147 | switch (icon_size) { |
| 148 | case IconLoader::SMALL: |
| 149 | size = SHGFI_SMALLICON; |
| 150 | break; |
| 151 | case IconLoader::NORMAL: |
| 152 | size = 0; |
| 153 | break; |
| 154 | case IconLoader::LARGE: |
| 155 | size = SHGFI_LARGEICON; |
| 156 | break; |
| 157 | default: |
| Peter Boström | 9be37efa | 2024-11-06 23:34:18 | [diff] [blame] | 158 | NOTREACHED(); |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | gfx::Image image; |
| 162 | |
| Gabriel Charette | bf383202 | 2021-09-17 22:37:08 | [diff] [blame] | 163 | // Not only is GetFileInfo a blocking call, it's also known to hang |
| 164 | // (crbug.com/1249943), add a ScopedBlockingCall to let the scheduler know |
| 165 | // when this hangs and to explicitly label this call in tracing. |
| 166 | base::ScopedBlockingCall blocking_call(FROM_HERE, |
| 167 | base::BlockingType::MAY_BLOCK); |
| 168 | |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 169 | SHFILEINFO file_info = {0}; |
| 170 | if (SHGetFileInfo(group.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info, |
| 171 | sizeof(file_info), |
| 172 | SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) { |
| Peter Kasting | 9d13b970 | 2025-01-31 15:15:51 | [diff] [blame] | 173 | base::win::ScopedGDIObject<HICON> file_icon(file_info.hIcon); |
| 174 | const SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(file_icon.get()); |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 175 | if (!bitmap.isNull()) { |
| 176 | gfx::ImageSkia image_skia( |
| 177 | gfx::ImageSkiaRep(bitmap, display::win::GetDPIScale())); |
| 178 | image_skia.MakeThreadSafe(); |
| 179 | image = gfx::Image(image_skia); |
| 180 | } |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 181 | } |
| 182 | return image; |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace |
| 186 | |
| [email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 187 | // static |
| avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 188 | IconLoader::IconGroup IconLoader::GroupForFilepath( |
| 189 | const base::FilePath& file_path) { |
| 190 | if (file_path.MatchesExtension(L".exe") || |
| 191 | file_path.MatchesExtension(L".dll") || |
| 192 | file_path.MatchesExtension(L".ico")) { |
| 193 | return file_path.value(); |
| 194 | } |
| [email protected] | 681b4b8 | 2013-04-09 23:34:21 | [diff] [blame] | 195 | |
| avi | 381f719f | 2016-12-16 00:05:02 | [diff] [blame] | 196 | return file_path.Extension(); |
| [email protected] | bc0147b | 2013-04-03 20:50:59 | [diff] [blame] | 197 | } |
| 198 | |
| [email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 199 | // static |
| avi | f4d431c | 2017-06-22 23:30:53 | [diff] [blame] | 200 | scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() { |
| 201 | // Technically speaking, only a thread with COM is needed, not one that has |
| 202 | // a COM STA. However, this is what is available for now. |
| Gabriel Charette | 5338185 | 2020-03-02 15:27:20 | [diff] [blame] | 203 | return base::ThreadPool::CreateCOMSTATaskRunner(traits()); |
| [email protected] | b3b6a37 | 2014-03-12 01:48:04 | [diff] [blame] | 204 | } |
| 205 | |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 206 | void IconLoader::ReadGroup() { |
| 207 | group_ = GroupForFilepath(file_path_); |
| 208 | |
| 209 | if (group_ == file_path_.value()) { |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 210 | // Calls a Windows API that parses the file so must be sandboxed. |
| 211 | GetReadIconTaskRunner()->PostTask( |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 212 | FROM_HERE, |
| 213 | base::BindOnce(&IconLoader::ReadIconInSandbox, base::Unretained(this))); |
| 214 | } else { |
| 215 | // Looks up generic icons for groups based only on the file's extension. |
| 216 | GetReadIconTaskRunner()->PostTask( |
| 217 | FROM_HERE, |
| 218 | base::BindOnce(&IconLoader::ReadIcon, base::Unretained(this))); |
| 219 | } |
| 220 | } |
| 221 | |
| [email protected] | 0f38ceae | 2009-05-08 19:01:02 | [diff] [blame] | 222 | void IconLoader::ReadIcon() { |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 223 | auto image = GetIconForFileExtension(group_, icon_size_); |
| [email protected] | ab6d23f | 2012-09-07 18:43:12 | [diff] [blame] | 224 | |
| avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 225 | target_task_runner_->PostTask( |
| Avi Drissman | efe4dc8 | 2018-02-23 17:55:39 | [diff] [blame] | 226 | FROM_HERE, |
| 227 | base::BindOnce(std::move(callback_), std::move(image), group_)); |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 228 | |
| avi | f0a7b5b81 | 2016-12-17 19:01:31 | [diff] [blame] | 229 | delete this; |
| [email protected] | 46728b1f | 2009-05-07 20:42:24 | [diff] [blame] | 230 | } |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 231 | |
| 232 | void IconLoader::ReadIconInSandbox() { |
| Alex Gough | 57ff3c5 | 2020-06-23 01:42:43 | [diff] [blame] | 233 | // Get default first as loader is deleted before ExecuteLoadIcon |
| 234 | // completes. |
| 235 | auto path = base::FilePath(group_); |
| 236 | auto default_icon = GetIconForFileExtension(path.Extension(), icon_size_); |
| 237 | |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 238 | chrome::mojom::IconSize size = chrome::mojom::IconSize::kNormal; |
| 239 | switch (icon_size_) { |
| 240 | case IconLoader::SMALL: |
| 241 | size = chrome::mojom::IconSize::kSmall; |
| 242 | break; |
| 243 | case IconLoader::NORMAL: |
| 244 | size = chrome::mojom::IconSize::kNormal; |
| 245 | break; |
| 246 | case IconLoader::LARGE: |
| 247 | size = chrome::mojom::IconSize::kLarge; |
| 248 | break; |
| 249 | default: |
| Peter Boström | 9be37efa | 2024-11-06 23:34:18 | [diff] [blame] | 250 | NOTREACHED(); |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 251 | } |
| 252 | |
| James Forshaw | 7148bc3 | 2024-06-04 17:41:18 | [diff] [blame] | 253 | base::File file; |
| 254 | file.Initialize(path, base::File::FLAG_READ | |
| 255 | base::File::FLAG_WIN_SHARE_DELETE | |
| 256 | base::File::FLAG_OPEN); |
| 257 | if (file.IsValid()) { |
| 258 | target_task_runner_->PostTask( |
| 259 | FROM_HERE, |
| 260 | base::BindOnce(&IconLoaderHelper::ExecuteLoadIcon, std::move(path), |
| 261 | std::move(file), size, scale_, std::move(default_icon), |
| 262 | target_task_runner_, std::move(callback_))); |
| 263 | } else { |
| 264 | target_task_runner_->PostTask( |
| 265 | FROM_HERE, base::BindOnce(std::move(callback_), std::move(default_icon), |
| 266 | path.value())); |
| 267 | } |
| Alex Gough | a4160b85 | 2020-06-23 00:21:29 | [diff] [blame] | 268 | delete this; |
| 269 | } |