| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 } | 102 } |
| 103 | 103 |
| 104 void PrivetInfoOperationImpl::OnError(PrivetURLFetcher* fetcher, | 104 void PrivetInfoOperationImpl::OnError(PrivetURLFetcher* fetcher, |
| 105 PrivetURLFetcher::ErrorType error) { | 105 PrivetURLFetcher::ErrorType error) { |
| 106 callback_.Run(NULL); | 106 callback_.Run(NULL); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void PrivetInfoOperationImpl::OnParsedJson(PrivetURLFetcher* fetcher, | 109 void PrivetInfoOperationImpl::OnParsedJson(PrivetURLFetcher* fetcher, |
| 110 const base::DictionaryValue* value, | 110 const base::DictionaryValue* value, |
| 111 bool has_error) { | 111 bool has_error) { |
| 112 if (!has_error) | |
| 113 privet_client_->CacheInfo(value); | |
| 114 callback_.Run(value); | 112 callback_.Run(value); |
| 115 } | 113 } |
| 116 | 114 |
| 117 PrivetRegisterOperationImpl::PrivetRegisterOperationImpl( | 115 PrivetRegisterOperationImpl::PrivetRegisterOperationImpl( |
| 118 PrivetHTTPClientImpl* privet_client, | 116 PrivetHTTPClientImpl* privet_client, |
| 119 const std::string& user, | 117 const std::string& user, |
| 120 PrivetRegisterOperation::Delegate* delegate) | 118 PrivetRegisterOperation::Delegate* delegate) |
| 121 : user_(user), delegate_(delegate), privet_client_(privet_client), | 119 : user_(user), delegate_(delegate), privet_client_(privet_client), |
| 122 ongoing_(false) { | 120 ongoing_(false) { |
| 123 } | 121 } |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 void PrivetLocalPrintOperationImpl::SetPWGRasterConverterForTesting( | 702 void PrivetLocalPrintOperationImpl::SetPWGRasterConverterForTesting( |
| 705 scoped_ptr<PWGRasterConverter> pwg_raster_converter) { | 703 scoped_ptr<PWGRasterConverter> pwg_raster_converter) { |
| 706 pwg_raster_converter_ = pwg_raster_converter.Pass(); | 704 pwg_raster_converter_ = pwg_raster_converter.Pass(); |
| 707 } | 705 } |
| 708 | 706 |
| 709 PrivetHTTPClientImpl::PrivetHTTPClientImpl( | 707 PrivetHTTPClientImpl::PrivetHTTPClientImpl( |
| 710 const std::string& name, | 708 const std::string& name, |
| 711 const net::HostPortPair& host_port, | 709 const net::HostPortPair& host_port, |
| 712 net::URLRequestContextGetter* request_context) | 710 net::URLRequestContextGetter* request_context) |
| 713 : name_(name), | 711 : name_(name), |
| 714 fetcher_factory_(request_context), | 712 request_context_(request_context), |
| 715 host_port_(host_port) { | 713 host_port_(host_port) { |
| 716 } | 714 } |
| 717 | 715 |
| 718 PrivetHTTPClientImpl::~PrivetHTTPClientImpl() { | 716 PrivetHTTPClientImpl::~PrivetHTTPClientImpl() { |
| 719 } | 717 } |
| 720 | 718 |
| 721 const base::DictionaryValue* PrivetHTTPClientImpl::GetCachedInfo() const { | |
| 722 return cached_info_.get(); | |
| 723 } | |
| 724 | |
| 725 scoped_ptr<PrivetRegisterOperation> | 719 scoped_ptr<PrivetRegisterOperation> |
| 726 PrivetHTTPClientImpl::CreateRegisterOperation( | 720 PrivetHTTPClientImpl::CreateRegisterOperation( |
| 727 const std::string& user, | 721 const std::string& user, |
| 728 PrivetRegisterOperation::Delegate* delegate) { | 722 PrivetRegisterOperation::Delegate* delegate) { |
| 729 return scoped_ptr<PrivetRegisterOperation>( | 723 return scoped_ptr<PrivetRegisterOperation>( |
| 730 new PrivetRegisterOperationImpl(this, user, delegate)); | 724 new PrivetRegisterOperationImpl(this, user, delegate)); |
| 731 } | 725 } |
| 732 | 726 |
| 733 scoped_ptr<PrivetJSONOperation> PrivetHTTPClientImpl::CreateInfoOperation( | 727 scoped_ptr<PrivetJSONOperation> PrivetHTTPClientImpl::CreateInfoOperation( |
| 734 const PrivetJSONOperation::ResultCallback& callback) { | 728 const PrivetJSONOperation::ResultCallback& callback) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 765 return name_; | 759 return name_; |
| 766 } | 760 } |
| 767 | 761 |
| 768 scoped_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( | 762 scoped_ptr<PrivetURLFetcher> PrivetHTTPClientImpl::CreateURLFetcher( |
| 769 const GURL& url, net::URLFetcher::RequestType request_type, | 763 const GURL& url, net::URLFetcher::RequestType request_type, |
| 770 PrivetURLFetcher::Delegate* delegate) const { | 764 PrivetURLFetcher::Delegate* delegate) const { |
| 771 GURL::Replacements replacements; | 765 GURL::Replacements replacements; |
| 772 replacements.SetHostStr(host_port_.host()); | 766 replacements.SetHostStr(host_port_.host()); |
| 773 std::string port(base::IntToString(host_port_.port())); // Keep string alive. | 767 std::string port(base::IntToString(host_port_.port())); // Keep string alive. |
| 774 replacements.SetPortStr(port); | 768 replacements.SetPortStr(port); |
| 775 return fetcher_factory_.CreateURLFetcher(url.ReplaceComponents(replacements), | 769 return scoped_ptr<PrivetURLFetcher>( |
| 776 request_type, delegate); | 770 new PrivetURLFetcher(url.ReplaceComponents(replacements), request_type, |
| 771 request_context_.get(), delegate)); |
| 777 } | 772 } |
| 778 | 773 |
| 779 void PrivetHTTPClientImpl::CacheInfo(const base::DictionaryValue* cached_info) { | |
| 780 cached_info_.reset(cached_info->DeepCopy()); | |
| 781 std::string token; | |
| 782 if (cached_info_->GetString(kPrivetInfoKeyToken, &token)) { | |
| 783 fetcher_factory_.set_token(token); | |
| 784 } | |
| 785 } | |
| 786 | |
| 787 bool PrivetHTTPClientImpl::HasToken() const { | |
| 788 return fetcher_factory_.get_token() != ""; | |
| 789 }; | |
| 790 | |
| 791 void PrivetHTTPClientImpl::RefreshPrivetToken( | 774 void PrivetHTTPClientImpl::RefreshPrivetToken( |
| 792 const PrivetURLFetcher::TokenCallback& callback) { | 775 const PrivetURLFetcher::TokenCallback& callback) { |
| 793 token_callbacks_.push_back(callback); | 776 token_callbacks_.push_back(callback); |
| 794 | 777 |
| 795 if (!info_operation_) { | 778 if (!info_operation_) { |
| 796 info_operation_ = CreateInfoOperation( | 779 info_operation_ = CreateInfoOperation( |
| 797 base::Bind(&PrivetHTTPClientImpl::OnPrivetInfoDone, | 780 base::Bind(&PrivetHTTPClientImpl::OnPrivetInfoDone, |
| 798 base::Unretained(this))); | 781 base::Unretained(this))); |
| 799 info_operation_->Start(); | 782 info_operation_->Start(); |
| 800 } | 783 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 814 TokenCallbackVector token_callbacks; | 797 TokenCallbackVector token_callbacks; |
| 815 token_callbacks_.swap(token_callbacks); | 798 token_callbacks_.swap(token_callbacks); |
| 816 | 799 |
| 817 for (TokenCallbackVector::iterator i = token_callbacks.begin(); | 800 for (TokenCallbackVector::iterator i = token_callbacks.begin(); |
| 818 i != token_callbacks.end(); i++) { | 801 i != token_callbacks.end(); i++) { |
| 819 i->Run(token); | 802 i->Run(token); |
| 820 } | 803 } |
| 821 } | 804 } |
| 822 | 805 |
| 823 } // namespace local_discovery | 806 } // namespace local_discovery |
| OLD | NEW |