15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef APPS_APP_RESTORE_SERVICE_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define APPS_APP_RESTORE_SERVICE_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "apps/app_lifetime_monitor.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/app_window/app_window_registry.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Extension;
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace apps {
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tracks what apps need to be restarted when the browser restarts.
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class AppRestoreService : public KeyedService,
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                          public AppLifetimeMonitor::Observer {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns true if apps should be restored on the current platform, given
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // whether this new browser process launched due to a restart.
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  static bool ShouldRestoreApps(bool is_browser_restart);
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit AppRestoreService(Profile* profile);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Restart apps that need to be restarted and clear the "running" preference
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from apps to prevent them being restarted in subsequent restarts.
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void HandleStartup(bool should_restore_apps);
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns whether this extension is running or, at startup, whether it was
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // running when Chrome was last terminated.
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool IsAppRestorable(const std::string& extension_id);
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static AppRestoreService* Get(Profile* profile);
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // AppLifetimeMonitor::Observer.
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnAppStart(Profile* profile, const std::string& app_id) OVERRIDE;
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnAppActivated(Profile* profile,
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                              const std::string& app_id) OVERRIDE;
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnAppDeactivated(Profile* profile,
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                const std::string& app_id) OVERRIDE;
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE;
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  virtual void OnChromeTerminating() OVERRIDE;
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // KeyedService.
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual void Shutdown() OVERRIDE;
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RecordAppStart(const std::string& extension_id);
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RecordAppStop(const std::string& extension_id);
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void RecordAppActiveState(const std::string& id, bool is_active);
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void RestoreApp(const extensions::Extension* extension);
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void StartObservingAppLifetime();
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void StopObservingAppLifetime();
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Profile* profile_;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AppRestoreService);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace apps
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // APPS_APP_RESTORE_SERVICE_H_
73