| 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_uploader_impl.h" | 5 #include "sync/internal_api/public/attachments/attachment_uploader_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 "base/threading/non_thread_safe.h" | 9 #include "base/threading/non_thread_safe.h" |
| 10 #include "google_apis/gaia/gaia_constants.h" | 10 #include "google_apis/gaia/gaia_constants.h" |
| 11 #include "net/base/load_flags.h" | 11 #include "net/base/load_flags.h" |
| 12 #include "net/http/http_status_code.h" | 12 #include "net/http/http_status_code.h" |
| 13 #include "net/url_request/url_fetcher.h" | 13 #include "net/url_request/url_fetcher.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" | 14 #include "net/url_request/url_fetcher_delegate.h" |
| 15 #include "sync/api/attachments/attachment.h" | 15 #include "sync/api/attachments/attachment.h" |
| 16 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
| 17 #include "url/gurl.h" | |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 const char kContentType[] = "application/octet-stream"; | 20 const char kContentType[] = "application/octet-stream"; |
| 21 const char kAttachments[] = "attachments/"; |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 namespace syncer { | 25 namespace syncer { |
| 26 | 26 |
| 27 // Encapsulates all the state associated with a single upload. | 27 // Encapsulates all the state associated with a single upload. |
| 28 class AttachmentUploaderImpl::UploadState : public net::URLFetcherDelegate, | 28 class AttachmentUploaderImpl::UploadState : public net::URLFetcherDelegate, |
| 29 public OAuth2TokenService::Consumer, | 29 public OAuth2TokenService::Consumer, |
| 30 public base::NonThreadSafe { | 30 public base::NonThreadSafe { |
| 31 public: | 31 public: |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 UploadCallbackList::const_iterator end = user_callbacks_.end(); | 201 UploadCallbackList::const_iterator end = user_callbacks_.end(); |
| 202 for (; iter != end; ++iter) { | 202 for (; iter != end; ++iter) { |
| 203 base::MessageLoop::current()->PostTask( | 203 base::MessageLoop::current()->PostTask( |
| 204 FROM_HERE, base::Bind(*iter, result, attachment_id)); | 204 FROM_HERE, base::Bind(*iter, result, attachment_id)); |
| 205 } | 205 } |
| 206 // Destroy this object and return immediately. | 206 // Destroy this object and return immediately. |
| 207 owner_->DeleteUploadStateFor(attachment_.GetId().GetProto().unique_id()); | 207 owner_->DeleteUploadStateFor(attachment_.GetId().GetProto().unique_id()); |
| 208 return; | 208 return; |
| 209 } | 209 } |
| 210 | 210 |
| 211 // Static. |
| 212 GURL AttachmentUploaderImpl::GetURLForAttachmentId( |
| 213 const GURL& sync_service_url, |
| 214 const AttachmentId& attachment_id) { |
| 215 std::string path = sync_service_url.path(); |
| 216 if (path.empty() || *path.rbegin() != '/') { |
| 217 path += '/'; |
| 218 } |
| 219 path += kAttachments; |
| 220 path += attachment_id.GetProto().unique_id(); |
| 221 GURL::Replacements replacements; |
| 222 replacements.SetPathStr(path); |
| 223 return sync_service_url.ReplaceComponents(replacements); |
| 224 } |
| 225 |
| 211 AttachmentUploaderImpl::AttachmentUploaderImpl( | 226 AttachmentUploaderImpl::AttachmentUploaderImpl( |
| 212 const std::string& url_prefix, | 227 const GURL& sync_service_url, |
| 213 const scoped_refptr<net::URLRequestContextGetter>& | 228 const scoped_refptr<net::URLRequestContextGetter>& |
| 214 url_request_context_getter, | 229 url_request_context_getter, |
| 215 const std::string& account_id, | 230 const std::string& account_id, |
| 216 const OAuth2TokenService::ScopeSet& scopes, | 231 const OAuth2TokenService::ScopeSet& scopes, |
| 217 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> | 232 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> |
| 218 token_service_provider) | 233 token_service_provider) |
| 219 : url_prefix_(url_prefix), | 234 : sync_service_url_(sync_service_url), |
| 220 url_request_context_getter_(url_request_context_getter), | 235 url_request_context_getter_(url_request_context_getter), |
| 221 account_id_(account_id), | 236 account_id_(account_id), |
| 222 scopes_(scopes), | 237 scopes_(scopes), |
| 223 token_service_provider_(token_service_provider.Pass()) { | 238 token_service_provider_(token_service_provider.Pass()) { |
| 224 DCHECK(CalledOnValidThread()); | 239 DCHECK(CalledOnValidThread()); |
| 225 DCHECK(token_service_provider_); | 240 DCHECK(token_service_provider_); |
| 226 } | 241 } |
| 227 | 242 |
| 228 AttachmentUploaderImpl::~AttachmentUploaderImpl() { | 243 AttachmentUploaderImpl::~AttachmentUploaderImpl() { |
| 229 DCHECK(CalledOnValidThread()); | 244 DCHECK(CalledOnValidThread()); |
| 230 } | 245 } |
| 231 | 246 |
| 232 void AttachmentUploaderImpl::UploadAttachment(const Attachment& attachment, | 247 void AttachmentUploaderImpl::UploadAttachment(const Attachment& attachment, |
| 233 const UploadCallback& callback) { | 248 const UploadCallback& callback) { |
| 234 DCHECK(CalledOnValidThread()); | 249 DCHECK(CalledOnValidThread()); |
| 235 const AttachmentId attachment_id = attachment.GetId(); | 250 const AttachmentId attachment_id = attachment.GetId(); |
| 236 const std::string unique_id = attachment_id.GetProto().unique_id(); | 251 const std::string unique_id = attachment_id.GetProto().unique_id(); |
| 237 DCHECK(!unique_id.empty()); | 252 DCHECK(!unique_id.empty()); |
| 238 StateMap::iterator iter = state_map_.find(unique_id); | 253 StateMap::iterator iter = state_map_.find(unique_id); |
| 239 if (iter == state_map_.end()) { | 254 if (iter == state_map_.end()) { |
| 240 const GURL url = GetUploadURLForAttachmentId(attachment_id); | 255 const GURL url = GetURLForAttachmentId(sync_service_url_, attachment_id); |
| 241 scoped_ptr<UploadState> upload_state( | 256 scoped_ptr<UploadState> upload_state( |
| 242 new UploadState(url, | 257 new UploadState(url, |
| 243 url_request_context_getter_, | 258 url_request_context_getter_, |
| 244 attachment, | 259 attachment, |
| 245 callback, | 260 callback, |
| 246 account_id_, | 261 account_id_, |
| 247 scopes_, | 262 scopes_, |
| 248 token_service_provider_.get(), | 263 token_service_provider_.get(), |
| 249 this)); | 264 this)); |
| 250 state_map_.add(unique_id, upload_state.Pass()); | 265 state_map_.add(unique_id, upload_state.Pass()); |
| 251 } else { | 266 } else { |
| 252 DCHECK( | 267 DCHECK( |
| 253 attachment.GetData()->Equals(iter->second->GetAttachment().GetData())); | 268 attachment.GetData()->Equals(iter->second->GetAttachment().GetData())); |
| 254 // We already have an upload for this attachment. "Join" it. | 269 // We already have an upload for this attachment. "Join" it. |
| 255 iter->second->AddUserCallback(callback); | 270 iter->second->AddUserCallback(callback); |
| 256 } | 271 } |
| 257 } | 272 } |
| 258 | 273 |
| 259 GURL AttachmentUploaderImpl::GetUploadURLForAttachmentId( | |
| 260 const AttachmentId& attachment_id) const { | |
| 261 return GURL(url_prefix_ + attachment_id.GetProto().unique_id()); | |
| 262 } | |
| 263 | |
| 264 void AttachmentUploaderImpl::DeleteUploadStateFor(const UniqueId& unique_id) { | 274 void AttachmentUploaderImpl::DeleteUploadStateFor(const UniqueId& unique_id) { |
| 265 state_map_.erase(unique_id); | 275 state_map_.erase(unique_id); |
| 266 } | 276 } |
| 267 | 277 |
| 268 } // namespace syncer | 278 } // namespace syncer |
| OLD | NEW |