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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_INSTALL_VERIFIER_H_
6 #define CHROME_BROWSER_EXTENSIONS_INSTALL_VERIFIER_H_
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 "base/memory/weak_ptr.h"
17 #include "extensions/browser/management_policy.h"
18 #include "extensions/common/extension.h"
25 class URLRequestContextGetter
;
28 namespace extensions
{
32 struct InstallSignature
;
34 // This class implements verification that a set of extensions are either from
35 // the webstore or are whitelisted by enterprise policy. The webstore
36 // verification process works by sending a request to a backend server to get a
37 // signature proving that a set of extensions are verified. This signature is
38 // written into the extension preferences and is checked for validity when
39 // being read back again.
41 // This class should be kept notified of runtime changes to the set of
42 // extensions installed from the webstore.
43 class InstallVerifier
: public ManagementPolicy::Provider
{
45 InstallVerifier(ExtensionPrefs
* prefs
, content::BrowserContext
* context
);
46 ~InstallVerifier() override
;
48 // Returns whether |extension| is of a type that needs verification.
49 static bool NeedsVerification(const Extension
& extension
);
51 // Determines if an extension claims to be from the webstore.
52 static bool IsFromStore(const Extension
& extension
);
54 // Initializes this object for use, including reading preferences and
55 // validating the stored signature.
58 // Returns the timestamp of our InstallSignature, if we have one.
59 base::Time
SignatureTimestamp();
61 // Returns true if |id| is either verified or our stored signature explicitly
62 // tells us that it was invalid when we asked the server about it.
63 bool IsKnownId(const std::string
& id
) const;
65 // Returns whether the given |id| is considered invalid by our verified
67 bool IsInvalid(const std::string
& id
) const;
69 // Attempts to verify a single extension and add it to the verified list.
70 void VerifyExtension(const std::string
& extension_id
);
72 // Attempts to verify all extensions.
73 void VerifyAllExtensions();
75 // Call this to add a set of ids that will immediately be considered allowed,
76 // and kick off an aysnchronous request to Add.
77 void AddProvisional(const ExtensionIdSet
& ids
);
79 // Removes an id or set of ids from the verified list.
80 void Remove(const std::string
& id
);
81 void RemoveMany(const ExtensionIdSet
& ids
);
83 // Returns whether an extension id is allowed by policy.
84 bool AllowedByEnterprisePolicy(const std::string
& id
) const;
86 // ManagementPolicy::Provider interface.
87 std::string
GetDebugPolicyProviderName() const override
;
88 bool MustRemainDisabled(const Extension
* extension
,
89 Extension::DisableReason
* reason
,
90 base::string16
* error
) const override
;
93 // We keep a list of operations to the current set of extensions.
95 ADD_SINGLE
, // Adding a single extension to be verified.
96 ADD_ALL
, // Adding all extensions to be verified.
97 ADD_ALL_BOOTSTRAP
, // Adding all extensions because of a bootstrapping.
98 ADD_PROVISIONAL
, // Adding one or more provisionally-allowed extensions.
99 REMOVE
// Remove one or more extensions.
102 // This is an operation we want to apply to the current set of verified ids.
103 struct PendingOperation
{
106 // This is the set of ids being either added or removed.
109 explicit PendingOperation(OperationType type
);
113 // Returns the set of IDs for all extensions that potentially need to be
115 ExtensionIdSet
GetExtensionsToVerify() const;
117 // Bootstrap the InstallVerifier if we do not already have a signature, or if
118 // there are unknown extensions which need to be verified.
119 void MaybeBootstrapSelf();
121 // Try adding a new set of |ids| to the list of verified ids.
122 void AddMany(const ExtensionIdSet
& ids
, OperationType type
);
124 // Record the result of the verification for the histograms, and notify the
125 // ExtensionPrefs if we verified all extensions.
126 void OnVerificationComplete(bool success
, OperationType type
);
128 // Removes any no-longer-installed ids, requesting a new signature if needed.
129 void GarbageCollect();
131 // Returns whether the given |id| is included in our verified signature.
132 bool IsVerified(const std::string
& id
) const;
134 // Returns true if the extension with |id| was installed later than the
135 // timestamp of our signature.
136 bool WasInstalledAfterSignature(const std::string
& id
) const;
138 // Begins the process of fetching a new signature, based on applying the
139 // operation at the head of the queue to the current set of ids in
140 // |signature_| (if any) and then sending a request to sign that.
143 // Saves the current value of |signature_| to the prefs;
146 // Called with the result of a signature request, or NULL on failure.
147 void SignatureCallback(scoped_ptr
<InstallSignature
> signature
);
149 ExtensionPrefs
* prefs_
;
151 // The context with which the InstallVerifier is associated.
152 content::BrowserContext
* context_
;
154 // Have we finished our bootstrap check yet?
155 bool bootstrap_check_complete_
;
157 // This is the most up-to-date signature, read out of |prefs_| during
158 // initialization and updated anytime we get new id's added.
159 scoped_ptr
<InstallSignature
> signature_
;
161 // The current InstallSigner, if we have a signature request running.
162 scoped_ptr
<InstallSigner
> signer_
;
164 // A queue of operations to apply to the current set of allowed ids.
165 std::queue
<linked_ptr
<PendingOperation
> > operation_queue_
;
167 // A set of ids that have been provisionally added, which we're willing to
168 // consider allowed until we hear back from the server signature request.
169 ExtensionIdSet provisional_
;
171 base::WeakPtrFactory
<InstallVerifier
> weak_factory_
;
173 DISALLOW_COPY_AND_ASSIGN(InstallVerifier
);
176 } // namespace extensions
178 #endif // CHROME_BROWSER_EXTENSIONS_INSTALL_VERIFIER_H_