| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" |
| 14 #include "chrome/browser/local_discovery/privet_constants.h" | 15 #include "chrome/browser/local_discovery/privet_constants.h" |
| 15 #include "net/base/url_util.h" | 16 #include "net/base/url_util.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace local_discovery { | 19 namespace local_discovery { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 const char kUrlPlaceHolder[] = "http://host/"; | 22 const char kUrlPlaceHolder[] = "http://host/"; |
| 22 const char kPrivetRegisterActionArgName[] = "action"; | 23 const char kPrivetRegisterActionArgName[] = "action"; |
| 23 const char kPrivetRegisterUserArgName[] = "user"; | 24 const char kPrivetRegisterUserArgName[] = "user"; |
| 24 | 25 |
| 25 const char kPrivetURLKeyUser[] = "user"; | 26 const char kPrivetURLKeyUser[] = "user"; |
| 26 const char kPrivetURLKeyJobname[] = "jobname"; | 27 const char kPrivetURLKeyJobname[] = "jobname"; |
| 27 const char kPrivetURLKeyOffline[] = "offline"; | 28 const char kPrivetURLKeyOffline[] = "offline"; |
| 28 const char kPrivetURLValueOffline[] = "1"; | 29 const char kPrivetURLValueOffline[] = "1"; |
| 29 | 30 |
| 30 const char kPrivetContentTypePDF[] = "application/pdf"; | 31 const char kPrivetContentTypePDF[] = "application/pdf"; |
| 31 const char kPrivetContentTypePWGRaster[] = "image/pwg-raster"; | 32 const char kPrivetContentTypePWGRaster[] = "image/pwg-raster"; |
| 32 const char kPrivetContentTypeAny[] = "*/*"; | 33 const char kPrivetContentTypeAny[] = "*/*"; |
| 33 const char kPrivetContentTypeCJT[] = "application/json"; | 34 const char kPrivetContentTypeCJT[] = "application/json"; |
| 34 | 35 |
| 36 const char kPrivetStorageListPath[] = "/privet/storage/list"; |
| 37 const char kPrivetStorageParamPathFormat[] = "path=%s"; |
| 38 |
| 35 const char kPrivetCDDKeySupportedContentTypes[] = | 39 const char kPrivetCDDKeySupportedContentTypes[] = |
| 36 "printer.supported_content_type"; | 40 "printer.supported_content_type"; |
| 37 | 41 |
| 38 const char kPrivetCDDKeyContentType[] = "content_type"; | 42 const char kPrivetCDDKeyContentType[] = "content_type"; |
| 39 | 43 |
| 40 const char kPrivetKeyJobID[] = "job_id"; | 44 const char kPrivetKeyJobID[] = "job_id"; |
| 41 | 45 |
| 42 const int kPrivetCancelationTimeoutSeconds = 3; | 46 const int kPrivetCancelationTimeoutSeconds = 3; |
| 43 | 47 |
| 44 const int kPrivetLocalPrintMaxRetries = 2; | 48 const int kPrivetLocalPrintMaxRetries = 2; |
| 45 | 49 |
| 46 const int kPrivetLocalPrintDefaultTimeout = 5; | 50 const int kPrivetLocalPrintDefaultTimeout = 5; |
| 47 | 51 |
| 48 GURL CreatePrivetURL(const std::string& path) { | 52 GURL CreatePrivetURL(const std::string& path) { |
| 49 GURL url(kUrlPlaceHolder); | 53 GURL url(kUrlPlaceHolder); |
| 50 GURL::Replacements replacements; | 54 GURL::Replacements replacements; |
| 51 replacements.SetPathStr(path); | 55 replacements.SetPathStr(path); |
| 52 return url.ReplaceComponents(replacements); | 56 return url.ReplaceComponents(replacements); |
| 53 } | 57 } |
| 54 | 58 |
| 55 GURL CreatePrivetRegisterURL(const std::string& action, | 59 GURL CreatePrivetRegisterURL(const std::string& action, |
| 56 const std::string& user) { | 60 const std::string& user) { |
| 57 GURL url = CreatePrivetURL(kPrivetRegisterPath); | 61 GURL url = CreatePrivetURL(kPrivetRegisterPath); |
| 58 url = net::AppendQueryParameter(url, kPrivetRegisterActionArgName, action); | 62 url = net::AppendQueryParameter(url, kPrivetRegisterActionArgName, action); |
| 59 return net::AppendQueryParameter(url, kPrivetRegisterUserArgName, user); | 63 return net::AppendQueryParameter(url, kPrivetRegisterUserArgName, user); |
| 60 } | 64 } |
| 61 | 65 |
| 66 GURL CreatePrivetParamURL(const std::string& path, |
| 67 const std::string& query_params) { |
| 68 GURL url(kUrlPlaceHolder); |
| 69 GURL::Replacements replacements; |
| 70 replacements.SetPathStr(path); |
| 71 if (!query_params.empty()) { |
| 72 replacements.SetQueryStr(query_params); |
| 73 } |
| 74 return url.ReplaceComponents(replacements); |
| 75 } |
| 76 |
| 62 } // namespace | 77 } // namespace |
| 63 | 78 |
| 64 PrivetInfoOperationImpl::PrivetInfoOperationImpl( | 79 PrivetInfoOperationImpl::PrivetInfoOperationImpl( |
| 65 PrivetHTTPClientImpl* privet_client, | 80 PrivetHTTPClientImpl* privet_client, |
| 66 const PrivetJSONOperation::ResultCallback& callback) | 81 const PrivetJSONOperation::ResultCallback& callback) |
| 67 : privet_client_(privet_client), callback_(callback) { | 82 : privet_client_(privet_client), callback_(callback) { |
| 68 } | 83 } |
| 69 | 84 |
| 70 PrivetInfoOperationImpl::~PrivetInfoOperationImpl() { | 85 PrivetInfoOperationImpl::~PrivetInfoOperationImpl() { |
| 71 } | 86 } |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 | 332 |
| 318 void PrivetRegisterOperationImpl::Cancelation::Cleanup() { | 333 void PrivetRegisterOperationImpl::Cancelation::Cleanup() { |
| 319 // Nothing needs to be done, as base::Owned will delete this object, | 334 // Nothing needs to be done, as base::Owned will delete this object, |
| 320 // this callback is just here to pass ownership of the Cancelation to | 335 // this callback is just here to pass ownership of the Cancelation to |
| 321 // the message loop. | 336 // the message loop. |
| 322 } | 337 } |
| 323 | 338 |
| 324 PrivetJSONOperationImpl::PrivetJSONOperationImpl( | 339 PrivetJSONOperationImpl::PrivetJSONOperationImpl( |
| 325 PrivetHTTPClientImpl* privet_client, | 340 PrivetHTTPClientImpl* privet_client, |
| 326 const std::string& path, | 341 const std::string& path, |
| 342 const std::string& query_params, |
| 327 const PrivetJSONOperation::ResultCallback& callback) | 343 const PrivetJSONOperation::ResultCallback& callback) |
| 328 : privet_client_(privet_client), path_(path), callback_(callback) { | 344 : privet_client_(privet_client), path_(path), query_params_(query_params), |
| 345 callback_(callback) { |
| 329 } | 346 } |
| 330 | 347 |
| 331 PrivetJSONOperationImpl::~PrivetJSONOperationImpl() { | 348 PrivetJSONOperationImpl::~PrivetJSONOperationImpl() { |
| 332 } | 349 } |
| 333 | 350 |
| 334 void PrivetJSONOperationImpl::Start() { | 351 void PrivetJSONOperationImpl::Start() { |
| 335 url_fetcher_ = privet_client_->CreateURLFetcher( | 352 url_fetcher_ = privet_client_->CreateURLFetcher( |
| 336 CreatePrivetURL(path_), net::URLFetcher::GET, this); | 353 CreatePrivetParamURL(path_, query_params_), net::URLFetcher::GET, this); |
| 337 url_fetcher_->DoNotRetryOnTransientError(); | 354 url_fetcher_->DoNotRetryOnTransientError(); |
| 338 url_fetcher_->Start(); | 355 url_fetcher_->Start(); |
| 339 } | 356 } |
| 340 | 357 |
| 341 PrivetHTTPClient* PrivetJSONOperationImpl::GetHTTPClient() { | 358 PrivetHTTPClient* PrivetJSONOperationImpl::GetHTTPClient() { |
| 342 return privet_client_; | 359 return privet_client_; |
| 343 } | 360 } |
| 344 | 361 |
| 345 void PrivetJSONOperationImpl::OnError( | 362 void PrivetJSONOperationImpl::OnError( |
| 346 PrivetURLFetcher* fetcher, | 363 PrivetURLFetcher* fetcher, |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 scoped_ptr<PrivetJSONOperation> PrivetHTTPClientImpl::CreateInfoOperation( | 727 scoped_ptr<PrivetJSONOperation> PrivetHTTPClientImpl::CreateInfoOperation( |
| 711 const PrivetJSONOperation::ResultCallback& callback) { | 728 const PrivetJSONOperation::ResultCallback& callback) { |
| 712 return scoped_ptr<PrivetJSONOperation>( | 729 return scoped_ptr<PrivetJSONOperation>( |
| 713 new PrivetInfoOperationImpl(this, callback)); | 730 new PrivetInfoOperationImpl(this, callback)); |
| 714 } | 731 } |
| 715 | 732 |
| 716 scoped_ptr<PrivetJSONOperation> | 733 scoped_ptr<PrivetJSONOperation> |
| 717 PrivetHTTPClientImpl::CreateCapabilitiesOperation( | 734 PrivetHTTPClientImpl::CreateCapabilitiesOperation( |
| 718 const PrivetJSONOperation::ResultCallback& callback) { | 735 const PrivetJSONOperation::ResultCallback& callback) { |
| 719 return scoped_ptr<PrivetJSONOperation>( | 736 return scoped_ptr<PrivetJSONOperation>( |
| 720 new PrivetJSONOperationImpl(this, kPrivetCapabilitiesPath, callback)); | 737 new PrivetJSONOperationImpl(this, kPrivetCapabilitiesPath, "", callback)); |
| 721 } | 738 } |
| 722 | 739 |
| 723 scoped_ptr<PrivetLocalPrintOperation> | 740 scoped_ptr<PrivetLocalPrintOperation> |
| 724 PrivetHTTPClientImpl::CreateLocalPrintOperation( | 741 PrivetHTTPClientImpl::CreateLocalPrintOperation( |
| 725 PrivetLocalPrintOperation::Delegate* delegate) { | 742 PrivetLocalPrintOperation::Delegate* delegate) { |
| 726 return scoped_ptr<PrivetLocalPrintOperation>( | 743 return scoped_ptr<PrivetLocalPrintOperation>( |
| 727 new PrivetLocalPrintOperationImpl(this, delegate)); | 744 new PrivetLocalPrintOperationImpl(this, delegate)); |
| 728 } | 745 } |
| 729 | 746 |
| 747 scoped_ptr<PrivetJSONOperation> |
| 748 PrivetHTTPClientImpl::CreateStorageListOperation( |
| 749 const std::string& path, |
| 750 const PrivetJSONOperation::ResultCallback& callback) { |
| 751 std::string url_param = base::StringPrintf(kPrivetStorageParamPathFormat, |
| 752 path.c_str()); |
| 753 return scoped_ptr<PrivetJSONOperation>( |
| 754 new PrivetJSONOperationImpl(this, kPrivetStorageListPath, url_param, |
| 755 callback)); |
| 756 } |
| 757 |
| 730 const std::string& PrivetHTTPClientImpl::GetName() { | 758 const std::string& PrivetHTTPClientImpl::GetName() { |
| 731 return name_; | 759 return name_; |
| 732 } | 760 } |
| 733 | 761 |
| 734 scoped_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( | 762 scoped_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( |
| 735 const GURL& url, net::URLFetcher::RequestType request_type, | 763 const GURL& url, net::URLFetcher::RequestType request_type, |
| 736 PrivetURLFetcher::Delegate* delegate) const { | 764 PrivetURLFetcher::Delegate* delegate) const { |
| 737 GURL::Replacements replacements; | 765 GURL::Replacements replacements; |
| 738 replacements.SetHostStr(host_port_.host()); | 766 replacements.SetHostStr(host_port_.host()); |
| 739 std::string port(base::IntToString(host_port_.port())); // Keep string alive. | 767 std::string port(base::IntToString(host_port_.port())); // Keep string alive. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 TokenCallbackVector token_callbacks; | 809 TokenCallbackVector token_callbacks; |
| 782 token_callbacks_.swap(token_callbacks); | 810 token_callbacks_.swap(token_callbacks); |
| 783 | 811 |
| 784 for (TokenCallbackVector::iterator i = token_callbacks.begin(); | 812 for (TokenCallbackVector::iterator i = token_callbacks.begin(); |
| 785 i != token_callbacks.end(); i++) { | 813 i != token_callbacks.end(); i++) { |
| 786 i->Run(token); | 814 i->Run(token); |
| 787 } | 815 } |
| 788 } | 816 } |
| 789 | 817 |
| 790 } // namespace local_discovery | 818 } // namespace local_discovery |
| OLD | NEW |