Commit 66e3178a authored by Istiaque Ahmed's avatar Istiaque Ahmed Committed by Commit Bot

Pass OnceClosure to MediaGalleriesPreferences::EnsureInitialized().

Instead of a RepeatingClosure, pass OnceClosure as the method
would call it at most once and is the correct thing to do.

Bug: None
Change-Id: I9ad50e31117f0fc677e21878b7c4787431cb8422
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2154586
Commit-Queue: Istiaque Ahmed <lazyboy@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760207}
parent 10b4af26
...@@ -111,7 +111,7 @@ GalleryWatchManager* gallery_watch_manager() { ...@@ -111,7 +111,7 @@ GalleryWatchManager* gallery_watch_manager() {
// Checks whether the MediaGalleries API is currently accessible (it may be // Checks whether the MediaGalleries API is currently accessible (it may be
// disallowed even if an extension has the requisite permission). Then // disallowed even if an extension has the requisite permission). Then
// initializes the MediaGalleriesPreferences // initializes the MediaGalleriesPreferences
bool Setup(Profile* profile, std::string* error, base::Closure callback) { bool Setup(Profile* profile, std::string* error, base::OnceClosure callback) {
if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) { if (!ChromeSelectFilePolicy::FileSelectDialogsAllowed()) {
*error = *error =
std::string(kDisallowedByPolicy) + prefs::kAllowFileSelectionDialogs; std::string(kDisallowedByPolicy) + prefs::kAllowFileSelectionDialogs;
...@@ -120,7 +120,7 @@ bool Setup(Profile* profile, std::string* error, base::Closure callback) { ...@@ -120,7 +120,7 @@ bool Setup(Profile* profile, std::string* error, base::Closure callback) {
MediaGalleriesPreferences* preferences = MediaGalleriesPreferences* preferences =
media_file_system_registry()->GetPreferences(profile); media_file_system_registry()->GetPreferences(profile);
preferences->EnsureInitialized(callback); preferences->EnsureInitialized(std::move(callback));
return true; return true;
} }
...@@ -407,8 +407,9 @@ bool MediaGalleriesGetMediaFileSystemsFunction::RunAsync() { ...@@ -407,8 +407,9 @@ bool MediaGalleriesGetMediaFileSystemsFunction::RunAsync() {
return Setup( return Setup(
GetProfile(), &error_, GetProfile(), &error_,
base::Bind(&MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, base::BindOnce(
this, interactive)); &MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, this,
interactive));
} }
void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit( void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit(
...@@ -508,7 +509,7 @@ bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() { ...@@ -508,7 +509,7 @@ bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() {
::media_galleries::UsageCount(::media_galleries::ADD_USER_SELECTED_FOLDER); ::media_galleries::UsageCount(::media_galleries::ADD_USER_SELECTED_FOLDER);
return Setup( return Setup(
GetProfile(), &error_, GetProfile(), &error_,
base::Bind( base::BindOnce(
&MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit, &MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit,
this)); this));
} }
...@@ -618,9 +619,10 @@ bool MediaGalleriesGetMetadataFunction::RunAsync() { ...@@ -618,9 +619,10 @@ bool MediaGalleriesGetMetadataFunction::RunAsync() {
if (!options) if (!options)
return false; return false;
return Setup(GetProfile(), &error_, return Setup(
base::Bind(&MediaGalleriesGetMetadataFunction::OnPreferencesInit, GetProfile(), &error_,
this, options->metadata_type, blob_uuid)); base::BindOnce(&MediaGalleriesGetMetadataFunction::OnPreferencesInit,
this, options->metadata_type, blob_uuid));
} }
void MediaGalleriesGetMetadataFunction::OnPreferencesInit( void MediaGalleriesGetMetadataFunction::OnPreferencesInit(
...@@ -798,8 +800,8 @@ bool MediaGalleriesAddGalleryWatchFunction::RunAsync() { ...@@ -798,8 +800,8 @@ bool MediaGalleriesAddGalleryWatchFunction::RunAsync() {
g_browser_process->media_file_system_registry()->GetPreferences( g_browser_process->media_file_system_registry()->GetPreferences(
GetProfile()); GetProfile());
preferences->EnsureInitialized( preferences->EnsureInitialized(
base::Bind(&MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit, base::BindOnce(&MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit,
this, params->gallery_id)); this, params->gallery_id));
return true; return true;
} }
...@@ -873,9 +875,9 @@ bool MediaGalleriesRemoveGalleryWatchFunction::RunAsync() { ...@@ -873,9 +875,9 @@ bool MediaGalleriesRemoveGalleryWatchFunction::RunAsync() {
MediaGalleriesPreferences* preferences = MediaGalleriesPreferences* preferences =
g_browser_process->media_file_system_registry()->GetPreferences( g_browser_process->media_file_system_registry()->GetPreferences(
GetProfile()); GetProfile());
preferences->EnsureInitialized( preferences->EnsureInitialized(base::BindOnce(
base::Bind(&MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit, &MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit, this,
this, params->gallery_id)); params->gallery_id));
return true; return true;
} }
......
...@@ -112,9 +112,9 @@ void AttemptAutoMountOnUIThread( ...@@ -112,9 +112,9 @@ void AttemptAutoMountOnUIThread(
profile); profile);
// Pass the WebContentsGetter to the closure to prevent a use-after-free // Pass the WebContentsGetter to the closure to prevent a use-after-free
// in the case that the web_contents is destroyed before the closure runs. // in the case that the web_contents is destroyed before the closure runs.
preferences->EnsureInitialized(base::Bind( preferences->EnsureInitialized(base::BindOnce(
&OnPreferencesInit, web_contents_getter, base::RetainedRef(extension), &OnPreferencesInit, web_contents_getter, base::RetainedRef(extension),
pref_id, base::Passed(&callback))); pref_id, std::move(callback)));
return; return;
} }
} }
......
...@@ -71,9 +71,9 @@ MediaGalleriesPermissionController::MediaGalleriesPermissionController( ...@@ -71,9 +71,9 @@ MediaGalleriesPermissionController::MediaGalleriesPermissionController(
// Passing unretained pointer is safe, since the dialog controller // Passing unretained pointer is safe, since the dialog controller
// is self-deleting, and so won't be deleted until it can be shown // is self-deleting, and so won't be deleted until it can be shown
// and then closed. // and then closed.
preferences_->EnsureInitialized( preferences_->EnsureInitialized(base::BindOnce(
base::Bind(&MediaGalleriesPermissionController::OnPreferencesInitialized, &MediaGalleriesPermissionController::OnPreferencesInitialized,
base::Unretained(this))); base::Unretained(this)));
// Unretained is safe because |this| owns |context_menu_|. // Unretained is safe because |this| owns |context_menu_|.
context_menu_.reset( context_menu_.reset(
......
...@@ -462,16 +462,16 @@ MediaGalleriesPreferences::~MediaGalleriesPreferences() { ...@@ -462,16 +462,16 @@ MediaGalleriesPreferences::~MediaGalleriesPreferences() {
StorageMonitor::GetInstance()->RemoveObserver(this); StorageMonitor::GetInstance()->RemoveObserver(this);
} }
void MediaGalleriesPreferences::EnsureInitialized(base::Closure callback) { void MediaGalleriesPreferences::EnsureInitialized(base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (IsInitialized()) { if (IsInitialized()) {
if (!callback.is_null()) if (callback)
callback.Run(); std::move(callback).Run();
return; return;
} }
on_initialize_callbacks_.push_back(callback); on_initialize_callbacks_.push_back(std::move(callback));
if (on_initialize_callbacks_.size() > 1) if (on_initialize_callbacks_.size() > 1)
return; return;
...@@ -572,10 +572,8 @@ void MediaGalleriesPreferences::OnStorageMonitorInit( ...@@ -572,10 +572,8 @@ void MediaGalleriesPreferences::OnStorageMonitorInit(
existing_devices[i].total_size_in_bytes(), base::Time::Now(), 0, 0, 0); existing_devices[i].total_size_in_bytes(), base::Time::Now(), 0, 0, 0);
} }
for (auto iter = on_initialize_callbacks_.begin(); for (base::OnceClosure& callback : on_initialize_callbacks_)
iter != on_initialize_callbacks_.end(); ++iter) { std::move(callback).Run();
iter->Run();
}
on_initialize_callbacks_.clear(); on_initialize_callbacks_.clear();
} }
......
...@@ -188,7 +188,7 @@ class MediaGalleriesPreferences ...@@ -188,7 +188,7 @@ class MediaGalleriesPreferences
// This call also ensures that the StorageMonitor is initialized. // This call also ensures that the StorageMonitor is initialized.
// Note for unit tests: This requires an active TaskEnvironment and // Note for unit tests: This requires an active TaskEnvironment and
// EnsureMediaDirectoriesExists instance to complete reliably. // EnsureMediaDirectoriesExists instance to complete reliably.
void EnsureInitialized(base::Closure callback); void EnsureInitialized(base::OnceClosure callback);
// Return true if the storage monitor has already been initialized. // Return true if the storage monitor has already been initialized.
bool IsInitialized() const; bool IsInitialized() const;
...@@ -348,7 +348,7 @@ class MediaGalleriesPreferences ...@@ -348,7 +348,7 @@ class MediaGalleriesPreferences
void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs); void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs);
bool initialized_; bool initialized_;
std::vector<base::Closure> on_initialize_callbacks_; std::vector<base::OnceClosure> on_initialize_callbacks_;
// The profile that owns |this|. // The profile that owns |this|.
Profile* profile_; Profile* profile_;
......
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