File: icon_loader_win.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (269 lines) | stat: -rw-r--r-- 9,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/icon_loader.h"

#include <windows.h>

#include <shellapi.h>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/scoped_blocking_call.h"
#include "base/threading/thread.h"
#include "base/win/scoped_gdi_object.h"
#include "chrome/browser/win/icon_reader_service.h"
#include "chrome/services/util_win/public/mojom/util_read_icon.mojom.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "ui/display/win/dpi.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/image/image_skia.h"

namespace {
// Helper class to manage lifetime of icon reader service.
class IconLoaderHelper {
 public:
  static void ExecuteLoadIcon(
      base::FilePath filename,
      base::File file,
      chrome::mojom::IconSize size,
      float scale,
      gfx::Image default_icon,
      scoped_refptr<base::SingleThreadTaskRunner> target_task_runner,
      IconLoader::IconLoadedCallback icon_loaded_callback);

  IconLoaderHelper(base::FilePath filename,
                   chrome::mojom::IconSize size,
                   float scale,
                   gfx::Image default_icon);

  IconLoaderHelper(const IconLoaderHelper&) = delete;
  IconLoaderHelper& operator=(const IconLoaderHelper&) = delete;

 private:
  void StartReadIconRequest(base::File file);
  void OnConnectionError();
  void OnReadIconExecuted(const gfx::ImageSkia& icon);

  using IconLoaderHelperCallback =
      base::OnceCallback<void(gfx::Image image,
                              const IconLoader::IconGroup& icon_group)>;

  void set_finally(IconLoaderHelperCallback finally) {
    finally_ = std::move(finally);
  }

  mojo::Remote<chrome::mojom::UtilReadIcon> remote_read_icon_;
  base::FilePath filename_;
  chrome::mojom::IconSize size_;
  const float scale_;
  // This callback owns the object until work is done.
  IconLoaderHelperCallback finally_;
  gfx::Image default_icon_;

  SEQUENCE_CHECKER(sequence_checker_);
};

void IconLoaderHelper::ExecuteLoadIcon(
    base::FilePath filename,
    base::File file,
    chrome::mojom::IconSize size,
    float scale,
    gfx::Image default_icon,
    scoped_refptr<base::SingleThreadTaskRunner> target_task_runner,
    IconLoader::IconLoadedCallback icon_loaded_callback) {
  // Self-deleting helper manages service lifetime.
  auto helper = std::make_unique<IconLoaderHelper>(filename, size, scale,
                                                   std::move(default_icon));
  auto* helper_raw = helper.get();
  // This callback owns the helper and extinguishes itself once work is done.
  auto finally_callback = base::BindOnce(
      [](std::unique_ptr<IconLoaderHelper> helper,
         IconLoader::IconLoadedCallback icon_loaded_callback,
         scoped_refptr<base::SingleThreadTaskRunner> target_task_runner,
         gfx::Image image, const IconLoader::IconGroup& icon_group) {
        target_task_runner->PostTask(
            FROM_HERE, base::BindOnce(std::move(icon_loaded_callback),
                                      std::move(image), icon_group));
      },
      std::move(helper), std::move(icon_loaded_callback), target_task_runner);

  helper_raw->set_finally(std::move(finally_callback));
  helper_raw->StartReadIconRequest(std::move(file));
}

IconLoaderHelper::IconLoaderHelper(base::FilePath filename,
                                   chrome::mojom::IconSize size,
                                   float scale,
                                   gfx::Image default_icon)
    : filename_(filename),
      size_(size),
      scale_(scale),
      default_icon_(std::move(default_icon)) {
  remote_read_icon_ = LaunchIconReaderInstance();
  remote_read_icon_.set_disconnect_handler(base::BindOnce(
      &IconLoaderHelper::OnConnectionError, base::Unretained(this)));
}

void IconLoaderHelper::StartReadIconRequest(base::File file) {
  remote_read_icon_->ReadIcon(
      std::move(file), size_, scale_,
      base::BindOnce(&IconLoaderHelper::OnReadIconExecuted,
                     base::Unretained(this)));
}

void IconLoaderHelper::OnConnectionError() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (finally_.is_null())
    return;

  std::move(finally_).Run(std::move(default_icon_), filename_.value());
}

