OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_DOWNLOADER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_DOWNLOADER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_DOWNLOADER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_DOWNLOADER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
12 #include <utility> | 12 #include <utility> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/memory/linked_ptr.h" | 17 #include "base/memory/linked_ptr.h" |
18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/version.h" | 20 #include "base/version.h" |
21 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h" | 21 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h" |
22 #include "chrome/browser/extensions/updater/manifest_fetch_data.h" | 22 #include "chrome/browser/extensions/updater/manifest_fetch_data.h" |
23 #include "chrome/browser/extensions/updater/request_queue.h" | 23 #include "chrome/browser/extensions/updater/request_queue.h" |
24 #include "chrome/common/extensions/update_manifest.h" | 24 #include "chrome/common/extensions/update_manifest.h" |
25 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
| 26 #include "google_apis/gaia/oauth2_token_service.h" |
26 #include "net/url_request/url_fetcher_delegate.h" | 27 #include "net/url_request/url_fetcher_delegate.h" |
27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
28 | 29 |
| 30 class IdentityProvider; |
| 31 |
29 namespace net { | 32 namespace net { |
30 class URLFetcher; | 33 class URLFetcher; |
31 class URLRequestContextGetter; | 34 class URLRequestContextGetter; |
32 class URLRequestStatus; | 35 class URLRequestStatus; |
33 } | 36 } |
34 | 37 |
35 namespace extensions { | 38 namespace extensions { |
36 | 39 |
37 struct UpdateDetails { | 40 struct UpdateDetails { |
38 UpdateDetails(const std::string& id, const base::Version& version); | 41 UpdateDetails(const std::string& id, const base::Version& version); |
39 ~UpdateDetails(); | 42 ~UpdateDetails(); |
40 | 43 |
41 std::string id; | 44 std::string id; |
42 base::Version version; | 45 base::Version version; |
43 }; | 46 }; |
44 | 47 |
45 class ExtensionCache; | 48 class ExtensionCache; |
46 class ExtensionUpdaterTest; | 49 class ExtensionUpdaterTest; |
| 50 class WebstoreOAuth2TokenProvider; |
47 | 51 |
48 // A class that checks for updates of a given list of extensions, and downloads | 52 // A class that checks for updates of a given list of extensions, and downloads |
49 // the crx file when updates are found. It uses a |ExtensionDownloaderDelegate| | 53 // the crx file when updates are found. It uses a |ExtensionDownloaderDelegate| |
50 // that takes ownership of the downloaded crx files, and handles events during | 54 // that takes ownership of the downloaded crx files, and handles events during |
51 // the update check. | 55 // the update check. |
52 class ExtensionDownloader : public net::URLFetcherDelegate { | 56 class ExtensionDownloader |
| 57 : public net::URLFetcherDelegate, |
| 58 public OAuth2TokenService::Consumer { |
53 public: | 59 public: |
54 // |delegate| is stored as a raw pointer and must outlive the | 60 // |delegate| is stored as a raw pointer and must outlive the |
55 // ExtensionDownloader. | 61 // ExtensionDownloader. |webstore_identity_provider| may be NULL if this |
| 62 // ExtensionDownloader does not need OAuth2 support; if not NULL, the |
| 63 // given IdentityProvider must outlive this ExtensionDownloader. |
56 ExtensionDownloader(ExtensionDownloaderDelegate* delegate, | 64 ExtensionDownloader(ExtensionDownloaderDelegate* delegate, |
57 net::URLRequestContextGetter* request_context); | 65 net::URLRequestContextGetter* request_context, |
| 66 IdentityProvider* webstore_identity_provider); |
58 virtual ~ExtensionDownloader(); | 67 virtual ~ExtensionDownloader(); |
59 | 68 |
60 // Adds |extension| to the list of extensions to check for updates. | 69 // Adds |extension| to the list of extensions to check for updates. |
61 // Returns false if the |extension| can't be updated due to invalid details. | 70 // Returns false if the |extension| can't be updated due to invalid details. |
62 // In that case, no callbacks will be performed on the |delegate_|. | 71 // In that case, no callbacks will be performed on the |delegate_|. |
63 // The |request_id| is passed on as is to the various |delegate_| callbacks. | 72 // The |request_id| is passed on as is to the various |delegate_| callbacks. |
64 // This is used for example by ExtensionUpdater to keep track of when | 73 // This is used for example by ExtensionUpdater to keep track of when |
65 // potentially concurrent update checks complete. | 74 // potentially concurrent update checks complete. |
66 bool AddExtension(const Extension& extension, int request_id); | 75 bool AddExtension(const Extension& extension, int request_id); |
67 | 76 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 const std::string& package_hash, const std::string& version, | 132 const std::string& package_hash, const std::string& version, |
124 const std::set<int>& request_ids); | 133 const std::set<int>& request_ids); |
125 ~ExtensionFetch(); | 134 ~ExtensionFetch(); |
126 | 135 |
127 std::string id; | 136 std::string id; |
128 GURL url; | 137 GURL url; |
129 std::string package_hash; | 138 std::string package_hash; |
130 std::string version; | 139 std::string version; |
131 std::set<int> request_ids; | 140 std::set<int> request_ids; |
132 | 141 |
133 // Indicates whether or not the fetch is known to require credentials. | 142 enum CredentialsMode { |
134 bool is_protected; | 143 CREDENTIALS_NONE = 0, |
| 144 CREDENTIALS_OAUTH2_TOKEN, |
| 145 CREDENTIALS_COOKIES, |
| 146 }; |
| 147 |
| 148 // Indicates the type of credentials to include with this fetch. |
| 149 CredentialsMode credentials; |
| 150 |
| 151 // Counts the number of times OAuth2 authentication has been attempted for |
| 152 // this fetch. |
| 153 int oauth2_attempt_count; |
135 }; | 154 }; |
136 | 155 |
137 // Helper for AddExtension() and AddPendingExtension(). | 156 // Helper for AddExtension() and AddPendingExtension(). |
138 bool AddExtensionData(const std::string& id, | 157 bool AddExtensionData(const std::string& id, |
139 const base::Version& version, | 158 const base::Version& version, |
140 Manifest::Type extension_type, | 159 Manifest::Type extension_type, |
141 const GURL& extension_update_url, | 160 const GURL& extension_update_url, |
142 const std::string& update_url_data, | 161 const std::string& update_url_data, |
143 int request_id); | 162 int request_id); |
144 | 163 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 | 216 |
198 // Do real work of StartAllPending. If .crx cache is used, this function | 217 // Do real work of StartAllPending. If .crx cache is used, this function |
199 // is called when cache is ready. | 218 // is called when cache is ready. |
200 void DoStartAllPending(); | 219 void DoStartAllPending(); |
201 | 220 |
202 // Notify delegate and remove ping results. | 221 // Notify delegate and remove ping results. |
203 void NotifyDelegateDownloadFinished(scoped_ptr<ExtensionFetch> fetch_data, | 222 void NotifyDelegateDownloadFinished(scoped_ptr<ExtensionFetch> fetch_data, |
204 const base::FilePath& crx_path, | 223 const base::FilePath& crx_path, |
205 bool file_ownership_passed); | 224 bool file_ownership_passed); |
206 | 225 |
| 226 // Potentially updates an ExtensionFetch's authentication state and returns |
| 227 // |true| if the fetch should be retried. Returns |false| if the failure was |
| 228 // not related to authentication, leaving the ExtensionFetch data unmodified. |
| 229 bool IterateFetchCredentialsAfterFailure(ExtensionFetch* fetch, |
| 230 const net::URLRequestStatus& status, |
| 231 int response_code); |
| 232 |
| 233 // OAuth2TokenService::Consumer implementation. |
| 234 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 235 const std::string& access_token, |
| 236 const base::Time& expiration_time) OVERRIDE; |
| 237 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 238 const GoogleServiceAuthError& error) OVERRIDE; |
| 239 |
207 // The delegate that receives the crx files downloaded by the | 240 // The delegate that receives the crx files downloaded by the |
208 // ExtensionDownloader, and that fills in optional ping and update url data. | 241 // ExtensionDownloader, and that fills in optional ping and update url data. |
209 ExtensionDownloaderDelegate* delegate_; | 242 ExtensionDownloaderDelegate* delegate_; |
210 | 243 |
211 // The request context to use for the URLFetchers. | 244 // The request context to use for the URLFetchers. |
212 scoped_refptr<net::URLRequestContextGetter> request_context_; | 245 scoped_refptr<net::URLRequestContextGetter> request_context_; |
213 | 246 |
214 // Used to create WeakPtrs to |this|. | 247 // Used to create WeakPtrs to |this|. |
215 base::WeakPtrFactory<ExtensionDownloader> weak_ptr_factory_; | 248 base::WeakPtrFactory<ExtensionDownloader> weak_ptr_factory_; |
216 | 249 |
(...skipping 16 matching lines...) Expand all Loading... |
233 // is available. | 266 // is available. |
234 RequestQueue<ManifestFetchData> manifests_queue_; | 267 RequestQueue<ManifestFetchData> manifests_queue_; |
235 RequestQueue<ExtensionFetch> extensions_queue_; | 268 RequestQueue<ExtensionFetch> extensions_queue_; |
236 | 269 |
237 // Maps an extension-id to its PingResult data. | 270 // Maps an extension-id to its PingResult data. |
238 std::map<std::string, ExtensionDownloaderDelegate::PingResult> ping_results_; | 271 std::map<std::string, ExtensionDownloaderDelegate::PingResult> ping_results_; |
239 | 272 |
240 // Cache for .crx files. | 273 // Cache for .crx files. |
241 ExtensionCache* extension_cache_; | 274 ExtensionCache* extension_cache_; |
242 | 275 |
| 276 // An IdentityProvider which may be used for authentication on protected |
| 277 // download requests. May be NULL. |
| 278 IdentityProvider* identity_provider_; |
| 279 |
| 280 // A Webstore download-scoped access token for the |identity_provider_|'s |
| 281 // active account, if any. |
| 282 std::string access_token_; |
| 283 |
| 284 // A pending token fetch request. |
| 285 scoped_ptr<OAuth2TokenService::Request> access_token_request_; |
| 286 |
243 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloader); | 287 DISALLOW_COPY_AND_ASSIGN(ExtensionDownloader); |
244 }; | 288 }; |
245 | 289 |
246 } // namespace extensions | 290 } // namespace extensions |
247 | 291 |
248 #endif // CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_DOWNLOADER_H_ | 292 #endif // CHROME_BROWSER_EXTENSIONS_UPDATER_EXTENSION_DOWNLOADER_H_ |
OLD | NEW |