Upstream version 7.36.149.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/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"
26 #include "url/gurl.h"
27
28 class ExtensionServiceInterface;
29 class PrefService;
30 class Profile;
31
32 namespace extensions {
33
34 class ExtensionCache;
35 class ExtensionDownloader;
36 class ExtensionPrefs;
37 class ExtensionSet;
38 class ExtensionUpdaterTest;
39
40 // A class for doing auto-updates of installed Extensions. Used like this:
41 //
42 // ExtensionUpdater* updater = new ExtensionUpdater(my_extensions_service,
43 //                                                  extension_prefs,
44 //                                                  pref_service,
45 //                                                  profile,
46 //                                                  update_frequency_secs);
47 // updater->Start();
48 // ....
49 // updater->Stop();
50 class ExtensionUpdater : public ExtensionDownloaderDelegate,
51                          public content::NotificationObserver {
52  public:
53   typedef base::Closure FinishedCallback;
54
55   struct CheckParams {
56     // Creates a default CheckParams instance that checks for all extensions.
57     CheckParams();
58     ~CheckParams();
59
60     // The set of extensions that should be checked for updates. If empty
61     // all extensions will be included in the update check.
62     std::list<std::string> ids;
63
64     // Normally extension updates get installed only when the extension is idle.
65     // Setting this to true causes any updates that are found to be installed
66     // right away.
67     bool install_immediately;
68
69     // Callback to call when the update check is complete. Can be null, if
70     // you're not interested in when this happens.
71     FinishedCallback callback;
72   };
73
74   // Holds a pointer to the passed |service|, using it for querying installed
75   // extensions and installing updated ones. The |frequency_seconds| parameter
76   // controls how often update checks are scheduled.
77   ExtensionUpdater(ExtensionServiceInterface* service,
78                    ExtensionPrefs* extension_prefs,
79                    PrefService* prefs,
80                    Profile* profile,
81                    int frequency_seconds,
82                    ExtensionCache* cache);
83
84   virtual ~ExtensionUpdater();
85
86   // Starts the updater running.  Should be called at most once.
87   void Start();
88
89   // Stops the updater running, cancelling any outstanding update manifest and
90   // crx downloads. Does not cancel any in-progress installs.
91   void Stop();
92
93   // Posts a task to do an update check.  Does nothing if there is
94   // already a pending task that has not yet run.
95   void CheckSoon();
96
97   // Starts an update check for the specified extension soon. If a check
98   // is already running, or finished too recently without an update being
99   // installed, this method returns false and the check won't be scheduled.
100   bool CheckExtensionSoon(const std::string& extension_id,
101                           const FinishedCallback& callback);
102
103   // Starts an update check right now, instead of waiting for the next
104   // regularly scheduled check or a pending check from CheckSoon().
105   void CheckNow(const CheckParams& params);
106
107   // Returns true iff CheckSoon() has been called but the update check
108   // hasn't been performed yet.  This is used mostly by tests; calling
109   // code should just call CheckSoon().
110   bool WillCheckSoon() const;
111
112   // Changes the params that are used for the automatic periodic update checks,
113   // as well as for explicit calls to CheckSoon.
114   void set_default_check_params(const CheckParams& params) {
115     default_params_ = params;
116   }
117
118  private:
119   friend class ExtensionUpdaterTest;
120   friend class ExtensionUpdaterFileHandler;
121
122   // FetchedCRXFile holds information about a CRX file we fetched to disk,
123   // but have not yet installed.
124   struct FetchedCRXFile {
125     FetchedCRXFile();
126     FetchedCRXFile(const std::string& id,
127                    const base::FilePath& path,
128                    bool file_ownership_passed,
129                    const std::set<int>& request_ids);
130     ~FetchedCRXFile();
131
132     std::string extension_id;
133     base::FilePath path;
134     bool file_ownership_passed;
135     std::set<int> request_ids;
136   };
137
138   struct InProgressCheck {
139     InProgressCheck();
140     ~InProgressCheck();
141
142     bool install_immediately;
143     FinishedCallback callback;
144     // The ids of extensions that have in-progress update checks.
145     std::list<std::string> in_progress_ids_;
146   };
147
148   struct ThrottleInfo;
149
150   // Computes when to schedule the first update check.
151   base::TimeDelta DetermineFirstCheckDelay();
152
153   // Sets the timer to call TimerFired after roughly |target_delay| from now.
154   // To help spread load evenly on servers, this method adds some random
155   // jitter. It also saves the scheduled time so it can be reloaded on
156   // browser restart.
157   void ScheduleNextCheck(const base::TimeDelta& target_delay);
158
159   // Add fetch records for extensions that are installed to the downloader,
160   // ignoring |pending_ids| so the extension isn't fetched again.
161   void AddToDownloader(const ExtensionSet* extensions,
162                        const std::list<std::string>& pending_ids,
163                        int request_id);
164
165   // BaseTimer::ReceiverMethod callback.
166   void TimerFired();
167
168   // Posted by CheckSoon().
169   void DoCheckSoon();
170
171   // Implenentation of ExtensionDownloaderDelegate.
172   virtual void OnExtensionDownloadFailed(
173       const std::string& id,
174       Error error,
175       const PingResult& ping,
176       const std::set<int>& request_ids) OVERRIDE;
177
178   virtual void OnExtensionDownloadFinished(
179       const std::string& id,
180       const base::FilePath& path,
181       bool file_ownership_passed,
182       const GURL& download_url,
183       const std::string& version,
184       const PingResult& ping,
185       const std::set<int>& request_id) OVERRIDE;
186
187   virtual bool GetPingDataForExtension(
188       const std::string& id,
189       ManifestFetchData::PingData* ping_data) OVERRIDE;
190
191   virtual std::string GetUpdateUrlData(const std::string& id) OVERRIDE;
192
193   virtual bool IsExtensionPending(const std::string& id) OVERRIDE;
194
195   virtual bool GetExtensionExistingVersion(const std::string& id,
196                                            std::string* version) OVERRIDE;
197
198   void UpdatePingData(const std::string& id, const PingResult& ping_result);
199
200   // Starts installing a crx file that has been fetched but not installed yet.
201   void MaybeInstallCRXFile();
202
203   // content::NotificationObserver implementation.
204   virtual void Observe(int type,
205                        const content::NotificationSource& source,
206                        const content::NotificationDetails& details) OVERRIDE;
207
208   // Send a notification that update checks are starting.
209   void NotifyStarted();
210
211   // Send a notification if we're finished updating.
212   void NotifyIfFinished(int request_id);
213
214   void ExtensionCheckFinished(const std::string& extension_id,
215                               const FinishedCallback& callback);
216
217   // Whether Start() has been called but not Stop().
218   bool alive_;
219
220   base::WeakPtrFactory<ExtensionUpdater> weak_ptr_factory_;
221
222   // Pointer back to the service that owns this ExtensionUpdater.
223   ExtensionServiceInterface* service_;
224
225   // Fetches the crx files for the extensions that have an available update.
226   scoped_ptr<ExtensionDownloader> downloader_;
227
228   base::OneShotTimer<ExtensionUpdater> timer_;
229   int frequency_seconds_;
230   bool will_check_soon_;
231
232   ExtensionPrefs* extension_prefs_;
233   PrefService* prefs_;
234   Profile* profile_;
235
236   std::map<int, InProgressCheck> requests_in_progress_;
237   int next_request_id_;
238
239   // Observes CRX installs we initiate.
240   content::NotificationRegistrar registrar_;
241
242   // True when a CrxInstaller is doing an install.  Used in MaybeUpdateCrxFile()
243   // to keep more than one install from running at once.
244   bool crx_install_is_running_;
245
246   // Fetched CRX files waiting to be installed.
247   std::stack<FetchedCRXFile> fetched_crx_files_;
248   FetchedCRXFile current_crx_file_;
249
250   CheckParams default_params_;
251
252   ExtensionCache* extension_cache_;
253
254   // Keeps track of when an extension tried to update itself, so we can throttle
255   // checks to prevent too many requests from being made.
256   std::map<std::string, ThrottleInfo> throttle_info_;
257
258   DISALLOW_COPY_AND_ASSIGN(ExtensionUpdater);
259 };
260
261 }  // namespace extensions
262
263 #endif  // CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_UPDATER_H_