Skip to content

Commit

Permalink
Media Galleries: Migrate MediaGalleriesPreferences to TaskScheduler
Browse files Browse the repository at this point in the history
Bug: 689520
Change-Id: Ie683b101ea28f68aa0e96696cccf5345a60bb1ea
Reviewed-on: https://chromium-review.googlesource.com/578440
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#489126}
  • Loading branch information
Tommy C. Li authored and Commit Bot committed Jul 24, 2017
1 parent 25222b0 commit 471aa60
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
15 changes: 10 additions & 5 deletions chrome/browser/media_galleries/media_galleries_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task_scheduler/post_task.h"
#include "base/task_scheduler/task_traits.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
Expand Down Expand Up @@ -363,8 +366,8 @@ base::string16 GetDisplayNameForSubFolder(const base::string16& device_name,
device_name);
}

void InitializeImportedMediaGalleryRegistryOnFileThread() {
DCHECK_CURRENTLY_ON(content::BrowserThread::FILE);
void InitializeImportedMediaGalleryRegistryInBackground() {
base::ThreadRestrictions::AssertIOAllowed();
ImportedMediaGalleryRegistry::GetInstance()->Initialize();
}

Expand Down Expand Up @@ -522,13 +525,15 @@ bool MediaGalleriesPreferences::IsInitialized() const { return initialized_; }
Profile* MediaGalleriesPreferences::profile() { return profile_; }

void MediaGalleriesPreferences::OnInitializationCallbackReturned() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!IsInitialized());
DCHECK_GT(pre_initialization_callbacks_waiting_, 0);
if (--pre_initialization_callbacks_waiting_ == 0)
FinishInitialization();
}

void MediaGalleriesPreferences::FinishInitialization() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!IsInitialized());

initialized_ = true;
Expand Down Expand Up @@ -669,9 +674,9 @@ void MediaGalleriesPreferences::OnFinderDeviceID(const std::string& device_id) {

if (!gallery_name.empty()) {
pre_initialization_callbacks_waiting_++;
content::BrowserThread::PostTaskAndReply(
content::BrowserThread::FILE, FROM_HERE,
base::BindOnce(&InitializeImportedMediaGalleryRegistryOnFileThread),
base::PostTaskWithTraitsAndReply(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BACKGROUND},
base::BindOnce(&InitializeImportedMediaGalleryRegistryInBackground),
base::BindOnce(
&MediaGalleriesPreferences::OnInitializationCallbackReturned,
weak_factory_.GetWeakPtr()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class MediaGalleriesPreferences
// Before the callback is run, other calls may not return the correct results.
// Should be invoked on the UI thread; callbacks will be run on the UI thread.
// This call also ensures that the StorageMonitor is initialized.
// Note for unit tests: This requires an active FILE thread and
// Note for unit tests: This requires an active ScopedTaskEnvironment and
// EnsureMediaDirectoriesExists instance to complete reliably.
void EnsureInitialized(base::Closure callback);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_task_environment.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/test_extension_system.h"
Expand Down Expand Up @@ -111,9 +112,10 @@ class MediaGalleriesPreferencesTest : public testing::Test {
DeviceIdPrefIdsMap;

MediaGalleriesPreferencesTest()
: profile_(new TestingProfile()),
default_galleries_count_(0) {
}
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::UI),
profile_(new TestingProfile()),
default_galleries_count_(0) {}

~MediaGalleriesPreferencesTest() override {}

Expand Down Expand Up @@ -364,6 +366,8 @@ class MediaGalleriesPreferencesTest : public testing::Test {
MediaGalleriesPrefInfoMap expected_galleries_;

private:
base::test::ScopedTaskEnvironment scoped_task_environment_;

// Needed for extension service & friends to work.
content::TestBrowserThreadBundle thread_bundle_;

Expand Down

0 comments on commit 471aa60

Please sign in to comment.