privetv3_setup_flow.h revision 116680a4aac90f2aa7413d9095a592090648e557
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#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SETUP_FLOW_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SETUP_FLOW_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/local_discovery/gcd_api_flow.h"
13#include "chrome/browser/local_discovery/privet_http.h"
14#include "chrome/browser/local_discovery/privetv3_session.h"
15
16namespace local_discovery {
17
18// Provides complete flow for Privet v3 device setup.
19class PrivetV3SetupFlow : public PrivetV3Session::Delegate {
20 public:
21  // Delegate to be implemented by client code.
22  class Delegate {
23   public:
24    typedef base::Callback<void(bool success)> ResultCallback;
25    // If |ssid| is empty, call failed to get credentials.
26    // If |key| is empty, network is open.
27    typedef base::Callback<void(const std::string& ssid,
28                                const std::string& key)> CredentialsCallback;
29
30    typedef base::Callback<void(scoped_ptr<PrivetHTTPClient>)>
31        PrivetClientCallback;
32
33    virtual ~Delegate();
34
35    // Creates |GCDApiFlowImpl| for making requests to GCD server.
36    virtual scoped_ptr<GCDApiFlow> CreateApiFlow() = 0;
37
38    // Requests WiFi credentials.
39    virtual void GetWiFiCredentials(const CredentialsCallback& callback) = 0;
40
41    // Switches to setup WiFi network.
42    // If switch was successfully |RestoreWifi| should be called later.
43    virtual void SwitchToSetupWiFi(const ResultCallback& callback) = 0;
44
45    // Starts device resolution that should callback with ready
46    // |PrivetV3HTTPClient|.
47    virtual void CreatePrivetV3Client(const std::string& service_name,
48                                      const PrivetClientCallback& callback) = 0;
49
50    // Requests client to prompt user to check |confirmation_code|.
51    virtual void ConfirmSecurityCode(const std::string& confirmation_code,
52                                     const ResultCallback& callback) = 0;
53
54    // Restores WiFi network.
55    virtual void RestoreWifi(const ResultCallback& callback) = 0;
56
57    // Notifies client that device is set up.
58    virtual void OnSetupDone() = 0;
59
60    // Notifies client setup failed.
61    virtual void OnSetupError() = 0;
62  };
63
64  explicit PrivetV3SetupFlow(Delegate* delegate);
65  virtual ~PrivetV3SetupFlow();
66
67  // Starts registration.
68  void Register(const std::string& service_name);
69
70#if defined(ENABLE_WIFI_BOOTSTRAPPING)
71  void SetupWifiAndRegister(const std::string& device_ssid);
72#endif  // ENABLE_WIFI_BOOTSTRAPPING
73
74  // PrivetV3Session::Delegate implementation.
75  virtual void OnSetupConfirmationNeeded(
76      const std::string& confirmation_code) OVERRIDE;
77  virtual void OnSessionEstablished() OVERRIDE;
78  virtual void OnCannotEstablishSession() OVERRIDE;
79
80  void OnSetupError();
81  void OnDeviceRegistered();
82
83 private:
84  void OnTicketCreated(const std::string& ticket_id);
85  void OnPrivetClientCreated(scoped_ptr<PrivetHTTPClient> privet_http_client);
86  void OnCodeConfirmed(bool success);
87
88  Delegate* delegate_;
89  std::string service_name_;
90  scoped_ptr<GCDApiFlow> ticket_request_;
91  scoped_ptr<PrivetV3Session> session_;
92  scoped_ptr<PrivetV3Session::Request> setup_request_;
93  base::WeakPtrFactory<PrivetV3SetupFlow> weak_ptr_factory_;
94
95  DISALLOW_COPY_AND_ASSIGN(PrivetV3SetupFlow);
96};
97
98}  // namespace local_discovery
99
100#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SETUP_FLOW_H_
101