#ifndef COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_
#define COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_
#include <memory>
#include <utility>
#include "base/component_export.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "components/app_restore/app_restore_arc_info.h"
#include "components/app_restore/arc_read_handler.h"
#include "components/app_restore/full_restore_utils.h"
#include "ui/aura/env.h"
#include "ui/aura/env_observer.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
namespace app_restore {
struct AppLaunchInfo;
struct AppRestoreData;
class RestoreData;
struct WindowInfo;
}
namespace ash {
class AppLaunchInfoSaveWaiter;
namespace full_restore {
class FullRestoreAppLaunchHandlerTestBase;
class FullRestoreServiceTestHavingFullRestoreFile;
}
}
namespace full_restore {
class FullRestoreFileHandler;
class COMPONENT_EXPORT(APP_RESTORE) FullRestoreReadHandler
: public aura::EnvObserver,
public aura::WindowObserver,
public app_restore::ArcReadHandler::Delegate,
public app_restore::AppRestoreArcInfo::Observer {
public:
using Callback =
base::OnceCallback<void(std::unique_ptr<app_restore::RestoreData>)>;
static FullRestoreReadHandler* GetInstance();
FullRestoreReadHandler();
FullRestoreReadHandler(const FullRestoreReadHandler&) = delete;
FullRestoreReadHandler& operator=(const FullRestoreReadHandler&) = delete;
~FullRestoreReadHandler() override;
app_restore::ArcReadHandler* arc_read_handler() {
return arc_read_handler_.get();
}
void OnWindowInitialized(aura::Window* window) override;
void OnWindowDestroyed(aura::Window* window) override;
std::unique_ptr<app_restore::AppLaunchInfo> GetAppLaunchInfo(
const base::FilePath& profile_path,
const std::string& app_id,
int32_t restore_window_id) override;
std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(
const base::FilePath& profile_path,
const std::string& app_id,
int32_t restore_window_id) override;
void RemoveAppRestoreData(const base::FilePath& profile_path,
const std::string& app_id,
int32_t restore_window_id) override;
void OnTaskCreated(const std::string& app_id,
int32_t task_id,
int32_t session_id) override;
void OnTaskDestroyed(int32_t task_id) override;
void SetPrimaryProfilePath(const base::FilePath& profile_path);
void SetActiveProfilePath(const base::FilePath& profile_path);
void SetCheckRestoreData(const base::FilePath& profile_path);
void ReadFromFile(const base::FilePath& profile_path, Callback callback);
void SetNextRestoreWindowIdForChromeApp(const base::FilePath& profile_path,
const std::string& app_id);
void RemoveApp(const base::FilePath& profile_path, const std::string& app_id);
bool HasAppTypeBrowser(const base::FilePath& profile_path);
bool HasBrowser(const base::FilePath& profile_path);
bool HasWindowInfo(int32_t restore_window_id);
std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(aura::Window* window);
std::unique_ptr<app_restore::WindowInfo> GetWindowInfoForActiveProfile(
int32_t restore_window_id);
std::unique_ptr<app_restore::AppLaunchInfo> GetArcAppLaunchInfo(
const std::string& app_id,
int32_t session_id);
int32_t FetchRestoreWindowId(const std::string& app_id);
int32_t GetArcRestoreWindowIdForTaskId(int32_t task_id);
int32_t GetArcRestoreWindowIdForSessionId(int32_t session_id);
void SetArcSessionIdForWindowId(int32_t arc_session_id, int32_t window_id);
void SetStartTimeForProfile(const base::FilePath& profile_path);
bool IsFullRestoreRunning() const;
void AddChromeBrowserLaunchInfoForTesting(const base::FilePath& profile_path);
private:
friend class ash::AppLaunchInfoSaveWaiter;
friend class ash::full_restore::FullRestoreAppLaunchHandlerTestBase;
friend class ash::full_restore::FullRestoreServiceTestHavingFullRestoreFile;
friend class FullRestoreReadHandlerTestApi;
std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(
int32_t restore_window_id);
bool IsArcRestoreRunning() const;
void OnGetRestoreData(const base::FilePath& profile_path,
Callback callback,
std::unique_ptr<app_restore::RestoreData>);
void RemoveAppRestoreData(int32_t restore_window_id);
app_restore::RestoreData* GetRestoreData(const base::FilePath& profile_path);
base::FilePath primary_profile_path_;
base::FilePath active_profile_path_;
base::flat_map<base::FilePath, std::unique_ptr<app_restore::RestoreData>>
profile_path_to_restore_data_;
base::flat_map<int32_t, std::pair<base::FilePath, std::string>>
window_id_to_app_restore_info_;
base::flat_map<base::FilePath, base::TimeTicks>
profile_path_to_start_time_data_;
std::unique_ptr<app_restore::ArcReadHandler> arc_read_handler_;
std::set<base::FilePath> should_check_restore_data_;
base::ScopedObservation<aura::Env, aura::EnvObserver> env_observer_{this};
base::ScopedMultiSourceObservation<aura::Window, aura::WindowObserver>
observed_windows_{this};
base::ScopedObservation<app_restore::AppRestoreArcInfo,
app_restore::AppRestoreArcInfo::Observer>
arc_info_observer_{this};
base::WeakPtrFactory<FullRestoreReadHandler> weak_factory_{this};
};
}
#endif