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

Side by Side Diff: net/http/http_server_properties_manager.cc

Issue 1626673002: Separate prefs from HttpServerPropertiesManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: NET_EXPORT Created 4 years, 11 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "net/http/http_server_properties_manager.h" 5 #include "net/http/http_server_properties_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h" 10 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
14 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
15 #include "base/values.h" 14 #include "base/values.h"
16 #include "net/base/ip_address_number.h" 15 #include "net/base/ip_address_number.h"
17 #include "net/base/port_util.h" 16 #include "net/base/port_util.h"
18 17
19 namespace net { 18 namespace net {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 const char kProbabilityKey[] = "probability"; 64 const char kProbabilityKey[] = "probability";
66 const char kExpirationKey[] = "expiration"; 65 const char kExpirationKey[] = "expiration";
67 const char kNetworkStatsKey[] = "network_stats"; 66 const char kNetworkStatsKey[] = "network_stats";
68 const char kSrttKey[] = "srtt"; 67 const char kSrttKey[] = "srtt";
69 68
70 } // namespace 69 } // namespace
71 70
72 //////////////////////////////////////////////////////////////////////////////// 71 ////////////////////////////////////////////////////////////////////////////////
73 // HttpServerPropertiesManager 72 // HttpServerPropertiesManager
74 73
74 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {}
75
75 HttpServerPropertiesManager::HttpServerPropertiesManager( 76 HttpServerPropertiesManager::HttpServerPropertiesManager(
76 PrefService* pref_service, 77 PrefDelegate* pref_delegate,
77 const char* pref_path,
78 scoped_refptr<base::SequencedTaskRunner> network_task_runner) 78 scoped_refptr<base::SequencedTaskRunner> network_task_runner)
79 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), 79 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()),
80 pref_service_(pref_service), 80 pref_delegate_(pref_delegate),
81 setting_prefs_(false), 81 setting_prefs_(false),
82 path_(pref_path),
83 network_task_runner_(network_task_runner) { 82 network_task_runner_(network_task_runner) {
84 DCHECK(pref_service); 83 DCHECK(pref_delegate_);
85 pref_weak_ptr_factory_.reset( 84 pref_weak_ptr_factory_.reset(
86 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 85 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
87 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); 86 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr();
88 pref_cache_update_timer_.reset(new base::OneShotTimer); 87 pref_cache_update_timer_.reset(new base::OneShotTimer);
89 pref_change_registrar_.Init(pref_service_); 88 pref_delegate_->StartListeningForUpdates(
90 pref_change_registrar_.Add(
91 path_,
92 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, 89 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged,
93 base::Unretained(this))); 90 base::Unretained(this)));
94 } 91 }
95 92
96 HttpServerPropertiesManager::~HttpServerPropertiesManager() { 93 HttpServerPropertiesManager::~HttpServerPropertiesManager() {
97 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 94 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
98 network_weak_ptr_factory_.reset(); 95 network_weak_ptr_factory_.reset();
99 } 96 }
100 97
101 void HttpServerPropertiesManager::InitializeOnNetworkThread() { 98 void HttpServerPropertiesManager::InitializeOnNetworkThread() {
102 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 99 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
103 network_weak_ptr_factory_.reset( 100 network_weak_ptr_factory_.reset(
104 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 101 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
105 http_server_properties_impl_.reset(new HttpServerPropertiesImpl()); 102 http_server_properties_impl_.reset(new HttpServerPropertiesImpl());
106 103
107 network_prefs_update_timer_.reset(new base::OneShotTimer); 104 network_prefs_update_timer_.reset(new base::OneShotTimer);
108 105
109 pref_task_runner_->PostTask( 106 pref_task_runner_->PostTask(
110 FROM_HERE, 107 FROM_HERE,
111 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread, 108 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread,
112 pref_weak_ptr_)); 109 pref_weak_ptr_));
113 } 110 }
114 111
115 void HttpServerPropertiesManager::ShutdownOnPrefThread() { 112 void HttpServerPropertiesManager::ShutdownOnPrefThread() {
116 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 113 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
117 // Cancel any pending updates, and stop listening for pref change updates. 114 // Cancel any pending updates, and stop listening for pref change updates.
118 pref_cache_update_timer_->Stop(); 115 pref_cache_update_timer_->Stop();
119 pref_weak_ptr_factory_.reset(); 116 pref_weak_ptr_factory_.reset();
120 pref_change_registrar_.RemoveAll(); 117 pref_delegate_->StopListeningForUpdates();
121 } 118 }
122 119
123 // static 120 // static
124 void HttpServerPropertiesManager::SetVersion( 121 void HttpServerPropertiesManager::SetVersion(
125 base::DictionaryValue* http_server_properties_dict, 122 base::DictionaryValue* http_server_properties_dict,
126 int version_number) { 123 int version_number) {
127 if (version_number < 0) 124 if (version_number < 0)
128 version_number = kVersionNumber; 125 version_number = kVersionNumber;
129 DCHECK_LE(version_number, kVersionNumber); 126 DCHECK_LE(version_number, kVersionNumber);
130 if (version_number <= kVersionNumber) 127 if (version_number <= kVersionNumber)
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 FROM_HERE, 435 FROM_HERE,
439 delay, 436 delay,
440 this, 437 this,
441 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); 438 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread);
442 } 439 }
443 440
444 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { 441 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() {
445 // The preferences can only be read on the pref thread. 442 // The preferences can only be read on the pref thread.
446 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 443 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
447 444
448 if (!pref_service_->HasPrefPath(path_)) 445 if (!pref_delegate_->HasServerProperties())
449 return; 446 return;
450 447
451 bool detected_corrupted_prefs = false; 448 bool detected_corrupted_prefs = false;
452 const base::DictionaryValue& http_server_properties_dict = 449 const base::DictionaryValue& http_server_properties_dict =
453 *pref_service_->GetDictionary(path_); 450 pref_delegate_->GetServerProperties();
454 451
455 int version = kMissingVersion; 452 int version = kMissingVersion;
456 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion(kVersionKey, 453 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion(kVersionKey,
457 &version)) { 454 &version)) {
458 DVLOG(1) << "Missing version. Clearing all properties."; 455 DVLOG(1) << "Missing version. Clearing all properties.";
459 return; 456 return;
460 } 457 }
461 458
462 const base::DictionaryValue* servers_dict = nullptr; 459 const base::DictionaryValue* servers_dict = nullptr;
463 const base::ListValue* servers_list = nullptr; 460 const base::ListValue* servers_list = nullptr;
(...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after
1110 ServerPrefMap::iterator it = server_pref_map.Get(server); 1107 ServerPrefMap::iterator it = server_pref_map.Get(server);
1111 if (it == server_pref_map.end()) { 1108 if (it == server_pref_map.end()) {
1112 ServerPref server_pref; 1109 ServerPref server_pref;
1113 server_pref.server_network_stats = &map_it->second; 1110 server_pref.server_network_stats = &map_it->second;
1114 server_pref_map.Put(server, server_pref); 1111 server_pref_map.Put(server, server_pref);
1115 } else { 1112 } else {
1116 it->second.server_network_stats = &map_it->second; 1113 it->second.server_network_stats = &map_it->second;
1117 } 1114 }
1118 } 1115 }
1119 1116
1120 // Persist properties to the |path_| in the MRU order. 1117 // Persist properties to the prefs in the MRU order.
1121 base::DictionaryValue http_server_properties_dict; 1118 base::DictionaryValue http_server_properties_dict;
1122 base::ListValue* servers_list = new base::ListValue; 1119 base::ListValue* servers_list = new base::ListValue;
1123 for (ServerPrefMap::const_reverse_iterator map_it = server_pref_map.rbegin(); 1120 for (ServerPrefMap::const_reverse_iterator map_it = server_pref_map.rbegin();
1124 map_it != server_pref_map.rend(); ++map_it) { 1121 map_it != server_pref_map.rend(); ++map_it) {
1125 const HostPortPair& server = map_it->first; 1122 const HostPortPair& server = map_it->first;
1126 const ServerPref& server_pref = map_it->second; 1123 const ServerPref& server_pref = map_it->second;
1127 1124
1128 base::DictionaryValue* servers_dict = new base::DictionaryValue; 1125 base::DictionaryValue* servers_dict = new base::DictionaryValue;
1129 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; 1126 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
1130 1127
(...skipping 14 matching lines...) Expand all
1145 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, 1142 http_server_properties_dict.SetWithoutPathExpansion(kServersKey,
1146 servers_list); 1143 servers_list);
1147 SetVersion(&http_server_properties_dict, kVersionNumber); 1144 SetVersion(&http_server_properties_dict, kVersionNumber);
1148 1145
1149 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict); 1146 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict);
1150 1147
1151 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map, 1148 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map,
1152 &http_server_properties_dict); 1149 &http_server_properties_dict);
1153 1150
1154 setting_prefs_ = true; 1151 setting_prefs_ = true;
1155 pref_service_->Set(path_, http_server_properties_dict); 1152 pref_delegate_->SetServerProperties(http_server_properties_dict);
1156 setting_prefs_ = false; 1153 setting_prefs_ = false;
1157 1154
1158 // Note that |completion| will be fired after we have written everything to 1155 // Note that |completion| will be fired after we have written everything to
1159 // the Preferences, but likely before these changes are serialized to disk. 1156 // the Preferences, but likely before these changes are serialized to disk.
1160 // This is not a problem though, as JSONPrefStore guarantees that this will 1157 // This is not a problem though, as JSONPrefStore guarantees that this will
1161 // happen, pretty soon, and even in the case we shut down immediately. 1158 // happen, pretty soon, and even in the case we shut down immediately.
1162 if (!completion.is_null()) 1159 if (!completion.is_null())
1163 completion.Run(); 1160 completion.Run();
1164 } 1161 }
1165 1162
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1265 quic_servers_dict); 1262 quic_servers_dict);
1266 } 1263 }
1267 1264
1268 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { 1265 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() {
1269 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 1266 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
1270 if (!setting_prefs_) 1267 if (!setting_prefs_)
1271 ScheduleUpdateCacheOnPrefThread(); 1268 ScheduleUpdateCacheOnPrefThread();
1272 } 1269 }
1273 1270
1274 } // namespace net 1271 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_server_properties_manager.h ('k') | net/http/http_server_properties_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698