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

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

Issue 2657013002: Introduce ThreadTaskRunnerHandle::OverrideForTesting and TestMockTimeTaskRunner::ScopedContext. (Closed)
Patch Set: fix task runner paradigms in SyncStoppedReporterTest and ServerBackedStateKeysBrokerTest Created 3 years, 10 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 <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_url_utils.h" 19 #include "net/quic/platform/api/quic_url_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 29 matching lines...) Expand all
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_(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_(network_task_runner) {
84 DCHECK(pref_delegate_); 84 DCHECK(pref_delegate_);
xunjieli 2017/02/16 02:49:18 nit: could you add a DCHECK(pref_task_runner->Run
gab 2017/02/16 16:50:15 Done (though then I guess you might as well store
xunjieli 2017/02/16 17:08:29 Acknowledged. It used to be base::ThreadTaskRunner
gab 2017/02/16 21:03:36 Ah well, that's no longer necessary with my additi
85 pref_weak_ptr_factory_.reset( 85 pref_weak_ptr_factory_.reset(
86 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 86 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
87 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); 87 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr();
88 pref_cache_update_timer_.reset(new base::OneShotTimer); 88 pref_cache_update_timer_.reset(new base::OneShotTimer);
89 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); 89 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_);
90 pref_delegate_->StartListeningForUpdates( 90 pref_delegate_->StartListeningForUpdates(
91 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, 91 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged,
92 base::Unretained(this))); 92 base::Unretained(this)));
93 } 93 }
94 94
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 return http_server_properties_impl_->SetMaxServerConfigsStoredInProperties( 359 return http_server_properties_impl_->SetMaxServerConfigsStoredInProperties(
360 max_server_configs_stored_in_properties); 360 max_server_configs_stored_in_properties);
361 } 361 }
362 362
363 bool HttpServerPropertiesManager::IsInitialized() const { 363 bool HttpServerPropertiesManager::IsInitialized() const {
364 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 364 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
365 365
366 return is_initialized_; 366 return is_initialized_;
367 } 367 }
368 368
369 // static
370 base::TimeDelta HttpServerPropertiesManager::GetUpdateCacheDelayForTesting() {
371 return kUpdateCacheDelay;
372 }
373
374 // static
375 base::TimeDelta HttpServerPropertiesManager::GetUpdatePrefsDelayForTesting() {
376 return kUpdatePrefsDelay;
377 }
378
369 // 379 //
370 // Update the HttpServerPropertiesImpl's cache with data from preferences. 380 // Update the HttpServerPropertiesImpl's cache with data from preferences.
371 // 381 //
372 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() { 382 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() {
373 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 383 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
374 // Do not schedule a new update if there is already one scheduled. 384 // Do not schedule a new update if there is already one scheduled.
375 if (pref_cache_update_timer_->IsRunning()) 385 if (pref_cache_update_timer_->IsRunning())
376 return; 386 return;
377 387
378 pref_cache_update_timer_->Start( 388 pref_cache_update_timer_->Start(
379 FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs), this, 389 FROM_HERE, kUpdateCacheDelay, this,
380 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); 390 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread);
381 } 391 }
382 392
383 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { 393 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() {
384 // The preferences can only be read on the pref thread. 394 // The preferences can only be read on the pref thread.
385 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 395 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
386 396
387 if (!pref_delegate_->HasServerProperties()) 397 if (!pref_delegate_->HasServerProperties())
388 return; 398 return;
389 399
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 // Update Preferences with data from the cached data. 795 // Update Preferences with data from the cached data.
786 // 796 //
787 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread( 797 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread(
788 Location location) { 798 Location location) {
789 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 799 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
790 // Do not schedule a new update if there is already one scheduled. 800 // Do not schedule a new update if there is already one scheduled.
791 if (network_prefs_update_timer_->IsRunning()) 801 if (network_prefs_update_timer_->IsRunning())
792 return; 802 return;
793 803
794 network_prefs_update_timer_->Start( 804 network_prefs_update_timer_->Start(
795 FROM_HERE, base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs), this, 805 FROM_HERE, kUpdatePrefsDelay, this,
796 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread); 806 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread);
797 807
798 // TODO(rtenneti): Delete the following histogram after collecting some data. 808 // TODO(rtenneti): Delete the following histogram after collecting some data.
799 UMA_HISTOGRAM_ENUMERATION("Net.HttpServerProperties.UpdatePrefs", location, 809 UMA_HISTOGRAM_ENUMERATION("Net.HttpServerProperties.UpdatePrefs", location,
800 HttpServerPropertiesManager::NUM_LOCATIONS); 810 HttpServerPropertiesManager::NUM_LOCATIONS);
801 } 811 }
802 812
803 // This is required so we can set this as the callback for a timer. 813 // This is required so we can set this as the callback for a timer.
804 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { 814 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() {
805 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); 815 UpdatePrefsFromCacheOnNetworkThread(base::Closure());
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 if (!setting_prefs_) 1131 if (!setting_prefs_)
1122 ScheduleUpdateCacheOnPrefThread(); 1132 ScheduleUpdateCacheOnPrefThread();
1123 } 1133 }
1124 1134
1125 void HttpServerPropertiesManager::SetInitialized() { 1135 void HttpServerPropertiesManager::SetInitialized() {
1126 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 1136 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
1127 is_initialized_ = true; 1137 is_initialized_ = true;
1128 } 1138 }
1129 1139
1130 } // namespace net 1140 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698