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