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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_
6 #define CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_
14 #include "base/callback_forward.h"
15 #include "base/compiler_specific.h"
16 #include "base/files/file_path.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/scoped_observer.h"
21 #include "base/time/time.h"
22 #include "base/timer/timer.h"
23 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "extensions/browser/extension_registry_observer.h"
26 #include "extensions/browser/updater/extension_downloader.h"
27 #include "extensions/browser/updater/extension_downloader_delegate.h"
28 #include "extensions/browser/updater/manifest_fetch_data.h"
31 class ExtensionServiceInterface
;
39 namespace extensions
{
43 class ExtensionRegistry
;
45 class ExtensionUpdaterTest
;
47 // A class for doing auto-updates of installed Extensions. Used like this:
49 // ExtensionUpdater* updater = new ExtensionUpdater(my_extensions_service,
53 // update_frequency_secs,
54 // downloader_factory);
58 class ExtensionUpdater
: public ExtensionDownloaderDelegate
,
59 public ExtensionRegistryObserver
,
60 public content::NotificationObserver
{
62 typedef base::Closure FinishedCallback
;
65 // Creates a default CheckParams instance that checks for all extensions.
69 // The set of extensions that should be checked for updates. If empty
70 // all extensions will be included in the update check.
71 std::list
<std::string
> ids
;
73 // Normally extension updates get installed only when the extension is idle.
74 // Setting this to true causes any updates that are found to be installed
76 bool install_immediately
;
78 // Callback to call when the update check is complete. Can be null, if
79 // you're not interested in when this happens.
80 FinishedCallback callback
;
83 // Holds a pointer to the passed |service|, using it for querying installed
84 // extensions and installing updated ones. The |frequency_seconds| parameter
85 // controls how often update checks are scheduled.
86 ExtensionUpdater(ExtensionServiceInterface
* service
,
87 ExtensionPrefs
* extension_prefs
,
90 int frequency_seconds
,
91 ExtensionCache
* cache
,
92 const ExtensionDownloader::Factory
& downloader_factory
);
94 ~ExtensionUpdater() override
;
96 // Starts the updater running. Should be called at most once.
99 // Stops the updater running, cancelling any outstanding update manifest and
100 // crx downloads. Does not cancel any in-progress installs.
103 // Posts a task to do an update check. Does nothing if there is
104 // already a pending task that has not yet run.
107 // Starts an update check for the specified extension soon. If a check
108 // is already running, or finished too recently without an update being
109 // installed, this method returns false and the check won't be scheduled.
110 bool CheckExtensionSoon(const std::string
& extension_id
,
111 const FinishedCallback
& callback
);
113 // Starts an update check right now, instead of waiting for the next
114 // regularly scheduled check or a pending check from CheckSoon().
115 void CheckNow(const CheckParams
& params
);
117 // Returns true iff CheckSoon() has been called but the update check
118 // hasn't been performed yet. This is used mostly by tests; calling
119 // code should just call CheckSoon().
120 bool WillCheckSoon() const;
122 // Changes the params that are used for the automatic periodic update checks,
123 // as well as for explicit calls to CheckSoon.
124 void set_default_check_params(const CheckParams
& params
) {
125 default_params_
= params
;
128 // Overrides the extension cache with |extension_cache| for testing.
129 void SetExtensionCacheForTesting(ExtensionCache
* extension_cache
);
131 // Stop the timer to prevent scheduled updates for testing.
132 void StopTimerForTesting();
135 friend class ExtensionUpdaterTest
;
136 friend class ExtensionUpdaterFileHandler
;
138 // FetchedCRXFile holds information about a CRX file we fetched to disk,
139 // but have not yet installed.
140 struct FetchedCRXFile
{
142 FetchedCRXFile(const CRXFileInfo
& file
,
143 bool file_ownership_passed
,
144 const std::set
<int>& request_ids
,
145 const InstallCallback
& callback
);
150 bool file_ownership_passed
;
151 std::set
<int> request_ids
;
152 InstallCallback callback
;
155 struct InProgressCheck
{
159 bool install_immediately
;
160 FinishedCallback callback
;
161 // The ids of extensions that have in-progress update checks.
162 std::list
<std::string
> in_progress_ids_
;
167 // Callback used to continue CheckNow after determining which extensions
168 // should be force-updated.
169 void OnForcedUpdatesDetermined(const CheckParams
& params
,
170 const std::set
<std::string
>& forced_updates
);
172 // Ensure that we have a valid ExtensionDownloader instance referenced by
174 void EnsureDownloaderCreated();
176 // Computes when to schedule the first update check.
177 base::TimeDelta
DetermineFirstCheckDelay();
179 // Sets the timer to call TimerFired after roughly |target_delay| from now.
180 // To help spread load evenly on servers, this method adds some random
181 // jitter. It also saves the scheduled time so it can be reloaded on
183 void ScheduleNextCheck(const base::TimeDelta
& target_delay
);
185 // Add fetch records for extensions that are installed to the downloader,
186 // ignoring |pending_ids| so the extension isn't fetched again.
187 void AddToDownloader(const ExtensionSet
* extensions
,
188 const std::list
<std::string
>& pending_ids
,
191 // BaseTimer::ReceiverMethod callback.
194 // Posted by CheckSoon().
197 // Implementation of ExtensionDownloaderDelegate.
198 void OnExtensionDownloadFailed(const std::string
& id
,
200 const PingResult
& ping
,
201 const std::set
<int>& request_ids
) override
;
202 void OnExtensionDownloadFinished(const CRXFileInfo
& file
,
203 bool file_ownership_passed
,
204 const GURL
& download_url
,
205 const std::string
& version
,
206 const PingResult
& ping
,
207 const std::set
<int>& request_id
,
208 const InstallCallback
& callback
) override
;
209 bool GetPingDataForExtension(const std::string
& id
,
210 ManifestFetchData::PingData
* ping_data
) override
;
211 std::string
GetUpdateUrlData(const std::string
& id
) override
;
212 bool IsExtensionPending(const std::string
& id
) override
;
213 bool GetExtensionExistingVersion(const std::string
& id
,
214 std::string
* version
) override
;
215 bool ShouldForceUpdate(const std::string
& extension_id
,
216 std::string
* source
) override
;
218 void UpdatePingData(const std::string
& id
, const PingResult
& ping_result
);
220 // Starts installing a crx file that has been fetched but not installed yet.
221 void MaybeInstallCRXFile();
223 // content::NotificationObserver implementation.
224 void Observe(int type
,
225 const content::NotificationSource
& source
,
226 const content::NotificationDetails
& details
) override
;
228 // Implementation of ExtensionRegistryObserver.
229 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
230 const Extension
* extension
,
233 const std::string
& old_name
) override
;
235 // Send a notification that update checks are starting.
236 void NotifyStarted();
238 // Send a notification if we're finished updating.
239 void NotifyIfFinished(int request_id
);
241 void ExtensionCheckFinished(const std::string
& extension_id
,
242 const FinishedCallback
& callback
);
244 // Whether Start() has been called but not Stop().
247 // Pointer back to the service that owns this ExtensionUpdater.
248 ExtensionServiceInterface
* service_
;
250 // A closure passed into the ExtensionUpdater to teach it how to construct
251 // new ExtensionDownloader instances.
252 const ExtensionDownloader::Factory downloader_factory_
;
254 // Fetches the crx files for the extensions that have an available update.
255 scoped_ptr
<ExtensionDownloader
> downloader_
;
257 base::OneShotTimer
<ExtensionUpdater
> timer_
;
258 int frequency_seconds_
;
259 bool will_check_soon_
;
261 ExtensionPrefs
* extension_prefs_
;
265 std::map
<int, InProgressCheck
> requests_in_progress_
;
266 int next_request_id_
;
268 // Observes CRX installs we initiate.
269 content::NotificationRegistrar registrar_
;
271 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
272 extension_registry_observer_
;
274 // True when a CrxInstaller is doing an install. Used in MaybeUpdateCrxFile()
275 // to keep more than one install from running at once.
276 bool crx_install_is_running_
;
278 // Fetched CRX files waiting to be installed.
279 std::stack
<FetchedCRXFile
> fetched_crx_files_
;
280 FetchedCRXFile current_crx_file_
;
282 CheckParams default_params_
;
284 ExtensionCache
* extension_cache_
;
286 // Keeps track of when an extension tried to update itself, so we can throttle
287 // checks to prevent too many requests from being made.
288 std::map
<std::string
, ThrottleInfo
> throttle_info_
;
290 // Keeps track of extensions (by ID) whose update should be forced during the
291 // next update check.
292 std::set
<std::string
> forced_updates_
;
294 base::WeakPtrFactory
<ExtensionUpdater
> weak_ptr_factory_
;
296 DISALLOW_COPY_AND_ASSIGN(ExtensionUpdater
);
299 } // namespace extensions
301 #endif // CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_