| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/local_discovery/privetv3_setup_flow.h" | 5 #include "chrome/browser/local_discovery/privetv3_setup_flow.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/local_discovery/gcd_registration_ticket_request.h" | 8 #include "chrome/browser/local_discovery/gcd_registration_ticket_request.h" |
| 9 | 9 |
| 10 namespace local_discovery { | 10 namespace local_discovery { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 const char kSsidJsonKeyName[] = "wifi.ssid"; | |
| 15 const char kPasswordJsonKeyName[] = "wifi.passphrase"; | |
| 16 const char kTicketJsonKeyName[] = "registration.ticketID"; | 14 const char kTicketJsonKeyName[] = "registration.ticketID"; |
| 17 const char kUserJsonKeyName[] = "registration.user"; | 15 const char kUserJsonKeyName[] = "registration.user"; |
| 18 | 16 |
| 19 class SetupRequest : public PrivetV3Session::Request { | |
| 20 public: | |
| 21 explicit SetupRequest(PrivetV3SetupFlow* setup_flow); | |
| 22 ~SetupRequest() override; | |
| 23 | |
| 24 std::string GetName() override { return "/privet/v3/setup/start"; } | |
| 25 const base::DictionaryValue& GetInput() override; | |
| 26 | |
| 27 void OnError() override; | |
| 28 void OnParsedJson(const base::DictionaryValue& value, | |
| 29 bool has_error) override; | |
| 30 | |
| 31 void SetWiFiCridentials(const std::string& ssid, const std::string& password); | |
| 32 | |
| 33 void SetRegistrationTicket(const std::string& ticket_id, | |
| 34 const std::string& owner_email); | |
| 35 | |
| 36 private: | |
| 37 base::DictionaryValue input_; | |
| 38 PrivetV3SetupFlow* setup_flow_; | |
| 39 }; | |
| 40 | |
| 41 SetupRequest::SetupRequest(PrivetV3SetupFlow* setup_flow) | |
| 42 : setup_flow_(setup_flow) { | |
| 43 } | |
| 44 | |
| 45 SetupRequest::~SetupRequest() { | |
| 46 } | |
| 47 | |
| 48 const base::DictionaryValue& SetupRequest::GetInput() { | |
| 49 return input_; | |
| 50 } | |
| 51 | |
| 52 void SetupRequest::OnError() { | |
| 53 setup_flow_->OnSetupError(); | |
| 54 } | |
| 55 | |
| 56 void SetupRequest::OnParsedJson(const base::DictionaryValue& value, | |
| 57 bool has_error) { | |
| 58 if (has_error) | |
| 59 return setup_flow_->OnSetupError(); | |
| 60 setup_flow_->OnDeviceRegistered(); | |
| 61 } | |
| 62 | |
| 63 void SetupRequest::SetWiFiCridentials(const std::string& ssid, | |
| 64 const std::string& password) { | |
| 65 DCHECK(!ssid.empty()); | |
| 66 DCHECK(!password.empty()); | |
| 67 input_.SetString(kSsidJsonKeyName, ssid); | |
| 68 input_.SetString(kPasswordJsonKeyName, password); | |
| 69 } | |
| 70 | |
| 71 void SetupRequest::SetRegistrationTicket(const std::string& ticket_id, | |
| 72 const std::string& owner_email) { | |
| 73 DCHECK(!ticket_id.empty()); | |
| 74 DCHECK(!owner_email.empty()); | |
| 75 input_.SetString(kTicketJsonKeyName, ticket_id); | |
| 76 input_.SetString(kUserJsonKeyName, owner_email); | |
| 77 } | |
| 78 | |
| 79 } // namespace | 17 } // namespace |
| 80 | 18 |
| 81 PrivetV3SetupFlow::Delegate::~Delegate() { | 19 PrivetV3SetupFlow::Delegate::~Delegate() { |
| 82 } | 20 } |
| 83 | 21 |
| 84 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate) | 22 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate) |
| 85 : delegate_(delegate), weak_ptr_factory_(this) { | 23 : delegate_(delegate), weak_ptr_factory_(this) { |
| 86 } | 24 } |
| 87 | 25 |
| 88 PrivetV3SetupFlow::~PrivetV3SetupFlow() { | 26 PrivetV3SetupFlow::~PrivetV3SetupFlow() { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 101 weak_ptr_factory_.GetWeakPtr()))); | 39 weak_ptr_factory_.GetWeakPtr()))); |
| 102 ticket_request_->Start(ticket_request.Pass()); | 40 ticket_request_->Start(ticket_request.Pass()); |
| 103 } | 41 } |
| 104 | 42 |
| 105 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 43 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
| 106 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) { | 44 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) { |
| 107 NOTIMPLEMENTED(); | 45 NOTIMPLEMENTED(); |
| 108 } | 46 } |
| 109 #endif // ENABLE_WIFI_BOOTSTRAPPING | 47 #endif // ENABLE_WIFI_BOOTSTRAPPING |
| 110 | 48 |
| 111 void PrivetV3SetupFlow::OnSetupConfirmationNeeded( | |
| 112 const std::string& confirmation_code, | |
| 113 extensions::api::gcd_private::ConfirmationType confirmation_type) { | |
| 114 delegate_->ConfirmSecurityCode(confirmation_code, | |
| 115 base::Bind(&PrivetV3SetupFlow::OnCodeConfirmed, | |
| 116 weak_ptr_factory_.GetWeakPtr(), | |
| 117 confirmation_code)); | |
| 118 } | |
| 119 | |
| 120 void PrivetV3SetupFlow::OnSessionStatus( | |
| 121 extensions::api::gcd_private::Status status) { | |
| 122 if (status == extensions::api::gcd_private::STATUS_SUCCESS) { | |
| 123 DCHECK(setup_request_); | |
| 124 session_->StartRequest(setup_request_.get()); | |
| 125 } else { | |
| 126 OnSetupError(); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void PrivetV3SetupFlow::OnSetupError() { | 49 void PrivetV3SetupFlow::OnSetupError() { |
| 131 delegate_->OnSetupError(); | 50 delegate_->OnSetupError(); |
| 132 } | 51 } |
| 133 | 52 |
| 134 void PrivetV3SetupFlow::OnDeviceRegistered() { | |
| 135 delegate_->OnSetupDone(); | |
| 136 } | |
| 137 | |
| 138 void PrivetV3SetupFlow::OnTicketCreated(const std::string& ticket_id, | 53 void PrivetV3SetupFlow::OnTicketCreated(const std::string& ticket_id, |
| 139 const std::string& device_id) { | 54 const std::string& device_id) { |
| 140 if (ticket_id.empty() || device_id.empty()) { | 55 if (ticket_id.empty() || device_id.empty()) { |
| 141 OnSetupError(); | 56 OnSetupError(); |
| 142 return; | 57 return; |
| 143 } | 58 } |
| 144 // TODO(vitalybuka): Implement success check by polling status of device_id_. | 59 // TODO(vitalybuka): Implement success check by polling status of device_id_. |
| 145 device_id_ = device_id; | 60 device_id_ = device_id; |
| 146 SetupRequest* setup_request = new SetupRequest(this); | 61 ticket_id_ = ticket_id; |
| 147 setup_request_.reset(setup_request); | |
| 148 setup_request->SetRegistrationTicket(ticket_id, "me"); | |
| 149 delegate_->CreatePrivetV3Client( | 62 delegate_->CreatePrivetV3Client( |
| 150 service_name_, | 63 service_name_, |
| 151 base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated, | 64 base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated, |
| 152 weak_ptr_factory_.GetWeakPtr())); | 65 weak_ptr_factory_.GetWeakPtr())); |
| 153 } | 66 } |
| 154 | 67 |
| 155 void PrivetV3SetupFlow::OnPrivetClientCreated( | 68 void PrivetV3SetupFlow::OnPrivetClientCreated( |
| 156 scoped_ptr<PrivetHTTPClient> privet_http_client) { | 69 scoped_ptr<PrivetHTTPClient> privet_http_client) { |
| 157 if (!privet_http_client) { | 70 if (!privet_http_client) { |
| 158 OnSetupError(); | 71 OnSetupError(); |
| 159 return; | 72 return; |
| 160 } | 73 } |
| 161 session_.reset(new PrivetV3Session(privet_http_client.Pass(), this)); | 74 session_.reset(new PrivetV3Session(privet_http_client.Pass())); |
| 162 session_->Start(); | 75 session_->Init(base::Bind(&PrivetV3SetupFlow::OnSessionInitialized, |
| 76 weak_ptr_factory_.GetWeakPtr())); |
| 163 } | 77 } |
| 164 | 78 |
| 165 void PrivetV3SetupFlow::OnCodeConfirmed(const std::string& code, bool success) { | 79 void PrivetV3SetupFlow::OnCodeConfirmed(bool success) { |
| 166 if (!success) | 80 if (!success) |
| 167 return OnSetupError(); | 81 return OnSetupError(); |
| 168 session_->ConfirmCode(code); | 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& responce) { |
| 117 if (result != PrivetV3Session::Result::STATUS_SUCCESS) |
| 118 return OnSetupError(); |
| 119 delegate_->OnSetupDone(); |
| 169 } | 120 } |
| 170 | 121 |
| 171 } // namespace local_discovery | 122 } // namespace local_discovery |
| OLD | NEW |