1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef APPS_APP_RESTORE_SERVICE_H_
6 #define APPS_APP_RESTORE_SERVICE_H_
11 #include "apps/app_lifetime_monitor.h"
12 #include "base/memory/raw_ptr.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "extensions/browser/app_window/app_window_registry.h"
16 namespace extensions {
26 // Tracks what apps need to be restarted when the browser restarts.
27 class AppRestoreService : public KeyedService,
28 public AppLifetimeMonitor::Observer {
30 // Returns true if apps should be restored on the current platform, given
31 // whether this new browser process launched due to a restart.
32 static bool ShouldRestoreApps(bool is_browser_restart);
34 explicit AppRestoreService(content::BrowserContext* context);
35 AppRestoreService(const AppRestoreService&) = delete;
36 AppRestoreService& operator=(const AppRestoreService&) = delete;
38 // Restart apps that need to be restarted and clear the "running" preference
39 // from apps to prevent them being restarted in subsequent restarts.
40 void HandleStartup(bool should_restore_apps);
42 // Returns whether this extension is running or, at startup, whether it was
43 // running when Chrome was last terminated.
44 bool IsAppRestorable(const std::string& extension_id);
46 // Called to notify that the application has begun to exit.
47 void OnApplicationTerminating();
49 static AppRestoreService* Get(content::BrowserContext* context);
52 // AppLifetimeMonitor::Observer.
53 void OnAppStart(content::BrowserContext* context,
54 const std::string& app_id) override;
55 void OnAppActivated(content::BrowserContext* context,
56 const std::string& app_id) override;
57 void OnAppDeactivated(content::BrowserContext* context,
58 const std::string& app_id) override;
59 void OnAppStop(content::BrowserContext* context,
60 const std::string& app_id) override;
63 void Shutdown() override;
65 void RecordAppStart(const std::string& extension_id);
66 void RecordAppStop(const std::string& extension_id);
67 void RecordAppActiveState(const std::string& id, bool is_active);
69 void RestoreApp(const extensions::Extension* extension);
71 void StartObservingAppLifetime();
72 void StopObservingAppLifetime();
74 raw_ptr<content::BrowserContext> context_;
79 #endif // APPS_APP_RESTORE_SERVICE_H_