1 // Copyright 2015 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.
5 #include "components/proximity_auth/webui/reachable_phone_flow.h"
10 #include "base/location.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "components/proximity_auth/cryptauth/cryptauth_client.h"
14 #include "components/proximity_auth/cryptauth/proto/cryptauth_api.pb.h"
15 #include "components/proximity_auth/logging/logging.h"
17 namespace proximity_auth
{
21 // The time, in milliseconds, to wait for phones to respond to the CryptAuth
22 // ping before querying for reachable devices.
23 const int kWaitTimeMillis
= 7000;
27 ReachablePhoneFlow::ReachablePhoneFlow(CryptAuthClientFactory
* client_factory
)
28 : client_factory_(client_factory
), weak_ptr_factory_(this) {}
30 ReachablePhoneFlow::~ReachablePhoneFlow() {}
32 void ReachablePhoneFlow::Run(const ReachablePhonesCallback
& callback
) {
33 if (!callback_
.is_null()) {
34 PA_LOG(ERROR
) << "Flow already started.";
35 callback
.Run(std::vector
<cryptauth::ExternalDeviceInfo
>());
40 client_
= client_factory_
->CreateInstance();
42 // Ping the user's devices to update themselves with CryptAuth.
43 cryptauth::SendDeviceSyncTickleRequest tickle_request
;
44 tickle_request
.set_tickle_type(cryptauth::UPDATE_ENROLLMENT
);
45 client_
->SendDeviceSyncTickle(
46 tickle_request
, base::Bind(&ReachablePhoneFlow::OnSyncTickleSuccess
,
47 weak_ptr_factory_
.GetWeakPtr()),
48 base::Bind(&ReachablePhoneFlow::OnApiCallError
,
49 weak_ptr_factory_
.GetWeakPtr()));
52 void ReachablePhoneFlow::OnSyncTickleSuccess(
53 const cryptauth::SendDeviceSyncTickleResponse
& response
) {
54 PA_LOG(INFO
) << "Waiting " << kWaitTimeMillis
55 << "ms for phones to callback to CryptAuth...";
56 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
57 FROM_HERE
, base::Bind(&ReachablePhoneFlow::QueryReachablePhones
,
58 weak_ptr_factory_
.GetWeakPtr()),
59 base::TimeDelta::FromMilliseconds(kWaitTimeMillis
));
62 void ReachablePhoneFlow::QueryReachablePhones() {
63 // Ask CryptAuth for the devices that updated themselves within the last
64 // |kWaitTimeMillis| milliseconds.
65 client_
= client_factory_
->CreateInstance();
66 cryptauth::FindEligibleUnlockDevicesRequest find_devices_request
;
67 find_devices_request
.set_max_last_update_time_delta_millis(kWaitTimeMillis
);
68 client_
->FindEligibleUnlockDevices(
70 base::Bind(&ReachablePhoneFlow::OnFindEligibleUnlockDevicesSuccess
,
71 weak_ptr_factory_
.GetWeakPtr()),
72 base::Bind(&ReachablePhoneFlow::OnApiCallError
,
73 weak_ptr_factory_
.GetWeakPtr()));
76 void ReachablePhoneFlow::OnFindEligibleUnlockDevicesSuccess(
77 const cryptauth::FindEligibleUnlockDevicesResponse
& response
) {
78 PA_LOG(INFO
) << "Found " << response
.eligible_devices_size()
79 << " reachable phone(s).";
80 std::vector
<cryptauth::ExternalDeviceInfo
> reachable_phones(
81 response
.eligible_devices_size());
82 std::copy(response
.eligible_devices().begin(),
83 response
.eligible_devices().end(), reachable_phones
.begin());
84 callback_
.Run(reachable_phones
);
87 void ReachablePhoneFlow::OnApiCallError(const std::string
& error
) {
88 PA_LOG(ERROR
) << "Error making api call: " << error
;
89 callback_
.Run(std::vector
<cryptauth::ExternalDeviceInfo
>());
92 } // namespace proximity_auth