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_http.h"
11 #include "chrome/common/cloud_print/cloud_print_constants.h"
13 namespace local_discovery
{
17 const char kUrlPlaceHolder
[] = "http://host/";
19 const char kStubPrivetCode
[] = "01234";
21 GURL
CreatePrivetURL(const std::string
& path
) {
22 GURL
url(kUrlPlaceHolder
);
23 GURL::Replacements replacements
;
24 replacements
.SetPathStr(path
);
25 return url
.ReplaceComponents(replacements
);
30 class PrivetV3Session::FetcherDelegate
: public PrivetURLFetcher::Delegate
{
32 FetcherDelegate(const base::WeakPtr
<PrivetV3Session
>& session
,
34 virtual ~FetcherDelegate();
36 // PrivetURLFetcher::Delegate methods.
37 virtual void OnNeedPrivetToken(
38 PrivetURLFetcher
* fetcher
,
39 const PrivetURLFetcher::TokenCallback
& callback
) OVERRIDE
;
40 virtual void OnError(PrivetURLFetcher
* fetcher
,
41 PrivetURLFetcher::ErrorType error
) OVERRIDE
;
42 virtual void OnParsedJson(PrivetURLFetcher
* fetcher
,
43 const base::DictionaryValue
& value
,
44 bool has_error
) OVERRIDE
;
47 friend class PrivetV3Session
;
48 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
49 base::WeakPtr
<PrivetV3Session
> session_
;
53 PrivetV3Session::FetcherDelegate::FetcherDelegate(
54 const base::WeakPtr
<PrivetV3Session
>& session
,
56 : session_(session
), request_(request
) {
59 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
62 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
63 PrivetURLFetcher
* fetcher
,
64 const PrivetURLFetcher::TokenCallback
& callback
) {
66 session_
->client_
->RefreshPrivetToken(callback
);
69 void PrivetV3Session::FetcherDelegate::OnError(
70 PrivetURLFetcher
* fetcher
,
71 PrivetURLFetcher::ErrorType error
) {
72 request_
->OnError(error
);
75 void PrivetV3Session::FetcherDelegate::OnParsedJson(
76 PrivetURLFetcher
* fetcher
,
77 const base::DictionaryValue
& value
,
79 request_
->OnParsedJson(value
, has_error
);
82 PrivetV3Session::Delegate::~Delegate() {
85 PrivetV3Session::Request::Request() {
88 PrivetV3Session::Request::~Request() {
91 PrivetV3Session::PrivetV3Session(scoped_ptr
<PrivetHTTPClient
> client
,
93 : delegate_(delegate
),
94 client_(client
.Pass()),
95 code_confirmed_(false),
96 weak_ptr_factory_(this) {
99 PrivetV3Session::~PrivetV3Session() {
102 void PrivetV3Session::Start() {
103 base::MessageLoop::current()->PostDelayedTask(
105 base::Bind(&PrivetV3Session::ConfirmFakeCode
,
106 weak_ptr_factory_
.GetWeakPtr()),
107 base::TimeDelta::FromSeconds(1));
110 void PrivetV3Session::ConfirmCode(const std::string
& code
) {
111 if (code
== kStubPrivetCode
) {
112 code_confirmed_
= true;
113 delegate_
->OnSessionStatus(extensions::api::gcd_private::STATUS_SUCCESS
);
115 delegate_
->OnSessionStatus(
116 extensions::api::gcd_private::STATUS_BADCONFIRMATIONCODEERROR
);
120 void PrivetV3Session::StartRequest(Request
* request
) {
121 CHECK(code_confirmed_
);
123 request
->fetcher_delegate_
.reset(
124 new FetcherDelegate(weak_ptr_factory_
.GetWeakPtr(), request
));
126 scoped_ptr
<PrivetURLFetcher
> url_fetcher
=
127 client_
->CreateURLFetcher(CreatePrivetURL(request
->GetName()),
128 net::URLFetcher::POST
,
129 request
->fetcher_delegate_
.get());
131 base::JSONWriter::WriteWithOptions(
132 &request
->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT
, &json
);
133 url_fetcher
->SetUploadData(cloud_print::kContentTypeJSON
, json
);
135 request
->fetcher_delegate_
->url_fetcher_
= url_fetcher
.Pass();
136 request
->fetcher_delegate_
->url_fetcher_
->Start();
139 void PrivetV3Session::ConfirmFakeCode() {
140 delegate_
->OnSetupConfirmationNeeded(
142 extensions::api::gcd_private::CONFIRMATION_TYPE_DISPLAYCODE
);
145 } // namespace local_discovery