OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/local_discovery/service_discovery_host_client.h" | 5 #include "chrome/browser/local_discovery/service_discovery_host_client.h" |
6 | 6 |
7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
8 #include "base/file_descriptor_posix.h" | 8 #include "base/file_descriptor_posix.h" |
9 #endif // OS_POSIX | 9 #endif // OS_POSIX |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 } | 37 } |
38 | 38 |
39 virtual void Start() OVERRIDE { | 39 virtual void Start() OVERRIDE { |
40 DVLOG(1) << "ServiceWatcher::Start with id " << id_; | 40 DVLOG(1) << "ServiceWatcher::Start with id " << id_; |
41 DCHECK(!started_); | 41 DCHECK(!started_); |
42 host_->Send(new LocalDiscoveryMsg_StartWatcher(id_, service_type_)); | 42 host_->Send(new LocalDiscoveryMsg_StartWatcher(id_, service_type_)); |
43 started_ = true; | 43 started_ = true; |
44 } | 44 } |
45 | 45 |
46 virtual void DiscoverNewServices(bool force_update) OVERRIDE { | 46 virtual void DiscoverNewServices(bool force_update) OVERRIDE { |
47 DVLOG(1) << "ServiceWatcher::DiscoverNewServices with id " | 47 DVLOG(1) << "ServiceWatcher::DiscoverNewServices with id " << id_; |
48 << id_; | |
49 DCHECK(started_); | 48 DCHECK(started_); |
50 host_->Send(new LocalDiscoveryMsg_DiscoverServices(id_, force_update)); | 49 host_->Send(new LocalDiscoveryMsg_DiscoverServices(id_, force_update)); |
51 } | 50 } |
52 | 51 |
53 virtual std::string GetServiceType() const OVERRIDE { | 52 virtual std::string GetServiceType() const OVERRIDE { |
54 return service_type_; | 53 return service_type_; |
55 } | 54 } |
56 | 55 |
57 private: | 56 private: |
58 scoped_refptr<ServiceDiscoveryHostClient> host_; | 57 scoped_refptr<ServiceDiscoveryHostClient> host_; |
(...skipping 15 matching lines...) Expand all Loading... |
74 } | 73 } |
75 | 74 |
76 virtual ~ServiceResolverProxy() { | 75 virtual ~ServiceResolverProxy() { |
77 DVLOG(1) << "~ServiceResolverProxy with id " << id_; | 76 DVLOG(1) << "~ServiceResolverProxy with id " << id_; |
78 host_->UnregisterResolverCallback(id_); | 77 host_->UnregisterResolverCallback(id_); |
79 if (started_) | 78 if (started_) |
80 host_->Send(new LocalDiscoveryMsg_DestroyResolver(id_)); | 79 host_->Send(new LocalDiscoveryMsg_DestroyResolver(id_)); |
81 } | 80 } |
82 | 81 |
83 virtual void StartResolving() OVERRIDE { | 82 virtual void StartResolving() OVERRIDE { |
84 DVLOG(1) | 83 DVLOG(1) << "ServiceResolverProxy::StartResolving with id " << id_; |
85 << "ServiceResolverProxy::StartResolving with id " | |
86 << id_; | |
87 DCHECK(!started_); | 84 DCHECK(!started_); |
88 host_->Send(new LocalDiscoveryMsg_ResolveService(id_, service_name_)); | 85 host_->Send(new LocalDiscoveryMsg_ResolveService(id_, service_name_)); |
89 started_ = true; | 86 started_ = true; |
90 } | 87 } |
91 | 88 |
92 virtual std::string GetName() const OVERRIDE { | 89 virtual std::string GetName() const OVERRIDE { |
93 return service_name_; | 90 return service_name_; |
94 } | 91 } |
95 | 92 |
96 private: | 93 private: |
(...skipping 11 matching lines...) Expand all Loading... |
108 net::AddressFamily address_family, | 105 net::AddressFamily address_family, |
109 const LocalDomainResolver::IPAddressCallback& callback) | 106 const LocalDomainResolver::IPAddressCallback& callback) |
110 : host_(host), | 107 : host_(host), |
111 domain_(domain), | 108 domain_(domain), |
112 address_family_(address_family), | 109 address_family_(address_family), |
113 id_(host->RegisterLocalDomainResolverCallback(callback)), | 110 id_(host->RegisterLocalDomainResolverCallback(callback)), |
114 started_(false) { | 111 started_(false) { |
115 } | 112 } |
116 | 113 |
117 virtual ~LocalDomainResolverProxy() { | 114 virtual ~LocalDomainResolverProxy() { |
118 DVLOG(1) << "~LocalDomainResolverProxy with id " | 115 DVLOG(1) << "~LocalDomainResolverProxy with id " << id_; |
119 << id_; | |
120 host_->UnregisterLocalDomainResolverCallback(id_); | 116 host_->UnregisterLocalDomainResolverCallback(id_); |
121 if (started_) | 117 if (started_) |
122 host_->Send(new LocalDiscoveryMsg_DestroyLocalDomainResolver(id_)); | 118 host_->Send(new LocalDiscoveryMsg_DestroyLocalDomainResolver(id_)); |
123 } | 119 } |
124 | 120 |
125 virtual void Start() OVERRIDE { | 121 virtual void Start() OVERRIDE { |
126 DVLOG(1) << "LocalDomainResolverProxy::Start with id " | 122 DVLOG(1) << "LocalDomainResolverProxy::Start with id " << id_; |
127 << id_; | |
128 DCHECK(!started_); | 123 DCHECK(!started_); |
129 host_->Send(new LocalDiscoveryMsg_ResolveLocalDomain(id_, domain_, | 124 host_->Send(new LocalDiscoveryMsg_ResolveLocalDomain(id_, domain_, |
130 address_family_)); | 125 address_family_)); |
131 started_ = true; | 126 started_ = true; |
132 } | 127 } |
133 | 128 |
134 private: | 129 private: |
135 scoped_refptr<ServiceDiscoveryHostClient> host_; | 130 scoped_refptr<ServiceDiscoveryHostClient> host_; |
136 std::string domain_; | 131 std::string domain_; |
137 net::AddressFamily address_family_; | 132 net::AddressFamily address_family_; |
138 const uint64 id_; | 133 const uint64 id_; |
139 bool started_; | 134 bool started_; |
140 }; | 135 }; |
141 | 136 |
142 ServiceDiscoveryHostClient::ServiceDiscoveryHostClient() : current_id_(0) { | 137 ServiceDiscoveryHostClient::ServiceDiscoveryHostClient() : current_id_(0) { |
143 callback_runner_ = base::MessageLoop::current()->message_loop_proxy(); | 138 callback_runner_ = base::MessageLoop::current()->message_loop_proxy(); |
| 139 io_runner_ = BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
144 } | 140 } |
145 | 141 |
146 ServiceDiscoveryHostClient::~ServiceDiscoveryHostClient() { | 142 ServiceDiscoveryHostClient::~ServiceDiscoveryHostClient() { |
147 // The ServiceDiscoveryHostClient may be destroyed from the IO thread or the | 143 // The ServiceDiscoveryHostClient may be destroyed from the IO thread or the |
148 // owning thread. | 144 // owning thread. |
149 DetachFromThread(); | 145 DetachFromThread(); |
150 DCHECK(service_watcher_callbacks_.empty()); | 146 DCHECK(service_watcher_callbacks_.empty()); |
151 DCHECK(service_resolver_callbacks_.empty()); | 147 DCHECK(service_resolver_callbacks_.empty()); |
152 DCHECK(domain_resolver_callbacks_.empty()); | 148 DCHECK(domain_resolver_callbacks_.empty()); |
153 } | 149 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 210 |
215 void ServiceDiscoveryHostClient::UnregisterLocalDomainResolverCallback( | 211 void ServiceDiscoveryHostClient::UnregisterLocalDomainResolverCallback( |
216 uint64 id) { | 212 uint64 id) { |
217 DCHECK(CalledOnValidThread()); | 213 DCHECK(CalledOnValidThread()); |
218 domain_resolver_callbacks_.erase(id); | 214 domain_resolver_callbacks_.erase(id); |
219 } | 215 } |
220 | 216 |
221 void ServiceDiscoveryHostClient::Start() { | 217 void ServiceDiscoveryHostClient::Start() { |
222 DCHECK(CalledOnValidThread()); | 218 DCHECK(CalledOnValidThread()); |
223 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 219 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
224 BrowserThread::PostTask( | 220 io_runner_->PostTask( |
225 BrowserThread::IO, | |
226 FROM_HERE, | 221 FROM_HERE, |
227 base::Bind(&ServiceDiscoveryHostClient::StartOnIOThread, this)); | 222 base::Bind(&ServiceDiscoveryHostClient::StartOnIOThread, this)); |
228 } | 223 } |
229 | 224 |
230 void ServiceDiscoveryHostClient::Shutdown() { | 225 void ServiceDiscoveryHostClient::Shutdown() { |
231 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 226 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
232 DCHECK(CalledOnValidThread()); | 227 DCHECK(CalledOnValidThread()); |
233 BrowserThread::PostTask( | 228 io_runner_->PostTask( |
234 BrowserThread::IO, | |
235 FROM_HERE, | 229 FROM_HERE, |
236 base::Bind(&ServiceDiscoveryHostClient::ShutdownOnIOThread, this)); | 230 base::Bind(&ServiceDiscoveryHostClient::ShutdownOnIOThread, this)); |
237 } | 231 } |
238 | 232 |
239 void ServiceDiscoveryHostClient::StartOnIOThread() { | 233 void ServiceDiscoveryHostClient::StartOnIOThread() { |
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
241 utility_host_ = UtilityProcessHost::Create( | 235 utility_host_ = UtilityProcessHost::Create( |
242 this, base::MessageLoopProxy::current().get())->AsWeakPtr(); | 236 this, base::MessageLoopProxy::current().get())->AsWeakPtr(); |
243 if (utility_host_) { | 237 if (utility_host_) { |
244 utility_host_->EnableZygote(); | 238 utility_host_->EnableZygote(); |
(...skipping 27 matching lines...) Expand all Loading... |
272 | 266 |
273 void ServiceDiscoveryHostClient::RestartOnIOThread() { | 267 void ServiceDiscoveryHostClient::RestartOnIOThread() { |
274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
275 | 269 |
276 ShutdownOnIOThread(); | 270 ShutdownOnIOThread(); |
277 StartOnIOThread(); | 271 StartOnIOThread(); |
278 } | 272 } |
279 | 273 |
280 void ServiceDiscoveryHostClient::Send(IPC::Message* msg) { | 274 void ServiceDiscoveryHostClient::Send(IPC::Message* msg) { |
281 DCHECK(CalledOnValidThread()); | 275 DCHECK(CalledOnValidThread()); |
282 BrowserThread::PostTask( | 276 io_runner_->PostTask( |
283 BrowserThread::IO, | |
284 FROM_HERE, | 277 FROM_HERE, |
285 base::Bind(&ServiceDiscoveryHostClient::SendOnIOThread, this, msg)); | 278 base::Bind(&ServiceDiscoveryHostClient::SendOnIOThread, this, msg)); |
286 } | 279 } |
287 | 280 |
288 void ServiceDiscoveryHostClient::SendOnIOThread(IPC::Message* msg) { | 281 void ServiceDiscoveryHostClient::SendOnIOThread(IPC::Message* msg) { |
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
290 if (utility_host_) | 283 if (utility_host_) |
291 utility_host_->Send(msg); | 284 utility_host_->Send(msg); |
292 } | 285 } |
293 | 286 |
294 void ServiceDiscoveryHostClient::OnIPAddressChanged() { | 287 void ServiceDiscoveryHostClient::OnIPAddressChanged() { |
295 BrowserThread::PostTask( | 288 io_runner_->PostTask( |
296 BrowserThread::IO, | |
297 FROM_HERE, | 289 FROM_HERE, |
298 base::Bind(&ServiceDiscoveryHostClient::RestartOnIOThread, this)); | 290 base::Bind(&ServiceDiscoveryHostClient::RestartOnIOThread, this)); |
299 | 291 |
300 WatcherCallbacks service_watcher_callbacks; | 292 WatcherCallbacks service_watcher_callbacks; |
301 service_watcher_callbacks_.swap(service_watcher_callbacks); | 293 service_watcher_callbacks_.swap(service_watcher_callbacks); |
302 | 294 |
303 for (WatcherCallbacks::iterator i = service_watcher_callbacks.begin(); | 295 for (WatcherCallbacks::iterator i = service_watcher_callbacks.begin(); |
304 i != service_watcher_callbacks.end(); i++) { | 296 i != service_watcher_callbacks.end(); i++) { |
305 if (!i->second.is_null()) { | 297 if (!i->second.is_null()) { |
306 i->second.Run(ServiceWatcher::UPDATE_INVALIDATED, ""); | 298 i->second.Run(ServiceWatcher::UPDATE_INVALIDATED, ""); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 void ServiceDiscoveryHostClientFactory::ReleaseClientInternal() { | 418 void ServiceDiscoveryHostClientFactory::ReleaseClientInternal() { |
427 DCHECK(CalledOnValidThread()); | 419 DCHECK(CalledOnValidThread()); |
428 references_--; | 420 references_--; |
429 if (references_ == 0) { | 421 if (references_ == 0) { |
430 instance_->Shutdown(); | 422 instance_->Shutdown(); |
431 instance_ = NULL; | 423 instance_ = NULL; |
432 } | 424 } |
433 } | 425 } |
434 | 426 |
435 } // namespace local_discovery | 427 } // namespace local_discovery |
OLD | NEW |