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 #include "sync/internal_api/public/attachments/attachment_downloader_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_downloader_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
10 #include "net/http/http_status_code.h" | 10 #include "net/http/http_status_code.h" |
11 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
| 12 #include "sync/internal_api/public/attachments/attachment_uploader_impl.h" |
12 #include "sync/protocol/sync.pb.h" | 13 #include "sync/protocol/sync.pb.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 | 15 |
15 namespace syncer { | 16 namespace syncer { |
16 | 17 |
17 struct AttachmentDownloaderImpl::DownloadState { | 18 struct AttachmentDownloaderImpl::DownloadState { |
18 public: | 19 public: |
19 DownloadState(const AttachmentId& attachment_id, | 20 DownloadState(const AttachmentId& attachment_id, |
20 const AttachmentUrl& attachment_url); | 21 const AttachmentUrl& attachment_url); |
21 | 22 |
22 AttachmentId attachment_id; | 23 AttachmentId attachment_id; |
23 AttachmentUrl attachment_url; | 24 AttachmentUrl attachment_url; |
24 // |access_token| needed to invalidate if downloading attachment fails with | 25 // |access_token| needed to invalidate if downloading attachment fails with |
25 // HTTP_UNAUTHORIZED. | 26 // HTTP_UNAUTHORIZED. |
26 std::string access_token; | 27 std::string access_token; |
27 scoped_ptr<net::URLFetcher> url_fetcher; | 28 scoped_ptr<net::URLFetcher> url_fetcher; |
28 std::vector<DownloadCallback> user_callbacks; | 29 std::vector<DownloadCallback> user_callbacks; |
29 }; | 30 }; |
30 | 31 |
31 AttachmentDownloaderImpl::DownloadState::DownloadState( | 32 AttachmentDownloaderImpl::DownloadState::DownloadState( |
32 const AttachmentId& attachment_id, | 33 const AttachmentId& attachment_id, |
33 const AttachmentUrl& attachment_url) | 34 const AttachmentUrl& attachment_url) |
34 : attachment_id(attachment_id), attachment_url(attachment_url) { | 35 : attachment_id(attachment_id), attachment_url(attachment_url) { |
35 } | 36 } |
36 | 37 |
37 AttachmentDownloaderImpl::AttachmentDownloaderImpl( | 38 AttachmentDownloaderImpl::AttachmentDownloaderImpl( |
38 const std::string& url_prefix, | 39 const GURL& sync_service_url, |
39 const scoped_refptr<net::URLRequestContextGetter>& | 40 const scoped_refptr<net::URLRequestContextGetter>& |
40 url_request_context_getter, | 41 url_request_context_getter, |
41 const std::string& account_id, | 42 const std::string& account_id, |
42 const OAuth2TokenService::ScopeSet& scopes, | 43 const OAuth2TokenService::ScopeSet& scopes, |
43 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 44 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
44 token_service_provider) | 45 token_service_provider) |
45 : OAuth2TokenService::Consumer("attachment-downloader-impl"), | 46 : OAuth2TokenService::Consumer("attachment-downloader-impl"), |
46 url_prefix_(url_prefix), | 47 sync_service_url_(sync_service_url), |
47 url_request_context_getter_(url_request_context_getter), | 48 url_request_context_getter_(url_request_context_getter), |
48 account_id_(account_id), | 49 account_id_(account_id), |
49 oauth2_scopes_(scopes), | 50 oauth2_scopes_(scopes), |
50 token_service_provider_(token_service_provider.Pass()) { | 51 token_service_provider_(token_service_provider.Pass()) { |
51 DCHECK(token_service_provider_); | 52 DCHECK(token_service_provider_); |
52 DCHECK(url_request_context_getter_); | 53 DCHECK(url_request_context_getter_); |
53 DCHECK(!url_prefix_.empty()); | |
54 } | 54 } |
55 | 55 |
56 AttachmentDownloaderImpl::~AttachmentDownloaderImpl() { | 56 AttachmentDownloaderImpl::~AttachmentDownloaderImpl() { |
57 } | 57 } |
58 | 58 |
59 void AttachmentDownloaderImpl::DownloadAttachment( | 59 void AttachmentDownloaderImpl::DownloadAttachment( |
60 const AttachmentId& attachment_id, | 60 const AttachmentId& attachment_id, |
61 const DownloadCallback& callback) { | 61 const DownloadCallback& callback) { |
62 DCHECK(CalledOnValidThread()); | 62 DCHECK(CalledOnValidThread()); |
63 | 63 |
64 AttachmentUrl url = GetAttachmentUrl(attachment_id); | 64 AttachmentUrl url = AttachmentUploaderImpl::GetURLForAttachmentId( |
| 65 sync_service_url_, attachment_id).spec(); |
65 | 66 |
66 StateMap::iterator iter = state_map_.find(url); | 67 StateMap::iterator iter = state_map_.find(url); |
67 if (iter == state_map_.end()) { | 68 if (iter == state_map_.end()) { |
68 // There is no request started for this attachment id. Let's create | 69 // There is no request started for this attachment id. Let's create |
69 // DownloadState and request access token for it. | 70 // DownloadState and request access token for it. |
70 scoped_ptr<DownloadState> new_download_state( | 71 scoped_ptr<DownloadState> new_download_state( |
71 new DownloadState(attachment_id, url)); | 72 new DownloadState(attachment_id, url)); |
72 iter = state_map_.add(url, new_download_state.Pass()).first; | 73 iter = state_map_.add(url, new_download_state.Pass()).first; |
73 RequestAccessToken(iter->second); | 74 RequestAccessToken(iter->second); |
74 } | 75 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 oauth2_scopes_, | 145 oauth2_scopes_, |
145 download_state.access_token); | 146 download_state.access_token); |
146 // TODO(pavely): crbug/380437. This is transient error. Request new access | 147 // TODO(pavely): crbug/380437. This is transient error. Request new access |
147 // token for this DownloadState. The only trick is to do it with exponential | 148 // token for this DownloadState. The only trick is to do it with exponential |
148 // backoff. | 149 // backoff. |
149 } | 150 } |
150 ReportResult(download_state, result, attachment_data); | 151 ReportResult(download_state, result, attachment_data); |
151 state_map_.erase(iter); | 152 state_map_.erase(iter); |
152 } | 153 } |
153 | 154 |
154 AttachmentDownloaderImpl::AttachmentUrl | |
155 AttachmentDownloaderImpl::GetAttachmentUrl(const AttachmentId& attachment_id) { | |
156 return url_prefix_ + attachment_id.GetProto().unique_id(); | |
157 } | |
158 | |
159 scoped_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( | 155 scoped_ptr<net::URLFetcher> AttachmentDownloaderImpl::CreateFetcher( |
160 const AttachmentUrl& url, | 156 const AttachmentUrl& url, |
161 const std::string& access_token) { | 157 const std::string& access_token) { |
162 scoped_ptr<net::URLFetcher> url_fetcher( | 158 scoped_ptr<net::URLFetcher> url_fetcher( |
163 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this)); | 159 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this)); |
164 const std::string auth_header("Authorization: Bearer " + access_token); | 160 const std::string auth_header("Authorization: Bearer " + access_token); |
165 url_fetcher->AddExtraRequestHeader(auth_header); | 161 url_fetcher->AddExtraRequestHeader(auth_header); |
166 url_fetcher->SetRequestContext(url_request_context_getter_.get()); | 162 url_fetcher->SetRequestContext(url_request_context_getter_.get()); |
167 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 163 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
168 net::LOAD_DO_NOT_SEND_COOKIES | | 164 net::LOAD_DO_NOT_SEND_COOKIES | |
(...skipping 26 matching lines...) Expand all Loading... |
195 attachment.reset(new Attachment(Attachment::CreateWithId( | 191 attachment.reset(new Attachment(Attachment::CreateWithId( |
196 download_state.attachment_id, attachment_data))); | 192 download_state.attachment_id, attachment_data))); |
197 } | 193 } |
198 | 194 |
199 base::MessageLoop::current()->PostTask( | 195 base::MessageLoop::current()->PostTask( |
200 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); | 196 FROM_HERE, base::Bind(*iter, result, base::Passed(&attachment))); |
201 } | 197 } |
202 } | 198 } |
203 | 199 |
204 } // namespace syncer | 200 } // namespace syncer |
OLD | NEW |