| 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_service.h" |
| 10 #include "components/cryptauth/remote_device_loader.h" | 11 #include "components/cryptauth/remote_device_loader.h" |
| 11 #include "components/cryptauth/secure_message_delegate.h" | 12 #include "components/cryptauth/secure_message_delegate.h" |
| 12 | 13 |
| 13 namespace chromeos { | 14 namespace chromeos { |
| 14 | 15 |
| 15 namespace tether { | 16 namespace tether { |
| 16 | 17 |
| 17 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( | 18 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( |
| 18 const TetherHostListCallback& list_callback) | 19 const TetherHostListCallback& list_callback) |
| 19 : device_id(""), list_callback(list_callback) {} | 20 : device_id(""), list_callback(list_callback) {} |
| 20 | 21 |
| 21 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( | 22 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( |
| 22 const std::string& device_id, | 23 const std::string& device_id, |
| 23 const TetherHostCallback& single_callback) | 24 const TetherHostCallback& single_callback) |
| 24 : device_id(device_id), single_callback(single_callback) {} | 25 : device_id(device_id), single_callback(single_callback) {} |
| 25 | 26 |
| 26 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( | 27 TetherHostFetcher::TetherHostFetchRequest::TetherHostFetchRequest( |
| 27 const TetherHostFetchRequest& other) | 28 const TetherHostFetchRequest& other) |
| 28 : device_id(other.device_id), | 29 : device_id(other.device_id), |
| 29 list_callback(other.list_callback), | 30 list_callback(other.list_callback), |
| 30 single_callback(other.single_callback) {} | 31 single_callback(other.single_callback) {} |
| 31 | 32 |
| 32 TetherHostFetcher::TetherHostFetchRequest::~TetherHostFetchRequest() {} | 33 TetherHostFetcher::TetherHostFetchRequest::~TetherHostFetchRequest() {} |
| 33 | 34 |
| 34 TetherHostFetcher::TetherHostFetcher( | 35 TetherHostFetcher::TetherHostFetcher( |
| 35 const std::string& user_id, | 36 const std::string& user_id, |
| 36 const std::string& user_private_key, | 37 const std::string& user_private_key, |
| 37 std::unique_ptr<Delegate> delegate, | 38 cryptauth::CryptAuthService* cryptauth_service, |
| 38 cryptauth::CryptAuthDeviceManager* device_manager) | 39 cryptauth::CryptAuthDeviceManager* device_manager) |
| 39 : user_id_(user_id), | 40 : user_id_(user_id), |
| 40 user_private_key_(user_private_key), | 41 user_private_key_(user_private_key), |
| 41 delegate_(std::move(delegate)), | 42 cryptauth_service_(cryptauth_service), |
| 42 device_manager_(device_manager), | 43 device_manager_(device_manager), |
| 43 weak_ptr_factory_(this) {} | 44 weak_ptr_factory_(this) {} |
| 44 | 45 |
| 45 TetherHostFetcher::~TetherHostFetcher() {} | 46 TetherHostFetcher::~TetherHostFetcher() {} |
| 46 | 47 |
| 47 void TetherHostFetcher::FetchAllTetherHosts( | 48 void TetherHostFetcher::FetchAllTetherHosts( |
| 48 const TetherHostListCallback& callback) { | 49 const TetherHostListCallback& callback) { |
| 49 requests_.push_back(TetherHostFetchRequest(callback)); | 50 requests_.push_back(TetherHostFetchRequest(callback)); |
| 50 StartLoadingDevicesIfNeeded(); | 51 StartLoadingDevicesIfNeeded(); |
| 51 } | 52 } |
| 52 | 53 |
| 53 void TetherHostFetcher::FetchTetherHost(const std::string& device_id, | 54 void TetherHostFetcher::FetchTetherHost(const std::string& device_id, |
| 54 const TetherHostCallback& callback) { | 55 const TetherHostCallback& callback) { |
| 55 requests_.push_back(TetherHostFetchRequest(device_id, callback)); | 56 requests_.push_back(TetherHostFetchRequest(device_id, callback)); |
| 56 StartLoadingDevicesIfNeeded(); | 57 StartLoadingDevicesIfNeeded(); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void TetherHostFetcher::StartLoadingDevicesIfNeeded() { | 60 void TetherHostFetcher::StartLoadingDevicesIfNeeded() { |
| 60 if (remote_device_loader_) { | 61 if (remote_device_loader_) { |
| 61 // If the loader is already active, there is nothing to do. | 62 // If the loader is already active, there is nothing to do. |
| 62 return; | 63 return; |
| 63 } | 64 } |
| 64 | 65 |
| 65 remote_device_loader_ = cryptauth::RemoteDeviceLoader::Factory::NewInstance( | 66 remote_device_loader_ = cryptauth::RemoteDeviceLoader::Factory::NewInstance( |
| 66 device_manager_->GetTetherHosts(), user_id_, user_private_key_, | 67 device_manager_->GetTetherHosts(), user_id_, user_private_key_, |
| 67 delegate_->CreateSecureMessageDelegate()); | 68 cryptauth_service_->CreateSecureMessageDelegate()); |
| 68 remote_device_loader_->Load( | 69 remote_device_loader_->Load( |
| 69 base::Bind(&TetherHostFetcher::OnRemoteDevicesLoaded, | 70 base::Bind(&TetherHostFetcher::OnRemoteDevicesLoaded, |
| 70 weak_ptr_factory_.GetWeakPtr())); | 71 weak_ptr_factory_.GetWeakPtr())); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void TetherHostFetcher::OnRemoteDevicesLoaded( | 74 void TetherHostFetcher::OnRemoteDevicesLoaded( |
| 74 const cryptauth::RemoteDeviceList& remote_devices) { | 75 const cryptauth::RemoteDeviceList& remote_devices) { |
| 75 // Make a copy of the list before deleting |remote_device_loader_|. | 76 // Make a copy of the list before deleting |remote_device_loader_|. |
| 76 cryptauth::RemoteDeviceList remote_devices_copy = remote_devices; | 77 cryptauth::RemoteDeviceList remote_devices_copy = remote_devices; |
| 77 remote_device_loader_.reset(); | 78 remote_device_loader_.reset(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 102 | 103 |
| 103 if (!has_run_callback) { | 104 if (!has_run_callback) { |
| 104 request.single_callback.Run(nullptr); | 105 request.single_callback.Run(nullptr); |
| 105 } | 106 } |
| 106 } | 107 } |
| 107 } | 108 } |
| 108 | 109 |
| 109 } // namespace tether | 110 } // namespace tether |
| 110 | 111 |
| 111 } // namespace chromeos | 112 } // namespace chromeos |
| OLD | NEW |