1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_UPDATE_SERVICE_H_
6#define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_UPDATE_SERVICE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/singleton.h"
13#include "base/timer/timer.h"
14#include "chrome/browser/chromeos/app_mode/kiosk_app_manager_observer.h"
15#include "chrome/browser/chromeos/system/automatic_reboot_manager_observer.h"
16#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
17#include "components/keyed_service/core/keyed_service.h"
18#include "extensions/browser/update_observer.h"
19
20class Profile;
21
22namespace extensions {
23class Extension;
24}
25
26namespace chromeos {
27
28namespace system {
29class AutomaticRebootManager;
30}
31
32// This class enforces automatic restart on app and Chrome updates in app mode.
33class KioskAppUpdateService : public KeyedService,
34                              public extensions::UpdateObserver,
35                              public system::AutomaticRebootManagerObserver,
36                              public KioskAppManagerObserver {
37 public:
38  KioskAppUpdateService(
39      Profile* profile,
40      system::AutomaticRebootManager* automatic_reboot_manager);
41  virtual ~KioskAppUpdateService();
42
43  void set_app_id(const std::string& app_id) { app_id_ = app_id; }
44  std::string get_app_id() const { return app_id_; }
45
46 private:
47  friend class KioskAppUpdateServiceTest;
48
49  void StartAppUpdateRestartTimer();
50  void ForceAppUpdateRestart();
51
52  // KeyedService overrides:
53  virtual void Shutdown() OVERRIDE;
54
55  // extensions::UpdateObserver overrides:
56  virtual void OnAppUpdateAvailable(
57      const extensions::Extension* extension) OVERRIDE;
58  virtual void OnChromeUpdateAvailable() OVERRIDE {}
59
60  // system::AutomaticRebootManagerObserver overrides:
61  virtual void OnRebootScheduled(Reason reason) OVERRIDE;
62  virtual void WillDestroyAutomaticRebootManager() OVERRIDE;
63
64  // KioskAppManagerObserver overrides:
65  virtual void OnKioskAppCacheUpdated(const std::string& app_id) OVERRIDE;
66
67  Profile* profile_;
68  std::string app_id_;
69
70  // After we detect an upgrade we start a one-short timer to force restart.
71  base::OneShotTimer<KioskAppUpdateService> restart_timer_;
72
73  system::AutomaticRebootManager* automatic_reboot_manager_;  // Not owned.
74
75  DISALLOW_COPY_AND_ASSIGN(KioskAppUpdateService);
76};
77
78// Singleton that owns all KioskAppUpdateServices and associates them with
79// profiles.
80class KioskAppUpdateServiceFactory : public BrowserContextKeyedServiceFactory {
81 public:
82  // Returns the KioskAppUpdateService for |profile|, creating it if it is not
83  // yet created.
84  static KioskAppUpdateService* GetForProfile(Profile* profile);
85
86  // Returns the KioskAppUpdateServiceFactory instance.
87  static KioskAppUpdateServiceFactory* GetInstance();
88
89 private:
90  friend struct DefaultSingletonTraits<KioskAppUpdateServiceFactory>;
91
92  KioskAppUpdateServiceFactory();
93  virtual ~KioskAppUpdateServiceFactory();
94
95  // BrowserContextKeyedServiceFactory overrides:
96  virtual KeyedService* BuildServiceInstanceFor(
97      content::BrowserContext* profile) const OVERRIDE;
98};
99
100}  // namespace chromeos
101
102#endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_UPDATE_SERVICE_H_
103