#ifndef CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_
#define CHROME_BROWSER_DEVTOOLS_DEVTOOLS_FILE_HELPER_H_
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/uuid.h"
#include "chrome/browser/devtools/devtools_file_watcher.h"
#include "chrome/browser/platform_util.h"
#include "components/prefs/pref_change_registrar.h"
class GURL;
class Profile;
namespace base {
class FilePath;
class SequencedTaskRunner;
}
namespace ui {
struct SelectedFileInfo;
}
class DevToolsFileHelper {
public:
struct FileSystem {
FileSystem();
~FileSystem();
FileSystem(const FileSystem& other);
FileSystem(const std::string& type,
const std::string& file_system_name,
const std::string& root_url,
const std::string& file_system_path);
friend constexpr bool operator==(const FileSystem&,
const FileSystem&) = default;
std::string type;
std::string file_system_name;
std::string root_url;
std::string file_system_path;
};
class Delegate {
public:
virtual ~Delegate() = default;
virtual void FileSystemAdded(const std::string& error,
const FileSystem* file_system) = 0;
virtual void FileSystemRemoved(const std::string& file_system_path) = 0;
virtual void FilePathsChanged(
const std::vector<std::string>& changed_paths,
const std::vector<std::string>& added_paths,
const std::vector<std::string>& removed_paths) = 0;
};
class Storage {
public:
virtual ~Storage();
virtual FileSystem RegisterFileSystem(const base::FilePath& path,
const std::string& type) = 0;
virtual void UnregisterFileSystem(const base::FilePath& path) = 0;
virtual std::vector<base::FilePath> GetDraggedFileSystemPaths(
const GURL& file_system_url) = 0;
};
DevToolsFileHelper(Profile* profile, Delegate* delegate, Storage* storage);
DevToolsFileHelper(const DevToolsFileHelper&) = delete;
DevToolsFileHelper& operator=(const DevToolsFileHelper&) = delete;
~DevToolsFileHelper();
using CanceledCallback = base::OnceClosure;
using ConnectCallback = base::OnceCallback<void(bool)>;
using SaveCallback = base::OnceCallback<void(const std::string&)>;
using SelectedCallback =
base::OnceCallback<void(const ui::SelectedFileInfo&)>;
using SelectFileCallback =
base::OnceCallback<void(SelectedCallback selected_callback,
CanceledCallback canceled_callback,
const base::FilePath& default_path)>;
using HandlePermissionsCallback =
base::RepeatingCallback<void(const std::string&,
const std::u16string&,
base::OnceCallback<void(bool)>)>;
void Save(const std::string& url,
const std::string& content,
bool save_as,
bool is_base64,
SelectFileCallback select_file_callback,
SaveCallback save_callback,
CanceledCallback canceled_callback);
void Append(const std::string& url,
const std::string& content,
base::OnceClosure callback);
void AddFileSystem(const std::string& type,
SelectFileCallback select_file_callback,
const HandlePermissionsCallback& show_info_bar_callback);
void UpgradeDraggedFileSystemPermissions(
const std::string& file_system_url,
const HandlePermissionsCallback& show_info_bar_callback);
void ConnectAutomaticFileSystem(
const std::string& file_system_path,
const base::Uuid& file_system_uuid,
bool add_if_missing,
const HandlePermissionsCallback& show_info_bar_callback,
ConnectCallback connect_callback);
void DisconnectAutomaticFileSystem(const std::string& file_system_path);
std::vector<FileSystem> GetFileSystems();
void RemoveFileSystem(const std::string& file_system_path);
bool IsFileSystemAdded(const std::string& file_system_path);
void ShowItemInFolder(const std::string& file_system_path);
private:
void OnOpenItemComplete(const base::FilePath& path,
platform_util::OpenOperationResult result);
void SaveToFileSelected(const std::string& url,
const std::string& content,
bool is_base64,
SaveCallback callback,
const ui::SelectedFileInfo& file_info);
void InnerAddFileSystem(
const HandlePermissionsCallback& show_info_bar_callback,
const std::string& type,
const ui::SelectedFileInfo& file_info);
void AddUserConfirmedFileSystem(const std::string& type,
const base::FilePath& path,
bool allowed);
void ConnectMissingAutomaticFileSystem(
const std::string& file_system_path,
const base::Uuid& file_system_uuid,
const HandlePermissionsCallback& handle_permissions_callback,
ConnectCallback connect_callback,
bool directory_exists);
void ConnectUserConfirmedAutomaticFileSystem(
ConnectCallback connect_callback,
const std::string& file_system_path,
const base::Uuid& file_system_uuid,
bool allowed);
bool IsUserConfirmedAutomaticFileSystem(
const std::string& file_system_path,
const base::Uuid& file_system_uuid) const;
void FailedToAddFileSystem(const std::string& error);
void UpdateFileSystemPathsOnUI();
void FilePathsChanged(const std::vector<std::string>& changed_paths,
const std::vector<std::string>& added_paths,
const std::vector<std::string>& removed_paths);
using PathToType = std::map<std::string, std::string>;
PathToType GetActiveFileSystemPaths();
raw_ptr<Profile> profile_;
raw_ptr<DevToolsFileHelper::Delegate> delegate_;
raw_ptr<DevToolsFileHelper::Storage> storage_;
typedef std::map<std::string, ui::SelectedFileInfo> SelectedFileInfoMap;
SelectedFileInfoMap saved_files_;
PrefChangeRegistrar pref_change_registrar_;
PathToType file_system_paths_;
std::set<std::string> connected_automatic_file_systems_;
std::unique_ptr<DevToolsFileWatcher, DevToolsFileWatcher::Deleter>
file_watcher_;
scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
base::WeakPtrFactory<DevToolsFileHelper> weak_factory_{this};
};
#endif