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

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

Issue 304253010: Add authentication support to AttachmentUploaderImpl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Apply more CR feedback from pavely 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
« no previous file with comments | « no previous file | sync/internal_api/attachments/attachment_uploader_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "net/base/load_flags.h" 11 #include "net/base/load_flags.h"
11 #include "net/http/http_status_code.h" 12 #include "net/http/http_status_code.h"
12 #include "net/url_request/url_fetcher.h" 13 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_fetcher_delegate.h" 14 #include "net/url_request/url_fetcher_delegate.h"
14 #include "sync/api/attachments/attachment.h" 15 #include "sync/api/attachments/attachment.h"
15 #include "sync/protocol/sync.pb.h" 16 #include "sync/protocol/sync.pb.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
17 18
18 namespace { 19 namespace {
19 20
20 const char kContentType[] = "application/octet-stream"; 21 const char kContentType[] = "application/octet-stream";
21 22
22 } // namespace 23 } // namespace
23 24
24 namespace syncer { 25 namespace syncer {
25 26
26 // Encapsulates all the state associated with a single upload. 27 // Encapsulates all the state associated with a single upload.
27 class AttachmentUploaderImpl::UploadState : public net::URLFetcherDelegate, 28 class AttachmentUploaderImpl::UploadState : public net::URLFetcherDelegate,
29 public OAuth2TokenService::Consumer,
28 public base::NonThreadSafe { 30 public base::NonThreadSafe {
29 public: 31 public:
30 // Construct an UploadState. 32 // Construct an UploadState.
31 // 33 //
32 // |owner| is a pointer to the object that will own (and must outlive!) this 34 // |owner| is a pointer to the object that will own (and must outlive!) this
33 // |UploadState. 35 // |UploadState.
34 UploadState(const GURL& upload_url, 36 UploadState(
35 const scoped_refptr<net::URLRequestContextGetter>& 37 const GURL& upload_url,
36 url_request_context_getter, 38 const scoped_refptr<net::URLRequestContextGetter>&
37 const Attachment& attachment, 39 url_request_context_getter,
38 const UploadCallback& user_callback, 40 const Attachment& attachment,
39 AttachmentUploaderImpl* owner); 41 const UploadCallback& user_callback,
42 const std::string& account_id,
43 const OAuth2TokenService::ScopeSet& scopes,
44 OAuth2TokenServiceRequest::TokenServiceProvider* token_service_provider,
45 AttachmentUploaderImpl* owner);
40 46
41 virtual ~UploadState(); 47 virtual ~UploadState();
42 48
43 // Add |user_callback| to the list of callbacks to be invoked when this upload 49 // Add |user_callback| to the list of callbacks to be invoked when this upload
44 // completed. 50 // completed.
45 void AddUserCallback(const UploadCallback& user_callback); 51 void AddUserCallback(const UploadCallback& user_callback);
46 52
47 // Return the Attachment this object is uploading. 53 // Return the Attachment this object is uploading.
48 const Attachment& GetAttachment(); 54 const Attachment& GetAttachment();
49 55
50 // URLFetcher implementation. 56 // URLFetcher implementation.
51 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 57 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
52 58
59 // OAuth2TokenService::Consumer.
60 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
61 const std::string& access_token,
62 const base::Time& expiration_time) OVERRIDE;
63 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
64 const GoogleServiceAuthError& error) OVERRIDE;
65
53 private: 66 private:
54 typedef std::vector<UploadCallback> UploadCallbackList; 67 typedef std::vector<UploadCallback> UploadCallbackList;
55 68
69 void GetToken();
70
71 void ReportResult(const UploadResult& result,
72 const AttachmentId& attachment_id);
73
56 GURL upload_url_; 74 GURL upload_url_;
57 const scoped_refptr<net::URLRequestContextGetter>& 75 const scoped_refptr<net::URLRequestContextGetter>&
58 url_request_context_getter_; 76 url_request_context_getter_;
59 Attachment attachment_; 77 Attachment attachment_;
60 UploadCallbackList user_callbacks_; 78 UploadCallbackList user_callbacks_;
61 scoped_ptr<net::URLFetcher> fetcher_; 79 scoped_ptr<net::URLFetcher> fetcher_;
80 std::string account_id_;
81 OAuth2TokenService::ScopeSet scopes_;
82 std::string access_token_;
83 OAuth2TokenServiceRequest::TokenServiceProvider* token_service_provider_;
62 // Pointer to the AttachmentUploaderImpl that owns this object. 84 // Pointer to the AttachmentUploaderImpl that owns this object.
63 AttachmentUploaderImpl* owner_; 85 AttachmentUploaderImpl* owner_;
86 scoped_ptr<OAuth2TokenServiceRequest> access_token_request_;
64 87
65 DISALLOW_COPY_AND_ASSIGN(UploadState); 88 DISALLOW_COPY_AND_ASSIGN(UploadState);
66 }; 89 };
67 90
68 AttachmentUploaderImpl::UploadState::UploadState( 91 AttachmentUploaderImpl::UploadState::UploadState(
69 const GURL& upload_url, 92 const GURL& upload_url,
70 const scoped_refptr<net::URLRequestContextGetter>& 93 const scoped_refptr<net::URLRequestContextGetter>&
71 url_request_context_getter, 94 url_request_context_getter,
72 const Attachment& attachment, 95 const Attachment& attachment,
73 const UploadCallback& user_callback, 96 const UploadCallback& user_callback,
97 const std::string& account_id,
98 const OAuth2TokenService::ScopeSet& scopes,
99 OAuth2TokenServiceRequest::TokenServiceProvider* token_service_provider,
74 AttachmentUploaderImpl* owner) 100 AttachmentUploaderImpl* owner)
75 : upload_url_(upload_url), 101 : OAuth2TokenService::Consumer("attachment-uploader-impl"),
102 upload_url_(upload_url),
76 url_request_context_getter_(url_request_context_getter), 103 url_request_context_getter_(url_request_context_getter),
77 attachment_(attachment), 104 attachment_(attachment),
78 user_callbacks_(1, user_callback), 105 user_callbacks_(1, user_callback),
106 account_id_(account_id),
107 scopes_(scopes),
108 token_service_provider_(token_service_provider),
79 owner_(owner) { 109 owner_(owner) {
110 DCHECK(upload_url_.is_valid());
80 DCHECK(url_request_context_getter_); 111 DCHECK(url_request_context_getter_);
81 DCHECK(upload_url_.is_valid()); 112 DCHECK(!account_id_.empty());
113 DCHECK(!scopes_.empty());
114 DCHECK(token_service_provider_);
82 DCHECK(owner_); 115 DCHECK(owner_);
83 fetcher_.reset( 116 GetToken();
84 net::URLFetcher::Create(upload_url_, net::URLFetcher::POST, this));
85 fetcher_->SetRequestContext(url_request_context_getter_.get());
86 // TODO(maniscalco): Is there a better way? Copying the attachment data into
87 // a string feels wrong given how large attachments may be (several MBs). If
88 // we may end up switching from URLFetcher to URLRequest, this copy won't be
89 // necessary.
90 scoped_refptr<base::RefCountedMemory> memory = attachment.GetData();
91 const std::string upload_content(memory->front_as<char>(), memory->size());
92 fetcher_->SetUploadData(kContentType, upload_content);
93 // TODO(maniscalco): Add authentication support (bug 371516).
94 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
95 net::LOAD_DO_NOT_SEND_COOKIES |
96 net::LOAD_DISABLE_CACHE);
97 // TODO(maniscalco): Set an appropriate headers (User-Agent, Content-type, and
98 // Content-length) on the request and include the content's MD5,
99 // AttachmentId's unique_id and the "sync birthday" (bug 371521).
100 fetcher_->Start();
101 } 117 }
102 118
103 AttachmentUploaderImpl::UploadState::~UploadState() { 119 AttachmentUploaderImpl::UploadState::~UploadState() {
104 } 120 }
105 121
106 void AttachmentUploaderImpl::UploadState::AddUserCallback( 122 void AttachmentUploaderImpl::UploadState::AddUserCallback(
107 const UploadCallback& user_callback) { 123 const UploadCallback& user_callback) {
108 DCHECK(CalledOnValidThread()); 124 DCHECK(CalledOnValidThread());
109 user_callbacks_.push_back(user_callback); 125 user_callbacks_.push_back(user_callback);
110 } 126 }
111 127
112 const Attachment& AttachmentUploaderImpl::UploadState::GetAttachment() { 128 const Attachment& AttachmentUploaderImpl::UploadState::GetAttachment() {
113 DCHECK(CalledOnValidThread()); 129 DCHECK(CalledOnValidThread());
114 return attachment_; 130 return attachment_;
115 } 131 }
116 132
117 void AttachmentUploaderImpl::UploadState::OnURLFetchComplete( 133 void AttachmentUploaderImpl::UploadState::OnURLFetchComplete(
118 const net::URLFetcher* source) { 134 const net::URLFetcher* source) {
119 DCHECK(CalledOnValidThread()); 135 DCHECK(CalledOnValidThread());
120 // TODO(maniscalco): Once the protocol is better defined, deal with the
121 // various HTTP response code we may encounter.
122 UploadResult result = UPLOAD_UNSPECIFIED_ERROR; 136 UploadResult result = UPLOAD_UNSPECIFIED_ERROR;
137 AttachmentId attachment_id = attachment_.GetId();
123 if (source->GetResponseCode() == net::HTTP_OK) { 138 if (source->GetResponseCode() == net::HTTP_OK) {
124 result = UPLOAD_SUCCESS; 139 result = UPLOAD_SUCCESS;
140 // TODO(maniscalco): Update the attachment id with server address
141 // information before passing it to the callback (bug 371522).
142 } else if (source->GetResponseCode() == net::HTTP_UNAUTHORIZED) {
143 // TODO(maniscalco): One possibility is that we received a 401 because our
144 // access token has expired. We should probably fetch a new access token
145 // and retry this upload before giving up and reporting failure to our
146 // caller (bug 380437).
147 OAuth2TokenServiceRequest::InvalidateToken(
148 token_service_provider_, account_id_, scopes_, access_token_);
149 } else {
150 // TODO(maniscalco): Once the protocol is better defined, deal with the
151 // various HTTP response codes we may encounter.
125 } 152 }
126 // TODO(maniscalco): Update the attachment id with server address information 153 ReportResult(result, attachment_id);
127 // before passing it to the callback (bug 371522). 154 }
128 AttachmentId updated_id = attachment_.GetId(); 155
156 void AttachmentUploaderImpl::UploadState::OnGetTokenSuccess(
157 const OAuth2TokenService::Request* request,
158 const std::string& access_token,
159 const base::Time& expiration_time) {
160 DCHECK_EQ(access_token_request_.get(), request);
161 access_token_request_.reset();
162 access_token_ = access_token;
163 fetcher_.reset(
164 net::URLFetcher::Create(upload_url_, net::URLFetcher::POST, this));
165 fetcher_->SetRequestContext(url_request_context_getter_.get());
166 // TODO(maniscalco): Is there a better way? Copying the attachment data into
167 // a string feels wrong given how large attachments may be (several MBs). If
168 // we may end up switching from URLFetcher to URLRequest, this copy won't be
169 // necessary.
170 scoped_refptr<base::RefCountedMemory> memory = attachment_.GetData();
171 const std::string upload_content(memory->front_as<char>(), memory->size());
172 fetcher_->SetUploadData(kContentType, upload_content);
173 const std::string auth_header("Authorization: Bearer " + access_token_);
174 fetcher_->AddExtraRequestHeader(auth_header);
175 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
176 net::LOAD_DO_NOT_SEND_COOKIES |
177 net::LOAD_DISABLE_CACHE);
178 // TODO(maniscalco): Set an appropriate headers (User-Agent, Content-type, and
179 // Content-length) on the request and include the content's MD5,
180 // AttachmentId's unique_id and the "sync birthday" (bug 371521).
181 fetcher_->Start();
182 }
183
184 void AttachmentUploaderImpl::UploadState::OnGetTokenFailure(
185 const OAuth2TokenService::Request* request,
186 const GoogleServiceAuthError& error) {
187 DCHECK_EQ(access_token_request_.get(), request);
188 access_token_request_.reset();
189 ReportResult(UPLOAD_UNSPECIFIED_ERROR, attachment_.GetId());
190 }
191
192 void AttachmentUploaderImpl::UploadState::GetToken() {
193 access_token_request_ = OAuth2TokenServiceRequest::CreateAndStart(
194 token_service_provider_, account_id_, scopes_, this);
195 }
196
197 void AttachmentUploaderImpl::UploadState::ReportResult(
198 const UploadResult& result,
199 const AttachmentId& attachment_id) {
129 UploadCallbackList::const_iterator iter = user_callbacks_.begin(); 200 UploadCallbackList::const_iterator iter = user_callbacks_.begin();
130 UploadCallbackList::const_iterator end = user_callbacks_.end(); 201 UploadCallbackList::const_iterator end = user_callbacks_.end();
131 for (; iter != end; ++iter) { 202 for (; iter != end; ++iter) {
132 base::MessageLoop::current()->PostTask( 203 base::MessageLoop::current()->PostTask(
133 FROM_HERE, base::Bind(*iter, result, updated_id)); 204 FROM_HERE, base::Bind(*iter, result, attachment_id));
134 } 205 }
135 // Destroy this object and return immediately. 206 // Destroy this object and return immediately.
136 owner_->DeleteUploadStateFor(attachment_.GetId().GetProto().unique_id()); 207 owner_->DeleteUploadStateFor(attachment_.GetId().GetProto().unique_id());
137 return; 208 return;
138 } 209 }
139 210
140 AttachmentUploaderImpl::AttachmentUploaderImpl( 211 AttachmentUploaderImpl::AttachmentUploaderImpl(
141 const std::string& url_prefix, 212 const std::string& url_prefix,
142 const scoped_refptr<net::URLRequestContextGetter>& 213 const scoped_refptr<net::URLRequestContextGetter>&
143 url_request_context_getter) 214 url_request_context_getter,
215 const std::string& account_id,
216 const OAuth2TokenService::ScopeSet& scopes,
217 scoped_ptr<OAuth2TokenServiceRequest::TokenServiceProvider>
218 token_service_provider)
144 : url_prefix_(url_prefix), 219 : url_prefix_(url_prefix),
145 url_request_context_getter_(url_request_context_getter) { 220 url_request_context_getter_(url_request_context_getter),
221 account_id_(account_id),
222 scopes_(scopes),
223 token_service_provider_(token_service_provider.Pass()) {
146 DCHECK(CalledOnValidThread()); 224 DCHECK(CalledOnValidThread());
225 DCHECK(token_service_provider_);
147 } 226 }
148 227
149 AttachmentUploaderImpl::~AttachmentUploaderImpl() { 228 AttachmentUploaderImpl::~AttachmentUploaderImpl() {
150 DCHECK(CalledOnValidThread()); 229 DCHECK(CalledOnValidThread());
151 } 230 }
152 231
153 void AttachmentUploaderImpl::UploadAttachment(const Attachment& attachment, 232 void AttachmentUploaderImpl::UploadAttachment(const Attachment& attachment,
154 const UploadCallback& callback) { 233 const UploadCallback& callback) {
155 DCHECK(CalledOnValidThread()); 234 DCHECK(CalledOnValidThread());
156 const AttachmentId attachment_id = attachment.GetId(); 235 const AttachmentId attachment_id = attachment.GetId();
157 const std::string unique_id = attachment_id.GetProto().unique_id(); 236 const std::string unique_id = attachment_id.GetProto().unique_id();
158 DCHECK(!unique_id.empty()); 237 DCHECK(!unique_id.empty());
159 StateMap::iterator iter = state_map_.find(unique_id); 238 StateMap::iterator iter = state_map_.find(unique_id);
160 if (iter == state_map_.end()) { 239 if (iter == state_map_.end()) {
161 const GURL url = GetUploadURLForAttachmentId(attachment_id); 240 const GURL url = GetUploadURLForAttachmentId(attachment_id);
162 scoped_ptr<UploadState> upload_state(new UploadState( 241 scoped_ptr<UploadState> upload_state(
163 url, url_request_context_getter_, attachment, callback, this)); 242 new UploadState(url,
243 url_request_context_getter_,
244 attachment,
245 callback,
246 account_id_,
247 scopes_,
248 token_service_provider_.get(),
249 this));
164 state_map_.add(unique_id, upload_state.Pass()); 250 state_map_.add(unique_id, upload_state.Pass());
165 } else { 251 } else {
166 DCHECK( 252 DCHECK(
167 attachment.GetData()->Equals(iter->second->GetAttachment().GetData())); 253 attachment.GetData()->Equals(iter->second->GetAttachment().GetData()));
168 // We already have an upload for this attachment. "Join" it. 254 // We already have an upload for this attachment. "Join" it.
169 iter->second->AddUserCallback(callback); 255 iter->second->AddUserCallback(callback);
170 } 256 }
171 } 257 }
172 258
173 GURL AttachmentUploaderImpl::GetUploadURLForAttachmentId( 259 GURL AttachmentUploaderImpl::GetUploadURLForAttachmentId(
174 const AttachmentId& attachment_id) const { 260 const AttachmentId& attachment_id) const {
175 return GURL(url_prefix_ + attachment_id.GetProto().unique_id()); 261 return GURL(url_prefix_ + attachment_id.GetProto().unique_id());
176 } 262 }
177 263
178 void AttachmentUploaderImpl::DeleteUploadStateFor(const UniqueId& unique_id) { 264 void AttachmentUploaderImpl::DeleteUploadStateFor(const UniqueId& unique_id) {
179 state_map_.erase(unique_id); 265 state_map_.erase(unique_id);
180 } 266 }
181 267
182 } // namespace syncer 268 } // namespace syncer
OLDNEW
« no previous file with comments | « no previous file | sync/internal_api/attachments/attachment_uploader_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698