Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / updater / extension_updater.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_
6 #define CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_
7
8 #include <list>
9 #include <map>
10 #include <set>
11 #include <stack>
12 #include <string>
13
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"
29 #include "url/gurl.h"
30
31 class ExtensionServiceInterface;
32 class PrefService;
33 class Profile;
34
35 namespace content {
36 class BrowserContext;
37 }
38
39 namespace extensions {
40
41 class ExtensionCache;
42 class ExtensionPrefs;
43 class ExtensionRegistry;
44 class ExtensionSet;
45 class ExtensionUpdaterTest;
46
47 // A class for doing auto-updates of installed Extensions. Used like this:
48 //
49 // ExtensionUpdater* updater = new ExtensionUpdater(my_extensions_service,
50 //                                                  extension_prefs,
51 //                                                  pref_service,
52 //                                                  profile,
53 //                                                  update_frequency_secs,
54 //                                                  downloader_factory);
55 // updater->Start();
56 // ....
57 // updater->Stop();
58 class ExtensionUpdater : public ExtensionDownloaderDelegate,
59                          public ExtensionRegistryObserver,
60                          public content::NotificationObserver {
61  public:
62   typedef base::Closure FinishedCallback;
63
64   struct CheckParams {
65     // Creates a default CheckParams instance that checks for all extensions.
66     CheckParams();
67     ~CheckParams();
68
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;
72
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
75     // right away.
76     bool install_immediately;
77
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;
81   };
82
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,
88                    PrefService* prefs,
89                    Profile* profile,
90                    int frequency_seconds,
91                    ExtensionCache* cache,
92                    const ExtensionDownloader::Factory& downloader_factory);
93
94   ~ExtensionUpdater() override;
95
96   // Starts the updater running.  Should be called at most once.
97   void Start();
98
99   // Stops the updater running, cancelling any outstanding update manifest and
100   // crx downloads. Does not cancel any in-progress installs.
101   void Stop();
102
103   // Posts a task to do an update check.  Does nothing if there is
104   // already a pending task that has not yet run.
105   void CheckSoon();
106
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);
112
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);
116
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;
121
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;
126   }
127
128   // Overrides the extension cache with |extension_cache| for testing.
129   void SetExtensionCacheForTesting(ExtensionCache* extension_cache);
130
131  private:
132   friend class ExtensionUpdaterTest;
133   friend class ExtensionUpdaterFileHandler;
134
135   // FetchedCRXFile holds information about a CRX file we fetched to disk,
136   // but have not yet installed.
137   struct FetchedCRXFile {
138     FetchedCRXFile();
139     FetchedCRXFile(const std::string& id,
140                    const base::FilePath& path,
141                    bool file_ownership_passed,
142                    const std::set<int>& request_ids);
143     ~FetchedCRXFile();
144
145     std::string extension_id;
146     base::FilePath path;
147     bool file_ownership_passed;
148     std::set<int> request_ids;
149   };
150
151   struct InProgressCheck {
152     InProgressCheck();
153     ~InProgressCheck();
154
155     bool install_immediately;
156     FinishedCallback callback;
157     // The ids of extensions that have in-progress update checks.
158     std::list<std::string> in_progress_ids_;
159   };
160
161   struct ThrottleInfo;
162
163   // Callback used to continue CheckNow after determining which extensions
164   // should be force-updated.
165   void OnForcedUpdatesDetermined(const CheckParams& params,
166                                  const std::set<std::string>& forced_updates);
167
168   // Ensure that we have a valid ExtensionDownloader instance referenced by
169   // |downloader|.
170   void EnsureDownloaderCreated();
171
172   // Computes when to schedule the first update check.
173   base::TimeDelta DetermineFirstCheckDelay();
174
175   // Sets the timer to call TimerFired after roughly |target_delay| from now.
176   // To help spread load evenly on servers, this method adds some random
177   // jitter. It also saves the scheduled time so it can be reloaded on
178   // browser restart.
179   void ScheduleNextCheck(const base::TimeDelta& target_delay);
180
181   // Add fetch records for extensions that are installed to the downloader,
182   // ignoring |pending_ids| so the extension isn't fetched again.
183   void AddToDownloader(const ExtensionSet* extensions,
184                        const std::list<std::string>& pending_ids,
185                        int request_id);
186
187   // BaseTimer::ReceiverMethod callback.
188   void TimerFired();
189
190   // Posted by CheckSoon().
191   void DoCheckSoon();
192
193   // Implementation of ExtensionDownloaderDelegate.
194   void OnExtensionDownloadFailed(const std::string& id,
195                                  Error error,
196                                  const PingResult& ping,
197                                  const std::set<int>& request_ids) override;
198   void OnExtensionDownloadFinished(const std::string& id,
199                                    const base::FilePath& path,
200                                    bool file_ownership_passed,
201                                    const GURL& download_url,
202                                    const std::string& version,
203                                    const PingResult& ping,
204                                    const std::set<int>& request_id) override;
205   bool GetPingDataForExtension(const std::string& id,
206                                ManifestFetchData::PingData* ping_data) override;
207   std::string GetUpdateUrlData(const std::string& id) override;
208   bool IsExtensionPending(const std::string& id) override;
209   bool GetExtensionExistingVersion(const std::string& id,
210                                    std::string* version) override;
211   bool ShouldForceUpdate(const std::string& extension_id,
212                          std::string* source) override;
213
214   void UpdatePingData(const std::string& id, const PingResult& ping_result);
215
216   // Starts installing a crx file that has been fetched but not installed yet.
217   void MaybeInstallCRXFile();
218
219   // content::NotificationObserver implementation.
220   void Observe(int type,
221                const content::NotificationSource& source,
222                const content::NotificationDetails& details) override;
223
224   // Implementation of ExtensionRegistryObserver.
225   void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
226                                   const Extension* extension,
227                                   bool is_update,
228                                   bool from_ephemeral,
229                                   const std::string& old_name) override;
230
231   // Send a notification that update checks are starting.
232   void NotifyStarted();
233
234   // Send a notification if we're finished updating.
235   void NotifyIfFinished(int request_id);
236
237   void ExtensionCheckFinished(const std::string& extension_id,
238                               const FinishedCallback& callback);
239
240   // Whether Start() has been called but not Stop().
241   bool alive_;
242
243   // Pointer back to the service that owns this ExtensionUpdater.
244   ExtensionServiceInterface* service_;
245
246   // A closure passed into the ExtensionUpdater to teach it how to construct
247   // new ExtensionDownloader instances.
248   const ExtensionDownloader::Factory downloader_factory_;
249
250   // Fetches the crx files for the extensions that have an available update.
251   scoped_ptr<ExtensionDownloader> downloader_;
252
253   base::OneShotTimer<ExtensionUpdater> timer_;
254   int frequency_seconds_;
255   bool will_check_soon_;
256
257   ExtensionPrefs* extension_prefs_;
258   PrefService* prefs_;
259   Profile* profile_;
260
261   std::map<int, InProgressCheck> requests_in_progress_;
262   int next_request_id_;
263
264   // Observes CRX installs we initiate.
265   content::NotificationRegistrar registrar_;
266
267   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
268       extension_registry_observer_;
269
270   // True when a CrxInstaller is doing an install.  Used in MaybeUpdateCrxFile()
271   // to keep more than one install from running at once.
272   bool crx_install_is_running_;
273
274   // Fetched CRX files waiting to be installed.
275   std::stack<FetchedCRXFile> fetched_crx_files_;
276   FetchedCRXFile current_crx_file_;
277
278   CheckParams default_params_;
279
280   ExtensionCache* extension_cache_;
281
282   // Keeps track of when an extension tried to update itself, so we can throttle
283   // checks to prevent too many requests from being made.
284   std::map<std::string, ThrottleInfo> throttle_info_;
285
286   // Keeps track of extensions (by ID) whose update should be forced during the
287   // next update check.
288   std::set<std::string> forced_updates_;
289
290   base::WeakPtrFactory<ExtensionUpdater> weak_ptr_factory_;
291
292   DISALLOW_COPY_AND_ASSIGN(ExtensionUpdater);
293 };
294
295 }  // namespace extensions
296
297 #endif  // CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_