OLD | NEW |
| (Empty) |
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 COMPONENTS_SYNC_CORE_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | |
6 #define COMPONENTS_SYNC_CORE_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 #include <unordered_map> | |
13 #include <vector> | |
14 | |
15 #include "base/gtest_prod_util.h" | |
16 #include "base/macros.h" | |
17 #include "base/threading/non_thread_safe.h" | |
18 #include "components/sync/base/model_type.h" | |
19 #include "components/sync/core/attachments/attachment_downloader.h" | |
20 #include "google_apis/gaia/oauth2_token_service_request.h" | |
21 #include "net/url_request/url_fetcher_delegate.h" | |
22 #include "net/url_request/url_request_context_getter.h" | |
23 #include "url/gurl.h" | |
24 | |
25 namespace base { | |
26 class RefCountedMemory; | |
27 } // namespace base | |
28 | |
29 namespace net { | |
30 class HttpResponseHeaders; | |
31 } // namespace net | |
32 | |
33 namespace syncer { | |
34 | |
35 // An implementation of AttachmentDownloader. | |
36 class AttachmentDownloaderImpl : public AttachmentDownloader, | |
37 public OAuth2TokenService::Consumer, | |
38 public net::URLFetcherDelegate, | |
39 public base::NonThreadSafe { | |
40 public: | |
41 // |sync_service_url| is the URL of the sync service. | |
42 // | |
43 // |url_request_context_getter| provides a URLRequestContext. | |
44 // | |
45 // |account_id| is the account id to use for downloads. | |
46 // | |
47 // |scopes| is the set of scopes to use for downloads. | |
48 // | |
49 // |token_service_provider| provides an OAuth2 token service. | |
50 // | |
51 // |store_birthday| is the raw, sync store birthday. | |
52 // | |
53 // |model_type| is the model type this downloader is used with. | |
54 AttachmentDownloaderImpl( | |
55 const GURL& sync_service_url, | |
56 const scoped_refptr<net::URLRequestContextGetter>& | |
57 url_request_context_getter, | |
58 const std::string& account_id, | |
59 const OAuth2TokenService::ScopeSet& scopes, | |
60 const scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider>& | |
61 token_service_provider, | |
62 const std::string& store_birthday, | |
63 ModelType model_type); | |
64 ~AttachmentDownloaderImpl() override; | |
65 | |
66 // AttachmentDownloader implementation. | |
67 void DownloadAttachment(const AttachmentId& attachment_id, | |
68 const DownloadCallback& callback) override; | |
69 | |
70 // OAuth2TokenService::Consumer implementation. | |
71 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
72 const std::string& access_token, | |
73 const base::Time& expiration_time) override; | |
74 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
75 const GoogleServiceAuthError& error) override; | |
76 | |
77 // net::URLFetcherDelegate implementation. | |
78 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
79 | |
80 private: | |
81 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, | |
82 ExtractCrc32c_NoHeaders); | |
83 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_First); | |
84 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_TooLong); | |
85 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_None); | |
86 FRIEND_TEST_ALL_PREFIXES(AttachmentDownloaderImplTest, ExtractCrc32c_Empty); | |
87 | |
88 struct DownloadState; | |
89 typedef std::string AttachmentUrl; | |
90 typedef std::unordered_map<AttachmentUrl, std::unique_ptr<DownloadState>> | |
91 StateMap; | |
92 typedef std::vector<DownloadState*> StateList; | |
93 | |
94 std::unique_ptr<net::URLFetcher> CreateFetcher( | |
95 const AttachmentUrl& url, | |
96 const std::string& access_token); | |
97 void RequestAccessToken(DownloadState* download_state); | |
98 void ReportResult( | |
99 const DownloadState& download_state, | |
100 const DownloadResult& result, | |
101 const scoped_refptr<base::RefCountedString>& attachment_data); | |
102 | |
103 // Extract the crc32c from an X-Goog-Hash header in |headers|. | |
104 // | |
105 // Return true if a crc32c was found and useable for checking data integrity. | |
106 // "Usable" means headers are present, there is "x-goog-hash" header with | |
107 // "crc32c" hash in it, this hash is correctly base64 encoded 32 bit integer. | |
108 static bool ExtractCrc32c(const net::HttpResponseHeaders* headers, | |
109 uint32_t* crc32c); | |
110 | |
111 GURL sync_service_url_; | |
112 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | |
113 | |
114 std::string account_id_; | |
115 OAuth2TokenService::ScopeSet oauth2_scopes_; | |
116 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> | |
117 token_service_provider_; | |
118 std::unique_ptr<OAuth2TokenService::Request> access_token_request_; | |
119 std::string raw_store_birthday_; | |
120 | |
121 StateMap state_map_; | |
122 // |requests_waiting_for_access_token_| only keeps references to DownloadState | |
123 // objects while access token request is pending. It doesn't control objects' | |
124 // lifetime. | |
125 StateList requests_waiting_for_access_token_; | |
126 | |
127 ModelType model_type_; | |
128 | |
129 DISALLOW_COPY_AND_ASSIGN(AttachmentDownloaderImpl); | |
130 }; | |
131 | |
132 } // namespace syncer | |
133 | |
134 #endif // COMPONENTS_SYNC_CORE_ATTACHMENTS_ATTACHMENT_DOWNLOADER_IMPL_H_ | |
OLD | NEW |