summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/privetv3_setup_flow.cc
blob: 97705c76725247908ed3e88f0067eb95dbfc5aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/local_discovery/privetv3_setup_flow.h"

#include "base/logging.h"
#include "chrome/browser/local_discovery/gcd_registration_ticket_request.h"

namespace local_discovery {

namespace {

const char kSsidJsonKeyName[] = "wifi.ssid";
const char kPasswordJsonKeyName[] = "wifi.passphrase";
const char kTicketJsonKeyName[] = "registration.ticketID";
const char kUserJsonKeyName[] = "registration.user";

class SetupRequest : public PrivetV3Session::Request {
 public:
  explicit SetupRequest(PrivetV3SetupFlow* setup_flow);
  virtual ~SetupRequest();

  virtual std::string GetName() OVERRIDE { return "/privet/v3/setup/start"; }
  virtual const base::DictionaryValue& GetInput() OVERRIDE;

  virtual void OnError(PrivetURLFetcher::ErrorType error) OVERRIDE;
  virtual void OnParsedJson(const base::DictionaryValue& value,
                            bool has_error) OVERRIDE;

  void SetWiFiCridentials(const std::string& ssid, const std::string& password);

  void SetRegistrationTicket(const std::string& ticket_id,
                             const std::string& owner_email);

 private:
  base::DictionaryValue input_;
  PrivetV3SetupFlow* setup_flow_;
};

SetupRequest::SetupRequest(PrivetV3SetupFlow* setup_flow)
    : setup_flow_(setup_flow) {
}

SetupRequest::~SetupRequest() {
}

const base::DictionaryValue& SetupRequest::GetInput() {
  return input_;
}

void SetupRequest::OnError(PrivetURLFetcher::ErrorType error) {
  setup_flow_->OnSetupError();
}

void SetupRequest::OnParsedJson(const base::DictionaryValue& value,
                                bool has_error) {
  if (has_error)
    return setup_flow_->OnSetupError();
  setup_flow_->OnDeviceRegistered();
}

void SetupRequest::SetWiFiCridentials(const std::string& ssid,
                                      const std::string& password) {
  DCHECK(!ssid.empty());
  DCHECK(!password.empty());
  input_.SetString(kSsidJsonKeyName, ssid);
  input_.SetString(kPasswordJsonKeyName, password);
}

void SetupRequest::SetRegistrationTicket(const std::string& ticket_id,
                                         const std::string& owner_email) {
  DCHECK(!ticket_id.empty());
  DCHECK(!owner_email.empty());
  input_.SetString(kTicketJsonKeyName, ticket_id);
  input_.SetString(kUserJsonKeyName, owner_email);
}

}  // namespace

PrivetV3SetupFlow::Delegate::~Delegate() {
}

PrivetV3SetupFlow::PrivetV3SetupFlow(Delegate* delegate)
    : delegate_(delegate), weak_ptr_factory_(this) {
}

PrivetV3SetupFlow::~PrivetV3SetupFlow() {
}

void PrivetV3SetupFlow::Register(const std::string& service_name) {
  service_name_ = service_name;
  ticket_request_ = delegate_->CreateApiFlow();
  if (!ticket_request_) {
    OnSetupError();
    return;
  }
  scoped_ptr<GCDApiFlow::Request> ticket_request(
      new GCDRegistrationTicketRequest(
          base::Bind(&PrivetV3SetupFlow::OnTicketCreated,
                     weak_ptr_factory_.GetWeakPtr())));
  ticket_request_->Start(ticket_request.Pass());
}

#if defined(ENABLE_WIFI_BOOTSTRAPPING)
void PrivetV3SetupFlow::SetupWifiAndRegister(const std::string& device_ssid) {
  NOTIMPLEMENTED();
}
#endif  // ENABLE_WIFI_BOOTSTRAPPING

void PrivetV3SetupFlow::OnSetupConfirmationNeeded(
    const std::string& confirmation_code) {
  delegate_->ConfirmSecurityCode(confirmation_code,
                                 base::Bind(&PrivetV3SetupFlow::OnCodeConfirmed,
                                            weak_ptr_factory_.GetWeakPtr()));
}

void PrivetV3SetupFlow::OnSessionEstablished() {
  DCHECK(setup_request_);
  session_->StartRequest(setup_request_.get());
}

void PrivetV3SetupFlow::OnCannotEstablishSession() {
  OnSetupError();
}

void PrivetV3SetupFlow::OnSetupError() {
  delegate_->OnSetupError();
}

void PrivetV3SetupFlow::OnDeviceRegistered() {
  delegate_->OnSetupDone();
}

void PrivetV3SetupFlow::OnTicketCreated(const std::string& ticket_id,
                                        const std::string& device_id) {
  if (ticket_id.empty() || device_id.empty()) {
    OnSetupError();
    return;
  }
  // TODO(vitalybuka): Implement success check by polling status of device_id_.
  device_id_ = device_id;
  SetupRequest* setup_request = new SetupRequest(this);
  setup_request_.reset(setup_request);
  setup_request->SetRegistrationTicket(ticket_id, "me");
  delegate_->CreatePrivetV3Client(
      service_name_,
      base::Bind(&PrivetV3SetupFlow::OnPrivetClientCreated,
                 weak_ptr_factory_.GetWeakPtr()));
}

void PrivetV3SetupFlow::OnPrivetClientCreated(
    scoped_ptr<PrivetHTTPClient> privet_http_client) {
  if (!privet_http_client) {
    OnSetupError();
    return;
  }
  session_.reset(new PrivetV3Session(privet_http_client.Pass(), this));
  session_->Start();
}

void PrivetV3SetupFlow::OnCodeConfirmed(bool success) {
  if (!success)
    return OnSetupError();
  session_->ConfirmCode();
}

}  // namespace local_discovery