Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / updater / extension_downloader.h
index a40427d..a040986 100644 (file)
 #include "chrome/browser/extensions/updater/extension_downloader_delegate.h"
 #include "chrome/browser/extensions/updater/manifest_fetch_data.h"
 #include "chrome/browser/extensions/updater/request_queue.h"
-#include "chrome/common/extensions/update_manifest.h"
 #include "extensions/common/extension.h"
+#include "extensions/common/update_manifest.h"
+#include "google_apis/gaia/oauth2_token_service.h"
 #include "net/url_request/url_fetcher_delegate.h"
 #include "url/gurl.h"
 
+class IdentityProvider;
+
 namespace net {
 class URLFetcher;
 class URLRequestContextGetter;
@@ -49,8 +52,16 @@ class ExtensionUpdaterTest;
 // the crx file when updates are found. It uses a |ExtensionDownloaderDelegate|
 // that takes ownership of the downloaded crx files, and handles events during
 // the update check.
-class ExtensionDownloader : public net::URLFetcherDelegate {
+class ExtensionDownloader
+    : public net::URLFetcherDelegate,
+      public OAuth2TokenService::Consumer {
  public:
+  // A closure which constructs a new ExtensionDownloader to be owned by the
+  // caller.
+  typedef base::Callback<
+      scoped_ptr<ExtensionDownloader>(ExtensionDownloaderDelegate* delegate)>
+      Factory;
+
   // |delegate| is stored as a raw pointer and must outlive the
   // ExtensionDownloader.
   ExtensionDownloader(ExtensionDownloaderDelegate* delegate,
@@ -84,6 +95,11 @@ class ExtensionDownloader : public net::URLFetcherDelegate {
                             const ManifestFetchData::PingData& ping_data,
                             int request_id);
 
+  // Sets an IdentityProvider to be used for OAuth2 authentication on protected
+  // Webstore downloads.
+  void SetWebstoreIdentityProvider(
+      scoped_ptr<IdentityProvider> identity_provider);
+
   // These are needed for unit testing, to help identify the correct mock
   // URLFetcher objects.
   static const int kManifestFetcherId = 1;
@@ -130,8 +146,18 @@ class ExtensionDownloader : public net::URLFetcherDelegate {
     std::string version;
     std::set<int> request_ids;
 
-    // Indicates whether or not the fetch is known to require credentials.
-    bool is_protected;
+    enum CredentialsMode {
+      CREDENTIALS_NONE = 0,
+      CREDENTIALS_OAUTH2_TOKEN,
+      CREDENTIALS_COOKIES,
+    };
+
+    // Indicates the type of credentials to include with this fetch.
+    CredentialsMode credentials;
+
+    // Counts the number of times OAuth2 authentication has been attempted for
+    // this fetch.
+    int oauth2_attempt_count;
   };
 
   // Helper for AddExtension() and AddPendingExtension().
@@ -204,6 +230,20 @@ class ExtensionDownloader : public net::URLFetcherDelegate {
                                       const base::FilePath& crx_path,
                                       bool file_ownership_passed);
 
+  // Potentially updates an ExtensionFetch's authentication state and returns
+  // |true| if the fetch should be retried. Returns |false| if the failure was
+  // not related to authentication, leaving the ExtensionFetch data unmodified.
+  bool IterateFetchCredentialsAfterFailure(ExtensionFetch* fetch,
+                                           const net::URLRequestStatus& status,
+                                           int response_code);
+
+  // OAuth2TokenService::Consumer implementation.
+  virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
+                                 const std::string& access_token,
+                                 const base::Time& expiration_time) OVERRIDE;
+  virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
+                                 const GoogleServiceAuthError& error) OVERRIDE;
+
   // The delegate that receives the crx files downloaded by the
   // ExtensionDownloader, and that fills in optional ping and update url data.
   ExtensionDownloaderDelegate* delegate_;
@@ -240,6 +280,17 @@ class ExtensionDownloader : public net::URLFetcherDelegate {
   // Cache for .crx files.
   ExtensionCache* extension_cache_;
 
+  // An IdentityProvider which may be used for authentication on protected
+  // download requests. May be NULL.
+  scoped_ptr<IdentityProvider> identity_provider_;
+
+  // A Webstore download-scoped access token for the |identity_provider_|'s
+  // active account, if any.
+  std::string access_token_;
+
+  // A pending token fetch request.
+  scoped_ptr<OAuth2TokenService::Request> access_token_request_;
+
   DISALLOW_COPY_AND_ASSIGN(ExtensionDownloader);
 };