Commit e61c23ac authored by Bernhard Bauer's avatar Bernhard Bauer Committed by Commit Bot

Migrate LogoTracker to TaskScheduler API.

Bug: 667892
Change-Id: Icaaef380816473c677cf0cbf9fd24e98dafd8325
Reviewed-on: https://chromium-review.googlesource.com/558919Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Bernhard Bauer <bauerb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#484947}
parent 5a2dd8c9
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/android/chrome_feature_list.h" #include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/image_decoder.h" #include "chrome/browser/image_decoder.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -52,9 +50,10 @@ class LogoDecoderDelegate : public ImageDecoder::ImageRequest { ...@@ -52,9 +50,10 @@ class LogoDecoderDelegate : public ImageDecoder::ImageRequest {
// If the ImageDecoder crashes or otherwise never completes, call // If the ImageDecoder crashes or otherwise never completes, call
// OnImageDecodeTimedOut() eventually to ensure that image_decoded_callback_ // OnImageDecodeTimedOut() eventually to ensure that image_decoded_callback_
// is run. // is run.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( task_runner()->PostDelayedTask(
FROM_HERE, base::Bind(&LogoDecoderDelegate::OnDecodeImageFailed, FROM_HERE,
weak_ptr_factory_.GetWeakPtr()), base::Bind(&LogoDecoderDelegate::OnDecodeImageFailed,
weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kDecodeLogoTimeoutSeconds)); base::TimeDelta::FromSeconds(kDecodeLogoTimeoutSeconds));
} }
...@@ -87,9 +86,10 @@ class ChromeLogoDelegate : public search_provider_logos::LogoDelegate { ...@@ -87,9 +86,10 @@ class ChromeLogoDelegate : public search_provider_logos::LogoDelegate {
void DecodeUntrustedImage( void DecodeUntrustedImage(
const scoped_refptr<base::RefCountedString>& encoded_image, const scoped_refptr<base::RefCountedString>& encoded_image,
base::Callback<void(const SkBitmap&)> image_decoded_callback) override { base::Callback<void(const SkBitmap&)> image_decoded_callback) override {
LogoDecoderDelegate* delegate = // TODO(bauerb): Switch to the components/image_fetcher implementation.
new LogoDecoderDelegate(image_decoded_callback); auto delegate =
ImageDecoder::Start(delegate, encoded_image->data()); base::MakeUnique<LogoDecoderDelegate>(image_decoded_callback);
ImageDecoder::Start(delegate.release(), encoded_image->data());
} }
private: private:
...@@ -136,9 +136,7 @@ void LogoService::GetLogo(search_provider_logos::LogoObserver* observer) { ...@@ -136,9 +136,7 @@ void LogoService::GetLogo(search_provider_logos::LogoObserver* observer) {
if (!logo_tracker_) { if (!logo_tracker_) {
logo_tracker_ = base::MakeUnique<LogoTracker>( logo_tracker_ = base::MakeUnique<LogoTracker>(
profile_->GetPath().Append(kCachedLogoDirectory), profile_->GetPath().Append(kCachedLogoDirectory),
BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE), profile_->GetRequestContext(), base::MakeUnique<ChromeLogoDelegate>());
BrowserThread::GetBlockingPool(), profile_->GetRequestContext(),
base::MakeUnique<ChromeLogoDelegate>());
} }
GURL url = use_fixed_logo ? logo_url : GetGoogleDoodleURL(profile_); GURL url = use_fixed_logo ? logo_url : GetGoogleDoodleURL(profile_);
......
...@@ -57,16 +57,16 @@ LogoCache::LogoCache(const base::FilePath& cache_directory) ...@@ -57,16 +57,16 @@ LogoCache::LogoCache(const base::FilePath& cache_directory)
: cache_directory_(cache_directory), : cache_directory_(cache_directory),
metadata_is_valid_(false) { metadata_is_valid_(false) {
// The LogoCache can be constructed on any thread, as long as it's used // The LogoCache can be constructed on any thread, as long as it's used
// on a single thread after construction. // on a single sequence after construction.
thread_checker_.DetachFromThread(); DETACH_FROM_SEQUENCE(sequence_checker_);
} }
LogoCache::~LogoCache() { LogoCache::~LogoCache() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
} }
void LogoCache::UpdateCachedLogoMetadata(const LogoMetadata& metadata) { void LogoCache::UpdateCachedLogoMetadata(const LogoMetadata& metadata) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(metadata_); DCHECK(metadata_);
DCHECK_EQ(metadata_->fingerprint, metadata.fingerprint); DCHECK_EQ(metadata_->fingerprint, metadata.fingerprint);
...@@ -75,24 +75,24 @@ void LogoCache::UpdateCachedLogoMetadata(const LogoMetadata& metadata) { ...@@ -75,24 +75,24 @@ void LogoCache::UpdateCachedLogoMetadata(const LogoMetadata& metadata) {
} }
const LogoMetadata* LogoCache::GetCachedLogoMetadata() { const LogoMetadata* LogoCache::GetCachedLogoMetadata() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ReadMetadataIfNeeded(); ReadMetadataIfNeeded();
return metadata_.get(); return metadata_.get();
} }
void LogoCache::SetCachedLogo(const EncodedLogo* logo) { void LogoCache::SetCachedLogo(const EncodedLogo* logo) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::unique_ptr<LogoMetadata> metadata; std::unique_ptr<LogoMetadata> metadata;
if (logo) { if (logo) {
metadata.reset(new LogoMetadata(logo->metadata)); metadata = base::MakeUnique<LogoMetadata>(logo->metadata);
logo_num_bytes_ = static_cast<int>(logo->encoded_image->size()); logo_num_bytes_ = static_cast<int>(logo->encoded_image->size());
} }
UpdateMetadata(std::move(metadata)); UpdateMetadata(std::move(metadata));
WriteLogo(logo ? logo->encoded_image : NULL); WriteLogo(logo ? logo->encoded_image : nullptr);
} }
std::unique_ptr<EncodedLogo> LogoCache::GetCachedLogo() { std::unique_ptr<EncodedLogo> LogoCache::GetCachedLogo() {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ReadMetadataIfNeeded(); ReadMetadataIfNeeded();
if (!metadata_) if (!metadata_)
......
...@@ -43,14 +43,14 @@ class LogoCache { ...@@ -43,14 +43,14 @@ class LogoCache {
// Updates the metadata for the cached logo. // Updates the metadata for the cached logo.
virtual void UpdateCachedLogoMetadata(const LogoMetadata& metadata); virtual void UpdateCachedLogoMetadata(const LogoMetadata& metadata);
// Returns metadata for the cached logo, or NULL if logo is cached. // Returns metadata for the cached logo, or null if logo is cached.
virtual const LogoMetadata* GetCachedLogoMetadata(); virtual const LogoMetadata* GetCachedLogoMetadata();
// Sets the cached logo and metadata. |logo| may be NULL, in which case the // Sets the cached logo and metadata. |logo| may be null, in which case the
// cached logo and metadata will be cleared. // cached logo and metadata will be cleared.
virtual void SetCachedLogo(const EncodedLogo* logo); virtual void SetCachedLogo(const EncodedLogo* logo);
// Returns the cached logo, or NULL if no logo is cached or the cached logo is // Returns the cached logo, or null if no logo is cached or the cached logo is
// corrupt. // corrupt.
virtual std::unique_ptr<EncodedLogo> GetCachedLogo(); virtual std::unique_ptr<EncodedLogo> GetCachedLogo();
...@@ -62,7 +62,7 @@ class LogoCache { ...@@ -62,7 +62,7 @@ class LogoCache {
FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, RetrieveCorruptMetadata); FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, RetrieveCorruptMetadata);
FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, RetrieveCorruptLogo); FRIEND_TEST_ALL_PREFIXES(LogoCacheTest, RetrieveCorruptLogo);
// Converts string |str| to a LogoMetadata object and returns it. Returns NULL // Converts string |str| to a LogoMetadata object and returns it. Returns null
// if |str| cannot be converted. // if |str| cannot be converted.
static std::unique_ptr<LogoMetadata> LogoMetadataFromString( static std::unique_ptr<LogoMetadata> LogoMetadataFromString(
const std::string& str, const std::string& str,
...@@ -84,7 +84,7 @@ class LogoCache { ...@@ -84,7 +84,7 @@ class LogoCache {
// If the cached logo's metadata isn't available in memory (i.e. // If the cached logo's metadata isn't available in memory (i.e.
// |metadata_is_valid_| is false), reads it from disk and stores it in // |metadata_is_valid_| is false), reads it from disk and stores it in
// |metadata_|. If no logo is cached, |metadata_| will be updated to NULL. // |metadata_|. If no logo is cached, |metadata_| will be updated to null.
void ReadMetadataIfNeeded(); void ReadMetadataIfNeeded();
// Writes the metadata for the cached logo to disk. // Writes the metadata for the cached logo to disk.
...@@ -104,9 +104,9 @@ class LogoCache { ...@@ -104,9 +104,9 @@ class LogoCache {
// The directory in which the cached logo and metadata will be saved. // The directory in which the cached logo and metadata will be saved.
base::FilePath cache_directory_; base::FilePath cache_directory_;
// The metadata describing the cached logo, or NULL if no logo is cached. This // The metadata describing the cached logo, or null if no logo is cached. This
// value is meaningful iff |metadata_is_valid_| is true; otherwise, the // value is meaningful iff |metadata_is_valid_| is true; otherwise, the
// metadata must be read from file and |metadata_| will be NULL. // metadata must be read from file and |metadata_| will be null.
// Note: Once read from file, metadata will be stored in memory indefinitely. // Note: Once read from file, metadata will be stored in memory indefinitely.
std::unique_ptr<LogoMetadata> metadata_; std::unique_ptr<LogoMetadata> metadata_;
bool metadata_is_valid_; bool metadata_is_valid_;
...@@ -116,8 +116,8 @@ class LogoCache { ...@@ -116,8 +116,8 @@ class LogoCache {
// is complete and corresponds to the current metadata file. // is complete and corresponds to the current metadata file.
int logo_num_bytes_; int logo_num_bytes_;
// Ensure LogoCache is only used on a single thread. // Ensure LogoCache is only used sequentially.
base::ThreadChecker thread_checker_; SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(LogoCache); DISALLOW_COPY_AND_ASSIGN(LogoCache);
}; };
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -55,17 +56,17 @@ base::RefCountedString* CreateExampleImage(size_t num_bytes) { ...@@ -55,17 +56,17 @@ base::RefCountedString* CreateExampleImage(size_t num_bytes) {
return encoded_image_str; return encoded_image_str;
} }
EncodedLogo GetExampleLogo() { std::unique_ptr<EncodedLogo> GetExampleLogo() {
EncodedLogo logo; auto logo = base::MakeUnique<EncodedLogo>();
logo.encoded_image = CreateExampleImage(837); logo->encoded_image = CreateExampleImage(837);
logo.metadata = GetExampleMetadata(); logo->metadata = GetExampleMetadata();
return logo; return logo;
} }
EncodedLogo GetExampleLogo2() { std::unique_ptr<EncodedLogo> GetExampleLogo2() {
EncodedLogo logo; auto logo = base::MakeUnique<EncodedLogo>();
logo.encoded_image = CreateExampleImage(345); logo->encoded_image = CreateExampleImage(345);
logo.metadata = GetExampleMetadata2(); logo->metadata = GetExampleMetadata2();
return logo; return logo;
} }
...@@ -106,27 +107,27 @@ class LogoCacheTest : public ::testing::Test { ...@@ -106,27 +107,27 @@ class LogoCacheTest : public ::testing::Test {
} }
void InitCache() { void InitCache() {
cache_.reset(new LogoCache( cache_ = base::MakeUnique<LogoCache>(
cache_parent_dir_.GetPath().Append(FILE_PATH_LITERAL("cache")))); cache_parent_dir_.GetPath().Append(FILE_PATH_LITERAL("cache")));
} }
void ExpectMetadata(const LogoMetadata* expected_metadata) { void ExpectMetadata(const LogoMetadata* expected_metadata) {
const LogoMetadata* retrieved_metadata = cache_->GetCachedLogoMetadata(); const LogoMetadata* retrieved_metadata = cache_->GetCachedLogoMetadata();
if (expected_metadata) { if (expected_metadata) {
ASSERT_TRUE(retrieved_metadata != NULL); ASSERT_TRUE(retrieved_metadata);
ExpectMetadataEqual(*expected_metadata, *retrieved_metadata); ExpectMetadataEqual(*expected_metadata, *retrieved_metadata);
} else { } else {
ASSERT_TRUE(retrieved_metadata == NULL); ASSERT_FALSE(retrieved_metadata);
} }
} }
void ExpectLogo(const EncodedLogo* expected_logo) { void ExpectLogo(const EncodedLogo* expected_logo) {
std::unique_ptr<EncodedLogo> retrieved_logo(cache_->GetCachedLogo()); std::unique_ptr<EncodedLogo> retrieved_logo(cache_->GetCachedLogo());
if (expected_logo) { if (expected_logo) {
ASSERT_TRUE(retrieved_logo.get() != NULL); ASSERT_TRUE(retrieved_logo.get());
ExpectLogosEqual(*expected_logo, *retrieved_logo); ExpectLogosEqual(*expected_logo, *retrieved_logo);
} else { } else {
ASSERT_TRUE(retrieved_logo.get() == NULL); ASSERT_FALSE(retrieved_logo.get());
} }
} }
...@@ -157,7 +158,7 @@ TEST(LogoCacheSerializationTest, DeserializeCorruptMetadata) { ...@@ -157,7 +158,7 @@ TEST(LogoCacheSerializationTest, DeserializeCorruptMetadata) {
int logo_num_bytes = 33; int logo_num_bytes = 33;
std::unique_ptr<LogoMetadata> metadata = std::unique_ptr<LogoMetadata> metadata =
LogoCache::LogoMetadataFromString("", &logo_num_bytes); LogoCache::LogoMetadataFromString("", &logo_num_bytes);
ASSERT_TRUE(metadata.get() == NULL); ASSERT_FALSE(metadata);
LogoMetadata example_metadata = GetExampleMetadata2(); LogoMetadata example_metadata = GetExampleMetadata2();
std::string corrupt_str; std::string corrupt_str;
...@@ -165,17 +166,17 @@ TEST(LogoCacheSerializationTest, DeserializeCorruptMetadata) { ...@@ -165,17 +166,17 @@ TEST(LogoCacheSerializationTest, DeserializeCorruptMetadata) {
example_metadata, logo_num_bytes, &corrupt_str); example_metadata, logo_num_bytes, &corrupt_str);
corrupt_str.append("@"); corrupt_str.append("@");
metadata = LogoCache::LogoMetadataFromString(corrupt_str, &logo_num_bytes); metadata = LogoCache::LogoMetadataFromString(corrupt_str, &logo_num_bytes);
ASSERT_TRUE(metadata.get() == NULL); ASSERT_FALSE(metadata);
} }
TEST_F(LogoCacheTest, StoreAndRetrieveMetadata) { TEST_F(LogoCacheTest, StoreAndRetrieveMetadata) {
// Expect no metadata at first. // Expect no metadata at first.
ExpectMetadata(NULL); ExpectMetadata(nullptr);
// Set initial metadata. // Set initial metadata.
EncodedLogo logo = GetExampleLogo(); std::unique_ptr<EncodedLogo> logo = GetExampleLogo();
LogoMetadata& metadata = logo.metadata; LogoMetadata& metadata = logo->metadata;
cache_->SetCachedLogo(&logo); cache_->SetCachedLogo(logo.get());
ExpectMetadata(&metadata); ExpectMetadata(&metadata);
// Update metadata. // Update metadata.
...@@ -194,42 +195,42 @@ TEST_F(LogoCacheTest, StoreAndRetrieveMetadata) { ...@@ -194,42 +195,42 @@ TEST_F(LogoCacheTest, StoreAndRetrieveMetadata) {
TEST_F(LogoCacheTest, StoreAndRetrieveLogo) { TEST_F(LogoCacheTest, StoreAndRetrieveLogo) {
// Expect no metadata at first. // Expect no metadata at first.
ExpectLogo(NULL); ExpectLogo(nullptr);
// Set initial logo. // Set initial logo.
EncodedLogo logo = GetExampleLogo(); std::unique_ptr<EncodedLogo> logo = GetExampleLogo();
cache_->SetCachedLogo(&logo); cache_->SetCachedLogo(logo.get());
ExpectLogo(&logo); ExpectLogo(logo.get());
// Update logo to NULL. // Update logo to null.
cache_->SetCachedLogo(NULL); cache_->SetCachedLogo(nullptr);
ExpectLogo(NULL); ExpectLogo(nullptr);
// Read logo back from disk. // Read logo back from disk.
SimulateRestart(); SimulateRestart();
ExpectLogo(NULL); ExpectLogo(nullptr);
// Update logo. // Update logo.
logo = GetExampleLogo2(); logo = GetExampleLogo2();
cache_->SetCachedLogo(&logo); cache_->SetCachedLogo(logo.get());
ExpectLogo(&logo); ExpectLogo(logo.get());
// Read logo back from disk. // Read logo back from disk.
SimulateRestart(); SimulateRestart();
ExpectLogo(&logo); ExpectLogo(logo.get());
} }
TEST_F(LogoCacheTest, RetrieveCorruptMetadata) { TEST_F(LogoCacheTest, RetrieveCorruptMetadata) {
// Set initial logo. // Set initial logo.
EncodedLogo logo = GetExampleLogo2(); std::unique_ptr<EncodedLogo> logo = GetExampleLogo2();
cache_->SetCachedLogo(&logo); cache_->SetCachedLogo(logo.get());
ExpectLogo(&logo); ExpectLogo(logo.get());
// Corrupt metadata and expect NULL for both logo and metadata. // Corrupt metadata and expect null for both logo and metadata.
SimulateRestart(); SimulateRestart();
ShortenFile(cache_->GetMetadataPath()); ShortenFile(cache_->GetMetadataPath());
ExpectMetadata(NULL); ExpectMetadata(nullptr);
ExpectLogo(NULL); ExpectLogo(nullptr);
// Ensure corrupt cache files are deleted. // Ensure corrupt cache files are deleted.
EXPECT_FALSE(base::PathExists(cache_->GetMetadataPath())); EXPECT_FALSE(base::PathExists(cache_->GetMetadataPath()));
...@@ -238,16 +239,17 @@ TEST_F(LogoCacheTest, RetrieveCorruptMetadata) { ...@@ -238,16 +239,17 @@ TEST_F(LogoCacheTest, RetrieveCorruptMetadata) {
TEST_F(LogoCacheTest, RetrieveCorruptLogo) { TEST_F(LogoCacheTest, RetrieveCorruptLogo) {
// Set initial logo. // Set initial logo.
EncodedLogo logo = GetExampleLogo(); std::unique_ptr<EncodedLogo> logo = GetExampleLogo();
cache_->SetCachedLogo(&logo); cache_->SetCachedLogo(logo.get());
ExpectLogo(&logo); ExpectLogo(logo.get());
// Corrupt logo and expect NULL. // Corrupt logo and expect nullptr.
SimulateRestart(); SimulateRestart();
ShortenFile(cache_->GetLogoPath()); ShortenFile(cache_->GetLogoPath());
ExpectLogo(NULL); ExpectLogo(nullptr);
// Once the logo is noticed to be NULL, the metadata should also be cleared.
ExpectMetadata(NULL); // Once the logo is noticed to be null, the metadata should also be cleared.
ExpectMetadata(nullptr);
// Ensure corrupt cache files are deleted. // Ensure corrupt cache files are deleted.
EXPECT_FALSE(base::PathExists(cache_->GetMetadataPath())); EXPECT_FALSE(base::PathExists(cache_->GetMetadataPath()));
......
...@@ -7,10 +7,12 @@ ...@@ -7,10 +7,12 @@
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include "base/bind_helpers.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/task_runner_util.h" #include "base/task_runner_util.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
#include "components/data_use_measurement/core/data_use_user_data.h" #include "components/data_use_measurement/core/data_use_user_data.h"
...@@ -62,33 +64,26 @@ std::unique_ptr<EncodedLogo> GetLogoFromCacheOnFileThread(LogoCache* logo_cache, ...@@ -62,33 +64,26 @@ std::unique_ptr<EncodedLogo> GetLogoFromCacheOnFileThread(LogoCache* logo_cache,
return logo_cache->GetCachedLogo(); return logo_cache->GetCachedLogo();
} }
void DeleteLogoCacheOnFileThread(LogoCache* logo_cache) {
delete logo_cache;
}
} // namespace } // namespace
LogoTracker::LogoTracker( LogoTracker::LogoTracker(
base::FilePath cached_logo_directory, base::FilePath cached_logo_directory,
scoped_refptr<base::SequencedTaskRunner> file_task_runner,
scoped_refptr<base::TaskRunner> background_task_runner,
scoped_refptr<net::URLRequestContextGetter> request_context_getter, scoped_refptr<net::URLRequestContextGetter> request_context_getter,
std::unique_ptr<LogoDelegate> delegate) std::unique_ptr<LogoDelegate> delegate)
: is_idle_(true), : is_idle_(true),
is_cached_logo_valid_(false), is_cached_logo_valid_(false),
logo_delegate_(std::move(delegate)), logo_delegate_(std::move(delegate)),
logo_cache_(new LogoCache(cached_logo_directory)), cache_task_runner_(base::CreateSequencedTaskRunnerWithTraits(
clock_(new base::DefaultClock()), {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
file_task_runner_(file_task_runner), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})),
background_task_runner_(background_task_runner), logo_cache_(new LogoCache(cached_logo_directory),
base::OnTaskRunnerDeleter(cache_task_runner_)),
clock_(base::MakeUnique<base::DefaultClock>()),
request_context_getter_(request_context_getter), request_context_getter_(request_context_getter),
weak_ptr_factory_(this) {} weak_ptr_factory_(this) {}
LogoTracker::~LogoTracker() { LogoTracker::~LogoTracker() {
ReturnToIdle(kDownloadOutcomeNotTracked); ReturnToIdle(kDownloadOutcomeNotTracked);
file_task_runner_->PostTask(
FROM_HERE, base::Bind(&DeleteLogoCacheOnFileThread, logo_cache_));
logo_cache_ = NULL;
} }
void LogoTracker::SetServerAPI( void LogoTracker::SetServerAPI(
...@@ -112,11 +107,9 @@ void LogoTracker::GetLogo(LogoObserver* observer) { ...@@ -112,11 +107,9 @@ void LogoTracker::GetLogo(LogoObserver* observer) {
if (is_idle_) { if (is_idle_) {
is_idle_ = false; is_idle_ = false;
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
file_task_runner_.get(), cache_task_runner_.get(), FROM_HERE,
FROM_HERE,
base::Bind(&GetLogoFromCacheOnFileThread, base::Bind(&GetLogoFromCacheOnFileThread,
logo_cache_, base::Unretained(logo_cache_.get()), logo_url_,
logo_url_,
clock_->Now()), clock_->Now()),
base::Bind(&LogoTracker::OnCachedLogoRead, base::Bind(&LogoTracker::OnCachedLogoRead,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
...@@ -131,9 +124,9 @@ void LogoTracker::RemoveObserver(LogoObserver* observer) { ...@@ -131,9 +124,9 @@ void LogoTracker::RemoveObserver(LogoObserver* observer) {
void LogoTracker::SetLogoCacheForTests(std::unique_ptr<LogoCache> cache) { void LogoTracker::SetLogoCacheForTests(std::unique_ptr<LogoCache> cache) {
DCHECK(cache); DCHECK(cache);
file_task_runner_->PostTask( // Call reset() and release() to keep the deleter of the |logo_cache_| member
FROM_HERE, base::Bind(&DeleteLogoCacheOnFileThread, logo_cache_)); // and run it on the old value.
logo_cache_ = cache.release(); logo_cache_.reset(cache.release());
} }
void LogoTracker::SetClockForTests(std::unique_ptr<base::Clock> clock) { void LogoTracker::SetClockForTests(std::unique_ptr<base::Clock> clock) {
...@@ -191,18 +184,16 @@ void LogoTracker::OnCachedLogoAvailable(const LogoMetadata& metadata, ...@@ -191,18 +184,16 @@ void LogoTracker::OnCachedLogoAvailable(const LogoMetadata& metadata,
} }
void LogoTracker::SetCachedLogo(std::unique_ptr<EncodedLogo> logo) { void LogoTracker::SetCachedLogo(std::unique_ptr<EncodedLogo> logo) {
file_task_runner_->PostTask( cache_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&LogoCache::SetCachedLogo, base::Bind(&LogoCache::SetCachedLogo, base::Unretained(logo_cache_.get()),
base::Unretained(logo_cache_),
base::Owned(logo.release()))); base::Owned(logo.release())));
} }
void LogoTracker::SetCachedMetadata(const LogoMetadata& metadata) { void LogoTracker::SetCachedMetadata(const LogoMetadata& metadata) {
file_task_runner_->PostTask(FROM_HERE, cache_task_runner_->PostTask(
base::Bind(&LogoCache::UpdateCachedLogoMetadata, FROM_HERE, base::Bind(&LogoCache::UpdateCachedLogoMetadata,
base::Unretained(logo_cache_), base::Unretained(logo_cache_.get()), metadata));
metadata));
} }
void LogoTracker::FetchLogo() { void LogoTracker::FetchLogo() {
...@@ -362,8 +353,10 @@ void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) { ...@@ -362,8 +353,10 @@ void LogoTracker::OnURLFetchComplete(const net::URLFetcher* source) {
bool from_http_cache = source->WasCached(); bool from_http_cache = source->WasCached();
bool* parsing_failed = new bool(false); bool* parsing_failed = new bool(false);
base::PostTaskAndReplyWithResult( base::PostTaskWithTraitsAndReplyWithResult(
background_task_runner_.get(), FROM_HERE, FROM_HERE,
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
base::Bind(parse_logo_response_func_, base::Passed(&response), base::Bind(parse_logo_response_func_, base::Passed(&response),
response_time, parsing_failed), response_time, parsing_failed),
base::Bind(&LogoTracker::OnFreshLogoParsed, base::Bind(&LogoTracker::OnFreshLogoParsed,
......
...@@ -93,9 +93,6 @@ class LogoTracker : public net::URLFetcherDelegate { ...@@ -93,9 +93,6 @@ class LogoTracker : public net::URLFetcherDelegate {
// |cached_logo_directory| is the directory in which the cached logo and its // |cached_logo_directory| is the directory in which the cached logo and its
// metadata should be saved. // metadata should be saved.
// //
// |file_task_runner| is the SequencedTaskRunner that should be used to run
// file system operations.
//
// |background_task_runner| is the TaskRunner that should be used to for // |background_task_runner| is the TaskRunner that should be used to for
// CPU-intensive background operations. // CPU-intensive background operations.
// //
...@@ -103,8 +100,6 @@ class LogoTracker : public net::URLFetcherDelegate { ...@@ -103,8 +100,6 @@ class LogoTracker : public net::URLFetcherDelegate {
// the logo. // the logo.
explicit LogoTracker( explicit LogoTracker(
base::FilePath cached_logo_directory, base::FilePath cached_logo_directory,
scoped_refptr<base::SequencedTaskRunner> file_task_runner,
scoped_refptr<base::TaskRunner> background_task_runner,
scoped_refptr<net::URLRequestContextGetter> request_context_getter, scoped_refptr<net::URLRequestContextGetter> request_context_getter,
std::unique_ptr<LogoDelegate> delegate); std::unique_ptr<LogoDelegate> delegate);
...@@ -234,18 +229,16 @@ class LogoTracker : public net::URLFetcherDelegate { ...@@ -234,18 +229,16 @@ class LogoTracker : public net::URLFetcherDelegate {
std::unique_ptr<LogoDelegate> logo_delegate_; std::unique_ptr<LogoDelegate> logo_delegate_;
// The cache used to persist the logo on disk. Used only on the file thread. // The SequencedTaskRunner on which the cache lives.
LogoCache* logo_cache_; scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
// The cache used to persist the logo on disk. Used only on a background
// SequencedTaskRunner.
std::unique_ptr<LogoCache, base::OnTaskRunnerDeleter> logo_cache_;
// Clock used to determine current time. Can be overridden in tests. // Clock used to determine current time. Can be overridden in tests.
std::unique_ptr<base::Clock> clock_; std::unique_ptr<base::Clock> clock_;
// The SequencedTaskRunner on which file system operations will be run.
scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
// The TaskRunner on which the server's response will be parsed.
scoped_refptr<base::TaskRunner> background_task_runner_;
// The URLRequestContextGetter used for network requests. // The URLRequestContextGetter used for network requests.
scoped_refptr<net::URLRequestContextGetter> request_context_getter_; scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
......
...@@ -8,14 +8,12 @@ ...@@ -8,14 +8,12 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "components/search_provider_logos/google_logo_api.h" #include "components/search_provider_logos/google_logo_api.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h" #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/search_engines/template_url_service_factory.h" #include "ios/chrome/browser/search_engines/template_url_service_factory.h"
#include "ios/chrome/browser/search_engines/ui_thread_search_terms_data.h" #include "ios/chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "ios/web/public/web_thread.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
...@@ -100,12 +98,9 @@ void GoogleLogoService::GetLogo(search_provider_logos::LogoObserver* observer) { ...@@ -100,12 +98,9 @@ void GoogleLogoService::GetLogo(search_provider_logos::LogoObserver* observer) {
return; return;
if (!logo_tracker_) { if (!logo_tracker_) {
logo_tracker_.reset(new LogoTracker( logo_tracker_ = base::MakeUnique<LogoTracker>(
DoodleDirectory(), DoodleDirectory(), browser_state_->GetRequestContext(),
web::WebThread::GetTaskRunnerForThread(web::WebThread::FILE), base::MakeUnique<IOSChromeLogoDelegate>());
web::WebThread::GetBlockingPool(), browser_state_->GetRequestContext(),
std::unique_ptr<search_provider_logos::LogoDelegate>(
new IOSChromeLogoDelegate())));
} }
logo_tracker_->SetServerAPI( logo_tracker_->SetServerAPI(
......
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