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

Unified Diff: chromeos/components/tether/fake_host_scan_cache.cc

Issue 2852693004: [CrOS Tether] Create HostScanCache, which caches scan results and inserts them into the network sta… (Closed)
Patch Set: stevenjb@ comments. Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromeos/components/tether/fake_host_scan_cache.h ('k') | chromeos/components/tether/host_scan_cache.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromeos/components/tether/fake_host_scan_cache.cc
diff --git a/chromeos/components/tether/fake_host_scan_cache.cc b/chromeos/components/tether/fake_host_scan_cache.cc
new file mode 100644
index 0000000000000000000000000000000000000000..917a13a0f0a44528e8166c0a008a497102efa928
--- /dev/null
+++ b/chromeos/components/tether/fake_host_scan_cache.cc
@@ -0,0 +1,93 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chromeos/components/tether/fake_host_scan_cache.h"
+
+#include "chromeos/components/tether/tether_host_response_recorder.h"
+
+namespace chromeos {
+
+namespace tether {
+
+namespace {
+
+// Shared among all FakeHostScanCache instances. This pointer is not used for
+// anything, but HostScanCache's constructor calls AddObserver() on the
+// TetherHostResponseRecorder* passed to its constructor, so a valid object must
+// be passed to the constructor to prevent a segfault.
+TetherHostResponseRecorder* kTetherHostResponseRecorder = nullptr;
+
+TetherHostResponseRecorder* GetTetherHostResponseRecorder() {
+ if (kTetherHostResponseRecorder)
+ return kTetherHostResponseRecorder;
+
+ kTetherHostResponseRecorder =
+ new TetherHostResponseRecorder(nullptr /* pref_service */);
+ return kTetherHostResponseRecorder;
+}
+
+} // namespace
+
+FakeHostScanCache::FakeHostScanCache()
+ : HostScanCache(nullptr /* network_state_handler */,
+ nullptr /* active_host */,
+ GetTetherHostResponseRecorder(),
+ nullptr /* device_id_tether_network_guid_map */) {}
+
+FakeHostScanCache::~FakeHostScanCache() {}
+
+const FakeHostScanCache::CacheEntry* FakeHostScanCache::GetCacheEntry(
+ const std::string& tether_network_guid) {
+ auto it = cache_.find(tether_network_guid);
+ if (it == cache_.end())
+ return nullptr;
+
+ return &it->second;
+}
+
+void FakeHostScanCache::SetHostScanResult(
+ const std::string& tether_network_guid,
+ const std::string& device_name,
+ const std::string& carrier,
+ int battery_percentage,
+ int signal_strength) {
+ auto it = cache_.find(tether_network_guid);
+ if (it != cache_.end()) {
+ // If already in the cache, update the cache with new values.
+ it->second.device_name = device_name;
+ it->second.carrier = carrier;
+ it->second.battery_percentage = battery_percentage;
+ it->second.signal_strength = signal_strength;
+ return;
+ }
+
+ // Otherwise, add a new entry.
+ cache_.emplace(
+ tether_network_guid,
+ CacheEntry{device_name, carrier, battery_percentage, signal_strength});
+}
+
+bool FakeHostScanCache::RemoveHostScanResult(
+ const std::string& tether_network_guid) {
+ if (tether_network_guid == active_host_tether_network_guid())
+ return false;
+
+ return cache_.erase(tether_network_guid) > 0;
+}
+
+void FakeHostScanCache::ClearCacheExceptForActiveHost() {
+ auto it = cache_.begin();
+ while (it != cache_.end()) {
+ if (it->first == active_host_tether_network_guid_)
+ it++;
+ else
+ it = cache_.erase(it);
+ }
+}
+
+void FakeHostScanCache::OnPreviouslyConnectedHostIdsChanged() {}
+
+} // namespace tether
+
+} // namespace chromeos
« no previous file with comments | « chromeos/components/tether/fake_host_scan_cache.h ('k') | chromeos/components/tether/host_scan_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698