Fixed Privet v3 auth header.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privetv3_session.cc
blobbd5f4ca678b97c43dd9d128de3c5543ec2125f98
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.
5 #include "chrome/browser/local_discovery/privetv3_session.h"
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_constants.h"
11 #include "chrome/browser/local_discovery/privet_http.h"
12 #include "chrome/browser/local_discovery/privet_url_fetcher.h"
13 #include "chrome/common/cloud_print/cloud_print_constants.h"
14 #include "url/gurl.h"
16 namespace local_discovery {
18 namespace {
20 const char kUrlPlaceHolder[] = "http://host/";
22 const char kStubPrivetCode[] = "1234";
24 const char kPrivetV3AuthAnonymous[] = "Privet anonymous";
26 GURL CreatePrivetURL(const std::string& path) {
27 GURL url(kUrlPlaceHolder);
28 GURL::Replacements replacements;
29 replacements.SetPathStr(path);
30 return url.ReplaceComponents(replacements);
33 } // namespace
35 class PrivetV3Session::FetcherDelegate : public PrivetURLFetcher::Delegate {
36 public:
37 FetcherDelegate(const base::WeakPtr<PrivetV3Session>& session,
38 const std::string& auth_token,
39 const PrivetV3Session::MessageCallback& callback);
40 ~FetcherDelegate() override;
42 // PrivetURLFetcher::Delegate methods.
43 std::string GetAuthToken() override;
44 void OnNeedPrivetToken(
45 PrivetURLFetcher* fetcher,
46 const PrivetURLFetcher::TokenCallback& callback) override;
47 void OnError(PrivetURLFetcher* fetcher,
48 PrivetURLFetcher::ErrorType error) override;
49 void OnParsedJson(PrivetURLFetcher* fetcher,
50 const base::DictionaryValue& value,
51 bool has_error) override;
53 private:
54 friend class PrivetV3Session;
55 void DeleteThis();
57 scoped_ptr<PrivetURLFetcher> url_fetcher_;
58 base::WeakPtr<PrivetV3Session> session_;
59 std::string auth_token_;
60 MessageCallback callback_;
63 PrivetV3Session::FetcherDelegate::FetcherDelegate(
64 const base::WeakPtr<PrivetV3Session>& session,
65 const std::string& auth_token,
66 const PrivetV3Session::MessageCallback& callback)
67 : session_(session), auth_token_(auth_token), callback_(callback) {
70 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
73 std::string PrivetV3Session::FetcherDelegate::GetAuthToken() {
74 return auth_token_;
77 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
78 PrivetURLFetcher* fetcher,
79 const PrivetURLFetcher::TokenCallback& callback) {
80 NOTREACHED();
81 OnError(fetcher, PrivetURLFetcher::URL_FETCH_ERROR);
84 void PrivetV3Session::FetcherDelegate::OnError(
85 PrivetURLFetcher* fetcher,
86 PrivetURLFetcher::ErrorType error) {
87 if (session_) {
88 DeleteThis();
89 callback_.Run(Result::STATUS_CONNECTIONERROR, base::DictionaryValue());
93 void PrivetV3Session::FetcherDelegate::OnParsedJson(
94 PrivetURLFetcher* fetcher,
95 const base::DictionaryValue& value,
96 bool has_error) {
97 if (session_) {
98 DeleteThis();
99 callback_.Run(
100 has_error ? Result::STATUS_DEVICEERROR : Result::STATUS_SUCCESS, value);
104 void PrivetV3Session::FetcherDelegate::DeleteThis() {
105 base::MessageLoop::current()->PostTask(
106 FROM_HERE, base::Bind(&PrivetV3Session::DeleteFetcher, session_,
107 base::Unretained(this)));
110 PrivetV3Session::PrivetV3Session(scoped_ptr<PrivetHTTPClient> client)
111 : client_(client.Pass()), code_confirmed_(false), weak_ptr_factory_(this) {
114 PrivetV3Session::~PrivetV3Session() {
117 void PrivetV3Session::Init(const InitCallback& callback) {
118 privet_auth_token_ = kPrivetV3AuthAnonymous;
120 // TODO: call /info.
121 base::MessageLoop::current()->PostDelayedTask(
122 FROM_HERE,
123 base::Bind(&PrivetV3Session::RunCallback, weak_ptr_factory_.GetWeakPtr(),
124 base::Bind(callback, Result::STATUS_SUCCESS,
125 std::vector<PairingType>(
126 1, PairingType::PAIRING_TYPE_EMBEDDEDCODE))),
127 base::TimeDelta::FromSeconds(1));
130 void PrivetV3Session::StartPairing(PairingType pairing_type,
131 const ResultCallback& callback) {
132 // TODO: call /privet/v3/pairing/start.
133 base::MessageLoop::current()->PostDelayedTask(
134 FROM_HERE,
135 base::Bind(&PrivetV3Session::RunCallback, weak_ptr_factory_.GetWeakPtr(),
136 base::Bind(callback, Result::STATUS_SUCCESS)),
137 base::TimeDelta::FromSeconds(1));
140 void PrivetV3Session::ConfirmCode(const std::string& code,
141 const ResultCallback& callback) {
142 // TODO: call /privet/v3/pairing/confirm.
143 if (code == kStubPrivetCode) {
144 code_confirmed_ = true;
145 callback.Run(Result::STATUS_SUCCESS);
146 } else {
147 callback.Run(Result::STATUS_BADPAIRINGCODEERROR);
151 void PrivetV3Session::SendMessage(const std::string& api,
152 const base::DictionaryValue& input,
153 const MessageCallback& callback) {
154 if (!code_confirmed_)
155 return callback.Run(Result::STATUS_SESSIONERROR, base::DictionaryValue());
157 FetcherDelegate* fetcher_delegate(new FetcherDelegate(
158 weak_ptr_factory_.GetWeakPtr(), privet_auth_token_, callback));
159 fetchers_.push_back(fetcher_delegate);
161 scoped_ptr<PrivetURLFetcher> url_fetcher(client_->CreateURLFetcher(
162 CreatePrivetURL(api), net::URLFetcher::POST, fetcher_delegate));
164 std::string json;
165 base::JSONWriter::WriteWithOptions(
166 &input, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
167 url_fetcher->SetUploadData(cloud_print::kContentTypeJSON, json);
169 fetcher_delegate->url_fetcher_ = url_fetcher.Pass();
170 fetcher_delegate->url_fetcher_->V3Mode();
171 fetcher_delegate->url_fetcher_->Start();
174 void PrivetV3Session::RunCallback(const base::Closure& callback) {
175 callback.Run();
178 void PrivetV3Session::DeleteFetcher(const FetcherDelegate* fetcher) {
179 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher));
182 } // namespace local_discovery