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_setup_flow.h"
7 #include "base/logging.h"
8 #include "chrome/browser/local_discovery/gcd_registration_ticket_request.h"
10 namespace local_discovery
{
14 const char kTicketJsonKeyName
[] = "registration.ticketID";
15 const char kUserJsonKeyName
[] = "registration.user";
19 PrivetV3SetupFlow::Delegate::~Delegate() {
22 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate
* delegate
)
23 : delegate_(delegate
), weak_ptr_factory_(this) {
26 PrivetV3SetupFlow::~PrivetV3SetupFlow() {
29 void PrivetV3SetupFlow::Register(const std::string
& service_name
) {
30 service_name_
= service_name
;
31 ticket_request_
= delegate_
->CreateApiFlow();
32 if (!ticket_request_
) {
36 scoped_ptr
<GCDApiFlow::Request
> ticket_request(
37 new GCDRegistrationTicketRequest(
38 base::Bind(&PrivetV3SetupFlow::OnTicketCreated
,
39 weak_ptr_factory_
.GetWeakPtr())));
40 ticket_request_
->Start(ticket_request
.Pass());
43 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
44 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string
& device_ssid
) {
47 #endif // ENABLE_WIFI_BOOTSTRAPPING
49 void PrivetV3SetupFlow::OnSetupError() {
50 delegate_
->OnSetupError();
53 void PrivetV3SetupFlow::OnTicketCreated(const std::string
& ticket_id
,
54 const std::string
& device_id
) {
55 if (ticket_id
.empty() || device_id
.empty()) {
59 // TODO(vitalybuka): Implement success check by polling status of device_id_.
60 device_id_
= device_id
;
61 ticket_id_
= ticket_id
;
62 delegate_
->CreatePrivetV3Client(
64 base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated
,
65 weak_ptr_factory_
.GetWeakPtr()));
68 void PrivetV3SetupFlow::OnPrivetClientCreated(
69 scoped_ptr
<PrivetHTTPClient
> privet_http_client
) {
70 if (!privet_http_client
) {
74 session_
.reset(new PrivetV3Session(privet_http_client
.Pass()));
75 session_
->Init(base::Bind(&PrivetV3SetupFlow::OnSessionInitialized
,
76 weak_ptr_factory_
.GetWeakPtr()));
79 void PrivetV3SetupFlow::OnCodeConfirmed(bool success
) {
81 return OnSetupError();
82 session_
->ConfirmCode("1234", base::Bind(&PrivetV3SetupFlow::OnPairingDone
,
83 weak_ptr_factory_
.GetWeakPtr()));
86 void PrivetV3SetupFlow::OnSessionInitialized(
87 PrivetV3Session::Result result
,
88 const std::vector
<PrivetV3Session::PairingType
>& types
) {
89 if (result
!= PrivetV3Session::Result::STATUS_SUCCESS
)
90 return OnSetupError();
91 session_
->StartPairing(PrivetV3Session::PairingType::PAIRING_TYPE_PINCODE
,
92 base::Bind(&PrivetV3SetupFlow::OnPairingStarted
,
93 weak_ptr_factory_
.GetWeakPtr()));
96 void PrivetV3SetupFlow::OnPairingStarted(PrivetV3Session::Result result
) {
97 if (result
!= PrivetV3Session::Result::STATUS_SUCCESS
)
98 return OnSetupError();
99 delegate_
->ConfirmSecurityCode(base::Bind(&PrivetV3SetupFlow::OnCodeConfirmed
,
100 weak_ptr_factory_
.GetWeakPtr()));
103 void PrivetV3SetupFlow::OnPairingDone(PrivetV3Session::Result result
) {
104 if (result
!= PrivetV3Session::Result::STATUS_SUCCESS
)
105 return OnSetupError();
106 base::DictionaryValue message
;
107 message
.SetString(kTicketJsonKeyName
, ticket_id_
);
108 message
.SetString(kUserJsonKeyName
, "me");
109 session_
->SendMessage("/privet/v3/setup/start", message
,
110 base::Bind(&PrivetV3SetupFlow::OnSetupMessageSent
,
111 weak_ptr_factory_
.GetWeakPtr()));
114 void PrivetV3SetupFlow::OnSetupMessageSent(
115 PrivetV3Session::Result result
,
116 const base::DictionaryValue
& response
) {
117 if (result
!= PrivetV3Session::Result::STATUS_SUCCESS
)
118 return OnSetupError();
119 delegate_
->OnSetupDone();
122 } // namespace local_discovery