Merge pull request #11595 from YurySolovyov/fileicon-task-scheduler

Use task scheduler for app.getFileIcon API
This commit is contained in:
Samuel Attard 2018-02-13 05:11:22 +11:00 committed by GitHub
commit 9f78ef0179
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 31 deletions

View file

@ -2,8 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <utility>
#include "chrome/browser/icon_loader.h"
#include "base/bind.h"
#include "base/task_scheduler/post_task.h"
#include "base/task_scheduler/task_traits.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/browser/browser_thread.h"
@ -18,10 +22,10 @@ IconLoader* IconLoader::Create(const base::FilePath& file_path,
void IconLoader::Start() {
target_task_runner_ = base::ThreadTaskRunnerHandle::Get();
BrowserThread::PostTaskAndReply(
BrowserThread::FILE, FROM_HERE,
base::Bind(&IconLoader::ReadGroup, base::Unretained(this)),
base::Bind(&IconLoader::OnReadGroup, base::Unretained(this)));
base::PostTaskWithTraits(
FROM_HERE, traits(),
base::BindOnce(&IconLoader::ReadGroup, base::Unretained(this)));
}
IconLoader::IconLoader(const base::FilePath& file_path,
@ -33,10 +37,6 @@ IconLoader::~IconLoader() {}
void IconLoader::ReadGroup() {
group_ = GroupForFilepath(file_path_);
}
void IconLoader::OnReadGroup() {
BrowserThread::PostTask(
ReadIconThreadID(), FROM_HERE,
base::Bind(&IconLoader::ReadIcon, base::Unretained(this)));
GetReadIconTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&IconLoader::ReadIcon, base::Unretained(this)));
}

View file

@ -12,6 +12,7 @@
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/single_thread_task_runner.h"
#include "base/task_scheduler/task_traits.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
#include "ui/gfx/image/image.h"
@ -66,13 +67,19 @@ class IconLoader {
// 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();
// The TaskRunner that ReadIcon() must be called on.
static scoped_refptr<base::TaskRunner> GetReadIconTaskRunner();
void ReadGroup();
void OnReadGroup();
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};
}
// The task runner object of the thread in which we notify the delegate.
scoped_refptr<base::SingleThreadTaskRunner> target_task_runner_;
@ -82,8 +89,6 @@ class IconLoader {
IconSize icon_size_;
std::unique_ptr<gfx::Image> image_;
IconLoadedCallback callback_;
DISALLOW_COPY_AND_ASSIGN(IconLoader);

View file

@ -5,6 +5,7 @@
#include "chrome/browser/icon_loader.h"
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/nix/mime_util_xdg.h"
#include "ui/views/linux_ui/linux_ui.h"
@ -16,10 +17,11 @@ IconLoader::IconGroup IconLoader::GroupForFilepath(
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
// ReadIcon() calls into views::LinuxUI and GTK2 code, so it must be on the UI
// thread.
return content::BrowserThread::UI;
return content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI);
}
void IconLoader::ReadIcon() {
@ -38,14 +40,17 @@ void IconLoader::ReadIcon() {
NOTREACHED();
}
std::unique_ptr<gfx::Image> image;
views::LinuxUI* ui = views::LinuxUI::instance();
if (ui) {
gfx::Image image = ui->GetIconForContentType(group_, size_pixels);
if (!image.IsEmpty())
image_.reset(new gfx::Image(image));
image = base::MakeUnique<gfx::Image>(
ui->GetIconForContentType(group_, size_pixels));
if (image->IsEmpty())
image = nullptr;
}
target_task_runner_->PostTask(
FROM_HERE, base::Bind(callback_, base::Passed(&image_), group_));
FROM_HERE, base::BindOnce(callback_, base::Passed(&image), group_));
delete this;
}

View file

@ -8,8 +8,10 @@
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/sys_string_conversions.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_util_mac.h"
@ -21,8 +23,9 @@ IconLoader::IconGroup IconLoader::GroupForFilepath(
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
return content::BrowserThread::FILE;
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
// NSWorkspace is thread-safe.
return base::CreateTaskRunnerWithTraits(traits());
}
void IconLoader::ReadIcon() {
@ -30,9 +33,11 @@ void IconLoader::ReadIcon() {
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSImage* icon = [workspace iconForFileType:group];
std::unique_ptr<gfx::Image> image;
if (icon_size_ == ALL) {
// The NSImage already has all sizes.
image_.reset(new gfx::Image([icon retain]));
image = base::MakeUnique<gfx::Image>([icon retain]);
} else {
NSSize size = NSZeroSize;
switch (icon_size_) {
@ -48,11 +53,11 @@ void IconLoader::ReadIcon() {
gfx::ImageSkia image_skia(gfx::ImageSkiaFromResizedNSImage(icon, size));
if (!image_skia.isNull()) {
image_skia.MakeThreadSafe();
image_.reset(new gfx::Image(image_skia));
image = base::MakeUnique<gfx::Image>(image_skia);
}
}
target_task_runner_->PostTask(
FROM_HERE, base::Bind(callback_, base::Passed(&image_), group_));
FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
delete this;
}

View file

@ -8,7 +8,9 @@
#include <shellapi.h>
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/display/win/dpi.h"
@ -29,8 +31,10 @@ IconLoader::IconGroup IconLoader::GroupForFilepath(
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
return content::BrowserThread::FILE;
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
// Technically speaking, only a thread with COM is needed, not one that has
// a COM STA. However, this is what is available for now.
return base::CreateCOMSTATaskRunnerWithTraits(traits());
}
void IconLoader::ReadIcon() {
@ -49,7 +53,7 @@ void IconLoader::ReadIcon() {
NOTREACHED();
}
image_.reset();
std::unique_ptr<gfx::Image> image;
SHFILEINFO file_info = { 0 };
if (SHGetFileInfo(group_.c_str(), FILE_ATTRIBUTE_NORMAL, &file_info,
@ -61,12 +65,12 @@ void IconLoader::ReadIcon() {
gfx::ImageSkia image_skia(gfx::ImageSkiaRep(*bitmap,
display::win::GetDPIScale()));
image_skia.MakeThreadSafe();
image_.reset(new gfx::Image(image_skia));
image = base::MakeUnique<gfx::Image>(image_skia);
DestroyIcon(file_info.hIcon);
}
}
target_task_runner_->PostTask(
FROM_HERE, base::Bind(callback_, base::Passed(&image_), group_));
FROM_HERE, base::Bind(callback_, base::Passed(&image), group_));
delete this;
}