Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(306)

Side by Side Diff: chromeos/components/tether/host_scan_cache.h

Issue 2852693004: [CrOS Tether] Create HostScanCache, which caches scan results and inserts them into the network sta… (Closed)
Patch Set: Rebased. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 private:
80 friend class HostScanCacheTest;
81
82 class TimerFactory {
83 public:
84 // Creates a timer which will be used to determine if the cache contains
85 // stale data for the Tether network with GUID |tether_network_guid|.
stevenjb 2017/05/01 18:26:11 Why does the timer vary based on |tether_network_g
Kyle Horimoto 2017/05/01 21:14:43 Removed this.
86 virtual std::unique_ptr<base::Timer> CreateOneShotTimer(
87 const std::string& tether_network_guid) = 0;
88 };
89
90 class TimerFactoryImpl : public TimerFactory {
91 public:
92 TimerFactoryImpl();
93 ~TimerFactoryImpl();
94
95 // TimerFactory:
96 std::unique_ptr<base::Timer> CreateOneShotTimer(
97 const std::string& tether_network_guid) override;
98 };
stevenjb 2017/05/01 18:26:11 Does this need to be a class member?
Kyle Horimoto 2017/05/01 21:14:43 Yes; otherwise, we would not be able to inject Moc
stevenjb 2017/05/01 22:13:01 TimerFactory needs to be public, but TimerFactoryI
Kyle Horimoto 2017/05/02 00:02:18 Done.
99
100 HostScanCache(
101 std::unique_ptr<TimerFactory> timer_factory,
102 NetworkStateHandler* network_state_handler,
103 ActiveHost* active_host,
104 TetherHostResponseRecorder* tether_host_response_recorder,
105 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map);
106
107 bool HasConnectedToHost(const std::string& tether_network_guid);
108 void StartTimer(const std::string& tether_network_guid);
109 void OnTimerFired(const std::string& tether_network_guid);
110
111 std::unique_ptr<TimerFactory> timer_factory_;
112 NetworkStateHandler* network_state_handler_;
113 ActiveHost* active_host_;
114 TetherHostResponseRecorder* tether_host_response_recorder_;
115 DeviceIdTetherNetworkGuidMap* device_id_tether_network_guid_map_;
116
117 // Maps from the Tether network GUID to a Timer object. While a scan result is
118 // active in the cache, the corresponding Timer object starts running; if the
119 // timer fires, the result is removed (unless it corresponds to the active
120 // host).
121 std::unordered_map<std::string, std::unique_ptr<base::Timer>>
122 tether_guid_to_timer_map_;
123 base::WeakPtrFactory<HostScanCache> weak_ptr_factory_;
124
125 DISALLOW_COPY_AND_ASSIGN(HostScanCache);
126 };
127
128 } // namespace tether
129
130 } // namespace chromeos
131
132 #endif // CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_CACHE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698