void IconLoaderHelper::OnReadIconExecuted(const gfx::ImageSkia& icon) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  std::wstring icon_group = filename_.value();
  if (icon.isNull()) {
    std::move(finally_).Run(std::move(default_icon_), icon_group);
  } else {
    gfx::Image image(icon);
    std::move(finally_).Run(std::move(image), icon_group);
  }
}

// Must be called in a COM context. |group| should be a file extension.
gfx::Image GetIconForFileExtension(const std::wstring& group,
                                   IconLoader::IconSize icon_size) {
  int size = 0;
  switch (icon_size) {
    case IconLoader::SMALL:
      size = SHGFI_SMALLICON;
      break;
    case IconLoader::NORMAL:
      size = 0;
      break;
    case IconLoader::LARGE:
      size = SHGFI_LARGEICON;
      break;
    default:
      NOTREACHED();
  }

  gfx::Image image;

  // Not only is GetFileInfo a blocking call, it's also known to hang
  // (crbug.com/1249943), add a ScopedBlockingCall to let the scheduler know
  // when this hangs and to explicitly label this call in tracing.
  base::ScopedBlockingCall blocking_call(FROM_HERE,
                                         base::BlockingType::MAY_BLOCK);

  SHFILEINFO file_info = {0};
  if (SHGetFileInfo(group.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info,
                    sizeof(file_info),
                    SHGFI_ICON | size | SHGFI_USEFILEATTRIBUTES)) {
    base::win::ScopedGDIObject<HICON> file_icon(file_info.hIcon);
    const SkBitmap bitmap = IconUtil::CreateSkBitmapFromHICON(file_icon.get());
    if (!bitmap.isNull()) {
      gfx::ImageSkia image_skia(
          gfx::ImageSkiaRep(bitmap, display::win::GetDPIScale()));
      image_skia.MakeThreadSafe();
      image = gfx::Image(image_skia);
    }
  }
  return image;
}

}  // namespace

// static
IconLoader::IconGroup IconLoader::GroupForFilepath(
    const base::FilePath& file_path) {
  if (file_path.MatchesExtension(L".exe") ||
      file_path.MatchesExtension(L".dll") ||
      file_path.MatchesExtension(L".ico")) {
    return file_path.value();
  }

  return file_path.Extension();
}

// static
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
  // Technically speaking, only a thread with COM is needed, not one that has
  // a COM STA. However, this is what is available for now.
  return base::ThreadPool::CreateCOMSTATaskRunner(traits());
}

void IconLoader::ReadGroup() {
  group_ = GroupForFilepath(file_path_);

  if (group_ == file_path_.value()) {
    // Calls a Windows API that parses the file so must be sandboxed.
    GetReadIconTaskRunner()->PostTask(
        FROM_HERE,
        base::BindOnce(&IconLoader::ReadIconInSandbox, base::Unretained(this)));
  } else {
    // Looks up generic icons for groups based only on the file's extension.
    GetReadIconTaskRunner()->PostTask(
        FROM_HERE,
        base::BindOnce(&IconLoader::ReadIcon, base::Unretained(this)));
  }
}

void IconLoader::ReadIcon() {
  auto image = GetIconForFileExtension(group_, icon_size_);

  target_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback_), std::move(image), group_));

  delete this;
}

void IconLoader::ReadIconInSandbox() {
  // Get default first as loader is deleted before ExecuteLoadIcon
  // completes.
  auto path = base::FilePath(group_);
  auto default_icon = GetIconForFileExtension(path.Extension(), icon_size_);

  chrome::mojom::IconSize size = chrome::mojom::IconSize::kNormal;
  switch (icon_size_) {
    case IconLoader::SMALL:
      size = chrome::mojom::IconSize::kSmall;
      break;
    case IconLoader::NORMAL:
      size = chrome::mojom::IconSize::kNormal;
      break;
    case IconLoader::LARGE:
      size = chrome::mojom::IconSize::kLarge;
      break;
    default:
      NOTREACHED();
  }

  base::File file;
  file.Initialize(path, base::File::FLAG_READ |
                            base::File::FLAG_WIN_SHARE_DELETE |
                            base::File::FLAG_OPEN);
  if (file.IsValid()) {
    target_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&IconLoaderHelper::ExecuteLoadIcon, std::move(path),
                       std::move(file), size, scale_, std::move(default_icon),
                       target_task_runner_, std::move(callback_)));
  } else {
    target_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback_), std::move(default_icon),
                                  path.value()));
  }
  delete this;
}