1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_ICON_LOADER_H_
6 #define CHROME_BROWSER_ICON_LOADER_H_
8 #include "base/files/file_path.h"
9 #include "base/functional/callback.h"
10 #include "base/task/single_thread_task_runner.h"
11 #include "base/task/task_traits.h"
12 #include "build/build_config.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "ui/gfx/image/image.h"
16 ////////////////////////////////////////////////////////////////////////////////
18 // A facility to read a file containing an icon asynchronously in the IO
19 // thread. Returns the icon in the form of an ImageSkia.
21 ////////////////////////////////////////////////////////////////////////////////
24 // An IconGroup is a class of files that all share the same icon.
26 // On the Mac, it's the UTType's identifier. (Apps do have unique icons, just
27 // like in Windows, below, but `IconLoader` is never used to get their icons,
28 // so that case isn't handled.)
29 using IconGroup = std::string;
31 // On all other platforms except Windows, and for most files on Windows, it is
32 // the file type (e.g. all .mp3 files share an icon, all .html files share an
33 // icon). On Windows, for certain file types (.exe, .dll, etc), each file of
34 // that type is assumed to have a unique icon. In that case, each of those
35 // files is a group to itself.
36 using IconGroup = base::FilePath::StringType;
42 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported
43 ALL, // All sizes available
46 // The callback invoked when an icon has been read. The parameters are:
47 // - The icon that was loaded (IsEmpty() will be true on failure to load).
48 // - The determined group from the original requested path.
49 using IconLoadedCallback =
50 base::OnceCallback<void(gfx::Image, const IconGroup&)>;
52 // Starts the process of reading the icon. When the reading of the icon is
53 // complete, the IconLoadedCallback callback will be fulfilled, and the
54 // IconLoader will delete itself.
55 static void LoadIcon(const base::FilePath& file_path,
58 IconLoadedCallback callback);
60 IconLoader(const IconLoader&) = delete;
61 IconLoader& operator=(const IconLoader&) = delete;
64 IconLoader(const base::FilePath& file_path,
67 IconLoadedCallback callback);
73 // Given a file path, get the group for the given file.
74 static IconGroup GroupForFilepath(const base::FilePath& file_path);
76 // The TaskRunner that ReadIcon() must be called on.
77 static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();
79 #if !BUILDFLAG(IS_CHROMEOS)
84 // Reads an icon in a sandboxed service. Use this when the file itself must
86 void ReadIconInSandbox();
89 // The traits of the tasks posted to base::ThreadPool by this class. These
90 // operations may block, because they are fetching icons from the disk, yet
91 // the result will be seen by the user so they should be prioritized
92 // accordingly. They should not however block shutdown if long running.
93 static constexpr base::TaskTraits traits() {
94 return {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
95 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
98 // The task runner object of the thread in which we notify the delegate.
99 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
101 base::FilePath file_path_;
105 #if !BUILDFLAG(IS_ANDROID)
107 #endif // !BUILDFLAG(IS_ANDROID)
109 IconLoadedCallback callback_;
112 #endif // CHROME_BROWSER_ICON_LOADER_H_