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() {
// Checks whether the MediaGalleries API is currently accessible (it may be
// disallowed even if an extension has the requisite permission). Then
// 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()) {
*error =
std::string(kDisallowedByPolicy) + prefs::kAllowFileSelectionDialogs;
......@@ -120,7 +120,7 @@ bool Setup(Profile* profile, std::string* error, base::Closure callback) {
MediaGalleriesPreferences* preferences =
media_file_system_registry()->GetPreferences(profile);
preferences->EnsureInitialized(callback);
preferences->EnsureInitialized(std::move(callback));
return true;
}
......@@ -407,8 +407,9 @@ bool MediaGalleriesGetMediaFileSystemsFunction::RunAsync() {
return Setup(
GetProfile(), &error_,
base::Bind(&MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit,
this, interactive));
base::BindOnce(
&MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit, this,
interactive));
}
void MediaGalleriesGetMediaFileSystemsFunction::OnPreferencesInit(
......@@ -508,7 +509,7 @@ bool MediaGalleriesAddUserSelectedFolderFunction::RunAsync() {
::media_galleries::UsageCount(::media_galleries::ADD_USER_SELECTED_FOLDER);
return Setup(
GetProfile(), &error_,
base::Bind(
base::BindOnce(
&MediaGalleriesAddUserSelectedFolderFunction::OnPreferencesInit,
this));
}
......@@ -618,9 +619,10 @@ bool MediaGalleriesGetMetadataFunction::RunAsync() {
if (!options)
return false;
return Setup(GetProfile(), &error_,
base::Bind(&MediaGalleriesGetMetadataFunction::OnPreferencesInit,
this, options->metadata_type, blob_uuid));
return Setup(
GetProfile(), &error_,
base::BindOnce(&MediaGalleriesGetMetadataFunction::OnPreferencesInit,
this, options->metadata_type, blob_uuid));
}
void MediaGalleriesGetMetadataFunction::OnPreferencesInit(
......@@ -798,8 +800,8 @@ bool MediaGalleriesAddGalleryWatchFunction::RunAsync() {
g_browser_process->media_file_system_registry()->GetPreferences(
GetProfile());
preferences->EnsureInitialized(
base::Bind(&MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit,
this, params->gallery_id));
base::BindOnce(&MediaGalleriesAddGalleryWatchFunction::OnPreferencesInit,
this, params->gallery_id));
return true;
}
......@@ -873,9 +875,9 @@ bool MediaGalleriesRemoveGalleryWatchFunction::RunAsync() {
MediaGalleriesPreferences* preferences =
g_browser_process->media_file_system_registry()->GetPreferences(
GetProfile());
preferences->EnsureInitialized(
base::Bind(&MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit,
this, params->gallery_id));
preferences->EnsureInitialized(base::BindOnce(
&MediaGalleriesRemoveGalleryWatchFunction::OnPreferencesInit, this,
params->gallery_id));
return true;
}
......
......@@ -112,9 +112,9 @@ void AttemptAutoMountOnUIThread(
profile);
// 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.
preferences->EnsureInitialized(base::Bind(
preferences->EnsureInitialized(base::BindOnce(
&OnPreferencesInit, web_contents_getter, base::RetainedRef(extension),
pref_id, base::Passed(&callback)));
pref_id, std::move(callback)));
return;
}
}
......
......@@ -71,9 +71,9 @@ MediaGalleriesPermissionController::MediaGalleriesPermissionController(
// Passing unretained pointer is safe, since the dialog controller
// is self-deleting, and so won't be deleted until it can be shown
// and then closed.
preferences_->EnsureInitialized(
base::Bind(&MediaGalleriesPermissionController::OnPreferencesInitialized,
base::Unretained(this)));
preferences_->EnsureInitialized(base::BindOnce(
&MediaGalleriesPermissionController::OnPreferencesInitialized,
base::Unretained(this)));
// Unretained is safe because |this| owns |context_menu_|.
context_menu_.reset(
......
......@@ -462,16 +462,16 @@ MediaGalleriesPreferences::~MediaGalleriesPreferences() {
StorageMonitor::GetInstance()->RemoveObserver(this);
}
void MediaGalleriesPreferences::EnsureInitialized(base::Closure callback) {
void MediaGalleriesPreferences::EnsureInitialized(base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (IsInitialized()) {
if (!callback.is_null())
callback.Run();
if (callback)
std::move(callback).Run();
return;
}
on_initialize_callbacks_.push_back(callback);
on_initialize_callbacks_.push_back(std::move(callback));
if (on_initialize_callbacks_.size() > 1)
return;
......@@ -572,10 +572,8 @@ void MediaGalleriesPreferences::OnStorageMonitorInit(
existing_devices[i].total_size_in_bytes(), base::Time::Now(), 0, 0, 0);
}
for (auto iter = on_initialize_callbacks_.begin();
iter != on_initialize_callbacks_.end(); ++iter) {
iter->Run();
}
for (base::OnceClosure& callback : on_initialize_callbacks_)
std::move(callback).Run();
on_initialize_callbacks_.clear();
}
......
......@@ -188,7 +188,7 @@ class MediaGalleriesPreferences
// This call also ensures that the StorageMonitor is initialized.
// Note for unit tests: This requires an active TaskEnvironment and
// 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.
bool IsInitialized() const;
......@@ -348,7 +348,7 @@ class MediaGalleriesPreferences
void SetExtensionPrefsForTesting(extensions::ExtensionPrefs* extension_prefs);
bool initialized_;
std::vector<base::Closure> on_initialize_callbacks_;
std::vector<base::OnceClosure> on_initialize_callbacks_;
// The profile that owns |this|.
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