1// Aseprite
2// Copyright (C) 2019-2020 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_BACKUP_OBSERVER_H_INCLUDED
9#define APP_CRASH_BACKUP_OBSERVER_H_INCLUDED
10#pragma once
11
12#include "app/context_observer.h"
13#include "app/doc_observer.h"
14#include "app/docs_observer.h"
15
16#include <atomic>
17#include <condition_variable>
18#include <mutex>
19#include <thread>
20#include <vector>
21
22namespace app {
23class Context;
24class Doc;
25namespace crash {
26 struct RecoveryConfig;
27 class Session;
28
29 class BackupObserver : public ContextObserver
30 , public DocsObserver
31 , public DocObserver {
32 public:
33 BackupObserver(RecoveryConfig* config,
34 Session* session,
35 Context* ctx);
36 ~BackupObserver();
37
38 void stop();
39
40 void onAddDocument(Doc* document) override;
41 void onRemoveDocument(Doc* document) override;
42
43 private:
44 void backgroundThread();
45 bool saveDocData(Doc* doc);
46
47 RecoveryConfig* m_config;
48 Session* m_session;
49 Context* m_ctx;
50 std::vector<Doc*> m_documents;
51 std::vector<Doc*> m_closedDocs;
52 std::atomic<bool> m_done;
53
54 std::mutex m_mutex;
55
56 // Used to wakeup the backgroundThread() when we have to stop the
57 // thread that saves backups (i.e. when we are closing the application).
58 std::condition_variable m_wakeup;
59
60 std::thread m_thread;
61 };
62
63} // namespace crash
64} // namespace app
65
66#endif
67