Commit f4d431c3 authored by avi's avatar avi Committed by Commit Bot

Move the IconLoader to use the task scheduler.

BUG=689520

Review-Url: https://codereview.chromium.org/2953633002
Cr-Commit-Position: refs/heads/master@{#481719}
parent d3043821
......@@ -4,7 +4,11 @@
#include "chrome/browser/icon_loader.h"
#include <utility>
#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"
......@@ -20,10 +24,9 @@ IconLoader* IconLoader::Create(const base::FilePath& file_path,
void IconLoader::Start() {
target_task_runner_ = base::ThreadTaskRunnerHandle::Get();
BrowserThread::PostTaskAndReply(
BrowserThread::FILE, FROM_HERE,
base::BindOnce(&IconLoader::ReadGroup, base::Unretained(this)),
base::BindOnce(&IconLoader::OnReadGroup, base::Unretained(this)));
base::PostTaskWithTraits(
FROM_HERE, traits(),
base::BindOnce(&IconLoader::ReadGroup, base::Unretained(this)));
}
IconLoader::IconLoader(const base::FilePath& file_path,
......@@ -35,10 +38,7 @@ IconLoader::~IconLoader() {}
void IconLoader::ReadGroup() {
group_ = GroupForFilepath(file_path_);
}
void IconLoader::OnReadGroup() {
BrowserThread::PostTask(
ReadIconThreadID(), FROM_HERE,
base::BindOnce(&IconLoader::ReadIcon, base::Unretained(this)));
GetReadIconTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&IconLoader::ReadIcon, base::Unretained(this)));
}
......@@ -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_;
......
......@@ -13,9 +13,9 @@ IconLoader::IconGroup IconLoader::GroupForFilepath(
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
NOTIMPLEMENTED();
return content::BrowserThread::FILE;
return nullptr;
}
void IconLoader::ReadIcon() {
......
......@@ -17,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() {
......
......@@ -191,10 +191,11 @@ IconLoader::IconGroup IconLoader::GroupForFilepath(
}
// static
content::BrowserThread::ID IconLoader::ReadIconThreadID() {
scoped_refptr<base::TaskRunner> IconLoader::GetReadIconTaskRunner() {
// ReadIcon touches non thread safe ResourceBundle images, so it must be on
// the UI thread.
return content::BrowserThread::UI;
return content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI);
}
void IconLoader::ReadIcon() {
......
......@@ -11,6 +11,7 @@
#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"
......@@ -22,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() {
......
......@@ -10,6 +10,7 @@
#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"
......@@ -30,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() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment