OLD | NEW |
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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "net/base/ip_address.h" | 17 #include "net/base/ip_address.h" |
18 #include "net/base/port_util.h" | 18 #include "net/base/port_util.h" |
19 #include "net/quic/platform/api/quic_hostname_utils.h" | 19 #include "net/quic/platform/api/quic_hostname_utils.h" |
20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
21 | 21 |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 // Time to wait before starting an update the http_server_properties_impl_ cache | 26 // Time to wait before starting an update the http_server_properties_impl_ cache |
27 // from preferences. Scheduling another update during this period will be a | 27 // from preferences. Scheduling another update during this period will be a |
28 // no-op. | 28 // no-op. |
29 const int64_t kUpdateCacheDelayMs = 1000; | 29 constexpr base::TimeDelta kUpdateCacheDelay = base::TimeDelta::FromSeconds(1); |
30 | 30 |
31 // Time to wait before starting an update the preferences from the | 31 // Time to wait before starting an update the preferences from the |
32 // http_server_properties_impl_ cache. Scheduling another update during this | 32 // http_server_properties_impl_ cache. Scheduling another update during this |
33 // period will be a no-op. | 33 // period will be a no-op. |
34 const int64_t kUpdatePrefsDelayMs = 60000; | 34 constexpr base::TimeDelta kUpdatePrefsDelay = base::TimeDelta::FromSeconds(60); |
35 | 35 |
36 // "version" 0 indicates, http_server_properties doesn't have "version" | 36 // "version" 0 indicates, http_server_properties doesn't have "version" |
37 // property. | 37 // property. |
38 const int kMissingVersion = 0; | 38 const int kMissingVersion = 0; |
39 | 39 |
40 // The version number of persisted http_server_properties. | 40 // The version number of persisted http_server_properties. |
41 const int kVersionNumber = 5; | 41 const int kVersionNumber = 5; |
42 | 42 |
43 // Persist 200 MRU AlternateProtocolHostPortPairs. | 43 // Persist 200 MRU AlternateProtocolHostPortPairs. |
44 const int kMaxAlternateProtocolHostsToPersist = 200; | 44 const int kMaxAlternateProtocolHostsToPersist = 200; |
(...skipping 24 matching lines...) Expand all Loading... |
69 | 69 |
70 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
71 // HttpServerPropertiesManager | 71 // HttpServerPropertiesManager |
72 | 72 |
73 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {} | 73 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {} |
74 | 74 |
75 HttpServerPropertiesManager::HttpServerPropertiesManager( | 75 HttpServerPropertiesManager::HttpServerPropertiesManager( |
76 PrefDelegate* pref_delegate, | 76 PrefDelegate* pref_delegate, |
77 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, | 77 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, |
78 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) | 78 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) |
79 : pref_task_runner_(pref_task_runner), | 79 : pref_task_runner_(std::move(pref_task_runner)), |
80 pref_delegate_(pref_delegate), | 80 pref_delegate_(pref_delegate), |
81 setting_prefs_(false), | 81 setting_prefs_(false), |
82 is_initialized_(false), | 82 is_initialized_(false), |
83 network_task_runner_(network_task_runner) { | 83 network_task_runner_(std::move(network_task_runner)) { |
| 84 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
84 DCHECK(pref_delegate_); | 85 DCHECK(pref_delegate_); |
85 pref_weak_ptr_factory_.reset( | 86 pref_weak_ptr_factory_.reset( |
86 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 87 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
87 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); | 88 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); |
88 pref_cache_update_timer_.reset(new base::OneShotTimer); | 89 pref_cache_update_timer_.reset(new base::OneShotTimer); |
89 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); | 90 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); |
90 pref_delegate_->StartListeningForUpdates( | 91 pref_delegate_->StartListeningForUpdates( |
91 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, | 92 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, |
92 base::Unretained(this))); | 93 base::Unretained(this))); |
93 } | 94 } |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 return http_server_properties_impl_->SetMaxServerConfigsStoredInProperties( | 360 return http_server_properties_impl_->SetMaxServerConfigsStoredInProperties( |
360 max_server_configs_stored_in_properties); | 361 max_server_configs_stored_in_properties); |
361 } | 362 } |
362 | 363 |
363 bool HttpServerPropertiesManager::IsInitialized() const { | 364 bool HttpServerPropertiesManager::IsInitialized() const { |
364 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 365 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
365 | 366 |
366 return is_initialized_; | 367 return is_initialized_; |
367 } | 368 } |
368 | 369 |
| 370 // static |
| 371 base::TimeDelta HttpServerPropertiesManager::GetUpdateCacheDelayForTesting() { |
| 372 return kUpdateCacheDelay; |
| 373 } |
| 374 |
| 375 // static |
| 376 base::TimeDelta HttpServerPropertiesManager::GetUpdatePrefsDelayForTesting() { |
| 377 return kUpdatePrefsDelay; |
| 378 } |
| 379 |
369 // | 380 // |
370 // Update the HttpServerPropertiesImpl's cache with data from preferences. | 381 // Update the HttpServerPropertiesImpl's cache with data from preferences. |
371 // | 382 // |
372 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() { | 383 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() { |
373 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 384 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
374 // Do not schedule a new update if there is already one scheduled. | 385 // Do not schedule a new update if there is already one scheduled. |
375 if (pref_cache_update_timer_->IsRunning()) | 386 if (pref_cache_update_timer_->IsRunning()) |
376 return; | 387 return; |
377 | 388 |
378 pref_cache_update_timer_->Start( | 389 pref_cache_update_timer_->Start( |
379 FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs), this, | 390 FROM_HERE, kUpdateCacheDelay, this, |
380 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); | 391 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); |
381 } | 392 } |
382 | 393 |
383 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { | 394 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { |
384 // The preferences can only be read on the pref thread. | 395 // The preferences can only be read on the pref thread. |
385 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 396 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
386 | 397 |
387 if (!pref_delegate_->HasServerProperties()) | 398 if (!pref_delegate_->HasServerProperties()) |
388 return; | 399 return; |
389 | 400 |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 // Update Preferences with data from the cached data. | 800 // Update Preferences with data from the cached data. |
790 // | 801 // |
791 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread( | 802 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread( |
792 Location location) { | 803 Location location) { |
793 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 804 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
794 // Do not schedule a new update if there is already one scheduled. | 805 // Do not schedule a new update if there is already one scheduled. |
795 if (network_prefs_update_timer_->IsRunning()) | 806 if (network_prefs_update_timer_->IsRunning()) |
796 return; | 807 return; |
797 | 808 |
798 network_prefs_update_timer_->Start( | 809 network_prefs_update_timer_->Start( |
799 FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs), this, | 810 FROM_HERE, kUpdatePrefsDelay, this, |
800 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread); | 811 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread); |
801 | 812 |
802 // TODO(rtenneti): Delete the following histogram after collecting some data. | 813 // TODO(rtenneti): Delete the following histogram after collecting some data. |
803 UMA_HISTOGRAM_ENUMERATION("Net.HttpServerProperties.UpdatePrefs", location, | 814 UMA_HISTOGRAM_ENUMERATION("Net.HttpServerProperties.UpdatePrefs", location, |
804 HttpServerPropertiesManager::NUM_LOCATIONS); | 815 HttpServerPropertiesManager::NUM_LOCATIONS); |
805 } | 816 } |
806 | 817 |
807 // This is required so we can set this as the callback for a timer. | 818 // This is required so we can set this as the callback for a timer. |
808 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { | 819 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { |
809 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); | 820 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 if (!setting_prefs_) | 1136 if (!setting_prefs_) |
1126 ScheduleUpdateCacheOnPrefThread(); | 1137 ScheduleUpdateCacheOnPrefThread(); |
1127 } | 1138 } |
1128 | 1139 |
1129 void HttpServerPropertiesManager::SetInitialized() { | 1140 void HttpServerPropertiesManager::SetInitialized() { |
1130 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 1141 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
1131 is_initialized_ = true; | 1142 is_initialized_ = true; |
1132 } | 1143 } |
1133 | 1144 |
1134 } // namespace net | 1145 } // namespace net |
OLD | NEW |