1// Copyright (c) 2012 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_POLICY_APP_PACK_UPDATER_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_APP_PACK_UPDATER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/files/file_path.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "chrome/browser/chromeos/extensions/external_cache.h"
17#include "chrome/browser/chromeos/settings/cros_settings.h"
18
19namespace extensions {
20class ExternalLoader;
21}
22
23namespace net {
24class URLRequestContextGetter;
25}
26
27namespace policy {
28
29class AppPackExternalLoader;
30class EnterpriseInstallAttributes;
31
32// The AppPackUpdater manages a set of extensions that are configured via a
33// device policy to be locally cached and installed into the Demo user account
34// at login time.
35class AppPackUpdater : public chromeos::ExternalCache::Delegate {
36 public:
37  // Callback to listen for updates to the screensaver extension's path.
38  typedef base::Callback<void(const base::FilePath&)> ScreenSaverUpdateCallback;
39
40  // The |request_context| is used for the update checks.
41  AppPackUpdater(net::URLRequestContextGetter* request_context,
42                 EnterpriseInstallAttributes* install_attributes);
43  virtual ~AppPackUpdater();
44
45  // Returns true if the ExternalLoader for the app pack has already been
46  // created.
47  bool created_external_loader() const { return created_extension_loader_; }
48
49  // Creates an extensions::ExternalLoader that will load the crx files
50  // downloaded by the AppPackUpdater. This can be called at most once, and the
51  // caller owns the returned value.
52  extensions::ExternalLoader* CreateExternalLoader();
53
54  // |callback| will be invoked whenever the screen saver extension's path
55  // changes. It will be invoked "soon" after this call if a valid path already
56  // exists. Subsequent calls will override the previous |callback|. A null
57  // |callback| can be used to remove a previous callback.
58  void SetScreenSaverUpdateCallback(const ScreenSaverUpdateCallback& callback);
59
60  // If a user of one of the AppPack's extensions detects that the extension
61  // is damaged then this method can be used to remove it from the cache and
62  // retry to download it after a restart.
63  void OnDamagedFileDetected(const base::FilePath& path);
64
65 private:
66  // Implementation of ExternalCache::Delegate:
67  virtual void OnExtensionListsUpdated(
68      const base::DictionaryValue* prefs) OVERRIDE;
69
70  // Called when the app pack device setting changes.
71  void AppPackChanged();
72
73  // Loads the current policy and schedules a cache update.
74  void LoadPolicy();
75
76  // Notifies the |extension_loader_| that the cache has been updated, providing
77  // it with an updated list of app-pack extensions.
78  void UpdateExtensionLoader();
79
80  // Sets |screen_saver_path_| and invokes |screen_saver_update_callback_| if
81  // appropriate.
82  void SetScreenSaverPath(const base::FilePath& path);
83
84  // The extension ID and path of the CRX file of the screen saver extension,
85  // if it is configured by the policy. Otherwise these fields are empty.
86  std::string screen_saver_id_;
87  base::FilePath screen_saver_path_;
88
89  // Callback to invoke whenever the screen saver's extension path changes.
90  // Can be null.
91  ScreenSaverUpdateCallback screen_saver_update_callback_;
92
93  // The extension loader wires the AppPackUpdater to the extensions system, and
94  // makes it install the currently cached extensions.
95  bool created_extension_loader_;
96  base::WeakPtr<AppPackExternalLoader> extension_loader_;
97
98  // For checking the device mode.
99  EnterpriseInstallAttributes* install_attributes_;
100
101  // Extension cache.
102  chromeos::ExternalCache external_cache_;
103
104  scoped_ptr<chromeos::CrosSettings::ObserverSubscription>
105      app_pack_subscription_;
106
107  base::WeakPtrFactory<AppPackUpdater> weak_ptr_factory_;
108
109  DISALLOW_COPY_AND_ASSIGN(AppPackUpdater);
110};
111
112}  // namespace policy
113
114#endif  // CHROME_BROWSER_CHROMEOS_POLICY_APP_PACK_UPDATER_H_
115