OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/proxy/sync_host_resolver_bridge.h" | 5 #include "net/proxy/sync_host_resolver_bridge.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 | 17 |
(...skipping 28 matching lines...) Expand all Loading... |
45 AddressList* addresses); | 46 AddressList* addresses); |
46 | 47 |
47 // Called on |host_resolver_loop_|. | 48 // Called on |host_resolver_loop_|. |
48 void OnResolveCompletion(int result); | 49 void OnResolveCompletion(int result); |
49 | 50 |
50 // Not called on |host_resolver_loop_|. | 51 // Not called on |host_resolver_loop_|. |
51 int WaitForResolveCompletion(); | 52 int WaitForResolveCompletion(); |
52 | 53 |
53 HostResolver* const host_resolver_; | 54 HostResolver* const host_resolver_; |
54 MessageLoop* const host_resolver_loop_; | 55 MessageLoop* const host_resolver_loop_; |
55 net::OldCompletionCallbackImpl<Core> callback_; | |
56 // The result from the current request (set on |host_resolver_loop_|). | 56 // The result from the current request (set on |host_resolver_loop_|). |
57 int err_; | 57 int err_; |
58 // The currently outstanding request to |host_resolver_|, or NULL. | 58 // The currently outstanding request to |host_resolver_|, or NULL. |
59 HostResolver::RequestHandle outstanding_request_; | 59 HostResolver::RequestHandle outstanding_request_; |
60 | 60 |
61 // Event to notify completion of resolve request. We always Signal() on | 61 // Event to notify completion of resolve request. We always Signal() on |
62 // |host_resolver_loop_| and Wait() on a different thread. | 62 // |host_resolver_loop_| and Wait() on a different thread. |
63 base::WaitableEvent event_; | 63 base::WaitableEvent event_; |
64 | 64 |
65 // True if Shutdown() has been called. Must hold |lock_| to access it. | 65 // True if Shutdown() has been called. Must hold |lock_| to access it. |
66 bool has_shutdown_; | 66 bool has_shutdown_; |
67 | 67 |
68 // Mutex to guard accesses to |has_shutdown_|. | 68 // Mutex to guard accesses to |has_shutdown_|. |
69 mutable base::Lock lock_; | 69 mutable base::Lock lock_; |
70 | 70 |
71 DISALLOW_COPY_AND_ASSIGN(Core); | 71 DISALLOW_COPY_AND_ASSIGN(Core); |
72 }; | 72 }; |
73 | 73 |
74 SyncHostResolverBridge::Core::Core(HostResolver* host_resolver, | 74 SyncHostResolverBridge::Core::Core(HostResolver* host_resolver, |
75 MessageLoop* host_resolver_loop) | 75 MessageLoop* host_resolver_loop) |
76 : host_resolver_(host_resolver), | 76 : host_resolver_(host_resolver), |
77 host_resolver_loop_(host_resolver_loop), | 77 host_resolver_loop_(host_resolver_loop), |
78 ALLOW_THIS_IN_INITIALIZER_LIST( | |
79 callback_(this, &Core::OnResolveCompletion)), | |
80 err_(0), | 78 err_(0), |
81 outstanding_request_(NULL), | 79 outstanding_request_(NULL), |
82 event_(true, false), | 80 event_(true, false), |
83 has_shutdown_(false) {} | 81 has_shutdown_(false) {} |
84 | 82 |
85 int SyncHostResolverBridge::Core::ResolveSynchronously( | 83 int SyncHostResolverBridge::Core::ResolveSynchronously( |
86 const HostResolver::RequestInfo& info, | 84 const HostResolver::RequestInfo& info, |
87 net::AddressList* addresses) { | 85 net::AddressList* addresses) { |
88 // Otherwise start an async resolve on the resolver's thread. | 86 // Otherwise start an async resolve on the resolver's thread. |
89 host_resolver_loop_->PostTask( | 87 host_resolver_loop_->PostTask( |
90 FROM_HERE, | 88 FROM_HERE, |
91 NewRunnableMethod(this, &Core::StartResolve, | 89 NewRunnableMethod(this, &Core::StartResolve, |
92 info, addresses)); | 90 info, addresses)); |
93 | 91 |
94 return WaitForResolveCompletion(); | 92 return WaitForResolveCompletion(); |
95 } | 93 } |
96 | 94 |
97 void SyncHostResolverBridge::Core::StartResolve( | 95 void SyncHostResolverBridge::Core::StartResolve( |
98 const HostResolver::RequestInfo& info, | 96 const HostResolver::RequestInfo& info, |
99 net::AddressList* addresses) { | 97 net::AddressList* addresses) { |
100 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); | 98 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); |
101 DCHECK(!outstanding_request_); | 99 DCHECK(!outstanding_request_); |
102 | 100 |
103 if (HasShutdown()) | 101 if (HasShutdown()) |
104 return; | 102 return; |
105 | 103 |
106 int error = host_resolver_->Resolve( | 104 int error = host_resolver_->Resolve( |
107 info, addresses, &callback_, &outstanding_request_, BoundNetLog()); | 105 info, addresses, base::Bind(&Core::OnResolveCompletion, this), |
| 106 &outstanding_request_, BoundNetLog()); |
108 if (error != ERR_IO_PENDING) | 107 if (error != ERR_IO_PENDING) |
109 OnResolveCompletion(error); // Completed synchronously. | 108 OnResolveCompletion(error); // Completed synchronously. |
110 } | 109 } |
111 | 110 |
112 void SyncHostResolverBridge::Core::OnResolveCompletion(int result) { | 111 void SyncHostResolverBridge::Core::OnResolveCompletion(int result) { |
113 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); | 112 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); |
114 err_ = result; | 113 err_ = result; |
115 outstanding_request_ = NULL; | 114 outstanding_request_ = NULL; |
116 event_.Signal(); | 115 event_.Signal(); |
117 } | 116 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 AddressList* addresses) { | 163 AddressList* addresses) { |
165 return core_->ResolveSynchronously(info, addresses); | 164 return core_->ResolveSynchronously(info, addresses); |
166 } | 165 } |
167 | 166 |
168 void SyncHostResolverBridge::Shutdown() { | 167 void SyncHostResolverBridge::Shutdown() { |
169 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); | 168 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); |
170 core_->Shutdown(); | 169 core_->Shutdown(); |
171 } | 170 } |
172 | 171 |
173 } // namespace net | 172 } // namespace net |
OLD | NEW |