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 GetCallName() OVERRIDE { |
| 25 return "/privet/v3/setup/start"; |
| 26 } |
| 27 virtual const base::DictionaryValue& GetInput() OVERRIDE; |
| 28 |
| 29 virtual void OnError(PrivetURLFetcher* fetcher, |
| 30 PrivetURLFetcher::ErrorType error) OVERRIDE; |
| 31 virtual void OnParsedJson(PrivetURLFetcher* fetcher, |
| 32 const base::DictionaryValue& value, |
| 33 bool has_error) OVERRIDE; |
| 34 |
| 35 void SetWiFiCridentials(const std::string& ssid, const std::string& password); |
| 36 |
| 37 void SetRegistrationTicket(const std::string& ticket_id, |
| 38 const std::string& owner_email); |
| 39 |
| 40 private: |
| 41 base::DictionaryValue input_; |
| 42 PrivetV3SetupFlow* setup_flow_; |
| 43 }; |
| 44 |
| 45 SetupRequest::SetupRequest(PrivetV3SetupFlow* setup_flow) |
| 46 : setup_flow_(setup_flow) { |
| 47 } |
| 48 |
| 49 SetupRequest::~SetupRequest() { |
| 50 } |
| 51 |
| 52 const base::DictionaryValue& SetupRequest::GetInput() { |
| 53 return input_; |
| 54 } |
| 55 |
| 56 void SetupRequest::OnError(PrivetURLFetcher* fetcher, |
| 57 PrivetURLFetcher::ErrorType error) { |
| 58 setup_flow_->OnSetupError(); |
| 59 } |
| 60 |
| 61 void SetupRequest::OnParsedJson(PrivetURLFetcher* fetcher, |
| 62 const base::DictionaryValue& value, |
| 63 bool has_error) { |
| 64 if (has_error) |
| 65 return setup_flow_->OnSetupError(); |
| 66 setup_flow_->OnDeviceRegistered(); |
| 67 } |
| 68 |
| 69 void SetupRequest::SetWiFiCridentials(const std::string& ssid, |
| 70 const std::string& password) { |
| 71 DCHECK(!ssid.empty()); |
| 72 DCHECK(!password.empty()); |
| 73 input_.SetString(kSsidJsonKeyName, ssid); |
| 74 input_.SetString(kPasswordJsonKeyName, password); |
| 75 } |
| 76 |
| 77 void SetupRequest::SetRegistrationTicket(const std::string& ticket_id, |
| 78 const std::string& owner_email) { |
| 79 DCHECK(!ticket_id.empty()); |
| 80 DCHECK(!owner_email.empty()); |
| 81 input_.SetString(kTicketJsonKeyName, ticket_id); |
| 82 input_.SetString(kUserJsonKeyName, owner_email); |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
11 PrivetV3SetupFlow::Delegate::~Delegate() { | 87 PrivetV3SetupFlow::Delegate::~Delegate() { |
12 } | 88 } |
13 | 89 |
14 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate) | 90 PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate) |
15 : delegate_(delegate), weak_ptr_factory_(this) { | 91 : delegate_(delegate), weak_ptr_factory_(this) { |
16 } | 92 } |
17 | 93 |
18 PrivetV3SetupFlow::~PrivetV3SetupFlow() { | 94 PrivetV3SetupFlow::~PrivetV3SetupFlow() { |
19 } | 95 } |
20 | 96 |
21 void PrivetV3SetupFlow::Register(const std::string& service_name) { | 97 void PrivetV3SetupFlow::Register(const std::string& service_name) { |
22 NOTIMPLEMENTED(); | 98 service_name_ = service_name; |
| 99 ticket_request_ = delegate_->CreateApiFlow(); |
| 100 if (!ticket_request_) { |
| 101 OnSetupError(); |
| 102 return; |
| 103 } |
| 104 scoped_ptr<GCDApiFlow::Request> ticket_request( |
| 105 new GCDRegistrationTicketRequest( |
| 106 base::Bind(&PrivetV3SetupFlow::OnTicketCreated, |
| 107 weak_ptr_factory_.GetWeakPtr()))); |
| 108 ticket_request_->Start(ticket_request.Pass()); |
23 } | 109 } |
24 | 110 |
25 #if defined(ENABLE_WIFI_BOOTSTRAPPING) | 111 #if defined(ENABLE_WIFI_BOOTSTRAPPING) |
26 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) { | 112 void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) { |
27 NOTIMPLEMENTED(); | 113 NOTIMPLEMENTED(); |
28 } | 114 } |
29 #endif // ENABLE_WIFI_BOOTSTRAPPING | 115 #endif // ENABLE_WIFI_BOOTSTRAPPING |
30 | 116 |
| 117 void PrivetV3SetupFlow::OnSetupConfirmationNeeded( |
| 118 const std::string& confirmation_code) { |
| 119 delegate_->ConfirmSecurityCode(confirmation_code, |
| 120 base::Bind(&PrivetV3SetupFlow::OnCodeConfirmed, |
| 121 weak_ptr_factory_.GetWeakPtr())); |
| 122 } |
| 123 |
| 124 void PrivetV3SetupFlow::OnSessionEstablished() { |
| 125 DCHECK(setup_request_); |
| 126 session_->StartRequest(setup_request_.get()); |
| 127 } |
| 128 |
| 129 void PrivetV3SetupFlow::OnCannotEstablishSession() { |
| 130 OnSetupError(); |
| 131 } |
| 132 |
| 133 void PrivetV3SetupFlow::OnSetupError() { |
| 134 delegate_->OnSetupError(); |
| 135 } |
| 136 |
| 137 void PrivetV3SetupFlow::OnDeviceRegistered() { |
| 138 delegate_->OnSetupDone(); |
| 139 } |
| 140 |
| 141 void PrivetV3SetupFlow::OnTicketCreated(const std::string& ticket_id) { |
| 142 if (ticket_id.empty()) { |
| 143 OnSetupError(); |
| 144 return; |
| 145 } |
| 146 SetupRequest* setup_request = new SetupRequest(this); |
| 147 setup_request_.reset(setup_request); |
| 148 setup_request->SetRegistrationTicket(ticket_id, "me"); |
| 149 delegate_->CreatePrivetV3Client( |
| 150 service_name_, |
| 151 base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated, |
| 152 weak_ptr_factory_.GetWeakPtr())); |
| 153 } |
| 154 |
| 155 void PrivetV3SetupFlow::OnPrivetClientCreated( |
| 156 scoped_ptr<PrivetHTTPClient> privet_http_client) { |
| 157 if (!privet_http_client) { |
| 158 OnSetupError(); |
| 159 return; |
| 160 } |
| 161 session_.reset(new PrivetV3Session(privet_http_client.Pass(), this)); |
| 162 session_->Start(); |
| 163 } |
| 164 |
| 165 void PrivetV3SetupFlow::OnCodeConfirmed(bool success) { |
| 166 if (!success) |
| 167 return OnSetupError(); |
| 168 session_->ConfirmCode(); |
| 169 } |
| 170 |
31 } // namespace local_discovery | 171 } // namespace local_discovery |
OLD | NEW |