| 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 "components/sync/engine_impl/attachments/attachment_uploader_impl.h" | 5 #include "components/sync/engine_impl/attachments/attachment_uploader_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/sequence_checker.h" |
| 18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
| 22 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
| 23 #include "base/threading/thread.h" | 24 #include "base/threading/thread.h" |
| 24 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
| 25 #include "components/sync/engine/attachments/attachment_util.h" | 26 #include "components/sync/engine/attachments/attachment_util.h" |
| 26 #include "components/sync/model/attachments/attachment.h" | 27 #include "components/sync/model/attachments/attachment.h" |
| 27 #include "components/sync/protocol/sync.pb.h" | 28 #include "components/sync/protocol/sync.pb.h" |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 void MockOAuth2TokenService::InvalidateAccessTokenImpl( | 138 void MockOAuth2TokenService::InvalidateAccessTokenImpl( |
| 138 const std::string& account_id, | 139 const std::string& account_id, |
| 139 const std::string& client_id, | 140 const std::string& client_id, |
| 140 const ScopeSet& scopes, | 141 const ScopeSet& scopes, |
| 141 const std::string& access_token) { | 142 const std::string& access_token) { |
| 142 ++num_invalidate_token_; | 143 ++num_invalidate_token_; |
| 143 last_token_invalidated_ = access_token; | 144 last_token_invalidated_ = access_token; |
| 144 } | 145 } |
| 145 | 146 |
| 146 class TokenServiceProvider | 147 class TokenServiceProvider |
| 147 : public OAuth2TokenServiceRequest::TokenServiceProvider, | 148 : public OAuth2TokenServiceRequest::TokenServiceProvider { |
| 148 base::NonThreadSafe { | |
| 149 public: | 149 public: |
| 150 explicit TokenServiceProvider(OAuth2TokenService* token_service); | 150 explicit TokenServiceProvider(OAuth2TokenService* token_service); |
| 151 | 151 |
| 152 // OAuth2TokenService::TokenServiceProvider implementation. | 152 // OAuth2TokenService::TokenServiceProvider implementation. |
| 153 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner() | 153 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner() |
| 154 override; | 154 override; |
| 155 OAuth2TokenService* GetTokenService() override; | 155 OAuth2TokenService* GetTokenService() override; |
| 156 | 156 |
| 157 private: | 157 private: |
| 158 ~TokenServiceProvider() override; | 158 ~TokenServiceProvider() override; |
| 159 | 159 |
| 160 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 160 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 161 OAuth2TokenService* token_service_; | 161 OAuth2TokenService* token_service_; |
| 162 |
| 163 SEQUENCE_CHECKER(sequence_checker_); |
| 162 }; | 164 }; |
| 163 | 165 |
| 164 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service) | 166 TokenServiceProvider::TokenServiceProvider(OAuth2TokenService* token_service) |
| 165 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | 167 : task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 166 token_service_(token_service) { | 168 token_service_(token_service) { |
| 167 DCHECK(token_service_); | 169 DCHECK(token_service_); |
| 168 } | 170 } |
| 169 | 171 |
| 170 TokenServiceProvider::~TokenServiceProvider() {} | 172 TokenServiceProvider::~TokenServiceProvider() { |
| 173 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 174 } |
| 171 | 175 |
| 172 scoped_refptr<base::SingleThreadTaskRunner> | 176 scoped_refptr<base::SingleThreadTaskRunner> |
| 173 TokenServiceProvider::GetTokenServiceTaskRunner() { | 177 TokenServiceProvider::GetTokenServiceTaskRunner() { |
| 174 return task_runner_; | 178 return task_runner_; |
| 175 } | 179 } |
| 176 | 180 |
| 177 OAuth2TokenService* TokenServiceProvider::GetTokenService() { | 181 OAuth2TokenService* TokenServiceProvider::GetTokenService() { |
| 178 DCHECK(task_runner_->BelongsToCurrentThread()); | 182 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 179 return token_service_; | 183 return token_service_; |
| 180 } | 184 } |
| 181 | 185 |
| 182 // Text fixture for AttachmentUploaderImpl test. | 186 // Text fixture for AttachmentUploaderImpl test. |
| 183 // | 187 // |
| 184 // This fixture provides an embedded HTTP server and a mock OAuth2 token service | 188 // This fixture provides an embedded HTTP server and a mock OAuth2 token service |
| 185 // for interacting with AttachmentUploaderImpl | 189 // for interacting with AttachmentUploaderImpl |
| 186 class AttachmentUploaderImplTest : public testing::Test, | 190 class AttachmentUploaderImplTest : public testing::Test { |
| 187 public base::NonThreadSafe { | |
| 188 public: | 191 public: |
| 189 void OnRequestReceived(const HttpRequest& request); | 192 void OnRequestReceived(const HttpRequest& request); |
| 190 | 193 |
| 191 protected: | 194 protected: |
| 192 AttachmentUploaderImplTest(); | 195 AttachmentUploaderImplTest(); |
| 196 ~AttachmentUploaderImplTest() override; |
| 193 void SetUp() override; | 197 void SetUp() override; |
| 194 void TearDown() override; | 198 void TearDown() override; |
| 195 | 199 |
| 196 // Run the message loop until UploadDone has been invoked |num_uploads| times. | 200 // Run the message loop until UploadDone has been invoked |num_uploads| times. |
| 197 void RunAndWaitFor(int num_uploads); | 201 void RunAndWaitFor(int num_uploads); |
| 198 | 202 |
| 199 // Upload an attachment and have the server respond with |status_code|. | 203 // Upload an attachment and have the server respond with |status_code|. |
| 200 // | 204 // |
| 201 // Returns the attachment that was uploaded. | 205 // Returns the attachment that was uploaded. |
| 202 Attachment UploadAndRespondWith(const net::HttpStatusCode& status_code); | 206 Attachment UploadAndRespondWith(const net::HttpStatusCode& status_code); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 221 std::unique_ptr<AttachmentUploader> uploader_; | 225 std::unique_ptr<AttachmentUploader> uploader_; |
| 222 AttachmentUploader::UploadCallback upload_callback_; | 226 AttachmentUploader::UploadCallback upload_callback_; |
| 223 net::EmbeddedTestServer server_; | 227 net::EmbeddedTestServer server_; |
| 224 // A closure that signals an upload has finished. | 228 // A closure that signals an upload has finished. |
| 225 base::Closure signal_upload_done_; | 229 base::Closure signal_upload_done_; |
| 226 std::vector<HttpRequest> http_requests_received_; | 230 std::vector<HttpRequest> http_requests_received_; |
| 227 std::vector<AttachmentUploader::UploadResult> upload_results_; | 231 std::vector<AttachmentUploader::UploadResult> upload_results_; |
| 228 std::vector<AttachmentId> attachment_ids_; | 232 std::vector<AttachmentId> attachment_ids_; |
| 229 std::unique_ptr<MockOAuth2TokenService> token_service_; | 233 std::unique_ptr<MockOAuth2TokenService> token_service_; |
| 230 | 234 |
| 235 SEQUENCE_CHECKER(sequence_checker_); |
| 236 |
| 231 // Must be last data member. | 237 // Must be last data member. |
| 232 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; | 238 base::WeakPtrFactory<AttachmentUploaderImplTest> weak_ptr_factory_; |
| 233 }; | 239 }; |
| 234 | 240 |
| 235 // Handles HTTP requests received by the EmbeddedTestServer. | 241 // Handles HTTP requests received by the EmbeddedTestServer. |
| 236 // | 242 // |
| 237 // Responds with HTTP_OK by default. See |SetStatusCode|. | 243 // Responds with HTTP_OK by default. See |SetStatusCode|. |
| 238 class RequestHandler : public base::NonThreadSafe { | 244 class RequestHandler { |
| 239 public: | 245 public: |
| 240 // Construct a RequestHandler that will PostTask to |test| using | 246 // Construct a RequestHandler that will PostTask to |test| using |
| 241 // |test_task_runner|. | 247 // |test_task_runner|. |
| 242 RequestHandler( | 248 RequestHandler( |
| 243 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, | 249 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, |
| 244 const base::WeakPtr<AttachmentUploaderImplTest>& test); | 250 const base::WeakPtr<AttachmentUploaderImplTest>& test); |
| 245 | 251 |
| 246 ~RequestHandler(); | 252 ~RequestHandler(); |
| 247 | 253 |
| 248 std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request); | 254 std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request); |
| 249 | 255 |
| 250 // Set the HTTP status code to respond with. | 256 // Set the HTTP status code to respond with. |
| 251 void SetStatusCode(const net::HttpStatusCode& status_code); | 257 void SetStatusCode(const net::HttpStatusCode& status_code); |
| 252 | 258 |
| 253 // Returns the HTTP status code that will be used in responses. | 259 // Returns the HTTP status code that will be used in responses. |
| 254 net::HttpStatusCode GetStatusCode() const; | 260 net::HttpStatusCode GetStatusCode() const; |
| 255 | 261 |
| 256 private: | 262 private: |
| 257 // Protects status_code_. | 263 // Protects status_code_. |
| 258 mutable base::Lock mutex_; | 264 mutable base::Lock mutex_; |
| 259 net::HttpStatusCode status_code_; | 265 net::HttpStatusCode status_code_; |
| 260 | 266 |
| 261 scoped_refptr<base::SingleThreadTaskRunner> test_task_runner_; | 267 scoped_refptr<base::SingleThreadTaskRunner> test_task_runner_; |
| 262 base::WeakPtr<AttachmentUploaderImplTest> test_; | 268 base::WeakPtr<AttachmentUploaderImplTest> test_; |
| 269 |
| 270 SEQUENCE_CHECKER(sequence_checker_); |
| 263 }; | 271 }; |
| 264 | 272 |
| 265 AttachmentUploaderImplTest::AttachmentUploaderImplTest() | 273 AttachmentUploaderImplTest::AttachmentUploaderImplTest() |
| 266 : weak_ptr_factory_(this) {} | 274 : weak_ptr_factory_(this) {} |
| 267 | 275 |
| 276 AttachmentUploaderImplTest::~AttachmentUploaderImplTest() { |
| 277 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 278 } |
| 279 |
| 268 void AttachmentUploaderImplTest::OnRequestReceived(const HttpRequest& request) { | 280 void AttachmentUploaderImplTest::OnRequestReceived(const HttpRequest& request) { |
| 269 DCHECK(CalledOnValidThread()); | 281 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 270 http_requests_received_.push_back(request); | 282 http_requests_received_.push_back(request); |
| 271 } | 283 } |
| 272 | 284 |
| 273 void AttachmentUploaderImplTest::SetUp() { | 285 void AttachmentUploaderImplTest::SetUp() { |
| 274 DCHECK(CalledOnValidThread()); | 286 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 275 request_handler_ = base::MakeUnique<RequestHandler>( | 287 request_handler_ = base::MakeUnique<RequestHandler>( |
| 276 message_loop_.task_runner(), weak_ptr_factory_.GetWeakPtr()); | 288 message_loop_.task_runner(), weak_ptr_factory_.GetWeakPtr()); |
| 277 url_request_context_getter_ = | 289 url_request_context_getter_ = |
| 278 new net::TestURLRequestContextGetter(message_loop_.task_runner()); | 290 new net::TestURLRequestContextGetter(message_loop_.task_runner()); |
| 279 | 291 |
| 280 server_.RegisterRequestHandler( | 292 server_.RegisterRequestHandler( |
| 281 base::Bind(&RequestHandler::HandleRequest, | 293 base::Bind(&RequestHandler::HandleRequest, |
| 282 base::Unretained(request_handler_.get()))); | 294 base::Unretained(request_handler_.get()))); |
| 283 ASSERT_TRUE(server_.Start()); | 295 ASSERT_TRUE(server_.Start()); |
| 284 | 296 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 return message_loop_; | 364 return message_loop_; |
| 353 } | 365 } |
| 354 | 366 |
| 355 RequestHandler& AttachmentUploaderImplTest::request_handler() { | 367 RequestHandler& AttachmentUploaderImplTest::request_handler() { |
| 356 return *request_handler_; | 368 return *request_handler_; |
| 357 } | 369 } |
| 358 | 370 |
| 359 void AttachmentUploaderImplTest::UploadDone( | 371 void AttachmentUploaderImplTest::UploadDone( |
| 360 const AttachmentUploader::UploadResult& result, | 372 const AttachmentUploader::UploadResult& result, |
| 361 const AttachmentId& attachment_id) { | 373 const AttachmentId& attachment_id) { |
| 362 DCHECK(CalledOnValidThread()); | 374 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 363 upload_results_.push_back(result); | 375 upload_results_.push_back(result); |
| 364 attachment_ids_.push_back(attachment_id); | 376 attachment_ids_.push_back(attachment_id); |
| 365 DCHECK(!signal_upload_done_.is_null()); | 377 DCHECK(!signal_upload_done_.is_null()); |
| 366 signal_upload_done_.Run(); | 378 signal_upload_done_.Run(); |
| 367 } | 379 } |
| 368 | 380 |
| 369 RequestHandler::RequestHandler( | 381 RequestHandler::RequestHandler( |
| 370 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, | 382 const scoped_refptr<base::SingleThreadTaskRunner>& test_task_runner, |
| 371 const base::WeakPtr<AttachmentUploaderImplTest>& test) | 383 const base::WeakPtr<AttachmentUploaderImplTest>& test) |
| 372 : status_code_(net::HTTP_OK), | 384 : status_code_(net::HTTP_OK), |
| 373 test_task_runner_(test_task_runner), | 385 test_task_runner_(test_task_runner), |
| 374 test_(test) { | 386 test_(test) { |
| 375 DetachFromThread(); | 387 DETACH_FROM_SEQUENCE(sequence_checker_); |
| 376 } | 388 } |
| 377 | 389 |
| 378 RequestHandler::~RequestHandler() { | 390 RequestHandler::~RequestHandler() = default; |
| 379 DetachFromThread(); | |
| 380 } | |
| 381 | 391 |
| 382 std::unique_ptr<HttpResponse> RequestHandler::HandleRequest( | 392 std::unique_ptr<HttpResponse> RequestHandler::HandleRequest( |
| 383 const HttpRequest& request) { | 393 const HttpRequest& request) { |
| 384 DCHECK(CalledOnValidThread()); | 394 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 385 test_task_runner_->PostTask( | 395 test_task_runner_->PostTask( |
| 386 FROM_HERE, base::Bind(&AttachmentUploaderImplTest::OnRequestReceived, | 396 FROM_HERE, base::Bind(&AttachmentUploaderImplTest::OnRequestReceived, |
| 387 test_, request)); | 397 test_, request)); |
| 388 std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse); | 398 std::unique_ptr<BasicHttpResponse> response(new BasicHttpResponse); |
| 389 response->set_code(GetStatusCode()); | 399 response->set_code(GetStatusCode()); |
| 390 response->set_content_type("text/plain"); | 400 response->set_content_type("text/plain"); |
| 391 return std::move(response); | 401 return std::move(response); |
| 392 } | 402 } |
| 393 | 403 |
| 394 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { | 404 void RequestHandler::SetStatusCode(const net::HttpStatusCode& status_code) { |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); | 674 scoped_refptr<base::RefCountedString> hello_world(new base::RefCountedString); |
| 665 hello_world->data() = "hello world"; | 675 hello_world->data() = "hello world"; |
| 666 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( | 676 EXPECT_EQ("yZRlqg==", AttachmentUploaderImpl::FormatCrc32cHash( |
| 667 ComputeCrc32c(hello_world))); | 677 ComputeCrc32c(hello_world))); |
| 668 } | 678 } |
| 669 | 679 |
| 670 // TODO(maniscalco): Add test case for when we are uploading an attachment that | 680 // TODO(maniscalco): Add test case for when we are uploading an attachment that |
| 671 // already exists. 409 Conflict? (bug 379825) | 681 // already exists. 409 Conflict? (bug 379825) |
| 672 | 682 |
| 673 } // namespace syncer | 683 } // namespace syncer |
| OLD | NEW |