Home | History | Annotate | Download | only in policy
      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/weak_ptr.h"
     15 #include "chrome/browser/chromeos/extensions/external_cache.h"
     16 #include "content/public/browser/notification_observer.h"
     17 
     18 namespace extensions {
     19 class ExternalLoader;
     20 }
     21 
     22 namespace net {
     23 class URLRequestContextGetter;
     24 }
     25 
     26 namespace policy {
     27 
     28 class AppPackExternalLoader;
     29 class EnterpriseInstallAttributes;
     30 
     31 // The AppPackUpdater manages a set of extensions that are configured via a
     32 // device policy to be locally cached and installed into the Demo user account
     33 // at login time.
     34 class AppPackUpdater : public content::NotificationObserver,
     35                        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   // content::NotificationObserver:
     67   virtual void Observe(int type,
     68                        const content::NotificationSource& source,
     69                        const content::NotificationDetails& details) OVERRIDE;
     70 
     71   // Implementation of ExternalCache::Delegate:
     72   virtual void OnExtensionListsUpdated(
     73       const base::DictionaryValue* prefs) OVERRIDE;
     74 
     75   // Loads the current policy and schedules a cache update.
     76   void LoadPolicy();
     77 
     78   // Notifies the |extension_loader_| that the cache has been updated, providing
     79   // it with an updated list of app-pack extensions.
     80   void UpdateExtensionLoader();
     81 
     82   // Sets |screen_saver_path_| and invokes |screen_saver_update_callback_| if
     83   // appropriate.
     84   void SetScreenSaverPath(const base::FilePath& path);
     85 
     86   base::WeakPtrFactory<AppPackUpdater> weak_ptr_factory_;
     87 
     88   // The extension ID and path of the CRX file of the screen saver extension,
     89   // if it is configured by the policy. Otherwise these fields are empty.
     90   std::string screen_saver_id_;
     91   base::FilePath screen_saver_path_;
     92 
     93   // Callback to invoke whenever the screen saver's extension path changes.
     94   // Can be null.
     95   ScreenSaverUpdateCallback screen_saver_update_callback_;
     96 
     97   // The extension loader wires the AppPackUpdater to the extensions system, and
     98   // makes it install the currently cached extensions.
     99   bool created_extension_loader_;
    100   base::WeakPtr<AppPackExternalLoader> extension_loader_;
    101 
    102   // For checking the device mode.
    103   EnterpriseInstallAttributes* install_attributes_;
    104 
    105   // Extension cache.
    106   chromeos::ExternalCache external_cache_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(AppPackUpdater);
    109 };
    110 
    111 }  // namespace policy
    112 
    113 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_APP_PACK_UPDATER_H_
    114