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 | 9 |
9 namespace local_discovery { | 10 namespace local_discovery { |
10 | 11 |
| 12 namespace { |
| 13 |
| 14 const char kSsidJsonKeyName[] = "wifi.ssid"; |
| 15 const char kPasswordJsonKeyName[] = "wifi.passphrase"; |
| 16 const char kTicketJsonKeyName[] = "registration.ticketID"; |
| 17 const char kUserJsonKeyName[] = "registration.user"; |
| 18 |
| 19 class SetupRequest : public PrivetV3Session::Request { |
| 20 public: |
| 21 explicit SetupRequest(PrivetV3SetupFlow* setup_flow); |
| 22 virtual ~SetupRequest(); |
| 23 |
| 24 virtual std::string GetName() OVERRIDE { return "/privet/v3/setup/start"; } |
| 25 virtual const base::DictionaryValue& GetInput() OVERRIDE; |
| 26 |
| 27 virtual void OnError(PrivetURLFetcher::ErrorType error) OVERRIDE; |
| 28 virtual 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(PrivetURLFetcher::ErrorType error) { |
| 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 |
| 80 |
11 PrivetV3SetupFlow::Delegate::~Delegate() { | 81 PrivetV3SetupFlow::Delegate::~Delegate() { |
12 } | 82 } |
13 | 83 |
14 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate) | 84 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate) |
15 : delegate_(delegate), weak_ptr_factory_(this) { | 85 : delegate_(delegate), weak_ptr_factory_(this) { |
16 } | 86 } |
17 | 87 |
18 PrivetV3SetupFlow::~PrivetV3SetupFlow() { | 88 PrivetV3SetupFlow::~PrivetV3SetupFlow() { |
19 } | 89 } |
20 | 90 |
21 void PrivetV3SetupFlow::Register(const std::string& service_name) { | 91 void PrivetV3SetupFlow::Register(const std::string& service_name) { |
22 NOTIMPLEMENTED(); | 92 service_name_ = service_name; |
| 93 ticket_request_ = delegate_->CreateApiFlow(); |
| 94 if (!ticket_request_) { |
| 95 OnSetupError(); |
| 96 return; |
| 97 } |
| 98 scoped_ptr<GCDApiFlow::Request> ticket_request( |
| 99 new GCDRegistrationTicketRequest( |
| 100 base::Bind(&PrivetV3SetupFlow::OnTicketCreated, |
| 101 weak_ptr_factory_.GetWeakPtr()))); |
| 102 ticket_request_->Start(ticket_request.Pass()); |
23 } | 103 } |
24 | 104 |
25 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 105 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
26 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) { | 106 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) { |
27 NOTIMPLEMENTED(); | 107 NOTIMPLEMENTED(); |
28 } | 108 } |
29 #endif // ENABLE_WIFI_BOOTSTRAPPING | 109 #endif // ENABLE_WIFI_BOOTSTRAPPING |
30 | 110 |
| 111 void PrivetV3SetupFlow::OnSetupConfirmationNeeded( |
| 112 const std::string& confirmation_code) { |
| 113 delegate_->ConfirmSecurityCode(confirmation_code, |
| 114 base::Bind(&PrivetV3SetupFlow::OnCodeConfirmed, |
| 115 weak_ptr_factory_.GetWeakPtr())); |
| 116 } |
| 117 |
| 118 void PrivetV3SetupFlow::OnSessionEstablished() { |
| 119 DCHECK(setup_request_); |
| 120 session_->StartRequest(setup_request_.get()); |
| 121 } |
| 122 |
| 123 void PrivetV3SetupFlow::OnCannotEstablishSession() { |
| 124 OnSetupError(); |
| 125 } |
| 126 |
| 127 void PrivetV3SetupFlow::OnSetupError() { |
| 128 delegate_->OnSetupError(); |
| 129 } |
| 130 |
| 131 void PrivetV3SetupFlow::OnDeviceRegistered() { |
| 132 delegate_->OnSetupDone(); |
| 133 } |
| 134 |
| 135 void PrivetV3SetupFlow::OnTicketCreated(const std::string& ticket_id) { |
| 136 if (ticket_id.empty()) { |
| 137 OnSetupError(); |
| 138 return; |
| 139 } |
| 140 SetupRequest* setup_request = new SetupRequest(this); |
| 141 setup_request_.reset(setup_request); |
| 142 setup_request->SetRegistrationTicket(ticket_id, "me"); |
| 143 delegate_->CreatePrivetV3Client( |
| 144 service_name_, |
| 145 base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated, |
| 146 weak_ptr_factory_.GetWeakPtr())); |
| 147 } |
| 148 |
| 149 void PrivetV3SetupFlow::OnPrivetClientCreated( |
| 150 scoped_ptr<PrivetHTTPClient> privet_http_client) { |
| 151 if (!privet_http_client) { |
| 152 OnSetupError(); |
| 153 return; |
| 154 } |
| 155 session_.reset(new PrivetV3Session(privet_http_client.Pass(), this)); |
| 156 session_->Start(); |
| 157 } |
| 158 |
| 159 void PrivetV3SetupFlow::OnCodeConfirmed(bool success) { |
| 160 if (!success) |
| 161 return OnSetupError(); |
| 162 session_->ConfirmCode(); |
| 163 } |
| 164 |
31 } // namespace local_discovery | 165 } // namespace local_discovery |
OLD | NEW |