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 #include "chromeos/components/tether/fake_host_scan_cache.h" | |
6 | |
7 #include "chromeos/components/tether/tether_host_response_recorder.h" | |
8 | |
9 namespace chromeos { | |
10 | |
11 namespace tether { | |
12 | |
13 namespace { | |
14 | |
15 // Shared among all FakeHostScanCache instances. This pointer is not used for | |
16 // anything, but HostScanCache's constructor calls AddObserver() on the | |
17 // TetherHostResponseRecorder* passed to its constructor, so a valid object must | |
18 // be passed to the constructor to prevent a segfault. | |
19 TetherHostResponseRecorder* kTetherHostResponseRecorder = nullptr; | |
20 | |
21 TetherHostResponseRecorder* GetTetherHostResponseRecorder() { | |
22 if (kTetherHostResponseRecorder) | |
23 return kTetherHostResponseRecorder; | |
24 | |
25 kTetherHostResponseRecorder = | |
26 new TetherHostResponseRecorder(nullptr /* pref_service */); | |
27 return kTetherHostResponseRecorder; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 FakeHostScanCache::FakeHostScanCache() | |
33 : HostScanCache(nullptr /* network_state_handler */, | |
34 nullptr /* active_host */, | |
35 GetTetherHostResponseRecorder(), | |
36 nullptr /* device_id_tether_network_guid_map */) {} | |
37 | |
38 FakeHostScanCache::~FakeHostScanCache() {} | |
39 | |
40 const FakeHostScanCache::CacheEntry* FakeHostScanCache::GetCacheEntry( | |
41 const std::string& tether_network_guid) { | |
42 auto it = cache_.find(tether_network_guid); | |
43 if (it == cache_.end()) | |
44 return nullptr; | |
45 | |
46 return &it->second; | |
47 } | |
48 | |
49 void FakeHostScanCache::SetHostScanResult( | |
50 const std::string& tether_network_guid, | |
51 const std::string& device_name, | |
52 const std::string& carrier, | |
53 int battery_percentage, | |
54 int signal_strength) { | |
55 auto it = cache_.find(tether_network_guid); | |
56 if (it != cache_.end()) { | |
57 // If already in the cache, update the cache with new values. | |
58 it->second.device_name = device_name; | |
59 it->second.carrier = carrier; | |
60 it->second.battery_percentage = battery_percentage; | |
61 it->second.signal_strength = signal_strength; | |
62 return; | |
63 } | |
64 | |
65 // Otherwise, add a new entry. | |
66 cache_.emplace( | |
67 tether_network_guid, | |
68 CacheEntry{device_name, carrier, battery_percentage, signal_strength}); | |
69 } | |
70 | |
71 bool FakeHostScanCache::RemoveHostScanResult( | |
72 const std::string& tether_network_guid) { | |
73 if (tether_network_guid == active_host_tether_network_guid()) | |
74 return false; | |
75 | |
76 return cache_.erase(tether_network_guid) > 0; | |
77 } | |
78 | |
79 void FakeHostScanCache::ClearCacheExceptForActiveHost() { | |
80 auto it = cache_.begin(); | |
81 while (it != cache_.end()) { | |
82 if (it->first == active_host_tether_network_guid_) | |
83 it++; | |
84 else | |
85 it = cache_.erase(it); | |
86 } | |
87 } | |
88 | |
89 void FakeHostScanCache::OnPreviouslyConnectedHostIdsChanged() {} | |
90 | |
91 } // namespace tether | |
92 | |
93 } // namespace chromeos | |
OLD | NEW |