Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / privetv3_setup_flow.cc
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_setup_flow.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/local_discovery/gcd_registration_ticket_request.h"
9
10 namespace local_discovery {
11
12 namespace {
13
14 const char kTicketJsonKeyName[] = "registration.ticketID";
15 const char kUserJsonKeyName[] = "registration.user";
16
17 }  // namespace
18
19 PrivetV3SetupFlow::Delegate::~Delegate() {
20 }
21
22 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate)
23     : delegate_(delegate), weak_ptr_factory_(this) {
24 }
25
26 PrivetV3SetupFlow::~PrivetV3SetupFlow() {
27 }
28
29 void PrivetV3SetupFlow::Register(const std::string& service_name) {
30   service_name_ = service_name;
31   ticket_request_ = delegate_->CreateApiFlow();
32   if (!ticket_request_) {
33     OnSetupError();
34     return;
35   }
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());
41 }
42
43 #if defined(ENABLE_WIFI_BOOTSTRAPPING)
44 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) {
45   NOTIMPLEMENTED();
46 }
47 #endif  // ENABLE_WIFI_BOOTSTRAPPING
48
49 void PrivetV3SetupFlow::OnSetupError() {
50   delegate_->OnSetupError();
51 }
52
53 void PrivetV3SetupFlow::OnTicketCreated(const std::string& ticket_id,
54                                         const std::string& device_id) {
55   if (ticket_id.empty() || device_id.empty()) {
56     OnSetupError();
57     return;
58   }
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(
63       service_name_,
64       base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated,
65                  weak_ptr_factory_.GetWeakPtr()));
66 }
67
68 void PrivetV3SetupFlow::OnPrivetClientCreated(
69     scoped_ptr<PrivetHTTPClient> privet_http_client) {
70   if (!privet_http_client) {
71     OnSetupError();
72     return;
73   }
74   session_.reset(new PrivetV3Session(privet_http_client.Pass()));
75   session_->Init(base::Bind(&PrivetV3SetupFlow::OnSessionInitialized,
76                             weak_ptr_factory_.GetWeakPtr()));
77 }
78
79 void PrivetV3SetupFlow::OnCodeConfirmed(bool success) {
80   if (!success)
81     return OnSetupError();
82   session_->ConfirmCode("1234", base::Bind(&PrivetV3SetupFlow::OnPairingDone,
83                                            weak_ptr_factory_.GetWeakPtr()));
84 }
85
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()));
94 }
95
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()));
101 }
102
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()));
112 }
113
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();
120 }
121
122 }  // namespace local_discovery