1// Aseprite
2// Copyright (C) 2019-2021 Igara Studio S.A.
3// Copyright (C) 2001-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifndef APP_CRASH_DATA_RECOVERY_H_INCLUDED
9#define APP_CRASH_DATA_RECOVERY_H_INCLUDED
10#pragma once
11
12#include "app/crash/recovery_config.h"
13#include "app/crash/session.h"
14#include "base/disable_copying.h"
15#include "obs/signal.h"
16
17#include <atomic>
18#include <mutex>
19#include <thread>
20#include <vector>
21
22namespace app {
23class Context;
24namespace crash {
25 class BackupObserver;
26
27 class DataRecovery {
28 public:
29 typedef std::vector<SessionPtr> Sessions;
30
31 DataRecovery(Context* context);
32 ~DataRecovery();
33
34 // Launches the thread to search for sessions.
35 void launchSearch();
36
37 bool isSearching() const { return m_searching; }
38
39 // Returns true if there is at least one sessions with sprites to
40 // recover (i.e. a crashed session were changes weren't saved)
41 bool hasRecoverySessions() const;
42
43 Session* activeSession() { return m_inProgress.get(); }
44
45 // Returns a copy of the list of sessions that can be recovered.
46 Sessions sessions();
47
48 // Triggered in the UI-thread from the m_thread using an
49 // ui::execute_from_ui_thread() when the list of sessions is ready
50 // to be used.
51 obs::signal<void()> SessionsListIsReady;
52
53 private:
54 // Executed from m_thread to search for the list of sessions.
55 void searchForSessions();
56
57 std::string m_sessionsDir;
58 mutable std::mutex m_sessionsMutex;
59 std::thread m_thread;
60 RecoveryConfig m_config;
61 Sessions m_sessions;
62 SessionPtr m_inProgress;
63 BackupObserver* m_backup;
64 std::atomic<bool> m_searching;
65
66 DISABLE_COPYING(DataRecovery);
67 };
68
69} // namespace crash
70} // namespace app
71
72#endif
73