OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/lock.h" | |
10 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/synchronization/lock.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 // SyncHostResolverBridge::Core ---------------------------------------------- | 17 // SyncHostResolverBridge::Core ---------------------------------------------- |
18 | 18 |
19 class SyncHostResolverBridge::Core | 19 class SyncHostResolverBridge::Core |
20 : public base::RefCountedThreadSafe<SyncHostResolverBridge::Core> { | 20 : public base::RefCountedThreadSafe<SyncHostResolverBridge::Core> { |
21 public: | 21 public: |
22 Core(HostResolver* resolver, MessageLoop* host_resolver_loop); | 22 Core(HostResolver* resolver, MessageLoop* host_resolver_loop); |
23 | 23 |
24 int ResolveSynchronously(const HostResolver::RequestInfo& info, | 24 int ResolveSynchronously(const HostResolver::RequestInfo& info, |
25 AddressList* addresses); | 25 AddressList* addresses); |
26 | 26 |
27 // Returns true if Shutdown() has been called. | 27 // Returns true if Shutdown() has been called. |
28 bool HasShutdown() const { | 28 bool HasShutdown() const { |
29 AutoLock l(lock_); | 29 base::AutoLock l(lock_); |
30 return HasShutdownLocked(); | 30 return HasShutdownLocked(); |
31 } | 31 } |
32 | 32 |
33 // Called on |host_resolver_loop_|. | 33 // Called on |host_resolver_loop_|. |
34 void Shutdown(); | 34 void Shutdown(); |
35 | 35 |
36 private: | 36 private: |
37 friend class base::RefCountedThreadSafe<SyncHostResolverBridge::Core>; | 37 friend class base::RefCountedThreadSafe<SyncHostResolverBridge::Core>; |
38 | 38 |
39 bool HasShutdownLocked() const { | 39 bool HasShutdownLocked() const { |
(...skipping 19 matching lines...) Expand all Loading... |
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 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( | 78 ALLOW_THIS_IN_INITIALIZER_LIST( |
79 callback_(this, &Core::OnResolveCompletion)), | 79 callback_(this, &Core::OnResolveCompletion)), |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 err_ = result; | 114 err_ = result; |
115 outstanding_request_ = NULL; | 115 outstanding_request_ = NULL; |
116 event_.Signal(); | 116 event_.Signal(); |
117 } | 117 } |
118 | 118 |
119 int SyncHostResolverBridge::Core::WaitForResolveCompletion() { | 119 int SyncHostResolverBridge::Core::WaitForResolveCompletion() { |
120 DCHECK_NE(MessageLoop::current(), host_resolver_loop_); | 120 DCHECK_NE(MessageLoop::current(), host_resolver_loop_); |
121 event_.Wait(); | 121 event_.Wait(); |
122 | 122 |
123 { | 123 { |
124 AutoLock l(lock_); | 124 base::AutoLock l(lock_); |
125 if (HasShutdownLocked()) | 125 if (HasShutdownLocked()) |
126 return ERR_ABORTED; | 126 return ERR_ABORTED; |
127 event_.Reset(); | 127 event_.Reset(); |
128 } | 128 } |
129 | 129 |
130 return err_; | 130 return err_; |
131 } | 131 } |
132 | 132 |
133 void SyncHostResolverBridge::Core::Shutdown() { | 133 void SyncHostResolverBridge::Core::Shutdown() { |
134 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); | 134 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); |
135 | 135 |
136 if (outstanding_request_) { | 136 if (outstanding_request_) { |
137 host_resolver_->CancelRequest(outstanding_request_); | 137 host_resolver_->CancelRequest(outstanding_request_); |
138 outstanding_request_ = NULL; | 138 outstanding_request_ = NULL; |
139 } | 139 } |
140 | 140 |
141 { | 141 { |
142 AutoLock l(lock_); | 142 base::AutoLock l(lock_); |
143 has_shutdown_ = true; | 143 has_shutdown_ = true; |
144 } | 144 } |
145 | 145 |
146 // Wake up the PAC thread in case it was waiting for resolve completion. | 146 // Wake up the PAC thread in case it was waiting for resolve completion. |
147 event_.Signal(); | 147 event_.Signal(); |
148 } | 148 } |
149 | 149 |
150 // SyncHostResolverBridge ----------------------------------------------------- | 150 // SyncHostResolverBridge ----------------------------------------------------- |
151 | 151 |
152 SyncHostResolverBridge::SyncHostResolverBridge(HostResolver* host_resolver, | 152 SyncHostResolverBridge::SyncHostResolverBridge(HostResolver* host_resolver, |
(...skipping 29 matching lines...) Expand all Loading... |
182 void SyncHostResolverBridge::RemoveObserver(Observer* observer) { | 182 void SyncHostResolverBridge::RemoveObserver(Observer* observer) { |
183 NOTREACHED(); | 183 NOTREACHED(); |
184 } | 184 } |
185 | 185 |
186 void SyncHostResolverBridge::Shutdown() { | 186 void SyncHostResolverBridge::Shutdown() { |
187 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); | 187 DCHECK_EQ(MessageLoop::current(), host_resolver_loop_); |
188 core_->Shutdown(); | 188 core_->Shutdown(); |
189 } | 189 } |
190 | 190 |
191 } // namespace net | 191 } // namespace net |
OLD | NEW |