privetv3_session.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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 3// found in the LICENSE file. 4 5#include "chrome/browser/local_discovery/privetv3_session.h" 6 7#include "base/json/json_writer.h" 8#include "base/logging.h" 9#include "base/message_loop/message_loop.h" 10#include "chrome/browser/local_discovery/privet_http.h" 11#include "chrome/common/cloud_print/cloud_print_constants.h" 12 13namespace local_discovery { 14 15namespace { 16 17const char kUrlPlaceHolder[] = "http://host/"; 18 19GURL CreatePrivetURL(const std::string& path) { 20 GURL url(kUrlPlaceHolder); 21 GURL::Replacements replacements; 22 replacements.SetPathStr(path); 23 return url.ReplaceComponents(replacements); 24} 25 26} // namespace 27 28class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate { 29 public: 30 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session, 31 Request* request); 32 virtual ~FetcherDelegate(); 33 34 // PrivetURLFetcher::Delegate methods. 35 virtual void OnNeedPrivetToken( 36 PrivetURLFetcher* fetcher, 37 const PrivetURLFetcher::TokenCallback& callback) OVERRIDE; 38 virtual void OnError(PrivetURLFetcher* fetcher, 39 PrivetURLFetcher::ErrorType error) OVERRIDE; 40 virtual void OnParsedJson(PrivetURLFetcher* fetcher, 41 const base::DictionaryValue& value, 42 bool has_error) OVERRIDE; 43 44 private: 45 friend class PrivetV3Session; 46 scoped_ptr<PrivetURLFetcher> url_fetcher_; 47 base::WeakPtr<PrivetV3Session> session_; 48 Request* request_; 49}; 50 51PrivetV3Session::FetcherDelegate::FetcherDelegate( 52 const base::WeakPtr<PrivetV3Session>& session, 53 Request* request) 54 : session_(session), request_(request) { 55} 56 57PrivetV3Session::FetcherDelegate::~FetcherDelegate() { 58} 59 60void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken( 61 PrivetURLFetcher* fetcher, 62 const PrivetURLFetcher::TokenCallback& callback) { 63 if (session_) 64 session_->client_->RefreshPrivetToken(callback); 65} 66 67void PrivetV3Session::FetcherDelegate::OnError( 68 PrivetURLFetcher* fetcher, 69 PrivetURLFetcher::ErrorType error) { 70 request_->OnError(error); 71} 72 73void PrivetV3Session::FetcherDelegate::OnParsedJson( 74 PrivetURLFetcher* fetcher, 75 const base::DictionaryValue& value, 76 bool has_error) { 77 request_->OnParsedJson(value, has_error); 78} 79 80PrivetV3Session::Delegate::~Delegate() { 81} 82 83PrivetV3Session::Request::Request() { 84} 85 86PrivetV3Session::Request::~Request() { 87} 88 89PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client, 90 Delegate* delegate) 91 : delegate_(delegate), 92 client_(client.Pass()), 93 code_confirmed_(false), 94 weak_ptr_factory_(this) { 95} 96 97PrivetV3Session::~PrivetV3Session() { 98} 99 100void PrivetV3Session::Start() { 101 base::MessageLoop::current()->PostDelayedTask( 102 FROM_HERE, 103 base::Bind(&PrivetV3Session::ConfirmFakeCode, 104 weak_ptr_factory_.GetWeakPtr()), 105 base::TimeDelta::FromSeconds(1)); 106} 107 108void PrivetV3Session::ConfirmCode() { 109 code_confirmed_ = true; 110 delegate_->OnSessionEstablished(); 111} 112 113void PrivetV3Session::StartRequest(Request* request) { 114 CHECK(code_confirmed_); 115 116 request->fetcher_delegate_.reset( 117 new FetcherDelegate(weak_ptr_factory_.GetWeakPtr(), request)); 118 119 scoped_ptr<PrivetURLFetcher> url_fetcher = 120 client_->CreateURLFetcher(CreatePrivetURL(request->GetName()), 121 net::URLFetcher::POST, 122 request->fetcher_delegate_.get()); 123 std::string json; 124 base::JSONWriter::WriteWithOptions( 125 &request->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); 126 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json); 127 128 request->fetcher_delegate_->url_fetcher_ = url_fetcher.Pass(); 129 request->fetcher_delegate_->url_fetcher_->Start(); 130} 131 132void PrivetV3Session::ConfirmFakeCode() { 133 delegate_->OnSetupConfirmationNeeded("01234"); 134} 135 136} // namespace local_discovery 137