xref: /haiku/src/servers/registrar/ShutdownProcess.h (revision 9d6d3fcf5fe8308cd020cecf89dede440346f8c4)
1 /*
2  * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net.
3  * Distributed under the terms of the MIT License.
4  *
5  * Manages the shutdown process.
6  */
7 #ifndef SHUTDOWN_PROCESS_H
8 #define SHUTDOWN_PROCESS_H
9 
10 #include <hash_set>
11 
12 #include <Looper.h>
13 
14 #include "AppInfoList.h"
15 #include "EventMaskWatcher.h"
16 #include "RosterAppInfo.h"
17 
18 #if __GNUC__ >= 4
19 using __gnu_cxx::hash_set;
20 #endif
21 
22 
23 class EventQueue;
24 class TRoster;
25 
26 // Note: EventMaskWatcher is inherited public due to a gcc bug/C++ feature:
27 // Once cast into a Watcher dynamic_cast<>()ing it back into an
28 // EventMaskWatcher fails otherwise.
29 class ShutdownProcess : public BLooper, public EventMaskWatcher {
30 public:
31 	ShutdownProcess(TRoster *roster, EventQueue *eventQueue);
32 	~ShutdownProcess();
33 
34 	status_t Init(BMessage *request);
35 
36 	virtual void MessageReceived(BMessage *message);
37 
38 	static void SendReply(BMessage *request, status_t error);
39 
40 private:
41 	void _SendReply(status_t error);
42 
43 	void _NegativeQuitRequestReply(thread_id thread);
44 
45 	void _PrepareShutdownMessage(BMessage &message) const;
46 	status_t _ShutDown();
47 
48 	status_t _PushEvent(uint32 eventType, team_id team, int32 phase);
49 	status_t _GetNextEvent(uint32 &eventType, team_id &team, int32 &phase,
50 		bool block);
51 
52 	void _SetPhase(int32 phase);
53 	void _ScheduleTimeoutEvent(bigtime_t timeout, team_id team = -1);
54 
55 	bool _LockAppLists();
56 	void _UnlockAppLists();
57 
58 	void _InitShutdownWindow();
59 	void _SetShowShutdownWindow(bool show);
60 	void _AddShutdownWindowApps(AppInfoList &infos);
61 	void _RemoveShutdownWindowApp(team_id team);
62 	void _SetShutdownWindowCurrentApp(team_id team);
63 	void _SetShutdownWindowText(const char *text);
64 	void _SetShutdownWindowCancelButtonEnabled(bool enabled);
65 	void _SetShutdownWindowKillButtonEnabled(bool enabled);
66 	void _SetShutdownWindowWaitForShutdown();
67 	void _SetShutdownWindowWaitForAbortedOK();
68 
69 	static status_t _WorkerEntry(void *data);
70 	status_t _Worker();
71 
72 	void _WorkerDoShutdown();
73 	void _QuitApps(AppInfoList &list, bool disableCancel);
74 	void _QuitBackgroundApps();
75 	void _WaitForBackgroundApps();
76 	void _KillBackgroundApps();
77 	void _QuitNonApps();
78 	void _QuitBlockingApp(AppInfoList &list, team_id team, const char *appName,
79 		bool cancelAllowed);
80 	void _DisplayAbortingApp(team_id team);
81 
82 private:
83 	class TimeoutEvent;
84 	class InternalEvent;
85 	struct InternalEventList;
86 	class QuitRequestReplyHandler;
87 	class ShutdownWindow;
88 
89 	friend class QuitRequestReplyHandler;
90 
91 	BLocker					fWorkerLock;	// protects fields shared by looper
92 											// and worker
93 	BMessage				*fRequest;
94 	TRoster					*fRoster;
95 	EventQueue				*fEventQueue;
96 	hash_set<team_id>		fVitalSystemApps;
97 	AppInfoList				fSystemApps;
98 	AppInfoList				fUserApps;
99 	AppInfoList				fBackgroundApps;
100 	//RosterAppInfo			fInputServer;
101 	TimeoutEvent			*fTimeoutEvent;
102 	InternalEventList		*fInternalEvents;
103 	sem_id					fInternalEventSemaphore;
104 	QuitRequestReplyHandler	*fQuitRequestReplyHandler;
105 	thread_id				fWorker;
106 	int32					fCurrentPhase;
107 	status_t				fShutdownError;
108 	bool					fHasGUI;
109 	bool					fReboot;
110 	bool					fRequestReplySent;
111 	ShutdownWindow			*fWindow;
112 };
113 
114 #endif	// SHUTDOWN_PROCESS_H
115