1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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_
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/macros.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/task/task_traits.h"
16 #include "build/build_config.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "ui/gfx/image/image.h"
20 ////////////////////////////////////////////////////////////////////////////////
22 // A facility to read a file containing an icon asynchronously in the IO
23 // thread. Returns the icon in the form of an ImageSkia.
25 ////////////////////////////////////////////////////////////////////////////////
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
34 using IconGroup = base::FilePath::StringType;
39 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported
40 ALL, // All sizes available
43 // The callback invoked when an icon has been read. The parameters are:
44 // - The icon that was loaded (IsEmpty() will be true on failure to load).
45 // - The determined group from the original requested path.
46 using IconLoadedCallback =
47 base::OnceCallback<void(gfx::Image, const IconGroup&)>;
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,
53 IconLoadedCallback callback);
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.
61 IconLoader(const base::FilePath& file_path,
63 IconLoadedCallback callback);
67 // Given a file path, get the group for the given file.
68 static IconGroup GroupForFilepath(const base::FilePath& file_path);
70 // The TaskRunner that ReadIcon() must be called on.
71 static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();
76 // Reads an icon in a sandboxed service. Use this when the file itself must
78 void ReadIconInSandbox();
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
85 static constexpr base::TaskTraits traits() {
86 return {base::MayBlock(), base::TaskPriority::USER_VISIBLE};
89 // The task runner object of the thread in which we notify the delegate.
90 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
92 base::FilePath file_path_;
96 #if !defined(OS_ANDROID)
98 #endif // !defined(OS_ANDROID)
100 IconLoadedCallback callback_;
102 DISALLOW_COPY_AND_ASSIGN(IconLoader);
105 #endif // CHROME_BROWSER_ICON_LOADER_H_