Home | History | Annotate | Download | only in extensions
      1 // Copyright 2014 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_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
      7 
      8 #include <list>
      9 #include <string>
     10 
     11 #include "chrome/browser/extensions/pending_extension_info.h"
     12 #include "extensions/common/manifest.h"
     13 
     14 class GURL;
     15 
     16 namespace base {
     17 class Version;
     18 }
     19 
     20 namespace content {
     21 class BrowserContext;
     22 }
     23 
     24 FORWARD_DECLARE_TEST(ExtensionServiceTest,
     25                      UpdatePendingExtensionAlreadyInstalled);
     26 
     27 namespace extensions {
     28 class Extension;
     29 class PendingExtensionManager;
     30 
     31 class ExtensionUpdaterTest;
     32 void SetupPendingExtensionManagerForTest(
     33     int count, const GURL& update_url,
     34     PendingExtensionManager* pending_extension_manager);
     35 
     36 // Class PendingExtensionManager manages the set of extensions which are
     37 // being installed or updated. In general, installation and updates take
     38 // time, because they involve downloading, unpacking, and installing.
     39 // This class allows us to avoid race cases where multiple sources install
     40 // the same extension.
     41 // The ExtensionService creates an instance of this class, and manages its
     42 // lifetime. This class should only be used from the UI thread.
     43 class PendingExtensionManager {
     44  public:
     45   explicit PendingExtensionManager(content::BrowserContext* context);
     46   ~PendingExtensionManager();
     47 
     48   // TODO(skerner): Many of these methods can be private once code in
     49   // ExtensionService is moved into methods of this class.
     50 
     51   // Remove extension with id |id| from the set of pending extensions. Returns
     52   // true if such an extension was found and removed, false otherwise.
     53   bool Remove(const std::string& id);
     54 
     55   // Get the  information for a pending extension.  Returns a pointer to the
     56   // pending extension with id |id|, or NULL if there is no such extension.
     57   const PendingExtensionInfo* GetById(const std::string& id) const;
     58 
     59   // Is |id| in the set of pending extensions?
     60   bool IsIdPending(const std::string& id) const;
     61 
     62   // Returns true if there are any extensions pending.
     63   bool HasPendingExtensions() const;
     64 
     65   // Whether there is pending extension install from sync.
     66   bool HasPendingExtensionFromSync() const;
     67 
     68   // Adds an extension in a pending state; the extension with the
     69   // given info will be installed on the next auto-update cycle.
     70   // Return true if the extension was added.  Will return false
     71   // if the extension is pending from another source which overrides
     72   // sync installs (such as a policy extension) or if the extension
     73   // is already installed.
     74   //
     75   // TODO(akalin): Replace |install_silently| with a list of
     76   // pre-enabled permissions.
     77   bool AddFromSync(
     78       const std::string& id,
     79       const GURL& update_url,
     80       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
     81       bool install_silently,
     82       bool remote_install,
     83       bool installed_by_custodian);
     84 
     85   // Adds an extension that was depended on by another extension.
     86   bool AddFromExtensionImport(
     87       const std::string& id,
     88       const GURL& update_url,
     89       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install);
     90 
     91   // Given an extension id and an update URL, schedule the extension
     92   // to be fetched, installed, and activated.
     93   bool AddFromExternalUpdateUrl(const std::string& id,
     94                                 const std::string& install_parameter,
     95                                 const GURL& update_url,
     96                                 Manifest::Location location,
     97                                 int creation_flags,
     98                                 bool mark_acknowledged);
     99 
    100   // Add a pending extension record for an external CRX file.
    101   // Return true if the CRX should be installed, false if an existing
    102   // pending record overrides it.
    103   bool AddFromExternalFile(
    104       const std::string& id,
    105       Manifest::Location location,
    106       const base::Version& version,
    107       int creation_flags,
    108       bool mark_acknowledged);
    109 
    110   // Get the list of pending IDs that should be installed from an update URL.
    111   // Pending extensions that will be installed from local files will not be
    112   // included in the set.
    113   void GetPendingIdsForUpdateCheck(
    114       std::list<std::string>* out_ids_for_update_check) const;
    115 
    116  private:
    117   typedef std::list<PendingExtensionInfo> PendingExtensionList;
    118 
    119   // Assumes an extension with id |id| is not already installed.
    120   // Return true if the extension was added.
    121   bool AddExtensionImpl(
    122       const std::string& id,
    123       const std::string& install_parameter,
    124       const GURL& update_url,
    125       const base::Version& version,
    126       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
    127       bool is_from_sync,
    128       bool install_silently,
    129       Manifest::Location install_source,
    130       int creation_flags,
    131       bool mark_acknowledged,
    132       bool remote_install);
    133 
    134   // Add a pending extension record directly.  Used for unit tests that need
    135   // to set an inital state. Use friendship to allow the tests to call this
    136   // method.
    137   void AddForTesting(const PendingExtensionInfo& pending_extension_info);
    138 
    139   // The BrowserContext with which the manager is associated.
    140   content::BrowserContext* context_;
    141 
    142   PendingExtensionList pending_extension_list_;
    143 
    144   FRIEND_TEST_ALL_PREFIXES(::ExtensionServiceTest,
    145                            UpdatePendingExtensionAlreadyInstalled);
    146   friend class ExtensionUpdaterTest;
    147   friend void SetupPendingExtensionManagerForTest(
    148       int count, const GURL& update_url,
    149       PendingExtensionManager* pending_extension_manager);
    150 
    151   DISALLOW_COPY_AND_ASSIGN(PendingExtensionManager);
    152 };
    153 
    154 }  // namespace extensions
    155 
    156 #endif  // CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
    157