| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ |
| 6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ |
| 7 | 7 |
| 8 #include "base/containers/scoped_ptr_hash_map.h" | 8 #include "base/containers/scoped_ptr_hash_map.h" |
| 9 #include "base/threading/non_thread_safe.h" | 9 #include "base/threading/non_thread_safe.h" |
| 10 #include "google_apis/gaia/oauth2_token_service_request.h" | 10 #include "google_apis/gaia/oauth2_token_service_request.h" |
| 11 #include "net/url_request/url_fetcher_delegate.h" | 11 #include "net/url_request/url_fetcher_delegate.h" |
| 12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
| 13 #include "sync/internal_api/public/attachments/attachment_downloader.h" | 13 #include "sync/internal_api/public/attachments/attachment_downloader.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace base { |
| 17 class RefCountedMemory; |
| 18 } // namespace base |
| 19 |
| 16 namespace net { | 20 namespace net { |
| 17 class HttpResponseHeaders; | 21 class HttpResponseHeaders; |
| 18 } // namespace net | 22 } // namespace net |
| 19 | 23 |
| 20 namespace syncer { | 24 namespace syncer { |
| 21 | 25 |
| 22 // An implementation of AttachmentDownloader. | 26 // An implementation of AttachmentDownloader. |
| 23 class AttachmentDownloaderImpl : public AttachmentDownloader, | 27 class AttachmentDownloaderImpl : public AttachmentDownloader, |
| 24 public OAuth2TokenService::Consumer, | 28 public OAuth2TokenService::Consumer, |
| 25 public net::URLFetcherDelegate, | 29 public net::URLFetcherDelegate, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 52 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 56 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 53 const std::string& access_token, | 57 const std::string& access_token, |
| 54 const base::Time& expiration_time) override; | 58 const base::Time& expiration_time) override; |
| 55 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 59 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 56 const GoogleServiceAuthError& error) override; | 60 const GoogleServiceAuthError& error) override; |
| 57 | 61 |
| 58 // net::URLFetcherDelegate implementation. | 62 // net::URLFetcherDelegate implementation. |
| 59 void OnURLFetchComplete(const net::URLFetcher* source) override; | 63 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 60 | 64 |
| 61 private: | 65 private: |
| 66 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, |
| 67 ExtractCrc32c_NoHeaders); |
| 62 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_First); | 68 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_First); |
| 69 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_TooLong); |
| 63 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_None); | 70 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_None); |
| 64 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_Empty); | 71 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_Empty); |
| 65 | 72 |
| 66 struct DownloadState; | 73 struct DownloadState; |
| 67 typedef std::string AttachmentUrl; | 74 typedef std::string AttachmentUrl; |
| 68 typedef base::ScopedPtrHashMap<AttachmentUrl, DownloadState> StateMap; | 75 typedef base::ScopedPtrHashMap<AttachmentUrl, DownloadState> StateMap; |
| 69 typedef std::vector<DownloadState*> StateList; | 76 typedef std::vector<DownloadState*> StateList; |
| 70 | 77 |
| 71 scoped_ptr<net::URLFetcher> CreateFetcher(const AttachmentUrl& url, | 78 scoped_ptr<net::URLFetcher> CreateFetcher(const AttachmentUrl& url, |
| 72 const std::string& access_token); | 79 const std::string& access_token); |
| 73 void RequestAccessToken(DownloadState* download_state); | 80 void RequestAccessToken(DownloadState* download_state); |
| 74 void ReportResult( | 81 void ReportResult( |
| 75 const DownloadState& download_state, | 82 const DownloadState& download_state, |
| 76 const DownloadResult& result, | 83 const DownloadResult& result, |
| 77 const scoped_refptr<base::RefCountedString>& attachment_data); | 84 const scoped_refptr<base::RefCountedString>& attachment_data, |
| 78 | 85 uint32_t attachment_crc); |
| 79 // Verify the integrity of |data| using the hash received in |fetcher|. | |
| 80 // | |
| 81 // Assumes that the request in |fetcher| has completed. | |
| 82 // | |
| 83 // Returns true if the hash of |data| matches the hash contained in |fetcher| | |
| 84 // or if |fetcher| contains no hash (no hash, no problem). | |
| 85 static bool VerifyHashIfPresent(const net::URLFetcher& fetcher, | |
| 86 const std::string& data); | |
| 87 | 86 |
| 88 // Extract the crc32c from an X-Goog-Hash header in |headers|. | 87 // Extract the crc32c from an X-Goog-Hash header in |headers|. |
| 89 // | 88 // |
| 90 // Return true if a crc32c was found and set |crc32c|. | 89 // Return true if a crc32c was found and useable for checking data integrity. |
| 90 // This means headers are present, there is "x-goog-hash" header with "crc32c" |
| 91 // hash in it, this hash is correctly base64 encoded 32 but value. |
| 91 SYNC_EXPORT_PRIVATE static bool ExtractCrc32c( | 92 SYNC_EXPORT_PRIVATE static bool ExtractCrc32c( |
| 92 const net::HttpResponseHeaders& headers, | 93 const net::HttpResponseHeaders* headers, |
| 93 std::string* crc32c); | 94 uint32_t* crc32c); |
| 94 | 95 |
| 95 GURL sync_service_url_; | 96 GURL sync_service_url_; |
| 96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 97 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 97 | 98 |
| 98 std::string account_id_; | 99 std::string account_id_; |
| 99 OAuth2TokenService::ScopeSet oauth2_scopes_; | 100 OAuth2TokenService::ScopeSet oauth2_scopes_; |
| 100 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 101 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
| 101 token_service_provider_; | 102 token_service_provider_; |
| 102 scoped_ptr<OAuth2TokenService::Request> access_token_request_; | 103 scoped_ptr<OAuth2TokenService::Request> access_token_request_; |
| 103 | 104 |
| 104 StateMap state_map_; | 105 StateMap state_map_; |
| 105 // |requests_waiting_for_access_token_| only keeps references to DownloadState | 106 // |requests_waiting_for_access_token_| only keeps references to DownloadState |
| 106 // objects while access token request is pending. It doesn't control objects' | 107 // objects while access token request is pending. It doesn't control objects' |
| 107 // lifetime. | 108 // lifetime. |
| 108 StateList requests_waiting_for_access_token_; | 109 StateList requests_waiting_for_access_token_; |
| 109 | 110 |
| 110 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); | 111 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); |
| 111 }; | 112 }; |
| 112 | 113 |
| 113 } // namespace syncer | 114 } // namespace syncer |
| 114 | 115 |
| 115 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | 116 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ |
| OLD | NEW |