blob: 26bd595a89b4652f3bfe2c077e773b292e4be90a [file] [log] [blame]
[email protected]679facce2012-07-25 16:13:121// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]7ced67af2009-04-14 23:25:165#ifndef CHROME_BROWSER_ICON_LOADER_H_
6#define CHROME_BROWSER_ICON_LOADER_H_
initial.commit09911bf2008-07-26 23:55:297
dcheng4af48582016-04-19 00:29:358#include <memory>
initial.commit09911bf2008-07-26 23:55:299#include <string>
initial.commit09911bf2008-07-26 23:55:2910
avif0a7b5b812016-12-17 19:01:3111#include "base/callback.h"
[email protected]bc0147b2013-04-03 20:50:5912#include "base/files/file_path.h"
avi6846aef2015-12-26 01:09:3813#include "base/macros.h"
pranay.kumar4b8db4d2015-04-29 11:12:0414#include "base/single_thread_task_runner.h"
Gabriel Charette44db1422018-08-06 11:19:3315#include "base/task/task_traits.h"
dcheng4af48582016-04-19 00:29:3516#include "build/build_config.h"
[email protected]b3b6a372014-03-12 01:48:0417#include "content/public/browser/browser_thread.h"
[email protected]f08e0512011-06-13 18:10:4418#include "ui/gfx/image/image.h"
[email protected]982735c22010-10-29 13:58:5719
initial.commit09911bf2008-07-26 23:55:2920////////////////////////////////////////////////////////////////////////////////
21//
22// A facility to read a file containing an icon asynchronously in the IO
[email protected]679facce2012-07-25 16:13:1223// thread. Returns the icon in the form of an ImageSkia.
initial.commit09911bf2008-07-26 23:55:2924//
25////////////////////////////////////////////////////////////////////////////////
avif0a7b5b812016-12-17 19:01:3126class IconLoader {
initial.commit09911bf2008-07-26 23:55:2927 public:
avi381f719f2016-12-16 00:05:0228 // 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
33 // group to itself.
34 using IconGroup = base::FilePath::StringType;
35
initial.commit09911bf2008-07-26 23:55:2936 enum IconSize {
37 SMALL = 0, // 16x16
38 NORMAL, // 32x32
[email protected]955c37b2011-06-08 21:26:3039 LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported
40 ALL, // All sizes available
initial.commit09911bf2008-07-26 23:55:2941 };
42
avif0a7b5b812016-12-17 19:01:3143 // The callback invoked when an icon has been read. The parameters are:
Dana Friedfa14d5f2019-04-21 20:49:3644 // - The icon that was loaded (IsEmpty() will be true on failure to load).
avif0a7b5b812016-12-17 19:01:3145 // - The determined group from the original requested path.
46 using IconLoadedCallback =
Dana Friedfa14d5f2019-04-21 20:49:3647 base::OnceCallback<void(gfx::Image, const IconGroup&)>;
[email protected]135fd3b62009-12-16 01:07:0848
avif0a7b5b812016-12-17 19:01:3149 // 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,
52 IconSize size,
53 IconLoadedCallback callback);
initial.commit09911bf2008-07-26 23:55:2954
avif0a7b5b812016-12-17 19:01:3155 // 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.
[email protected]0f38ceae2009-05-08 19:01:0258 void Start();
initial.commit09911bf2008-07-26 23:55:2959
60 private:
avif0a7b5b812016-12-17 19:01:3161 IconLoader(const base::FilePath& file_path,
62 IconSize size,
63 IconLoadedCallback callback);
[email protected]e6e6ba42009-11-07 01:56:1964
avif0a7b5b812016-12-17 19:01:3165 ~IconLoader();
[email protected]e6e6ba42009-11-07 01:56:1966
avi381f719f2016-12-16 00:05:0267 // Given a file path, get the group for the given file.
68 static IconGroup GroupForFilepath(const base::FilePath& file_path);
[email protected]681b4b82013-04-09 23:34:2169
avif4d431c2017-06-22 23:30:5370 // The TaskRunner that ReadIcon() must be called on.
71 static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();
[email protected]b3b6a372014-03-12 01:48:0472
[email protected]bc0147b2013-04-03 20:50:5973 void ReadGroup();
[email protected]0f38ceae2009-05-08 19:01:0274 void ReadIcon();
Alex Gougha4160b852020-06-23 00:21:2975#if defined(OS_WIN)
76 // Reads an icon in a sandboxed service. Use this when the file itself must
77 // be parsed.
78 void ReadIconInSandbox();
79#endif
[email protected]0f38ceae2009-05-08 19:01:0280
Gabriel Charette53381852020-03-02 15:27:2081 // 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
84 // accordingly.
avif4d431c2017-06-22 23:30:5385 static constexpr base::TaskTraits traits() {
Gabriel Charette53381852020-03-02 15:27:2086 return {base::MayBlock(), base::TaskPriority::USER_VISIBLE};
avif4d431c2017-06-22 23:30:5387 }
88
pranay.kumar4b8db4d2015-04-29 11:12:0489 // The task runner object of the thread in which we notify the delegate.
90 scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
[email protected]0f38ceae2009-05-08 19:01:0291
[email protected]bc0147b2013-04-03 20:50:5992 base::FilePath file_path_;
93
avi381f719f2016-12-16 00:05:0294 IconGroup group_;
[email protected]0f38ceae2009-05-08 19:01:0295
Daniel Bratell71e143702017-10-11 11:18:2696#if !defined(OS_ANDROID)
[email protected]0f38ceae2009-05-08 19:01:0297 IconSize icon_size_;
Daniel Bratell71e143702017-10-11 11:18:2698#endif // !defined(OS_ANDROID)
[email protected]0f38ceae2009-05-08 19:01:0299
avif0a7b5b812016-12-17 19:01:31100 IconLoadedCallback callback_;
[email protected]0f38ceae2009-05-08 19:01:02101
[email protected]7ced67af2009-04-14 23:25:16102 DISALLOW_COPY_AND_ASSIGN(IconLoader);
initial.commit09911bf2008-07-26 23:55:29103};
104
[email protected]7ced67af2009-04-14 23:25:16105#endif // CHROME_BROWSER_ICON_LOADER_H_