#include "extensions/browser/api/file_handlers/mime_util.h"
#include <memory>
#include <string_view>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/browser_context.h"
#include "net/base/filename_util.h"
#include "net/base/mime_sniffer.h"
#include "net/base/mime_util.h"
#include "storage/browser/file_system/file_system_url.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/file_handlers/non_native_file_system_delegate.h"
#endif
namespace extensions::app_file_handler_util {
const char kMimeTypeApplicationOctetStream[] = …;
const char kMimeTypeInodeDirectory[] = …;
namespace {
void SniffMimeType(const base::FilePath& local_path, std::string* result) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
void OnGetMimeTypeFromFileForNonNativeLocalPathCompleted(
std::unique_ptr<std::string> mime_type,
base::OnceCallback<void(const std::string&)> callback) {
std::move(callback).Run(*mime_type);
}
void OnGetMimeTypeFromMetadataForNonNativeLocalPathCompleted(
const base::FilePath& local_path,
base::OnceCallback<void(const std::string&)> callback,
const std::optional<std::string>& mime_type) {
if (mime_type) {
std::move(callback).Run(mime_type.value());
return;
}
std::unique_ptr<std::string> mime_type_from_extension(new std::string);
std::string* const mime_type_from_extension_ptr =
mime_type_from_extension.get();
base::ThreadPool::PostTaskAndReply(
FROM_HERE, {base::MayBlock()},
base::BindOnce(base::IgnoreResult(&net::GetMimeTypeFromFile), local_path,
mime_type_from_extension_ptr),
base::BindOnce(&OnGetMimeTypeFromFileForNonNativeLocalPathCompleted,
std::move(mime_type_from_extension), std::move(callback)));
}
#endif
void OnSniffMimeTypeForNativeLocalPathCompleted(
std::unique_ptr<std::string> mime_type,
base::OnceCallback<void(const std::string&)> callback) { … }
}
void OnGetMimeTypeFromFileForNativeLocalPathCompleted(
const base::FilePath& local_path,
std::unique_ptr<std::string> mime_type,
base::OnceCallback<void(const std::string&)> callback) { … }
void GetMimeTypeForLocalPath(
content::BrowserContext* context,
const base::FilePath& local_path,
base::OnceCallback<void(const std::string&)> callback) { … }
MimeTypeCollector::MimeTypeCollector(content::BrowserContext* context)
: … { … }
MimeTypeCollector::~MimeTypeCollector() = default;
void MimeTypeCollector::CollectForURLs(
const std::vector<storage::FileSystemURL>& urls,
CompletionCallback callback) { … }
void MimeTypeCollector::CollectForLocalPaths(
const std::vector<base::FilePath>& local_paths,
CompletionCallback callback) { … }
void MimeTypeCollector::OnMimeTypeCollected(size_t index,
const std::string& mime_type) { … }
}