1// Copyright 2014 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 SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ 6#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ 7 8#include "base/containers/scoped_ptr_hash_map.h" 9#include "base/threading/non_thread_safe.h" 10#include "google_apis/gaia/oauth2_token_service_request.h" 11#include "net/url_request/url_fetcher_delegate.h" 12#include "net/url_request/url_request_context_getter.h" 13#include "sync/internal_api/public/attachments/attachment_downloader.h" 14#include "url/gurl.h" 15 16namespace syncer { 17 18// An implementation of AttachmentDownloader. 19class AttachmentDownloaderImpl : public AttachmentDownloader, 20 public OAuth2TokenService::Consumer, 21 public net::URLFetcherDelegate, 22 public base::NonThreadSafe { 23 public: 24 // |sync_service_url| is the URL of the sync service. 25 // 26 // |url_request_context_getter| provides a URLRequestContext. 27 // 28 // |account_id| is the account id to use for downloads. 29 // 30 // |scopes| is the set of scopes to use for downloads. 31 // 32 // |token_service_provider| provides an OAuth2 token service. 33 AttachmentDownloaderImpl( 34 const GURL& sync_service_url, 35 const scoped_refptr<net::URLRequestContextGetter>& 36 url_request_context_getter, 37 const std::string& account_id, 38 const OAuth2TokenService::ScopeSet& scopes, 39 const scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>& 40 token_service_provider); 41 virtual ~AttachmentDownloaderImpl(); 42 43 // AttachmentDownloader implementation. 44 virtual void DownloadAttachment(const AttachmentId& attachment_id, 45 const DownloadCallback& callback) OVERRIDE; 46 47 // OAuth2TokenService::Consumer implementation. 48 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 49 const std::string& access_token, 50 const base::Time& expiration_time) OVERRIDE; 51 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, 52 const GoogleServiceAuthError& error) OVERRIDE; 53 54 // net::URLFetcherDelegate implementation. 55 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 56 57 private: 58 struct DownloadState; 59 typedef std::string AttachmentUrl; 60 typedef base::ScopedPtrHashMap<AttachmentUrl, DownloadState> StateMap; 61 typedef std::vector<DownloadState*> StateList; 62 63 scoped_ptr<net::URLFetcher> CreateFetcher(const AttachmentUrl& url, 64 const std::string& access_token); 65 void RequestAccessToken(DownloadState* download_state); 66 void ReportResult( 67 const DownloadState& download_state, 68 const DownloadResult& result, 69 const scoped_refptr<base::RefCountedString>& attachment_data); 70 71 GURL sync_service_url_; 72 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; 73 74 std::string account_id_; 75 OAuth2TokenService::ScopeSet oauth2_scopes_; 76 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> 77 token_service_provider_; 78 scoped_ptr<OAuth2TokenService::Request> access_token_request_; 79 80 StateMap state_map_; 81 // |requests_waiting_for_access_token_| only keeps references to DownloadState 82 // objects while access token request is pending. It doesn't control objects' 83 // lifetime. 84 StateList requests_waiting_for_access_token_; 85 86 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); 87}; 88 89} // namespace syncer 90 91#endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ 92