File: icon_loader.h

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 (112 lines) | stat: -rw-r--r-- 4,021 bytes parent folder | download | duplicates (4)
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
// 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.

#ifndef CHROME_BROWSER_ICON_LOADER_H_
#define CHROME_BROWSER_ICON_LOADER_H_

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_traits.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
#include "ui/gfx/image/image.h"

////////////////////////////////////////////////////////////////////////////////
//
// A facility to read a file containing an icon asynchronously in the IO
// thread. Returns the icon in the form of an ImageSkia.
//
////////////////////////////////////////////////////////////////////////////////
class IconLoader {
 public:
  // An IconGroup is a class of files that all share the same icon.
#if BUILDFLAG(IS_MAC)
  // On the Mac, it's the UTType's identifier. (Apps do have unique icons, just
  // like in Windows, below, but `IconLoader` is never used to get their icons,
  // so that case isn't handled.)
  using IconGroup = std::string;
#else
  // On all other platforms except Windows, and for most files on Windows, it is
  // the file type (e.g. all .mp3 files share an icon, all .html files share an
  // icon). On Windows, for certain file types (.exe, .dll, etc), each file of
  // that type is assumed to have a unique icon. In that case, each of those
  // files is a group to itself.
  using IconGroup = base::FilePath::StringType;
#endif

  enum IconSize {
    SMALL = 0,  // 16x16
    NORMAL,     // 32x32
    LARGE,      // Windows: 32x32, Linux: 48x48, Mac: Unsupported
    ALL,        // All sizes available
  };

  // The callback invoked when an icon has been read. The parameters are:
  // - The icon that was loaded (IsEmpty() will be true on failure to load).
  // - The determined group from the original requested path.
  using IconLoadedCallback =
      base::OnceCallback<void(gfx::Image, const IconGroup&)>;

  // Starts the process of reading the icon. When the reading of the icon is
  // complete, the IconLoadedCallback callback will be fulfilled, and the
  // IconLoader will delete itself.
  static void LoadIcon(const base::FilePath& file_path,
                       IconSize size,
                       float scale,
                       IconLoadedCallback callback);

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

 private:
  IconLoader(const base::FilePath& file_path,
             IconSize size,
             float scale,
             IconLoadedCallback callback);

  ~IconLoader();

  void Start();

  // Given a file path, get the group for the given file.
  static IconGroup GroupForFilepath(const base::FilePath& file_path);

  // The TaskRunner that ReadIcon() must be called on.
  static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();

#if !BUILDFLAG(IS_CHROMEOS)
  void ReadGroup();
  void ReadIcon();
#endif
#if BUILDFLAG(IS_WIN)
  // Reads an icon in a sandboxed service. Use this when the file itself must
  // be parsed.
  void ReadIconInSandbox();
#endif

  // The traits of the tasks posted to base::ThreadPool by this class. These
  // operations may block, because they are fetching icons from the disk, yet
  // the result will be seen by the user so they should be prioritized
  // accordingly. They should not however block shutdown if long running.
  static constexpr base::TaskTraits traits() {
    return {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
            base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
  }

  // The task runner object of the thread in which we notify the delegate.
  scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;

  base::FilePath file_path_;

  IconGroup group_;

#if !BUILDFLAG(IS_ANDROID)
  IconSize icon_size_;
#endif  // !BUILDFLAG(IS_ANDROID)
  const float scale_;
  IconLoadedCallback callback_;
};

#endif  // CHROME_BROWSER_ICON_LOADER_H_