1// Aseprite
2// Copyright (C) 2019-2022 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_SESSION_H_INCLUDED
9#define APP_CRASH_SESSION_H_INCLUDED
10#pragma once
11
12#include "app/crash/raw_images_as.h"
13#include "base/disable_copying.h"
14#include "base/process.h"
15#include "base/task.h"
16#include "doc/object_id.h"
17
18#include <fstream>
19#include <memory>
20#include <string>
21#include <vector>
22
23namespace app {
24class Doc;
25namespace crash {
26 struct RecoveryConfig;
27
28 // A class to record/restore session information.
29 class Session {
30 public:
31 class Backup {
32 public:
33 Backup(const std::string& dir);
34 const std::string& dir() const { return m_dir; }
35 std::string description(const bool withFullPath) const;
36 private:
37 std::string m_dir;
38 std::string m_desc;
39 std::string m_fn;
40 };
41 using BackupPtr = std::shared_ptr<Backup>;
42 using Backups = std::vector<BackupPtr>;
43
44 Session(RecoveryConfig* config,
45 const std::string& path);
46 ~Session();
47
48 std::string name() const;
49 std::string version();
50 const Backups& backups();
51
52 bool isRunning();
53 bool isCrashedSession();
54 bool isOldSession();
55 bool isEmpty();
56
57 void create(base::pid pid);
58 void close();
59 void removeFromDisk();
60
61 bool saveDocumentChanges(Doc* doc);
62 void removeDocument(Doc* doc);
63
64 Doc* restoreBackupDoc(const BackupPtr& backup,
65 base::task_token* t);
66 Doc* restoreBackupById(const doc::ObjectId id, base::task_token* t);
67 Doc* restoreBackupDocById(const doc::ObjectId id, base::task_token* t);
68 Doc* restoreBackupRawImages(const BackupPtr& backup,
69 const RawImagesAs as, base::task_token* t);
70 void deleteBackup(const BackupPtr& backup);
71
72 private:
73 Doc* restoreBackupDoc(const std::string& backupDir,
74 base::task_token* t);
75 void loadPid();
76 std::string pidFilename() const;
77 std::string verFilename() const;
78 void markDocumentAsCorrectlyClosed(Doc* doc);
79 void deleteDirectory(const std::string& dir);
80 void fixFilename(Doc* doc);
81
82 base::pid m_pid;
83 std::string m_path;
84 std::string m_version;
85 Backups m_backups;
86 RecoveryConfig* m_config;
87
88 DISABLE_COPYING(Session);
89 };
90
91 typedef std::shared_ptr<Session> SessionPtr;
92
93} // namespace crash
94} // namespace app
95
96#endif
97