OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 NET_NQE_NETWORK_QUALITY_STORE_H_ | 5 #ifndef NET_NQE_NETWORK_QUALITY_STORE_H_ |
6 #define NET_NQE_NETWORK_QUALITY_STORE_H_ | 6 #define NET_NQE_NETWORK_QUALITY_STORE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/observer_list.h" |
11 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
12 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
13 #include "net/nqe/cached_network_quality.h" | 14 #include "net/nqe/cached_network_quality.h" |
| 15 #include "net/nqe/effective_connection_type.h" |
14 #include "net/nqe/network_id.h" | 16 #include "net/nqe/network_id.h" |
15 | 17 |
16 namespace net { | 18 namespace net { |
17 | 19 |
18 namespace nqe { | 20 namespace nqe { |
19 | 21 |
20 namespace internal { | 22 namespace internal { |
21 | 23 |
22 // NetworkQualityStore holds the network qualities of different networks in | 24 // NetworkQualityStore holds the network qualities of different networks in |
23 // memory. Entries are stored in LRU order, and older entries may be evicted. | 25 // memory. Entries are stored in LRU order, and older entries may be evicted. |
24 class NET_EXPORT_PRIVATE NetworkQualityStore { | 26 class NET_EXPORT_PRIVATE NetworkQualityStore { |
25 public: | 27 public: |
| 28 // Observes changes in the cached network qualities. |
| 29 class NET_EXPORT NetworkQualitiesCacheObserver { |
| 30 public: |
| 31 // Notifies the observer of a change in the cached network quality. The |
| 32 // observer must register and unregister itself on the IO thread. All the |
| 33 // observers would be notified on the IO thread. |network_id| is the ID of |
| 34 // the network whose cached quality is being reported. |
| 35 virtual void OnChangeInCachedNetworkQuality( |
| 36 const nqe::internal::NetworkID& network_id, |
| 37 const nqe::internal::CachedNetworkQuality& cached_network_quality) = 0; |
| 38 |
| 39 protected: |
| 40 NetworkQualitiesCacheObserver() {} |
| 41 virtual ~NetworkQualitiesCacheObserver() {} |
| 42 |
| 43 private: |
| 44 DISALLOW_COPY_AND_ASSIGN(NetworkQualitiesCacheObserver); |
| 45 }; |
| 46 |
26 NetworkQualityStore(); | 47 NetworkQualityStore(); |
27 ~NetworkQualityStore(); | 48 ~NetworkQualityStore(); |
28 | 49 |
29 // Stores the network quality |cached_network_quality| of network with ID | 50 // Stores the network quality |cached_network_quality| of network with ID |
30 // |network_id|. | 51 // |network_id|. |
31 void Add(const nqe::internal::NetworkID& network_id, | 52 void Add(const nqe::internal::NetworkID& network_id, |
32 const nqe::internal::CachedNetworkQuality& cached_network_quality); | 53 const nqe::internal::CachedNetworkQuality& cached_network_quality); |
33 | 54 |
34 // Returns true if the network quality estimate was successfully read | 55 // Returns true if the network quality estimate was successfully read |
35 // for a network with ID |network_id|, and sets |cached_network_quality| to | 56 // for a network with ID |network_id|, and sets |cached_network_quality| to |
36 // the estimate read. | 57 // the estimate read. |
37 bool GetById(const nqe::internal::NetworkID& network_id, | 58 bool GetById(const nqe::internal::NetworkID& network_id, |
38 nqe::internal::CachedNetworkQuality* cached_network_quality); | 59 nqe::internal::CachedNetworkQuality* cached_network_quality); |
39 | 60 |
| 61 // Adds and removes |observer| from the list of cache observers. The |
| 62 // observers are notified on the same thread on which it was added. Addition |
| 63 // and removal of the observer must happen on the same thread. |
| 64 void AddNetworkQualitiesCacheObserver( |
| 65 NetworkQualitiesCacheObserver* observer); |
| 66 void RemoveNetworkQualitiesCacheObserver( |
| 67 NetworkQualitiesCacheObserver* observer); |
| 68 |
40 private: | 69 private: |
41 // Maximum size of the store that holds network quality estimates. | 70 // Maximum size of the store that holds network quality estimates. |
42 // A smaller size may reduce the cache hit rate due to frequent evictions. | 71 // A smaller size may reduce the cache hit rate due to frequent evictions. |
43 // A larger size may affect performance. | 72 // A larger size may affect performance. |
44 static const size_t kMaximumNetworkQualityCacheSize = 10; | 73 static const size_t kMaximumNetworkQualityCacheSize = 10; |
45 | 74 |
46 // This does not use an unordered_map or hash_map for code simplicity (the key | 75 // This does not use an unordered_map or hash_map for code simplicity (the key |
47 // just implements operator<, rather than hash and equality) and because the | 76 // just implements operator<, rather than hash and equality) and because the |
48 // map is tiny. | 77 // map is tiny. |
49 typedef std::map<nqe::internal::NetworkID, | 78 typedef std::map<nqe::internal::NetworkID, |
50 nqe::internal::CachedNetworkQuality> | 79 nqe::internal::CachedNetworkQuality> |
51 CachedNetworkQualities; | 80 CachedNetworkQualities; |
52 | 81 |
53 // Data structure that stores the qualities of networks. | 82 // Data structure that stores the qualities of networks. |
54 CachedNetworkQualities cached_network_qualities_; | 83 CachedNetworkQualities cached_network_qualities_; |
55 | 84 |
| 85 // Observer list for changes in the cached network quality. |
| 86 base::ObserverList<NetworkQualitiesCacheObserver> |
| 87 network_qualities_cache_observer_list_; |
| 88 |
56 base::ThreadChecker thread_checker_; | 89 base::ThreadChecker thread_checker_; |
57 | 90 |
58 DISALLOW_COPY_AND_ASSIGN(NetworkQualityStore); | 91 DISALLOW_COPY_AND_ASSIGN(NetworkQualityStore); |
59 }; | 92 }; |
60 | 93 |
61 } // namespace internal | 94 } // namespace internal |
62 | 95 |
63 } // namespace nqe | 96 } // namespace nqe |
64 | 97 |
65 } // namespace net | 98 } // namespace net |
66 | 99 |
67 #endif // NET_NQE_NETWORK_QUALITY_STORE_H_ | 100 #endif // NET_NQE_NETWORK_QUALITY_STORE_H_ |
OLD | NEW |