Commit d37ba940 authored by Stuart Langley's avatar Stuart Langley Committed by Commit Bot

Replace (bool, const std::string&) with base::Optional<std::string>

As per the attached bug, replace usage of Replace (bool, const std::string&) in
filesystem_api_util.cc with base::Optional<std::string> for code health.

Also, change from RepeatingCallback to OnceCallback for all filesystem_api_util
defined methods.

Also, fix linter warnings.

There are no logic changes.

Bug: 729442, 875700
Change-Id: Ib27634b9ba1372cb007ba6a190608ec4128bbeb3
Reviewed-on: https://chromium-review.googlesource.com/1215425Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Stuart Langley <slangley@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589842}
parent 1e7d2679
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h"
#include <memory> #include <memory>
#include <utility>
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file.h" #include "base/files/file.h"
...@@ -35,53 +36,52 @@ namespace { ...@@ -35,53 +36,52 @@ namespace {
// Helper function used to implement GetNonNativeLocalPathMimeType. It extracts // Helper function used to implement GetNonNativeLocalPathMimeType. It extracts
// the mime type from the passed Drive resource entry. // the mime type from the passed Drive resource entry.
void GetMimeTypeAfterGetResourceEntryForDrive( void GetMimeTypeAfterGetResourceEntryForDrive(
const base::Callback<void(bool, const std::string&)>& callback, base::OnceCallback<void(const base::Optional<std::string>&)> callback,
drive::FileError error, drive::FileError error,
std::unique_ptr<drive::ResourceEntry> entry) { std::unique_ptr<drive::ResourceEntry> entry) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (error != drive::FILE_ERROR_OK || !entry->has_file_specific_info() || if (error != drive::FILE_ERROR_OK || !entry->has_file_specific_info() ||
entry->file_specific_info().content_mime_type().empty()) { entry->file_specific_info().content_mime_type().empty()) {
callback.Run(false, std::string()); std::move(callback).Run(base::nullopt);
return; return;
} }
callback.Run(true, entry->file_specific_info().content_mime_type()); std::move(callback).Run(entry->file_specific_info().content_mime_type());
} }
// Helper function used to implement GetNonNativeLocalPathMimeType. It extracts // Helper function used to implement GetNonNativeLocalPathMimeType. It extracts
// the mime type from the passed metadata from a providing extension. // the mime type from the passed metadata from a providing extension.
void GetMimeTypeAfterGetMetadataForProvidedFileSystem( void GetMimeTypeAfterGetMetadataForProvidedFileSystem(
const base::Callback<void(bool, const std::string&)>& callback, base::OnceCallback<void(const base::Optional<std::string>&)> callback,
std::unique_ptr<chromeos::file_system_provider::EntryMetadata> metadata, std::unique_ptr<chromeos::file_system_provider::EntryMetadata> metadata,
base::File::Error result) { base::File::Error result) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (result != base::File::FILE_OK || !metadata->mime_type.get()) { if (result != base::File::FILE_OK || !metadata->mime_type.get()) {
callback.Run(false, std::string()); std::move(callback).Run(base::nullopt);
return; return;
} }
callback.Run(true, *metadata->mime_type); std::move(callback).Run(*metadata->mime_type);
} }
// Helper function used to implement GetNonNativeLocalPathMimeType. It passes // Helper function used to implement GetNonNativeLocalPathMimeType. It passes
// the returned mime type to the callback. // the returned mime type to the callback.
void GetMimeTypeAfterGetMimeTypeForArcContentFileSystem( void GetMimeTypeAfterGetMimeTypeForArcContentFileSystem(
const base::Callback<void(bool, const std::string&)>& callback, base::OnceCallback<void(const base::Optional<std::string>&)> callback,
const base::Optional<std::string>& mime_type) { const base::Optional<std::string>& mime_type) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (mime_type.has_value()) { if (mime_type.has_value()) {
callback.Run(true, mime_type.value()); std::move(callback).Run(mime_type.value());
} else { } else {
callback.Run(false, std::string()); std::move(callback).Run(base::nullopt);
} }
} }
// Helper function to converts a callback that takes boolean value to that takes // Helper function to converts a callback that takes boolean value to that takes
// File::Error, by regarding FILE_OK as the only successful value. // File::Error, by regarding FILE_OK as the only successful value.
void BoolCallbackAsFileErrorCallback( void BoolCallbackAsFileErrorCallback(base::OnceCallback<void(bool)> callback,
const base::Callback<void(bool)>& callback, base::File::Error error) {
base::File::Error error) { return std::move(callback).Run(error == base::File::FILE_OK);
return callback.Run(error == base::File::FILE_OK);
} }
// Part of PrepareFileOnIOThread. It tries to create a new file if the given // Part of PrepareFileOnIOThread. It tries to create a new file if the given
...@@ -112,15 +112,14 @@ void PrepareFileAfterCheckExistOnIOThread( ...@@ -112,15 +112,14 @@ void PrepareFileAfterCheckExistOnIOThread(
void PrepareFileOnIOThread( void PrepareFileOnIOThread(
scoped_refptr<storage::FileSystemContext> file_system_context, scoped_refptr<storage::FileSystemContext> file_system_context,
const storage::FileSystemURL& url, const storage::FileSystemURL& url,
const base::Callback<void(bool)>& callback) { base::OnceCallback<void(bool)> callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO); DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
file_system_context->operation_runner()->FileExists( file_system_context->operation_runner()->FileExists(
url, url, base::Bind(&PrepareFileAfterCheckExistOnIOThread,
base::Bind(&PrepareFileAfterCheckExistOnIOThread, file_system_context, url,
file_system_context, base::Bind(&BoolCallbackAsFileErrorCallback,
url, base::Passed(std::move(callback)))));
base::Bind(&BoolCallbackAsFileErrorCallback, callback)));
} }
} // namespace } // namespace
...@@ -160,7 +159,7 @@ bool IsUnderNonNativeLocalPath(Profile* profile, ...@@ -160,7 +159,7 @@ bool IsUnderNonNativeLocalPath(Profile* profile,
void GetNonNativeLocalPathMimeType( void GetNonNativeLocalPathMimeType(
Profile* profile, Profile* profile,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool, const std::string&)>& callback) { base::OnceCallback<void(const base::Optional<std::string>&)> callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(IsUnderNonNativeLocalPath(profile, path)); DCHECK(IsUnderNonNativeLocalPath(profile, path));
...@@ -170,13 +169,14 @@ void GetNonNativeLocalPathMimeType( ...@@ -170,13 +169,14 @@ void GetNonNativeLocalPathMimeType(
if (!file_system) { if (!file_system) {
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE,
base::BindOnce(callback, false, std::string())); base::BindOnce(std::move(callback), base::nullopt));
return; return;
} }
file_system->GetResourceEntry( file_system->GetResourceEntry(
drive::util::ExtractDrivePath(path), drive::util::ExtractDrivePath(path),
base::BindOnce(&GetMimeTypeAfterGetResourceEntryForDrive, callback)); base::BindOnce(&GetMimeTypeAfterGetResourceEntryForDrive,
std::move(callback)));
return; return;
} }
...@@ -186,7 +186,7 @@ void GetNonNativeLocalPathMimeType( ...@@ -186,7 +186,7 @@ void GetNonNativeLocalPathMimeType(
if (!parser.Parse()) { if (!parser.Parse()) {
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE,
base::BindOnce(callback, false, std::string())); base::BindOnce(std::move(callback), base::nullopt));
return; return;
} }
...@@ -194,8 +194,8 @@ void GetNonNativeLocalPathMimeType( ...@@ -194,8 +194,8 @@ void GetNonNativeLocalPathMimeType(
parser.file_path(), parser.file_path(),
chromeos::file_system_provider::ProvidedFileSystemInterface:: chromeos::file_system_provider::ProvidedFileSystemInterface::
METADATA_FIELD_MIME_TYPE, METADATA_FIELD_MIME_TYPE,
base::Bind(&GetMimeTypeAfterGetMetadataForProvidedFileSystem, base::BindOnce(&GetMimeTypeAfterGetMetadataForProvidedFileSystem,
callback)); std::move(callback)));
return; return;
} }
...@@ -207,12 +207,13 @@ void GetNonNativeLocalPathMimeType( ...@@ -207,12 +207,13 @@ void GetNonNativeLocalPathMimeType(
if (!runner) { if (!runner) {
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE,
base::BindOnce(callback, false, std::string())); base::BindOnce(std::move(callback), base::nullopt));
return; return;
} }
runner->GetMimeType( runner->GetMimeType(
arc_url, base::Bind(&GetMimeTypeAfterGetMimeTypeForArcContentFileSystem, arc_url,
callback)); base::BindOnce(&GetMimeTypeAfterGetMimeTypeForArcContentFileSystem,
std::move(callback)));
return; return;
} }
...@@ -221,25 +222,25 @@ void GetNonNativeLocalPathMimeType( ...@@ -221,25 +222,25 @@ void GetNonNativeLocalPathMimeType(
// file extensions. // file extensions.
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE,
base::BindOnce(callback, false /* failure */, std::string())); base::BindOnce(std::move(callback), base::nullopt));
} }
void IsNonNativeLocalPathDirectory( void IsNonNativeLocalPathDirectory(Profile* profile,
Profile* profile, const base::FilePath& path,
const base::FilePath& path, base::OnceCallback<void(bool)> callback) {
const base::Callback<void(bool)>& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(IsUnderNonNativeLocalPath(profile, path)); DCHECK(IsUnderNonNativeLocalPath(profile, path));
util::CheckIfDirectoryExists( util::CheckIfDirectoryExists(
GetFileSystemContextForExtensionId(profile, kFileManagerAppId), path, GetFileSystemContextForExtensionId(profile, kFileManagerAppId), path,
base::Bind(&BoolCallbackAsFileErrorCallback, callback)); base::Bind(&BoolCallbackAsFileErrorCallback,
base::Passed(std::move(callback))));
} }
void PrepareNonNativeLocalFileForWritableApp( void PrepareNonNativeLocalFileForWritableApp(
Profile* profile, Profile* profile,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool)>& callback) { base::OnceCallback<void(bool)> callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(IsUnderNonNativeLocalPath(profile, path)); DCHECK(IsUnderNonNativeLocalPath(profile, path));
...@@ -248,8 +249,9 @@ void PrepareNonNativeLocalFileForWritableApp( ...@@ -248,8 +249,9 @@ void PrepareNonNativeLocalFileForWritableApp(
profile, path, kFileManagerAppId, &url)) { profile, path, kFileManagerAppId, &url)) {
// Posting to the current thread, so that we always call back asynchronously // Posting to the current thread, so that we always call back asynchronously
// independent from whether or not the operation succeeds. // independent from whether or not the operation succeeds.
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, content::BrowserThread::PostTask(
base::BindOnce(callback, false)); content::BrowserThread::UI, FROM_HERE,
base::BindOnce(std::move(callback), false));
return; return;
} }
...@@ -265,7 +267,7 @@ void PrepareNonNativeLocalFileForWritableApp( ...@@ -265,7 +267,7 @@ void PrepareNonNativeLocalFileForWritableApp(
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&PrepareFileOnIOThread, file_system_context, internal_url, base::BindOnce(&PrepareFileOnIOThread, file_system_context, internal_url,
google_apis::CreateRelayCallback(callback))); google_apis::CreateRelayCallback(std::move(callback))));
} }
} // namespace util } // namespace util
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/optional.h"
#include "storage/common/fileapi/file_system_types.h" #include "storage/common/fileapi/file_system_types.h"
class Profile; class Profile;
...@@ -36,14 +37,13 @@ bool IsUnderNonNativeLocalPath(Profile* profile, const base::FilePath& path); ...@@ -36,14 +37,13 @@ bool IsUnderNonNativeLocalPath(Profile* profile, const base::FilePath& path);
void GetNonNativeLocalPathMimeType( void GetNonNativeLocalPathMimeType(
Profile* profile, Profile* profile,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool, const std::string&)>& callback); base::OnceCallback<void(const base::Optional<std::string>&)> callback);
// Checks whether the |path| points to a directory, and asynchronously sends // Checks whether the |path| points to a directory, and asynchronously sends
// the result to |callback|. // the result to |callback|.
void IsNonNativeLocalPathDirectory( void IsNonNativeLocalPathDirectory(Profile* profile,
Profile* profile, const base::FilePath& path,
const base::FilePath& path, base::OnceCallback<void(bool)> callback);
const base::Callback<void(bool)>& callback);
// Ensures a file exists at |path|, i.e., it does nothing if a file is already // Ensures a file exists at |path|, i.e., it does nothing if a file is already
// present, or creates a file there if it isn't, and asynchronously sends to // present, or creates a file there if it isn't, and asynchronously sends to
...@@ -51,7 +51,7 @@ void IsNonNativeLocalPathDirectory( ...@@ -51,7 +51,7 @@ void IsNonNativeLocalPathDirectory(
void PrepareNonNativeLocalFileForWritableApp( void PrepareNonNativeLocalFileForWritableApp(
Profile* profile, Profile* profile,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool)>& callback); base::OnceCallback<void(bool)> callback);
} // namespace util } // namespace util
} // namespace file_manager } // namespace file_manager
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "chrome/browser/extensions/api/file_handlers/non_native_file_system_delegate_chromeos.h" #include "chrome/browser/extensions/api/file_handlers/non_native_file_system_delegate_chromeos.h"
#include <string>
#include <utility>
#include "chrome/browser/chromeos/file_manager/filesystem_api_util.h" #include "chrome/browser/chromeos/file_manager/filesystem_api_util.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
...@@ -24,9 +27,9 @@ bool NonNativeFileSystemDelegateChromeOS::IsUnderNonNativeLocalPath( ...@@ -24,9 +27,9 @@ bool NonNativeFileSystemDelegateChromeOS::IsUnderNonNativeLocalPath(
void NonNativeFileSystemDelegateChromeOS::GetNonNativeLocalPathMimeType( void NonNativeFileSystemDelegateChromeOS::GetNonNativeLocalPathMimeType(
content::BrowserContext* context, content::BrowserContext* context,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool, const std::string&)>& callback) { base::OnceCallback<void(const base::Optional<std::string>&)> callback) {
return file_manager::util::GetNonNativeLocalPathMimeType( return file_manager::util::GetNonNativeLocalPathMimeType(
Profile::FromBrowserContext(context), path, callback); Profile::FromBrowserContext(context), path, std::move(callback));
} }
// Checks whether |path| points to a non-local filesystem directory and calls // Checks whether |path| points to a non-local filesystem directory and calls
......
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
#ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_CHROMEOS_H_ #ifndef CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_CHROMEOS_H_
#define CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_CHROMEOS_H_ #define CHROME_BROWSER_EXTENSIONS_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_CHROMEOS_H_
#include <string>
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/optional.h"
#include "extensions/browser/api/file_handlers/non_native_file_system_delegate.h" #include "extensions/browser/api/file_handlers/non_native_file_system_delegate.h"
namespace content { namespace content {
...@@ -27,7 +30,8 @@ class NonNativeFileSystemDelegateChromeOS ...@@ -27,7 +30,8 @@ class NonNativeFileSystemDelegateChromeOS
void GetNonNativeLocalPathMimeType( void GetNonNativeLocalPathMimeType(
content::BrowserContext* context, content::BrowserContext* context,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool, const std::string&)>& callback) override; base::OnceCallback<void(const base::Optional<std::string>&)> callback)
override;
void IsNonNativeLocalPathDirectory( void IsNonNativeLocalPathDirectory(
content::BrowserContext* context, content::BrowserContext* context,
const base::FilePath& path, const base::FilePath& path,
......
...@@ -61,10 +61,9 @@ void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted( ...@@ -61,10 +61,9 @@ void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted(
void OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted( void OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted(
const base::FilePath& local_path, const base::FilePath& local_path,
const base::Callback<void(const std::string&)>& callback, const base::Callback<void(const std::string&)>& callback,
bool success, const base::Optional<std::string>& mime_type) {
const std::string& mime_type) { if (mime_type) {
if (success) { callback.Run(mime_type.value());
callback.Run(mime_type);
return; return;
} }
...@@ -136,8 +135,8 @@ void GetMimeTypeForLocalPath( ...@@ -136,8 +135,8 @@ void GetMimeTypeForLocalPath(
// it can be very slow). // it can be very slow).
delegate->GetNonNativeLocalPathMimeType( delegate->GetNonNativeLocalPathMimeType(
context, local_path, context, local_path,
base::Bind(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted, base::BindOnce(&OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted,
local_path, callback)); local_path, callback));
return; return;
} }
#endif #endif
......
...@@ -5,8 +5,11 @@ ...@@ -5,8 +5,11 @@
#ifndef EXTENSIONS_BROWSER_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_H_ #ifndef EXTENSIONS_BROWSER_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_H_
#define EXTENSIONS_BROWSER_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_H_ #define EXTENSIONS_BROWSER_API_FILE_HANDLERS_NON_NATIVE_FILE_SYSTEM_DELEGATE_H_
#include <string>
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/optional.h"
namespace content { namespace content {
class BrowserContext; class BrowserContext;
...@@ -30,7 +33,8 @@ class NonNativeFileSystemDelegate { ...@@ -30,7 +33,8 @@ class NonNativeFileSystemDelegate {
virtual void GetNonNativeLocalPathMimeType( virtual void GetNonNativeLocalPathMimeType(
content::BrowserContext* context, content::BrowserContext* context,
const base::FilePath& path, const base::FilePath& path,
const base::Callback<void(bool, const std::string&)>& callback) = 0; base::OnceCallback<void(const base::Optional<std::string>&)>
callback) = 0;
// Checks whether |path| points to a non-local filesystem directory and calls // Checks whether |path| points to a non-local filesystem directory and calls
// |callback| with the result asynchronously. // |callback| with the result asynchronously.
......
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