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 #ifndef CHROMEOS_COMPONENTS_TETHER_MESSAGE_TRANSFER_OPERATION_H_ | 5 #ifndef CHROMEOS_COMPONENTS_TETHER_MESSAGE_TRANSFER_OPERATION_H_ |
6 #define CHROMEOS_COMPONENTS_TETHER_MESSAGE_TRANSFER_OPERATION_H_ | 6 #define CHROMEOS_COMPONENTS_TETHER_MESSAGE_TRANSFER_OPERATION_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/timer/timer.h" |
12 #include "chromeos/components/tether/ble_connection_manager.h" | 13 #include "chromeos/components/tether/ble_connection_manager.h" |
13 | 14 |
14 namespace chromeos { | 15 namespace chromeos { |
15 | 16 |
16 namespace tether { | 17 namespace tether { |
17 | 18 |
18 class MessageWrapper; | 19 class MessageWrapper; |
| 20 class TimerFactory; |
19 | 21 |
20 // Abstract base class used for operations which send and/or receive messages | 22 // Abstract base class used for operations which send and/or receive messages |
21 // from remote devices. | 23 // from remote devices. |
22 class MessageTransferOperation : public BleConnectionManager::Observer { | 24 class MessageTransferOperation : public BleConnectionManager::Observer { |
23 public: | 25 public: |
24 MessageTransferOperation( | 26 MessageTransferOperation( |
25 const std::vector<cryptauth::RemoteDevice>& devices_to_connect, | 27 const std::vector<cryptauth::RemoteDevice>& devices_to_connect, |
26 BleConnectionManager* connection_manager); | 28 BleConnectionManager* connection_manager); |
27 virtual ~MessageTransferOperation(); | 29 virtual ~MessageTransferOperation(); |
28 | 30 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // Callback executed when the operation has started (i.e., in Initialize()). | 65 // Callback executed when the operation has started (i.e., in Initialize()). |
64 virtual void OnOperationStarted() {} | 66 virtual void OnOperationStarted() {} |
65 | 67 |
66 // Callback executed when the operation has finished (i.e., when all devices | 68 // Callback executed when the operation has finished (i.e., when all devices |
67 // have been unregistered). | 69 // have been unregistered). |
68 virtual void OnOperationFinished() {} | 70 virtual void OnOperationFinished() {} |
69 | 71 |
70 // Returns the type of message that this operation intends to send. | 72 // Returns the type of message that this operation intends to send. |
71 virtual MessageType GetMessageTypeForConnection() = 0; | 73 virtual MessageType GetMessageTypeForConnection() = 0; |
72 | 74 |
| 75 // Returns true if we should wait for a response from host devices. Note |
| 76 // that if ShouldWaitForResponse() returns true and a response is not received |
| 77 // within the amount of time returned by GetResponseTimeoutSeconds() after a |
| 78 // host device authenticates, that host device will be unregistered. |
| 79 virtual bool ShouldWaitForResponse(); |
| 80 |
| 81 // The number of seconds that we should wait for a response from the host. If |
| 82 // ShouldWaitForResponse() returns false, this method is never used. |
| 83 virtual uint32_t GetResponseTimeoutSeconds(); |
| 84 |
73 std::vector<cryptauth::RemoteDevice>& remote_devices() { | 85 std::vector<cryptauth::RemoteDevice>& remote_devices() { |
74 return remote_devices_; | 86 return remote_devices_; |
75 } | 87 } |
76 | 88 |
77 private: | 89 private: |
78 friend class ConnectTetheringOperationTest; | 90 friend class ConnectTetheringOperationTest; |
79 friend class DisconnectTetheringOperationTest; | 91 friend class DisconnectTetheringOperationTest; |
80 friend class HostScannerOperationTest; | 92 friend class HostScannerOperationTest; |
81 friend class MessageTransferOperationTest; | 93 friend class MessageTransferOperationTest; |
82 | 94 |
83 static uint32_t kMaxConnectionAttemptsPerDevice; | 95 static uint32_t kMaxConnectionAttemptsPerDevice; |
84 | 96 |
| 97 // The default number of seconds the client should generally wait for a |
| 98 // response from the host once an authenticated connection is established. |
| 99 // Once this amount of time passes, the connection will be closed. Subclasses |
| 100 // of MessageTransferOperation should override GetResponseTimeoutSeconds() |
| 101 // if they desire a different duration. |
| 102 static uint32_t kDefaultResponseTimeoutSeconds; |
| 103 |
| 104 void SetTimerFactoryForTest( |
| 105 std::unique_ptr<TimerFactory> timer_factory_for_test); |
| 106 void StartResponseTimerForDevice( |
| 107 const cryptauth::RemoteDevice& remote_device); |
| 108 void StopResponseTimerForDeviceIfRunning( |
| 109 const cryptauth::RemoteDevice& remote_device); |
| 110 void OnResponseTimeout(const cryptauth::RemoteDevice& remote_device); |
| 111 |
85 std::vector<cryptauth::RemoteDevice> remote_devices_; | 112 std::vector<cryptauth::RemoteDevice> remote_devices_; |
86 BleConnectionManager* connection_manager_; | 113 BleConnectionManager* connection_manager_; |
| 114 std::unique_ptr<TimerFactory> timer_factory_; |
87 | 115 |
88 bool initialized_; | 116 bool initialized_; |
89 std::map<cryptauth::RemoteDevice, uint32_t> | 117 std::map<cryptauth::RemoteDevice, uint32_t> |
90 remote_device_to_num_attempts_map_; | 118 remote_device_to_num_attempts_map_; |
| 119 std::map<cryptauth::RemoteDevice, std::unique_ptr<base::Timer>> |
| 120 remote_device_to_timer_map_; |
| 121 base::WeakPtrFactory<MessageTransferOperation> weak_ptr_factory_; |
91 | 122 |
92 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperation); | 123 DISALLOW_COPY_AND_ASSIGN(MessageTransferOperation); |
93 }; | 124 }; |
94 | 125 |
95 } // namespace tether | 126 } // namespace tether |
96 | 127 |
97 } // namespace chromeos | 128 } // namespace chromeos |
98 | 129 |
99 #endif // CHROMEOS_COMPONENTS_TETHER_MESSAGE_TRANSFER_OPERATION_H_ | 130 #endif // CHROMEOS_COMPONENTS_TETHER_MESSAGE_TRANSFER_OPERATION_H_ |
OLD | NEW |