OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_CACHE_H_ | |
6 #define CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_CACHE_H_ | |
7 | |
8 #include <memory> | |
9 #include <unordered_map> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/timer/timer.h" | |
14 #include "chromeos/components/tether/tether_host_response_recorder.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 class NetworkStateHandler; | |
19 | |
20 namespace tether { | |
21 | |
22 class ActiveHost; | |
23 class DeviceIdTetherNetworkGuidMap; | |
24 class TetherHostResponseRecorder; | |
25 | |
26 // Caches scan results and inserts them into the network stack. | |
27 class HostScanCache : public TetherHostResponseRecorder::Observer { | |
28 public: | |
29 // The number of minutes that a cache entry is considered to be valid before | |
30 // it becomes stale. Once a cache entry is inserted, it will be automatically | |
31 // removed after this amount of time passes unless it corresponds to the | |
32 // active host. This value was chosen for two reasons: | |
33 // (1) Tether properties such as battery percentage and signal strength | |
34 // are ephemeral in nature, so keeping these values cached for more | |
35 // than a short period makes it likely that the values will be wrong. | |
36 // (2) Tether networks rely on proximity to a tether host device, so it is | |
37 // possible that host devices have physically moved away from each | |
38 // other. We assume that the devices do not stay in proximity to one | |
39 // another until a new scan result is received which proves that they | |
40 // are still within the distance needed to communicate. | |
41 static constexpr int kNumMinutesBeforeCacheEntryExpires = 5; | |
42 | |
43 HostScanCache( | |
44 NetworkStateHandler* network_state_handler, | |
45 ActiveHost* active_host, | |
46 TetherHostResponseRecorder* tether_host_response_recorder, | |
47 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map); | |
48 virtual ~HostScanCache(); | |
49 | |
50 // Updates the cache to include this scan result. If no scan result for | |
51 // |tether_network_guid| exists in the cache, a scan result will be added; | |
52 // if a scan result is already present, it is updated with the new data | |
53 // provided as parameters to this function. Once scan results have been | |
54 // updated, a timer starts counting down |kNumMinutesBeforeCacheEntryExpires| | |
55 // minutes. Once the timer fires, the scan result is automatically removed | |
56 // from the cache unless it corresponds to the active host. | |
57 // Note: |signal_strength| should be in the range [0, 100]. This is different | |
58 // from the |connection_strength| field received in ConnectTetheringResponse | |
59 // and KeepAliveTickleResponse messages (the range is [0, 4] in those cases). | |
60 virtual void SetHostScanResult(const std::string& tether_network_guid, | |
61 const std::string& device_name, | |
62 const std::string& carrier, | |
63 int battery_percentage, | |
64 int signal_strength); | |
65 | |
66 // Removes the scan result with GUID |tether_network_guid| from the cache. If | |
67 // no cache result with that GUID was present in the cache, this function is | |
68 // a no-op. Returns whether a scan result was actually removed. | |
69 virtual bool RemoveHostScanResult(const std::string& tether_network_guid); | |
70 | |
71 // Removes all scan results from the cache unless they correspond to the | |
72 // active host; the active host must always remain in the cache while | |
73 // connecting/connected to ensure the UI is up to date. | |
74 virtual void ClearCacheExceptForActiveHost(); | |
75 | |
76 // TetherHostResponseRecorder::Observer: | |
77 void OnPreviouslyConnectedHostIdsChanged() override; | |
78 | |
79 class TimerFactory { | |
80 public: | |
81 virtual std::unique_ptr<base::Timer> CreateOneShotTimer(); | |
82 }; | |
83 | |
84 private: | |
85 friend class HostScanCacheTest; | |
86 | |
87 void SetTimerFactoryForTest( | |
88 std::unique_ptr<TimerFactory> timer_factory_for_test); | |
89 | |
90 bool HasConnectedToHost(const std::string& tether_network_guid); | |
91 void StartTimer(const std::string& tether_network_guid); | |
92 void OnTimerFired(const std::string& tether_network_guid); | |
93 | |
94 std::unique_ptr<TimerFactory> timer_factory_; | |
95 NetworkStateHandler* network_state_handler_; | |
96 ActiveHost* active_host_; | |
97 TetherHostResponseRecorder* tether_host_response_recorder_; | |
98 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map_; | |
99 | |
100 // Maps from the Tether network GUID to a Timer object. While a scan result is | |
101 // active in the cache, the corresponding Timer object starts running; if the | |
102 // timer fires, the result is removed (unless it corresponds to the active | |
103 // host). | |
104 std::unordered_map<std::string, std::unique_ptr<base::Timer>> | |
105 tether_guid_to_timer_map_; | |
106 base::WeakPtrFactory<HostScanCache> weak_ptr_factory_; | |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(HostScanCache); | |
109 }; | |
110 | |
111 } // namespace tether | |
112 | |
113 } // namespace chromeos | |
114 | |
115 #endif // CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_CACHE_H_ | |
OLD | NEW |