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/time/time.h"
21 #include "base/timer/timer.h"
22 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h"
23 #include "chrome/browser/extensions/updater/manifest_fetch_data.h"
24 #include "content/public/browser/notification_observer.h"
25 #include "content/public/browser/notification_registrar.h"
28 class ExtensionServiceInterface
;
32 namespace extensions
{
34 class ExtensionDownloader
;
37 class ExtensionUpdaterTest
;
39 // A class for doing auto-updates of installed Extensions. Used like this:
41 // ExtensionUpdater* updater = new ExtensionUpdater(my_extensions_service,
45 // update_frequency_secs);
49 class ExtensionUpdater
: public ExtensionDownloaderDelegate
,
50 public content::NotificationObserver
{
52 typedef base::Closure FinishedCallback
;
55 // Creates a default CheckParams instance that checks for all extensions.
59 // The set of extensions that should be checked for updates. If empty
60 // all extensions will be included in the update check.
61 std::list
<std::string
> ids
;
63 // Normally extension updates get installed only when the extension is idle.
64 // Setting this to true causes any updates that are found to be installed
66 bool install_immediately
;
68 // Callback to call when the update check is complete. Can be null, if
69 // you're not interested in when this happens.
70 FinishedCallback callback
;
73 // Holds a pointer to the passed |service|, using it for querying installed
74 // extensions and installing updated ones. The |frequency_seconds| parameter
75 // controls how often update checks are scheduled.
76 ExtensionUpdater(ExtensionServiceInterface
* service
,
77 ExtensionPrefs
* extension_prefs
,
80 int frequency_seconds
);
82 virtual ~ExtensionUpdater();
84 // Starts the updater running. Should be called at most once.
87 // Stops the updater running, cancelling any outstanding update manifest and
88 // crx downloads. Does not cancel any in-progress installs.
91 // Posts a task to do an update check. Does nothing if there is
92 // already a pending task that has not yet run.
95 // Starts an update check for the specified extension soon. If a check
96 // is already running, or finished too recently without an update being
97 // installed, this method returns false and the check won't be scheduled.
98 bool CheckExtensionSoon(const std::string
& extension_id
,
99 const FinishedCallback
& callback
);
101 // Starts an update check right now, instead of waiting for the next
102 // regularly scheduled check or a pending check from CheckSoon().
103 void CheckNow(const CheckParams
& params
);
105 // Returns true iff CheckSoon() has been called but the update check
106 // hasn't been performed yet. This is used mostly by tests; calling
107 // code should just call CheckSoon().
108 bool WillCheckSoon() const;
110 // Changes the params that are used for the automatic periodic update checks,
111 // as well as for explicit calls to CheckSoon.
112 void set_default_check_params(const CheckParams
& params
) {
113 default_params_
= params
;
117 friend class ExtensionUpdaterTest
;
118 friend class ExtensionUpdaterFileHandler
;
120 // FetchedCRXFile holds information about a CRX file we fetched to disk,
121 // but have not yet installed.
122 struct FetchedCRXFile
{
124 FetchedCRXFile(const std::string
& id
,
125 const base::FilePath
& path
,
126 const GURL
& download_url
,
127 const std::set
<int>& request_ids
);
130 std::string extension_id
;
133 std::set
<int> request_ids
;
136 struct InProgressCheck
{
140 bool install_immediately
;
141 FinishedCallback callback
;
142 // The ids of extensions that have in-progress update checks.
143 std::list
<std::string
> in_progress_ids_
;
148 // Computes when to schedule the first update check.
149 base::TimeDelta
DetermineFirstCheckDelay();
151 // Sets the timer to call TimerFired after roughly |target_delay| from now.
152 // To help spread load evenly on servers, this method adds some random
153 // jitter. It also saves the scheduled time so it can be reloaded on
155 void ScheduleNextCheck(const base::TimeDelta
& target_delay
);
157 // Add fetch records for extensions that are installed to the downloader,
158 // ignoring |pending_ids| so the extension isn't fetched again.
159 void AddToDownloader(const ExtensionSet
* extensions
,
160 const std::list
<std::string
>& pending_ids
,
163 // BaseTimer::ReceiverMethod callback.
166 // Posted by CheckSoon().
169 // Implenentation of ExtensionDownloaderDelegate.
170 virtual void OnExtensionDownloadFailed(
171 const std::string
& id
,
173 const PingResult
& ping
,
174 const std::set
<int>& request_ids
) OVERRIDE
;
176 virtual void OnExtensionDownloadFinished(
177 const std::string
& id
,
178 const base::FilePath
& path
,
179 const GURL
& download_url
,
180 const std::string
& version
,
181 const PingResult
& ping
,
182 const std::set
<int>& request_id
) OVERRIDE
;
184 virtual bool GetPingDataForExtension(
185 const std::string
& id
,
186 ManifestFetchData::PingData
* ping_data
) OVERRIDE
;
188 virtual std::string
GetUpdateUrlData(const std::string
& id
) OVERRIDE
;
190 virtual bool IsExtensionPending(const std::string
& id
) OVERRIDE
;
192 virtual bool GetExtensionExistingVersion(const std::string
& id
,
193 std::string
* version
) OVERRIDE
;
195 void UpdatePingData(const std::string
& id
, const PingResult
& ping_result
);
197 // Starts installing a crx file that has been fetched but not installed yet.
198 void MaybeInstallCRXFile();
200 // content::NotificationObserver implementation.
201 virtual void Observe(int type
,
202 const content::NotificationSource
& source
,
203 const content::NotificationDetails
& details
) OVERRIDE
;
205 // Send a notification that update checks are starting.
206 void NotifyStarted();
208 // Send a notification if we're finished updating.
209 void NotifyIfFinished(int request_id
);
211 void ExtensionCheckFinished(const std::string
& extension_id
,
212 const FinishedCallback
& callback
);
214 // Whether Start() has been called but not Stop().
217 base::WeakPtrFactory
<ExtensionUpdater
> weak_ptr_factory_
;
219 // Pointer back to the service that owns this ExtensionUpdater.
220 ExtensionServiceInterface
* service_
;
222 // Fetches the crx files for the extensions that have an available update.
223 scoped_ptr
<ExtensionDownloader
> downloader_
;
225 base::OneShotTimer
<ExtensionUpdater
> timer_
;
226 int frequency_seconds_
;
227 bool will_check_soon_
;
229 ExtensionPrefs
* extension_prefs_
;
233 std::map
<int, InProgressCheck
> requests_in_progress_
;
234 int next_request_id_
;
236 // Observes CRX installs we initiate.
237 content::NotificationRegistrar registrar_
;
239 // True when a CrxInstaller is doing an install. Used in MaybeUpdateCrxFile()
240 // to keep more than one install from running at once.
241 bool crx_install_is_running_
;
243 // Fetched CRX files waiting to be installed.
244 std::stack
<FetchedCRXFile
> fetched_crx_files_
;
245 FetchedCRXFile current_crx_file_
;
247 CheckParams default_params_
;
249 // Keeps track of when an extension tried to update itself, so we can throttle
250 // checks to prevent too many requests from being made.
251 std::map
<std::string
, ThrottleInfo
> throttle_info_
;
253 DISALLOW_COPY_AND_ASSIGN(ExtensionUpdater
);
256 } // namespace extensions
258 #endif // CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_