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. | |
bengr
2016/08/25 18:42:59
Might be good to say that these are not threadsafe
tbansal1
2016/08/25 20:31:07
Done.
| |
62 void AddNetworkQualitiesCacheObserver( | |
63 NetworkQualitiesCacheObserver* observer); | |
64 void RemoveNetworkQualitiesCacheObserver( | |
65 NetworkQualitiesCacheObserver* observer); | |
66 | |
40 private: | 67 private: |
41 // Maximum size of the store that holds network quality estimates. | 68 // Maximum size of the store that holds network quality estimates. |
42 // A smaller size may reduce the cache hit rate due to frequent evictions. | 69 // A smaller size may reduce the cache hit rate due to frequent evictions. |
43 // A larger size may affect performance. | 70 // A larger size may affect performance. |
44 static const size_t kMaximumNetworkQualityCacheSize = 10; | 71 static const size_t kMaximumNetworkQualityCacheSize = 10; |
45 | 72 |
46 // This does not use an unordered_map or hash_map for code simplicity (the key | 73 // 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 | 74 // just implements operator<, rather than hash and equality) and because the |
48 // map is tiny. | 75 // map is tiny. |
49 typedef std::map<nqe::internal::NetworkID, | 76 typedef std::map<nqe::internal::NetworkID, |
50 nqe::internal::CachedNetworkQuality> | 77 nqe::internal::CachedNetworkQuality> |
51 CachedNetworkQualities; | 78 CachedNetworkQualities; |
52 | 79 |
53 // Data structure that stores the qualities of networks. | 80 // Data structure that stores the qualities of networks. |
54 CachedNetworkQualities cached_network_qualities_; | 81 CachedNetworkQualities cached_network_qualities_; |
55 | 82 |
83 // Observer list for changes in the cached network quality. | |
84 base::ObserverList<NetworkQualitiesCacheObserver> | |
85 network_qualities_cache_observer_list_; | |
86 | |
56 base::ThreadChecker thread_checker_; | 87 base::ThreadChecker thread_checker_; |
57 | 88 |
58 DISALLOW_COPY_AND_ASSIGN(NetworkQualityStore); | 89 DISALLOW_COPY_AND_ASSIGN(NetworkQualityStore); |
59 }; | 90 }; |
60 | 91 |
61 } // namespace internal | 92 } // namespace internal |
62 | 93 |
63 } // namespace nqe | 94 } // namespace nqe |
64 | 95 |
65 } // namespace net | 96 } // namespace net |
66 | 97 |
67 #endif // NET_NQE_NETWORK_QUALITY_STORE_H_ | 98 #endif // NET_NQE_NETWORK_QUALITY_STORE_H_ |
OLD | NEW |