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 GURL
CreatePrivetURL(const std::string
& path
) {
20 GURL
url(kUrlPlaceHolder
);
21 GURL::Replacements replacements
;
22 replacements
.SetPathStr(path
);
23 return url
.ReplaceComponents(replacements
);
28 class PrivetV3Session::FetcherDelegate
: public PrivetURLFetcher::Delegate
{
30 FetcherDelegate(const base::WeakPtr
<PrivetV3Session
>& session
,
32 virtual ~FetcherDelegate();
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
;
45 friend class PrivetV3Session
;
46 scoped_ptr
<PrivetURLFetcher
> url_fetcher_
;
47 base::WeakPtr
<PrivetV3Session
> session_
;
51 PrivetV3Session::FetcherDelegate::FetcherDelegate(
52 const base::WeakPtr
<PrivetV3Session
>& session
,
54 : session_(session
), request_(request
) {
57 PrivetV3Session::FetcherDelegate::~FetcherDelegate() {
60 void PrivetV3Session::FetcherDelegate::OnNeedPrivetToken(
61 PrivetURLFetcher
* fetcher
,
62 const PrivetURLFetcher::TokenCallback
& callback
) {
64 session_
->client_
->RefreshPrivetToken(callback
);
67 void PrivetV3Session::FetcherDelegate::OnError(
68 PrivetURLFetcher
* fetcher
,
69 PrivetURLFetcher::ErrorType error
) {
70 request_
->OnError(error
);
73 void PrivetV3Session::FetcherDelegate::OnParsedJson(
74 PrivetURLFetcher
* fetcher
,
75 const base::DictionaryValue
& value
,
77 request_
->OnParsedJson(value
, has_error
);
80 PrivetV3Session::Delegate::~Delegate() {
83 PrivetV3Session::Request::Request() {
86 PrivetV3Session::Request::~Request() {
89 PrivetV3Session::PrivetV3Session(scoped_ptr
<PrivetHTTPClient
> client
,
91 : delegate_(delegate
),
92 client_(client
.Pass()),
93 code_confirmed_(false),
94 weak_ptr_factory_(this) {
97 PrivetV3Session::~PrivetV3Session() {
100 void PrivetV3Session::Start() {
101 base::MessageLoop::current()->PostDelayedTask(
103 base::Bind(&PrivetV3Session::ConfirmFakeCode
,
104 weak_ptr_factory_
.GetWeakPtr()),
105 base::TimeDelta::FromSeconds(1));
108 void PrivetV3Session::ConfirmCode() {
109 code_confirmed_
= true;
110 delegate_
->OnSessionEstablished();
113 void PrivetV3Session::StartRequest(Request
* request
) {
114 CHECK(code_confirmed_
);
116 request
->fetcher_delegate_
.reset(
117 new FetcherDelegate(weak_ptr_factory_
.GetWeakPtr(), request
));
119 scoped_ptr
<PrivetURLFetcher
> url_fetcher
=
120 client_
->CreateURLFetcher(CreatePrivetURL(request
->GetName()),
121 net::URLFetcher::POST
,
122 request
->fetcher_delegate_
.get());
124 base::JSONWriter::WriteWithOptions(
125 &request
->GetInput(), base::JSONWriter::OPTIONS_PRETTY_PRINT
, &json
);
126 url_fetcher
->SetUploadData(cloud_print::kContentTypeJSON
, json
);
128 request
->fetcher_delegate_
->url_fetcher_
= url_fetcher
.Pass();
129 request
->fetcher_delegate_
->url_fetcher_
->Start();
132 void PrivetV3Session::ConfirmFakeCode() {
133 delegate_
->OnSetupConfirmationNeeded("01234");
136 } // namespace local_discovery