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.
5 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_
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 "sync/internal_api/public/base/model_type.h"
18 class RefCountedMemory
;
22 class HttpResponseHeaders
;
27 // An implementation of AttachmentDownloader.
28 class AttachmentDownloaderImpl
: public AttachmentDownloader
,
29 public OAuth2TokenService::Consumer
,
30 public net::URLFetcherDelegate
,
31 public base::NonThreadSafe
{
33 // |sync_service_url| is the URL of the sync service.
35 // |url_request_context_getter| provides a URLRequestContext.
37 // |account_id| is the account id to use for downloads.
39 // |scopes| is the set of scopes to use for downloads.
41 // |token_service_provider| provides an OAuth2 token service.
43 // |store_birthday| is the raw, sync store birthday.
45 // |model_type| is the model type this downloader is used with.
46 AttachmentDownloaderImpl(
47 const GURL
& sync_service_url
,
48 const scoped_refptr
<net::URLRequestContextGetter
>&
49 url_request_context_getter
,
50 const std::string
& account_id
,
51 const OAuth2TokenService::ScopeSet
& scopes
,
52 const scoped_refptr
<OAuth2TokenServiceRequest::TokenServiceProvider
>&
53 token_service_provider
,
54 const std::string
& store_birthday
,
55 ModelType model_type
);
56 ~AttachmentDownloaderImpl() override
;
58 // AttachmentDownloader implementation.
59 void DownloadAttachment(const AttachmentId
& attachment_id
,
60 const DownloadCallback
& callback
) override
;
62 // OAuth2TokenService::Consumer implementation.
63 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
64 const std::string
& access_token
,
65 const base::Time
& expiration_time
) override
;
66 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
67 const GoogleServiceAuthError
& error
) override
;
69 // net::URLFetcherDelegate implementation.
70 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
73 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest
,
74 ExtractCrc32c_NoHeaders
);
75 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest
, ExtractCrc32c_First
);
76 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest
, ExtractCrc32c_TooLong
);
77 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest
, ExtractCrc32c_None
);
78 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest
, ExtractCrc32c_Empty
);
81 typedef std::string AttachmentUrl
;
82 typedef base::ScopedPtrHashMap
<AttachmentUrl
, scoped_ptr
<DownloadState
>>
84 typedef std::vector
<DownloadState
*> StateList
;
86 scoped_ptr
<net::URLFetcher
> CreateFetcher(const AttachmentUrl
& url
,
87 const std::string
& access_token
);
88 void RequestAccessToken(DownloadState
* download_state
);
90 const DownloadState
& download_state
,
91 const DownloadResult
& result
,
92 const scoped_refptr
<base::RefCountedString
>& attachment_data
);
94 // Extract the crc32c from an X-Goog-Hash header in |headers|.
96 // Return true if a crc32c was found and useable for checking data integrity.
97 // "Usable" means headers are present, there is "x-goog-hash" header with
98 // "crc32c" hash in it, this hash is correctly base64 encoded 32 bit integer.
99 SYNC_EXPORT_PRIVATE
static bool ExtractCrc32c(
100 const net::HttpResponseHeaders
* headers
,
103 GURL sync_service_url_
;
104 scoped_refptr
<net::URLRequestContextGetter
> url_request_context_getter_
;
106 std::string account_id_
;
107 OAuth2TokenService::ScopeSet oauth2_scopes_
;
108 scoped_refptr
<OAuth2TokenServiceRequest::TokenServiceProvider
>
109 token_service_provider_
;
110 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
111 std::string raw_store_birthday_
;
114 // |requests_waiting_for_access_token_| only keeps references to DownloadState
115 // objects while access token request is pending. It doesn't control objects'
117 StateList requests_waiting_for_access_token_
;
119 ModelType model_type_
;
121 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl
);
124 } // namespace syncer
126 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_