Home | History | Annotate | Download | only in local_discovery
      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 
     16 namespace local_discovery {
     17 
     18 // Provides complete flow for Privet v3 device setup.
     19 class 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,
     77       extensions::api::gcd_private::ConfirmationType confirmation_type)
     78       OVERRIDE;
     79   virtual void OnSessionStatus(
     80       extensions::api::gcd_private::Status status) OVERRIDE;
     81 
     82   void OnSetupError();
     83   void OnDeviceRegistered();
     84 
     85   const std::string& service_name() const { return service_name_; }
     86 
     87  private:
     88   void OnTicketCreated(const std::string& ticket_id,
     89                        const std::string& device_id);
     90   void OnPrivetClientCreated(scoped_ptr<PrivetHTTPClient> privet_http_client);
     91   void OnCodeConfirmed(const std::string& code, bool success);
     92 
     93   Delegate* delegate_;
     94   std::string service_name_;
     95   std::string device_id_;
     96   scoped_ptr<GCDApiFlow> ticket_request_;
     97   scoped_ptr<PrivetV3Session> session_;
     98   scoped_ptr<PrivetV3Session::Request> setup_request_;
     99   base::WeakPtrFactory<PrivetV3SetupFlow> weak_ptr_factory_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(PrivetV3SetupFlow);
    102 };
    103 
    104 }  // namespace local_discovery
    105 
    106 #endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVETV3_SETUP_FLOW_H_
    107