Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 
     12 #include "chrome/browser/extensions/pending_extension_info.h"
     13 #include "chrome/common/extensions/extension.h"
     14 
     15 class ExtensionServiceInterface;
     16 class GURL;
     17 
     18 // Class PendingExtensionManager manages the set of extensions which are
     19 // being installed or updated. In general, instalation and updates take
     20 // time, because they involve downloading, unpacking, and installing.
     21 // This class allows us to avoid race cases where multiple sources install
     22 // the same extension.
     23 // The extensions service creates an instance of this class, and manages
     24 // its lifetime. This class should only be used from the UI thread.
     25 class PendingExtensionManager {
     26  public:
     27   // TODO(skerner): Make PendingExtensionMap a private implementation
     28   // detail of this class.
     29   typedef std::map<std::string, PendingExtensionInfo> PendingExtensionMap;
     30   typedef PendingExtensionMap::const_iterator const_iterator;
     31 
     32   // |service| is a reference to the ExtensionService whose pending
     33   // extensions we are managing. The service creates an instance of
     34   // this class on construction, and destroys it on destruction.
     35   // The service remains valid over the entire lifetime of this class.
     36   explicit PendingExtensionManager(const ExtensionServiceInterface& service);
     37   ~PendingExtensionManager();
     38 
     39   // TODO(skerner): Many of these methods can be private once code in
     40   // ExtensionService is moved into methods of this class.
     41 
     42   // Remove |id| from the set of pending extensions.
     43   void Remove(const std::string& id);
     44 
     45   // Get the  information for a pending extension.  Returns true and sets
     46   // |out_pending_extension_info| if there is a pending extension with id
     47   // |id|.  Returns false otherwise.
     48   bool GetById(const std::string& id,
     49                PendingExtensionInfo* out_pending_extension_info) const;
     50 
     51   // Is |id| in the set of pending extensions?
     52   bool IsIdPending(const std::string& id) const;
     53 
     54   // Iterate over the pending extensions.
     55   const_iterator begin() const { return  pending_extension_map_.begin(); }
     56   const_iterator end() const { return  pending_extension_map_.end(); }
     57 
     58   // Adds an extension in a pending state; the extension with the
     59   // given info will be installed on the next auto-update cycle.
     60   // Return true if the extension was added.  Will return false
     61   // if the extension is pending from another source which overrides
     62   // sync installs (such as a policy extension) or if the extension
     63   // is already installed.
     64   //
     65   // TODO(akalin): Replace |install_silently| with a list of
     66   // pre-enabled permissions.
     67   bool AddFromSync(
     68       const std::string& id,
     69       const GURL& update_url,
     70       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
     71       bool install_silently,
     72       bool enable_on_install,
     73       bool enable_incognito_on_install);
     74 
     75   // Given an extension id and an update URL, schedule the extension
     76   // to be fetched, installed, and activated.
     77   void AddFromExternalUpdateUrl(const std::string& id,
     78                                 const GURL& update_url,
     79                                 Extension::Location location);
     80 
     81   // Add a default app, using the default update url.
     82   void AddFromDefaultAppList(const std::string& id);
     83 
     84   // Add a pending extension record for an external CRX file.
     85   void AddFromExternalFile(
     86       const std::string& id,
     87       Extension::Location location);
     88 
     89  private:
     90   // Assumes an extension with id |id| is not already installed.
     91   // Return true if the extension was added.
     92   bool AddExtensionImpl(
     93       const std::string& id,
     94       const GURL& update_url,
     95       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
     96       bool is_from_sync,
     97       bool install_silently,
     98       bool enable_on_install,
     99       bool enable_incognito_on_install,
    100       Extension::Location install_source);
    101 
    102   // Add a pending extension record directly.  Used for unit tests that need
    103   // to set an inital state. Use friendship to allow the tests to call this
    104   // method.
    105   void AddForTesting(const std::string& id,
    106                      const PendingExtensionInfo& pending_etension_info);
    107 
    108   // Reference to the extension service whose pending extensions this class is
    109   // managing.  Because this class is a member of |service_|, it is created
    110   // and destroyed with |service_|. We only use methods from the interface
    111   // ExtensionServiceInterface.
    112   const ExtensionServiceInterface& service_;
    113 
    114   // A map from extension id to the pending extension info for that extension.
    115   PendingExtensionMap pending_extension_map_;
    116 
    117   FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
    118                            UpdatePendingExtensionAlreadyInstalled);
    119   friend class ExtensionUpdaterTest;
    120   friend void SetupPendingExtensionManagerForTest(
    121       int count, const GURL& update_url,
    122       PendingExtensionManager* pending_extension_manager);
    123 
    124   DISALLOW_COPY_AND_ASSIGN(PendingExtensionManager);
    125 };
    126 
    127 #endif  // CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
    128