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