summaryrefslogtreecommitdiffstats
path: root/chrome/browser/local_discovery/wifi/bootstrapping_network_switcher.cc
blob: f99adea6274538bb03e9bdd438c1b2a9a00dfb06 (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
// 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/wifi/bootstrapping_network_switcher.h"

#include "base/bind.h"
#include "components/onc/onc_constants.h"

namespace local_discovery {

namespace wifi {

BootstrappingNetworkSwitcher::BootstrappingNetworkSwitcher(
    WifiManager* wifi_manager,
    const std::string& connect_ssid,
    const SuccessCallback& callback)
    : wifi_manager_(wifi_manager),
      connect_ssid_(connect_ssid),
      callback_(callback),
      connected_(false),
      weak_factory_(this) {
}

BootstrappingNetworkSwitcher::~BootstrappingNetworkSwitcher() {
  Disconnect();
}

void BootstrappingNetworkSwitcher::Connect() {
  wifi_manager_->GetSSIDList(
      base::Bind(&BootstrappingNetworkSwitcher::OnGotNetworkList,
                 weak_factory_.GetWeakPtr()));
}

void BootstrappingNetworkSwitcher::Disconnect() {
  if (connected_ && !return_guid_.empty()) {
    connected_ = false;
    wifi_manager_->ConnectToNetworkByID(return_guid_,
                                        WifiManager::SuccessCallback());
  }
}

void BootstrappingNetworkSwitcher::OnGotNetworkList(
    const NetworkPropertiesList& networks) {
  for (NetworkPropertiesList::const_iterator i = networks.begin();
       i != networks.end();
       i++) {
    if (i->connection_state == onc::connection_state::kConnected) {
      return_guid_ = i->guid;
      break;
    }
  }

  // We are "connected" while attempting to connect even if we fail
  connected_ = true;

  // Bootstrapping devices should have an empty PSK.
  wifi_manager_->ConfigureAndConnectNetwork(
      connect_ssid_,
      WifiCredentials::FromPSK(std::string()),
      base::Bind(&BootstrappingNetworkSwitcher::OnConnectStatus,
                 weak_factory_.GetWeakPtr()));
}

void BootstrappingNetworkSwitcher::OnConnectStatus(bool status) {
  connected_ = status;
  callback_.Run(status);
}

}  // namespace wifi

}  // namespace local_discovery