| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/components/tether/tether_host_fetcher.h" | 5 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "components/cryptauth/cryptauth_device_manager.h" | 9 #include "components/cryptauth/cryptauth_device_manager.h" |
| 10 #include "components/cryptauth/cryptauth_enrollment_manager.h" |
| 10 #include "components/cryptauth/cryptauth_service.h" | 11 #include "components/cryptauth/cryptauth_service.h" |
| 11 #include "components/cryptauth/remote_device_loader.h" | 12 #include "components/cryptauth/remote_device_loader.h" |
| 12 #include "components/cryptauth/secure_message_delegate.h" | 13 #include "components/cryptauth/secure_message_delegate.h" |
| 13 | 14 |
| 14 namespace chromeos { | 15 namespace chromeos { |
| 15 | 16 |
| 16 namespace tether { | 17 namespace tether { |
| 17 | 18 |
| 18 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( | 19 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( |
| 19 const TetherHostListCallback& list_callback) | 20 const TetherHostListCallback& list_callback) |
| 20 : device_id(""), list_callback(list_callback) {} | 21 : device_id(""), list_callback(list_callback) {} |
| 21 | 22 |
| 22 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( | 23 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( |
| 23 const std::string& device_id, | 24 const std::string& device_id, |
| 24 const TetherHostCallback& single_callback) | 25 const TetherHostCallback& single_callback) |
| 25 : device_id(device_id), single_callback(single_callback) {} | 26 : device_id(device_id), single_callback(single_callback) {} |
| 26 | 27 |
| 27 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( | 28 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( |
| 28 const TetherHostFetchRequest& other) | 29 const TetherHostFetchRequest& other) |
| 29 : device_id(other.device_id), | 30 : device_id(other.device_id), |
| 30 list_callback(other.list_callback), | 31 list_callback(other.list_callback), |
| 31 single_callback(other.single_callback) {} | 32 single_callback(other.single_callback) {} |
| 32 | 33 |
| 33 TetherHostFetcher::TetherHostFetchRequest::~TetherHostFetchRequest() {} | 34 TetherHostFetcher::TetherHostFetchRequest::~TetherHostFetchRequest() {} |
| 34 | 35 |
| 35 TetherHostFetcher::TetherHostFetcher( | 36 TetherHostFetcher::TetherHostFetcher( |
| 36 const std::string& user_id, | 37 cryptauth::CryptAuthService* cryptauth_service) |
| 37 const std::string& user_private_key, | 38 : cryptauth_service_(cryptauth_service), weak_ptr_factory_(this) {} |
| 38 cryptauth::CryptAuthService* cryptauth_service, | |
| 39 cryptauth::CryptAuthDeviceManager* device_manager) | |
| 40 : user_id_(user_id), | |
| 41 user_private_key_(user_private_key), | |
| 42 cryptauth_service_(cryptauth_service), | |
| 43 device_manager_(device_manager), | |
| 44 weak_ptr_factory_(this) {} | |
| 45 | 39 |
| 46 TetherHostFetcher::~TetherHostFetcher() {} | 40 TetherHostFetcher::~TetherHostFetcher() {} |
| 47 | 41 |
| 48 void TetherHostFetcher::FetchAllTetherHosts( | 42 void TetherHostFetcher::FetchAllTetherHosts( |
| 49 const TetherHostListCallback& callback) { | 43 const TetherHostListCallback& callback) { |
| 50 requests_.push_back(TetherHostFetchRequest(callback)); | 44 requests_.push_back(TetherHostFetchRequest(callback)); |
| 51 StartLoadingDevicesIfNeeded(); | 45 StartLoadingDevicesIfNeeded(); |
| 52 } | 46 } |
| 53 | 47 |
| 54 void TetherHostFetcher::FetchTetherHost(const std::string& device_id, | 48 void TetherHostFetcher::FetchTetherHost(const std::string& device_id, |
| 55 const TetherHostCallback& callback) { | 49 const TetherHostCallback& callback) { |
| 56 requests_.push_back(TetherHostFetchRequest(device_id, callback)); | 50 requests_.push_back(TetherHostFetchRequest(device_id, callback)); |
| 57 StartLoadingDevicesIfNeeded(); | 51 StartLoadingDevicesIfNeeded(); |
| 58 } | 52 } |
| 59 | 53 |
| 60 void TetherHostFetcher::StartLoadingDevicesIfNeeded() { | 54 void TetherHostFetcher::StartLoadingDevicesIfNeeded() { |
| 61 if (remote_device_loader_) { | 55 if (remote_device_loader_) { |
| 62 // If the loader is already active, there is nothing to do. | 56 // If the loader is already active, there is nothing to do. |
| 63 return; | 57 return; |
| 64 } | 58 } |
| 65 | 59 |
| 66 remote_device_loader_ = cryptauth::RemoteDeviceLoader::Factory::NewInstance( | 60 remote_device_loader_ = cryptauth::RemoteDeviceLoader::Factory::NewInstance( |
| 67 device_manager_->GetTetherHosts(), user_id_, user_private_key_, | 61 cryptauth_service_->GetCryptAuthDeviceManager()->GetTetherHosts(), |
| 62 cryptauth_service_->GetAccountId(), |
| 63 cryptauth_service_->GetCryptAuthEnrollmentManager()->GetUserPrivateKey(), |
| 68 cryptauth_service_->CreateSecureMessageDelegate()); | 64 cryptauth_service_->CreateSecureMessageDelegate()); |
| 69 remote_device_loader_->Load( | 65 remote_device_loader_->Load( |
| 70 base::Bind(&TetherHostFetcher::OnRemoteDevicesLoaded, | 66 base::Bind(&TetherHostFetcher::OnRemoteDevicesLoaded, |
| 71 weak_ptr_factory_.GetWeakPtr())); | 67 weak_ptr_factory_.GetWeakPtr())); |
| 72 } | 68 } |
| 73 | 69 |
| 74 void TetherHostFetcher::OnRemoteDevicesLoaded( | 70 void TetherHostFetcher::OnRemoteDevicesLoaded( |
| 75 const cryptauth::RemoteDeviceList& remote_devices) { | 71 const cryptauth::RemoteDeviceList& remote_devices) { |
| 76 // Make a copy of the list before deleting |remote_device_loader_|. | 72 // Make a copy of the list before deleting |remote_device_loader_|. |
| 77 cryptauth::RemoteDeviceList remote_devices_copy = remote_devices; | 73 cryptauth::RemoteDeviceList remote_devices_copy = remote_devices; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 103 | 99 |
| 104 if (!has_run_callback) { | 100 if (!has_run_callback) { |
| 105 request.single_callback.Run(nullptr); | 101 request.single_callback.Run(nullptr); |
| 106 } | 102 } |
| 107 } | 103 } |
| 108 } | 104 } |
| 109 | 105 |
| 110 } // namespace tether | 106 } // namespace tether |
| 111 | 107 |
| 112 } // namespace chromeos | 108 } // namespace chromeos |
| OLD | NEW |