icon_loader.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.
  4. #ifndef CHROME_BROWSER_ICON_LOADER_H_
  5. #define CHROME_BROWSER_ICON_LOADER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/macros.h"
  11. #include "base/single_thread_task_runner.h"
  12. #include "base/task_scheduler/task_traits.h"
  13. #include "build/build_config.h"
  14. #include "content/public/browser/browser_thread.h"
  15. #include "ui/gfx/image/image.h"
  16. ////////////////////////////////////////////////////////////////////////////////
  17. //
  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.
  20. //
  21. ////////////////////////////////////////////////////////////////////////////////
  22. class IconLoader {
  23. public:
  24. // An IconGroup is a class of files that all share the same icon. For all
  25. // platforms but Windows, and for most files on Windows, it is the file type
  26. // (e.g. all .mp3 files share an icon, all .html files share an icon). On
  27. // Windows, for certain file types (.exe, .dll, etc), each file of that type
  28. // is assumed to have a unique icon. In that case, each of those files is a
  29. // group to itself.
  30. using IconGroup = base::FilePath::StringType;
  31. enum IconSize {
  32. SMALL = 0, // 16x16
  33. NORMAL, // 32x32
  34. LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported
  35. ALL, // All sizes available
  36. };
  37. // The callback invoked when an icon has been read. The parameters are:
  38. // - The icon that was loaded, or null if there was a failure to load it.
  39. // - The determined group from the original requested path.
  40. using IconLoadedCallback =
  41. base::Callback<void(std::unique_ptr<gfx::Image>, const IconGroup&)>;
  42. // Creates an IconLoader, which owns itself. If the IconLoader might outlive
  43. // the caller, be sure to use a weak pointer in the |callback|.
  44. static IconLoader* Create(const base::FilePath& file_path,
  45. IconSize size,
  46. IconLoadedCallback callback);
  47. // Starts the process of reading the icon. When the reading of the icon is
  48. // complete, the IconLoadedCallback callback will be fulfilled, and the
  49. // IconLoader will delete itself.
  50. void Start();
  51. private:
  52. IconLoader(const base::FilePath& file_path,
  53. IconSize size,
  54. IconLoadedCallback callback);
  55. ~IconLoader();
  56. // Given a file path, get the group for the given file.
  57. static IconGroup GroupForFilepath(const base::FilePath& file_path);
  58. // The TaskRunner that ReadIcon() must be called on.
  59. static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();
  60. void ReadGroup();
  61. void ReadIcon();
  62. // The traits of the tasks posted by this class. These operations may block,
  63. // because they are fetching icons from the disk, yet the result will be seen
  64. // by the user so they should be prioritized accordingly.
  65. static constexpr base::TaskTraits traits() {
  66. return {base::MayBlock(), base::TaskPriority::USER_VISIBLE};
  67. }
  68. // The task runner object of the thread in which we notify the delegate.
  69. scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
  70. base::FilePath file_path_;
  71. IconGroup group_;
  72. IconSize icon_size_;
  73. IconLoadedCallback callback_;
  74. DISALLOW_COPY_AND_ASSIGN(IconLoader);
  75. };
  76. #endif // CHROME_BROWSER_ICON_LOADER_H_