Home | History | Annotate | Download | only in extensions
      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_EXTENSIONS_INSTALL_VERIFIER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_INSTALL_VERIFIER_H_
      7 
      8 #include <queue>
      9 #include <set>
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/callback.h"
     14 #include "base/memory/linked_ptr.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "extensions/browser/management_policy.h"
     17 #include "extensions/common/extension.h"
     18 
     19 namespace net {
     20 class URLRequestContextGetter;
     21 }
     22 
     23 namespace extensions {
     24 
     25 class ExtensionPrefs;
     26 class InstallSigner;
     27 struct InstallSignature;
     28 
     29 // This class implements verification that a set of extensions are either from
     30 // the webstore or are whitelisted by enterprise policy.  The webstore
     31 // verification process works by sending a request to a backend server to get a
     32 // signature proving that a set of extensions are verified. This signature is
     33 // written into the extension preferences and is checked for validity when
     34 // being read back again.
     35 //
     36 // This class should be kept notified of runtime changes to the set of
     37 // extensions installed from the webstore.
     38 class InstallVerifier : public ManagementPolicy::Provider {
     39  public:
     40   InstallVerifier(ExtensionPrefs* prefs,
     41                   net::URLRequestContextGetter* context_getter);
     42   virtual ~InstallVerifier();
     43 
     44   // Returns whether |extension| is of a type that needs verification.
     45   static bool NeedsVerification(const Extension& extension);
     46 
     47   // Initializes this object for use, including reading preferences and
     48   // validating the stored signature.
     49   void Init();
     50 
     51   // Do we need to be bootstrapped? (i.e. do we have a signature already). If
     52   // this is true, then consumers of this class should use Add/AddMany to get
     53   // an initial one so that MustRemainDisabled can actually check against it.
     54   bool NeedsBootstrap();
     55 
     56   // Returns the timestamp of our InstallSignature, if we have one.
     57   base::Time SignatureTimestamp();
     58 
     59   // A callback for indicating success/failure of adding new ids.
     60   typedef base::Callback<void(bool)> AddResultCallback;
     61 
     62   // Try adding a new |id| (or set of ids) to the list of verified ids. When
     63   // this process is finished |callback| will be run with success/failure of
     64   // the signature request (not necessarily whether the ids were verified).
     65   void Add(const std::string& id, const AddResultCallback& callback);
     66   void AddMany(const ExtensionIdSet& ids,
     67                const AddResultCallback& callback);
     68 
     69   // Call this to add a set of ids that will immediately be considered allowed,
     70   // and kick off an aysnchronous request to Add.
     71   void AddProvisional(const ExtensionIdSet& ids);
     72 
     73   // Removes an id or set of ids from the verified list.
     74   void Remove(const std::string& id);
     75   void RemoveMany(const ExtensionIdSet& ids);
     76 
     77   // ManagementPolicy::Provider interface.
     78   virtual std::string GetDebugPolicyProviderName() const OVERRIDE;
     79   virtual bool MustRemainDisabled(const Extension* extension,
     80                                   Extension::DisableReason* reason,
     81                                   base::string16* error) const OVERRIDE;
     82 
     83  private:
     84   // We keep a list of operations to the current set of extensions - either
     85   // additions or removals.
     86   enum OperationType {
     87     ADD,
     88     REMOVE
     89   };
     90 
     91   // This is an operation we want to apply to the current set of verified ids.
     92   struct PendingOperation {
     93     OperationType type;
     94 
     95     // This is the set of ids being either added or removed.
     96     ExtensionIdSet ids;
     97 
     98     AddResultCallback callback;
     99 
    100     explicit PendingOperation();
    101     ~PendingOperation();
    102   };
    103 
    104   // Removes any no-longer-installed ids, requesting a new signature if needed.
    105   void GarbageCollect();
    106 
    107   // Returns whether an extension id is allowed by policy.
    108   bool AllowedByEnterprisePolicy(const std::string& id) const;
    109 
    110   // Returns whether the given |id| is included in our verified signature.
    111   bool IsVerified(const std::string& id) const;
    112 
    113   // Returns true if the extension with |id| was installed later than the
    114   // timestamp of our signature.
    115   bool WasInstalledAfterSignature(const std::string& id) const;
    116 
    117   // Begins the process of fetching a new signature, based on applying the
    118   // operation at the head of the queue to the current set of ids in
    119   // |signature_| (if any) and then sending a request to sign that.
    120   void BeginFetch();
    121 
    122   // Saves the current value of |signature_| to the prefs;
    123   void SaveToPrefs();
    124 
    125   // Called with the result of a signature request, or NULL on failure.
    126   void SignatureCallback(scoped_ptr<InstallSignature> signature);
    127 
    128   ExtensionPrefs* prefs_;
    129   net::URLRequestContextGetter* context_getter_;
    130 
    131   // This is the most up-to-date signature, read out of |prefs_| during
    132   // initialization and updated anytime we get new id's added.
    133   scoped_ptr<InstallSignature> signature_;
    134 
    135   // The current InstallSigner, if we have a signature request running.
    136   scoped_ptr<InstallSigner> signer_;
    137 
    138   // A queue of operations to apply to the current set of allowed ids.
    139   std::queue<linked_ptr<PendingOperation> > operation_queue_;
    140 
    141   // A set of ids that have been provisionally added, which we're willing to
    142   // consider allowed until we hear back from the server signature request.
    143   ExtensionIdSet provisional_;
    144 
    145   DISALLOW_COPY_AND_ASSIGN(InstallVerifier);
    146 };
    147 
    148 }  // namespace extensions
    149 
    150 #endif  // CHROME_BROWSER_EXTENSIONS_INSTALL_VERIFIER_H_
    151