OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/local_discovery/privet_http_impl.h" | 5 #include "chrome/browser/local_discovery/privet_http_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 const char kPrivetTypeFile[] = "file"; | 53 const char kPrivetTypeFile[] = "file"; |
54 | 54 |
55 const char kPrivetKeyJobID[] = "job_id"; | 55 const char kPrivetKeyJobID[] = "job_id"; |
56 | 56 |
57 const int kPrivetCancelationTimeoutSeconds = 3; | 57 const int kPrivetCancelationTimeoutSeconds = 3; |
58 | 58 |
59 const int kPrivetLocalPrintMaxRetries = 2; | 59 const int kPrivetLocalPrintMaxRetries = 2; |
60 | 60 |
61 const int kPrivetLocalPrintDefaultTimeout = 5; | 61 const int kPrivetLocalPrintDefaultTimeout = 5; |
62 | 62 |
| 63 const char kFileTypeOctetStream[] = "application/octet-stream"; |
| 64 |
63 GURL CreatePrivetURL(const std::string& path) { | 65 GURL CreatePrivetURL(const std::string& path) { |
64 GURL url(kUrlPlaceHolder); | 66 GURL url(kUrlPlaceHolder); |
65 GURL::Replacements replacements; | 67 GURL::Replacements replacements; |
66 replacements.SetPathStr(path); | 68 replacements.SetPathStr(path); |
67 return url.ReplaceComponents(replacements); | 69 return url.ReplaceComponents(replacements); |
68 } | 70 } |
69 | 71 |
70 GURL CreatePrivetRegisterURL(const std::string& action, | 72 GURL CreatePrivetRegisterURL(const std::string& action, |
71 const std::string& user) { | 73 const std::string& user) { |
72 GURL url = CreatePrivetURL(kPrivetRegisterPath); | 74 GURL url = CreatePrivetURL(kPrivetRegisterPath); |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 } | 382 } |
381 | 383 |
382 void PrivetJSONOperationImpl::AppendQueryParameter(const std::string& name, | 384 void PrivetJSONOperationImpl::AppendQueryParameter(const std::string& name, |
383 const std::string& value) { | 385 const std::string& value) { |
384 url_ = net::AppendQueryParameter(url_, name, value); | 386 url_ = net::AppendQueryParameter(url_, name, value); |
385 } | 387 } |
386 | 388 |
387 PrivetDataReadOperationImpl::PrivetDataReadOperationImpl( | 389 PrivetDataReadOperationImpl::PrivetDataReadOperationImpl( |
388 PrivetHTTPClientImpl* privet_client, | 390 PrivetHTTPClientImpl* privet_client, |
389 const std::string& path, | 391 const std::string& path, |
| 392 net::URLFetcher::RequestType request_type, |
390 const PrivetDataReadOperation::ResultCallback& callback) | 393 const PrivetDataReadOperation::ResultCallback& callback) |
391 : privet_client_(privet_client), | 394 : privet_client_(privet_client), |
392 url_(CreatePrivetURL(path)), | 395 url_(CreatePrivetURL(path)), |
| 396 request_type_(request_type), |
393 callback_(callback), | 397 callback_(callback), |
394 has_range_(false), | 398 has_range_(false), |
395 save_to_file_(false) {} | 399 save_to_file_(false), |
| 400 has_upload_file_(false) {} |
396 | 401 |
397 PrivetDataReadOperationImpl::~PrivetDataReadOperationImpl() { | 402 PrivetDataReadOperationImpl::~PrivetDataReadOperationImpl() { |
398 } | 403 } |
399 | 404 |
400 void PrivetDataReadOperationImpl::Start() { | 405 void PrivetDataReadOperationImpl::Start() { |
401 url_fetcher_ = | 406 url_fetcher_ = privet_client_->CreateURLFetcher(url_, request_type_, this); |
402 privet_client_->CreateURLFetcher(url_, net::URLFetcher::GET, this); | |
403 url_fetcher_->DoNotRetryOnTransientError(); | 407 url_fetcher_->DoNotRetryOnTransientError(); |
404 | 408 |
405 if (has_range_) { | 409 if (has_range_) { |
406 url_fetcher_->SetByteRange(range_start_, range_end_); | 410 url_fetcher_->SetByteRange(range_start_, range_end_); |
407 } | 411 } |
408 | 412 |
409 if (save_to_file_) { | 413 if (save_to_file_) { |
410 url_fetcher_->SaveResponseToFile(); | 414 url_fetcher_->SaveResponseToFile(); |
411 } | 415 } |
412 | 416 |
| 417 if (has_upload_file_) { |
| 418 DCHECK(request_type_ == net::URLFetcher::PUT); |
| 419 url_fetcher_->SetUploadFilePath(kFileTypeOctetStream, upload_file_); |
| 420 } |
| 421 |
413 url_fetcher_->Start(); | 422 url_fetcher_->Start(); |
414 } | 423 } |
415 | 424 |
416 void PrivetDataReadOperationImpl::SetDataRange(int range_start, int range_end) { | 425 void PrivetDataReadOperationImpl::SetDataRange(int range_start, int range_end) { |
417 has_range_ = true; | 426 has_range_ = true; |
418 range_start_ = range_start; | 427 range_start_ = range_start; |
419 range_end_ = range_end; | 428 range_end_ = range_end; |
420 } | 429 } |
421 | 430 |
422 void PrivetDataReadOperationImpl::SaveDataToFile() { save_to_file_ = true; } | 431 void PrivetDataReadOperationImpl::SaveDataToFile() { save_to_file_ = true; } |
423 | 432 |
| 433 void PrivetDataReadOperationImpl::SetUploadFile( |
| 434 const base::FilePath& upload_file) { |
| 435 has_upload_file_ = true; |
| 436 upload_file_ = upload_file; |
| 437 } |
| 438 |
424 PrivetHTTPClient* PrivetDataReadOperationImpl::GetHTTPClient() { | 439 PrivetHTTPClient* PrivetDataReadOperationImpl::GetHTTPClient() { |
425 return privet_client_; | 440 return privet_client_; |
426 } | 441 } |
427 | 442 |
428 void PrivetDataReadOperationImpl::OnError( | 443 void PrivetDataReadOperationImpl::OnError( |
429 PrivetURLFetcher* fetcher, | 444 PrivetURLFetcher* fetcher, |
430 PrivetURLFetcher::ErrorType error) { | 445 PrivetURLFetcher::ErrorType error) { |
431 callback_.Run(RESPONSE_TYPE_ERROR, std::string(), base::FilePath()); | 446 callback_.Run(RESPONSE_TYPE_ERROR, std::string(), base::FilePath()); |
432 } | 447 } |
433 | 448 |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
840 operation->AppendQueryParameter(kPrivetStorageParamPath, path); | 855 operation->AppendQueryParameter(kPrivetStorageParamPath, path); |
841 return operation.PassAs<PrivetJSONOperation>(); | 856 return operation.PassAs<PrivetJSONOperation>(); |
842 } | 857 } |
843 | 858 |
844 scoped_ptr<PrivetDataReadOperation> | 859 scoped_ptr<PrivetDataReadOperation> |
845 PrivetHTTPClientImpl::CreateStorageReadOperation( | 860 PrivetHTTPClientImpl::CreateStorageReadOperation( |
846 const std::string& path, | 861 const std::string& path, |
847 const PrivetDataReadOperation::ResultCallback& callback) { | 862 const PrivetDataReadOperation::ResultCallback& callback) { |
848 scoped_ptr<PrivetDataReadOperationImpl> operation( | 863 scoped_ptr<PrivetDataReadOperationImpl> operation( |
849 new PrivetDataReadOperationImpl( | 864 new PrivetDataReadOperationImpl( |
850 this, kPrivetStorageContentPath, callback)); | 865 this, kPrivetStorageContentPath, net::URLFetcher::GET, callback)); |
851 | 866 |
852 operation->AppendQueryParameter(kPrivetStorageParamPath, path); | 867 operation->AppendQueryParameter(kPrivetStorageParamPath, path); |
853 return operation.PassAs<PrivetDataReadOperation>(); | 868 return operation.PassAs<PrivetDataReadOperation>(); |
854 } | 869 } |
855 | 870 |
856 scoped_ptr<PrivetJSONOperation> | 871 scoped_ptr<PrivetJSONOperation> |
857 PrivetHTTPClientImpl::CreateStorageDeleteOperation( | 872 PrivetHTTPClientImpl::CreateStorageDeleteOperation( |
858 const std::string& path, | 873 const std::string& path, |
859 const PrivetJSONOperation::ResultCallback& callback) { | 874 const PrivetJSONOperation::ResultCallback& callback) { |
860 scoped_ptr<PrivetJSONOperationImpl> operation(new PrivetJSONOperationImpl( | 875 scoped_ptr<PrivetJSONOperationImpl> operation(new PrivetJSONOperationImpl( |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
900 this, kPrivetStorageCreatePath, net::URLFetcher::POST, callback)); | 915 this, kPrivetStorageCreatePath, net::URLFetcher::POST, callback)); |
901 | 916 |
902 operation->AppendQueryParameter(kPrivetStorageParamPath, path); | 917 operation->AppendQueryParameter(kPrivetStorageParamPath, path); |
903 operation->AppendQueryParameter(kPrivetStorageParamName, name); | 918 operation->AppendQueryParameter(kPrivetStorageParamName, name); |
904 operation->AppendQueryParameter(kPrivetStorageParamType, | 919 operation->AppendQueryParameter(kPrivetStorageParamType, |
905 is_dir ? kPrivetTypeDir : kPrivetTypeFile); | 920 is_dir ? kPrivetTypeDir : kPrivetTypeFile); |
906 | 921 |
907 return operation.PassAs<PrivetJSONOperation>(); | 922 return operation.PassAs<PrivetJSONOperation>(); |
908 } | 923 } |
909 | 924 |
| 925 scoped_ptr<PrivetDataReadOperation> |
| 926 PrivetHTTPClientImpl::CreateStorageOverwriteOperation( |
| 927 const std::string& path, |
| 928 const PrivetDataReadOperation::ResultCallback& callback) { |
| 929 scoped_ptr<PrivetDataReadOperationImpl> operation( |
| 930 new PrivetDataReadOperationImpl( |
| 931 this, kPrivetStorageContentPath, net::URLFetcher::PUT, callback)); |
| 932 |
| 933 operation->AppendQueryParameter(kPrivetStorageParamPath, path); |
| 934 return operation.PassAs<PrivetDataReadOperation>(); |
| 935 } |
| 936 |
910 const std::string& PrivetHTTPClientImpl::GetName() { | 937 const std::string& PrivetHTTPClientImpl::GetName() { |
911 return name_; | 938 return name_; |
912 } | 939 } |
913 | 940 |
914 scoped_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( | 941 scoped_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( |
915 const GURL& url, net::URLFetcher::RequestType request_type, | 942 const GURL& url, net::URLFetcher::RequestType request_type, |
916 PrivetURLFetcher::Delegate* delegate) const { | 943 PrivetURLFetcher::Delegate* delegate) const { |
917 GURL::Replacements replacements; | 944 GURL::Replacements replacements; |
918 replacements.SetHostStr(host_port_.host()); | 945 replacements.SetHostStr(host_port_.host()); |
919 std::string port(base::IntToString(host_port_.port())); // Keep string alive. | 946 std::string port(base::IntToString(host_port_.port())); // Keep string alive. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 TokenCallbackVector token_callbacks; | 978 TokenCallbackVector token_callbacks; |
952 token_callbacks_.swap(token_callbacks); | 979 token_callbacks_.swap(token_callbacks); |
953 | 980 |
954 for (TokenCallbackVector::iterator i = token_callbacks.begin(); | 981 for (TokenCallbackVector::iterator i = token_callbacks.begin(); |
955 i != token_callbacks.end(); i++) { | 982 i != token_callbacks.end(); i++) { |
956 i->Run(token); | 983 i->Run(token); |
957 } | 984 } |
958 } | 985 } |
959 | 986 |
960 } // namespace local_discovery | 987 } // namespace local_discovery |
OLD | NEW |