Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: sync/internal_api/attachments/attachment_uploader_impl.cc

Issue 355093002: Consolidate attachment URL construction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move static method declaration and definition. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 AttachmentUploaderImpl::AttachmentUploaderImpl( 211 AttachmentUploaderImpl::AttachmentUploaderImpl(
212 const std::string& url_prefix, 212 const GURL& sync_service_url,
213 const scoped_refptr<net::URLRequestContextGetter>& 213 const scoped_refptr<net::URLRequestContextGetter>&
214 url_request_context_getter, 214 url_request_context_getter,
215 const std::string& account_id, 215 const std::string& account_id,
216 const OAuth2TokenService::ScopeSet& scopes, 216 const OAuth2TokenService::ScopeSet& scopes,
217 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider> 217 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider>
218 token_service_provider) 218 token_service_provider)
219 : url_prefix_(url_prefix), 219 : sync_service_url_(sync_service_url),
220 url_request_context_getter_(url_request_context_getter), 220 url_request_context_getter_(url_request_context_getter),
221 account_id_(account_id), 221 account_id_(account_id),
222 scopes_(scopes), 222 scopes_(scopes),
223 token_service_provider_(token_service_provider.Pass()) { 223 token_service_provider_(token_service_provider.Pass()) {
224 DCHECK(CalledOnValidThread()); 224 DCHECK(CalledOnValidThread());
225 DCHECK(token_service_provider_); 225 DCHECK(token_service_provider_);
226 } 226 }
227 227
228 AttachmentUploaderImpl::~AttachmentUploaderImpl() { 228 AttachmentUploaderImpl::~AttachmentUploaderImpl() {
229 DCHECK(CalledOnValidThread()); 229 DCHECK(CalledOnValidThread());
230 } 230 }
231 231
232 void AttachmentUploaderImpl::UploadAttachment(const Attachment& attachment, 232 void AttachmentUploaderImpl::UploadAttachment(const Attachment& attachment,
233 const UploadCallback& callback) { 233 const UploadCallback& callback) {
234 DCHECK(CalledOnValidThread()); 234 DCHECK(CalledOnValidThread());
235 const AttachmentId attachment_id = attachment.GetId(); 235 const AttachmentId attachment_id = attachment.GetId();
236 const std::string unique_id = attachment_id.GetProto().unique_id(); 236 const std::string unique_id = attachment_id.GetProto().unique_id();
237 DCHECK(!unique_id.empty()); 237 DCHECK(!unique_id.empty());
238 StateMap::iterator iter = state_map_.find(unique_id); 238 StateMap::iterator iter = state_map_.find(unique_id);
239 if (iter == state_map_.end()) { 239 if (iter == state_map_.end()) {
240 const GURL url = GetUploadURLForAttachmentId(attachment_id); 240 const GURL url = GetURLForAttachmentId(sync_service_url_, attachment_id);
241 scoped_ptr<UploadState> upload_state( 241 scoped_ptr<UploadState> upload_state(
242 new UploadState(url, 242 new UploadState(url,
243 url_request_context_getter_, 243 url_request_context_getter_,
244 attachment, 244 attachment,
245 callback, 245 callback,
246 account_id_, 246 account_id_,
247 scopes_, 247 scopes_,
248 token_service_provider_.get(), 248 token_service_provider_.get(),
249 this)); 249 this));
250 state_map_.add(unique_id, upload_state.Pass()); 250 state_map_.add(unique_id, upload_state.Pass());
251 } else { 251 } else {
252 DCHECK( 252 DCHECK(
253 attachment.GetData()->Equals(iter->second->GetAttachment().GetData())); 253 attachment.GetData()->Equals(iter->second->GetAttachment().GetData()));
254 // We already have an upload for this attachment. "Join" it. 254 // We already have an upload for this attachment. "Join" it.
255 iter->second->AddUserCallback(callback); 255 iter->second->AddUserCallback(callback);
256 } 256 }
257 } 257 }
258 258
259 GURL AttachmentUploaderImpl::GetUploadURLForAttachmentId( 259 // Static.
260 const AttachmentId& attachment_id) const { 260 GURL AttachmentUploaderImpl::GetURLForAttachmentId(
261 return GURL(url_prefix_ + attachment_id.GetProto().unique_id()); 261 const GURL& sync_service_url,
262 const AttachmentId& attachment_id) {
263 std::string path = sync_service_url.path();
264 if (path.empty() || *path.rbegin() != '/') {
265 path += '/';
266 }
267 path += kAttachments;
268 path += attachment_id.GetProto().unique_id();
269 GURL::Replacements replacements;
270 replacements.SetPathStr(path);
271 return sync_service_url.ReplaceComponents(replacements);
262 } 272 }
263 273
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698