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

Side by Side Diff: net/android/network_change_notifier_android.cc

Issue 1011683002: Lazily initialize MessageLoop for faster thread startup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: weak ptr Created 5 years, 7 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
« no previous file with comments | « content/public/test/test_browser_thread.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 //////////////////////////////////////////////////////////////////////////////// 5 ////////////////////////////////////////////////////////////////////////////////
6 // Threading considerations: 6 // Threading considerations:
7 // 7 //
8 // This class is designed to meet various threading guarantees starting from the 8 // This class is designed to meet various threading guarantees starting from the
9 // ones imposed by NetworkChangeNotifier: 9 // ones imposed by NetworkChangeNotifier:
10 // - The notifier can be constructed on any thread. 10 // - The notifier can be constructed on any thread.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // delegate then forwards these notifications to the threads of each observer 52 // delegate then forwards these notifications to the threads of each observer
53 // (network change notifier). The network change notifier than processes the 53 // (network change notifier). The network change notifier than processes the
54 // state change, and notifies each of its observers on their threads. 54 // state change, and notifies each of its observers on their threads.
55 // 55 //
56 // This can also be seen as: 56 // This can also be seen as:
57 // Android platform -> NetworkChangeNotifier (Java) -> 57 // Android platform -> NetworkChangeNotifier (Java) ->
58 // NetworkChangeNotifierDelegateAndroid -> NetworkChangeNotifierAndroid. 58 // NetworkChangeNotifierDelegateAndroid -> NetworkChangeNotifierAndroid.
59 59
60 #include "net/android/network_change_notifier_android.h" 60 #include "net/android/network_change_notifier_android.h"
61 61
62 #include "base/memory/weak_ptr.h"
63 #include "base/single_thread_task_runner.h"
64 #include "base/thread_task_runner_handle.h"
62 #include "base/threading/thread.h" 65 #include "base/threading/thread.h"
63 #include "net/base/address_tracker_linux.h" 66 #include "net/base/address_tracker_linux.h"
64 #include "net/dns/dns_config_service_posix.h" 67 #include "net/dns/dns_config_service_posix.h"
65 68
66 namespace net { 69 namespace net {
67 70
68 // Thread on which we can run DnsConfigService, which requires a TYPE_IO 71 // Thread on which we can run DnsConfigService, which requires a TYPE_IO
69 // message loop to monitor /system/etc/hosts. 72 // message loop to monitor /system/etc/hosts.
70 class NetworkChangeNotifierAndroid::DnsConfigServiceThread 73 class NetworkChangeNotifierAndroid::DnsConfigServiceThread
71 : public base::Thread, 74 : public base::Thread,
72 public NetworkChangeNotifier::NetworkChangeObserver { 75 public NetworkChangeNotifier::NetworkChangeObserver {
73 public: 76 public:
74 DnsConfigServiceThread(const DnsConfig* dns_config_for_testing) 77 DnsConfigServiceThread(const DnsConfig* dns_config_for_testing)
75 : base::Thread("DnsConfigService"), 78 : base::Thread("DnsConfigService"),
79 calling_task_runner_(base::ThreadTaskRunnerHandle::Get()),
76 dns_config_for_testing_(dns_config_for_testing), 80 dns_config_for_testing_(dns_config_for_testing),
77 creation_time_(base::Time::Now()), 81 creation_time_(base::Time::Now()),
78 address_tracker_(base::Bind(base::DoNothing), 82 address_tracker_(base::Bind(base::DoNothing),
79 base::Bind(base::DoNothing), 83 base::Bind(base::DoNothing),
80 // We're only interested in tunnel interface changes. 84 // We're only interested in tunnel interface changes.
81 base::Bind(NotifyNetworkChangeNotifierObservers)) {} 85 base::Bind(NotifyNetworkChangeNotifierObservers)),
86 weak_ptr_factory_(this) {
87 weak_ptr_ = weak_ptr_factory_.GetWeakPtr();
88 }
82 89
83 ~DnsConfigServiceThread() override { 90 ~DnsConfigServiceThread() override {
84 NetworkChangeNotifier::RemoveNetworkChangeObserver(this); 91 NetworkChangeNotifier::RemoveNetworkChangeObserver(this);
85 Stop(); 92 Stop();
86 } 93 }
87 94
88 void InitAfterStart() { 95 void InitAfterStart() {
89 DCHECK(IsRunning()); 96 DCHECK(IsRunning());
90 NetworkChangeNotifier::AddNetworkChangeObserver(this); 97 NetworkChangeNotifier::AddNetworkChangeObserver(this);
91 } 98 }
92 99
93 void Init() override { 100 void Init() override {
94 address_tracker_.Init(); 101 address_tracker_.Init();
95 dns_config_service_.reset(new internal::DnsConfigServicePosix()); 102 dns_config_service_.reset(new internal::DnsConfigServicePosix());
96 if (dns_config_for_testing_) 103 if (dns_config_for_testing_)
97 dns_config_service_->SetDnsConfigForTesting(dns_config_for_testing_); 104 dns_config_service_->SetDnsConfigForTesting(dns_config_for_testing_);
98 dns_config_service_->WatchConfig( 105 dns_config_service_->WatchConfig(
99 base::Bind(&DnsConfigServiceThread::DnsConfigChangeCallback, 106 base::Bind(&DnsConfigServiceThread::DnsConfigChangeCallback,
100 base::Unretained(this))); 107 base::Unretained(this)));
108 calling_task_runner_->PostTask(
109 FROM_HERE,
110 base::Bind(&DnsConfigServiceThread::InitAfterStart, weak_ptr_));
pauljensen 2015/05/01 11:54:44 This is not good. This will allow changes to esca
kinuko 2015/05/01 13:58:05 Ok thanks, I suspected it might be the case. I can
101 } 111 }
102 112
103 void CleanUp() override { dns_config_service_.reset(); } 113 void CleanUp() override { dns_config_service_.reset(); }
104 114
105 static void NotifyNetworkChangeNotifierObservers() { 115 static void NotifyNetworkChangeNotifierObservers() {
106 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); 116 NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
107 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange(); 117 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
108 } 118 }
109 119
110 private: 120 private:
111 void DnsConfigChangeCallback(const DnsConfig& config) { 121 void DnsConfigChangeCallback(const DnsConfig& config) {
112 DCHECK(task_runner()->BelongsToCurrentThread()); 122 DCHECK(task_runner()->BelongsToCurrentThread());
113 if (dns_config_service_->SeenChangeSince(creation_time_)) { 123 if (dns_config_service_->SeenChangeSince(creation_time_)) {
114 NetworkChangeNotifier::SetDnsConfig(config); 124 NetworkChangeNotifier::SetDnsConfig(config);
115 } else { 125 } else {
116 NetworkChangeNotifier::SetInitialDnsConfig(config); 126 NetworkChangeNotifier::SetInitialDnsConfig(config);
117 } 127 }
118 } 128 }
119 129
120 // NetworkChangeNotifier::NetworkChangeObserver implementation: 130 // NetworkChangeNotifier::NetworkChangeObserver implementation:
121 void OnNetworkChanged(NetworkChangeNotifier::ConnectionType type) override { 131 void OnNetworkChanged(NetworkChangeNotifier::ConnectionType type) override {
122 task_runner()->PostTask( 132 task_runner()->PostTask(
123 FROM_HERE, 133 FROM_HERE,
124 base::Bind(&internal::DnsConfigServicePosix::OnNetworkChanged, 134 base::Bind(&internal::DnsConfigServicePosix::OnNetworkChanged,
125 base::Unretained(dns_config_service_.get()), type)); 135 base::Unretained(dns_config_service_.get()), type));
126 } 136 }
127 137
138 scoped_refptr<base::SingleThreadTaskRunner> calling_task_runner_;
128 const DnsConfig* dns_config_for_testing_; 139 const DnsConfig* dns_config_for_testing_;
129 const base::Time creation_time_; 140 const base::Time creation_time_;
130 scoped_ptr<internal::DnsConfigServicePosix> dns_config_service_; 141 scoped_ptr<internal::DnsConfigServicePosix> dns_config_service_;
131 // Used to detect tunnel state changes. 142 // Used to detect tunnel state changes.
132 internal::AddressTrackerLinux address_tracker_; 143 internal::AddressTrackerLinux address_tracker_;
133 144
145 // Used only on the calling thread that created this thread.
146 base::WeakPtr<DnsConfigServiceThread> weak_ptr_;
147 base::WeakPtrFactory<DnsConfigServiceThread> weak_ptr_factory_;
148
134 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread); 149 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread);
135 }; 150 };
136 151
137 NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() { 152 NetworkChangeNotifierAndroid::~NetworkChangeNotifierAndroid() {
138 delegate_->RemoveObserver(this); 153 delegate_->RemoveObserver(this);
139 } 154 }
140 155
141 NetworkChangeNotifier::ConnectionType 156 NetworkChangeNotifier::ConnectionType
142 NetworkChangeNotifierAndroid::GetCurrentConnectionType() const { 157 NetworkChangeNotifierAndroid::GetCurrentConnectionType() const {
143 return delegate_->GetCurrentConnectionType(); 158 return delegate_->GetCurrentConnectionType();
(...skipping 21 matching lines...) Expand all
165 NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid( 180 NetworkChangeNotifierAndroid::NetworkChangeNotifierAndroid(
166 NetworkChangeNotifierDelegateAndroid* delegate, 181 NetworkChangeNotifierDelegateAndroid* delegate,
167 const DnsConfig* dns_config_for_testing) 182 const DnsConfig* dns_config_for_testing)
168 : NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()), 183 : NetworkChangeNotifier(NetworkChangeCalculatorParamsAndroid()),
169 delegate_(delegate), 184 delegate_(delegate),
170 dns_config_service_thread_( 185 dns_config_service_thread_(
171 new DnsConfigServiceThread(dns_config_for_testing)) { 186 new DnsConfigServiceThread(dns_config_for_testing)) {
172 delegate_->AddObserver(this); 187 delegate_->AddObserver(this);
173 dns_config_service_thread_->StartWithOptions( 188 dns_config_service_thread_->StartWithOptions(
174 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); 189 base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
175 dns_config_service_thread_->InitAfterStart();
176 } 190 }
177 191
178 // static 192 // static
179 NetworkChangeNotifier::NetworkChangeCalculatorParams 193 NetworkChangeNotifier::NetworkChangeCalculatorParams
180 NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid() { 194 NetworkChangeNotifierAndroid::NetworkChangeCalculatorParamsAndroid() {
181 NetworkChangeCalculatorParams params; 195 NetworkChangeCalculatorParams params;
182 // IPAddressChanged is produced immediately prior to ConnectionTypeChanged 196 // IPAddressChanged is produced immediately prior to ConnectionTypeChanged
183 // so delay IPAddressChanged so they get merged with the following 197 // so delay IPAddressChanged so they get merged with the following
184 // ConnectionTypeChanged signal. 198 // ConnectionTypeChanged signal.
185 params.ip_address_offline_delay_ = base::TimeDelta::FromSeconds(1); 199 params.ip_address_offline_delay_ = base::TimeDelta::FromSeconds(1);
186 params.ip_address_online_delay_ = base::TimeDelta::FromSeconds(1); 200 params.ip_address_online_delay_ = base::TimeDelta::FromSeconds(1);
187 params.connection_type_offline_delay_ = base::TimeDelta::FromSeconds(0); 201 params.connection_type_offline_delay_ = base::TimeDelta::FromSeconds(0);
188 params.connection_type_online_delay_ = base::TimeDelta::FromSeconds(0); 202 params.connection_type_online_delay_ = base::TimeDelta::FromSeconds(0);
189 return params; 203 return params;
190 } 204 }
191 205
192 } // namespace net 206 } // namespace net
OLDNEW
« no previous file with comments | « content/public/test/test_browser_thread.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698