File: reachable_phone_flow.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (93 lines) | stat: -rw-r--r-- 3,589 bytes parent folder | download
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
// Copyright 2015 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 "components/proximity_auth/webui/reachable_phone_flow.h"

#include <algorithm>

#include "base/bind.h"
#include "base/location.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "components/cryptauth/cryptauth_client.h"
#include "components/cryptauth/proto/cryptauth_api.pb.h"
#include "components/proximity_auth/logging/logging.h"

namespace proximity_auth {

namespace {

// The time, in milliseconds, to wait for phones to respond to the CryptAuth
// ping before querying for reachable devices.
const int kWaitTimeMillis = 7000;

}  // namespace

ReachablePhoneFlow::ReachablePhoneFlow(
    cryptauth::CryptAuthClientFactory* client_factory)
    : client_factory_(client_factory), weak_ptr_factory_(this) {}

ReachablePhoneFlow::~ReachablePhoneFlow() {}

void ReachablePhoneFlow::Run(const ReachablePhonesCallback& callback) {
  if (!callback_.is_null()) {
    PA_LOG(ERROR) << "Flow already started.";
    callback.Run(std::vector<cryptauth::ExternalDeviceInfo>());
    return;
  }

  callback_ = callback;
  client_ = client_factory_->CreateInstance();

  // Ping the user's devices to update themselves with CryptAuth.
  cryptauth::SendDeviceSyncTickleRequest tickle_request;
  tickle_request.set_tickle_type(cryptauth::UPDATE_ENROLLMENT);
  client_->SendDeviceSyncTickle(
      tickle_request, base::Bind(&ReachablePhoneFlow::OnSyncTickleSuccess,
                                 weak_ptr_factory_.GetWeakPtr()),
      base::Bind(&ReachablePhoneFlow::OnApiCallError,
                 weak_ptr_factory_.GetWeakPtr()));
}

void ReachablePhoneFlow::OnSyncTickleSuccess(
    const cryptauth::SendDeviceSyncTickleResponse& response) {
  PA_LOG(INFO) << "Waiting " << kWaitTimeMillis
               << "ms for phones to callback to CryptAuth...";
  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE, base::Bind(&ReachablePhoneFlow::QueryReachablePhones,
                            weak_ptr_factory_.GetWeakPtr()),
      base::TimeDelta::FromMilliseconds(kWaitTimeMillis));
}

void ReachablePhoneFlow::QueryReachablePhones() {
  // Ask CryptAuth for the devices that updated themselves within the last
  // |kWaitTimeMillis| milliseconds.
  client_ = client_factory_->CreateInstance();
  cryptauth::FindEligibleUnlockDevicesRequest find_devices_request;
  find_devices_request.set_max_last_update_time_delta_millis(kWaitTimeMillis);
  client_->FindEligibleUnlockDevices(
      find_devices_request,
      base::Bind(&ReachablePhoneFlow::OnFindEligibleUnlockDevicesSuccess,
                 weak_ptr_factory_.GetWeakPtr()),
      base::Bind(&ReachablePhoneFlow::OnApiCallError,
                 weak_ptr_factory_.GetWeakPtr()));
}

void ReachablePhoneFlow::OnFindEligibleUnlockDevicesSuccess(
    const cryptauth::FindEligibleUnlockDevicesResponse& response) {
  PA_LOG(INFO) << "Found " << response.eligible_devices_size()
               << " reachable phone(s).";
  std::vector<cryptauth::ExternalDeviceInfo> reachable_phones(
      response.eligible_devices_size());
  std::copy(response.eligible_devices().begin(),
            response.eligible_devices().end(), reachable_phones.begin());
  callback_.Run(reachable_phones);
}

void ReachablePhoneFlow::OnApiCallError(const std::string& error) {
  PA_LOG(ERROR) << "Error making api call: " << error;
  callback_.Run(std::vector<cryptauth::ExternalDeviceInfo>());
}

}  // namespace proximity_auth