Update icon loading API implementation

This commit is contained in:
Yury Solovyov 2017-02-16 22:19:19 +03:00
commit 977abc6458
9 changed files with 176 additions and 243 deletions

View file

@ -8,31 +8,30 @@
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
#include "ui/gfx/image/image.h"
#if defined(OS_WIN)
// On Windows, we group files by their extension, with several exceptions:
// .dll, .exe, .ico. See IconManager.h for explanation.
typedef std::wstring IconGroupID;
#elif defined(OS_POSIX)
// On POSIX, we group files by MIME type.
typedef std::string IconGroupID;
#endif
////////////////////////////////////////////////////////////////////////////////
//
// A facility to read a file containing an icon asynchronously in the IO
// thread. Returns the icon in the form of an ImageSkia.
//
////////////////////////////////////////////////////////////////////////////////
class IconLoader : public base::RefCountedThreadSafe<IconLoader> {
class IconLoader {
public:
// 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;
enum IconSize {
SMALL = 0, // 16x16
NORMAL, // 32x32
@ -40,42 +39,32 @@ class IconLoader : public base::RefCountedThreadSafe<IconLoader> {
ALL, // All sizes available
};
class Delegate {
public:
// Invoked when an icon group has been read, but before the icon data
// is read. If the icon is already cached, this method should call and
// return the results of OnImageLoaded with the cached image.
virtual bool OnGroupLoaded(IconLoader* source,
const IconGroupID& group) = 0;
// Invoked when an icon has been read. |source| is the IconLoader. If the
// icon has been successfully loaded, result is non-null. This method must
// return true if it is taking ownership of the returned image.
virtual bool OnImageLoaded(IconLoader* source,
gfx::Image* result,
const IconGroupID& group) = 0;
// 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&)>;
protected:
virtual ~Delegate() {}
};
// 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);
IconLoader(const base::FilePath& file_path,
IconSize size,
Delegate* delegate);
// Start reading the icon on the file thread.
// 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.
void Start();
private:
friend class base::RefCountedThreadSafe<IconLoader>;
IconLoader(const base::FilePath& file_path,
IconSize size,
IconLoadedCallback callback);
virtual ~IconLoader();
~IconLoader();
// Get the identifying string for the given file. The implementation
// is in icon_loader_[platform].cc.
static IconGroupID ReadGroupIDFromFilepath(const base::FilePath& path);
// Some icons (exe's on windows) can change as they're loaded.
static bool IsIconMutableFromFilepath(const base::FilePath& path);
// Given a file path, get the group for the given file.
static IconGroup GroupForFilepath(const base::FilePath& file_path);
// The thread ReadIcon() should be called on.
static content::BrowserThread::ID ReadIconThreadID();
@ -84,20 +73,18 @@ class IconLoader : public base::RefCountedThreadSafe<IconLoader> {
void OnReadGroup();
void ReadIcon();
void NotifyDelegate();
// The task runner object of the thread in which we notify the delegate.
scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
base::FilePath file_path_;
IconGroupID group_;
IconGroup group_;
IconSize icon_size_;
std::unique_ptr<gfx::Image> image_;
Delegate* delegate_;
IconLoadedCallback callback_;
DISALLOW_COPY_AND_ASSIGN(IconLoader);
};