electron/chromium_src/chrome/browser/icon_loader.h

98 lines
3.4 KiB
C
Raw Normal View History

2016-10-29 12:35:50 +00:00
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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 <memory>
#include <string>
2017-02-16 19:19:19 +00:00
#include "base/callback.h"
2016-10-29 12:35:50 +00:00
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/task_scheduler/task_traits.h"
2016-10-29 12:35:50 +00:00
#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.
//
////////////////////////////////////////////////////////////////////////////////
2017-02-16 19:19:19 +00:00
class IconLoader {
2016-10-29 12:35:50 +00:00
public:
2017-02-16 19:19:19 +00:00
// An IconGroup is a class of files that all share the same icon. For all
// platforms but 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;
2016-10-29 12:35:50 +00:00
enum IconSize {
SMALL = 0, // 16x16
NORMAL, // 32x32
LARGE, // Windows: 32x32, Linux: 48x48, Mac: Unsupported
ALL, // All sizes available
};
2017-02-16 19:19:19 +00:00
// The callback invoked when an icon has been read. The parameters are:
// - The icon that was loaded, or null if there was a failure to load it.
// - The determined group from the original requested path.
using IconLoadedCallback =
base::Callback<void(std::unique_ptr<gfx::Image>, const IconGroup&)>;
// Creates an IconLoader, which owns itself. If the IconLoader might outlive
// the caller, be sure to use a weak pointer in the |callback|.
static IconLoader* Create(const base::FilePath& file_path,
IconSize size,
IconLoadedCallback callback);
// 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.
2016-10-29 12:35:50 +00:00
void Start();
private:
2017-02-16 19:19:19 +00:00
IconLoader(const base::FilePath& file_path,
IconSize size,
IconLoadedCallback callback);
2016-10-29 12:35:50 +00:00
2017-02-16 19:19:19 +00:00
~IconLoader();
2016-10-29 12:35:50 +00:00
2017-02-16 19:19:19 +00:00
// Given a file path, get the group for the given file.
static IconGroup GroupForFilepath(const base::FilePath& file_path);
2016-10-29 12:35:50 +00:00
// The TaskRunner that ReadIcon() must be called on.
static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();
2016-10-29 12:35:50 +00:00
void ReadGroup();
void ReadIcon();
// The traits of the tasks posted 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.
static constexpr base::TaskTraits traits() {
return {base::MayBlock(), base::TaskPriority::USER_VISIBLE};
}
2016-10-29 12:35:50 +00:00
// The task runner object of the thread in which we notify the delegate.
scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
base::FilePath file_path_;
2017-02-16 19:19:19 +00:00
IconGroup group_;
2016-10-29 12:35:50 +00:00
IconSize icon_size_;
2017-02-16 19:19:19 +00:00
IconLoadedCallback callback_;
2016-10-29 12:35:50 +00:00
DISALLOW_COPY_AND_ASSIGN(IconLoader);
};
#endif // CHROME_BROWSER_ICON_LOADER_H_