OLD | NEW |
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 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // FakeURLRequestJobFactory returns NULL for all job creation requests and false | 59 // FakeURLRequestJobFactory returns NULL for all job creation requests and false |
60 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to | 60 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to |
61 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of | 61 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of |
62 // MaybeCreateJobWithProtocolHandler() indicates whether the | 62 // MaybeCreateJobWithProtocolHandler() indicates whether the |
63 // ProtocolHandlerRegistry properly handled a job creation request. | 63 // ProtocolHandlerRegistry properly handled a job creation request. |
64 class FakeURLRequestJobFactory : public net::URLRequestJobFactory { | 64 class FakeURLRequestJobFactory : public net::URLRequestJobFactory { |
65 // net::URLRequestJobFactory implementation: | 65 // net::URLRequestJobFactory implementation: |
66 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 66 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
67 const std::string& scheme, | 67 const std::string& scheme, |
68 net::URLRequest* request, | 68 net::URLRequest* request, |
69 net::NetworkDelegate* network_delegate) const OVERRIDE { | 69 net::NetworkDelegate* network_delegate) const override { |
70 return NULL; | 70 return NULL; |
71 } | 71 } |
72 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 72 virtual bool IsHandledProtocol(const std::string& scheme) const override { |
73 return false; | 73 return false; |
74 } | 74 } |
75 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 75 virtual bool IsHandledURL(const GURL& url) const override { |
76 return false; | 76 return false; |
77 } | 77 } |
78 virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { | 78 virtual bool IsSafeRedirectTarget(const GURL& location) const override { |
79 return true; | 79 return true; |
80 } | 80 } |
81 }; | 81 }; |
82 | 82 |
83 void AssertWillHandleIO( | 83 void AssertWillHandleIO( |
84 const std::string& scheme, | 84 const std::string& scheme, |
85 bool expected, | 85 bool expected, |
86 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { | 86 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
88 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>( | 88 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>( |
(...skipping 29 matching lines...) Expand all Loading... |
118 bool is_default) { | 118 bool is_default) { |
119 base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url); | 119 base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url); |
120 value->SetBoolean("default", is_default); | 120 value->SetBoolean("default", is_default); |
121 return value; | 121 return value; |
122 } | 122 } |
123 | 123 |
124 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { | 124 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
125 public: | 125 public: |
126 FakeDelegate() : force_os_failure_(false) {} | 126 FakeDelegate() : force_os_failure_(false) {} |
127 virtual ~FakeDelegate() { } | 127 virtual ~FakeDelegate() { } |
128 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { | 128 virtual void RegisterExternalHandler(const std::string& protocol) override { |
129 ASSERT_TRUE( | 129 ASSERT_TRUE( |
130 registered_protocols_.find(protocol) == registered_protocols_.end()); | 130 registered_protocols_.find(protocol) == registered_protocols_.end()); |
131 registered_protocols_.insert(protocol); | 131 registered_protocols_.insert(protocol); |
132 } | 132 } |
133 | 133 |
134 virtual void DeregisterExternalHandler(const std::string& protocol) OVERRIDE { | 134 virtual void DeregisterExternalHandler(const std::string& protocol) override { |
135 registered_protocols_.erase(protocol); | 135 registered_protocols_.erase(protocol); |
136 } | 136 } |
137 | 137 |
138 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( | 138 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( |
139 ShellIntegration::DefaultWebClientObserver* observer, | 139 ShellIntegration::DefaultWebClientObserver* observer, |
140 const std::string& protocol) OVERRIDE; | 140 const std::string& protocol) override; |
141 | 141 |
142 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( | 142 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( |
143 ProtocolHandlerRegistry* registry) OVERRIDE; | 143 ProtocolHandlerRegistry* registry) override; |
144 | 144 |
145 virtual void RegisterWithOSAsDefaultClient( | 145 virtual void RegisterWithOSAsDefaultClient( |
146 const std::string& protocol, | 146 const std::string& protocol, |
147 ProtocolHandlerRegistry* reg) OVERRIDE { | 147 ProtocolHandlerRegistry* reg) override { |
148 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, | 148 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, |
149 reg); | 149 reg); |
150 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); | 150 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); |
151 } | 151 } |
152 | 152 |
153 virtual bool IsExternalHandlerRegistered( | 153 virtual bool IsExternalHandlerRegistered( |
154 const std::string& protocol) OVERRIDE { | 154 const std::string& protocol) override { |
155 return registered_protocols_.find(protocol) != registered_protocols_.end(); | 155 return registered_protocols_.find(protocol) != registered_protocols_.end(); |
156 } | 156 } |
157 | 157 |
158 bool IsFakeRegisteredWithOS(const std::string& protocol) { | 158 bool IsFakeRegisteredWithOS(const std::string& protocol) { |
159 return os_registered_protocols_.find(protocol) != | 159 return os_registered_protocols_.find(protocol) != |
160 os_registered_protocols_.end(); | 160 os_registered_protocols_.end(); |
161 } | 161 } |
162 | 162 |
163 void FakeRegisterWithOS(const std::string& protocol) { | 163 void FakeRegisterWithOS(const std::string& protocol) { |
164 os_registered_protocols_.insert(protocol); | 164 os_registered_protocols_.insert(protocol); |
(...skipping 17 matching lines...) Expand all Loading... |
182 | 182 |
183 class FakeClientObserver | 183 class FakeClientObserver |
184 : public ProtocolHandlerRegistry::DefaultClientObserver { | 184 : public ProtocolHandlerRegistry::DefaultClientObserver { |
185 public: | 185 public: |
186 FakeClientObserver(ProtocolHandlerRegistry* registry, | 186 FakeClientObserver(ProtocolHandlerRegistry* registry, |
187 FakeDelegate* registry_delegate) | 187 FakeDelegate* registry_delegate) |
188 : ProtocolHandlerRegistry::DefaultClientObserver(registry), | 188 : ProtocolHandlerRegistry::DefaultClientObserver(registry), |
189 delegate_(registry_delegate) {} | 189 delegate_(registry_delegate) {} |
190 | 190 |
191 virtual void SetDefaultWebClientUIState( | 191 virtual void SetDefaultWebClientUIState( |
192 ShellIntegration::DefaultWebClientUIState state) OVERRIDE { | 192 ShellIntegration::DefaultWebClientUIState state) override { |
193 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( | 193 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( |
194 state); | 194 state); |
195 if (state == ShellIntegration::STATE_IS_DEFAULT) { | 195 if (state == ShellIntegration::STATE_IS_DEFAULT) { |
196 delegate_->FakeRegisterWithOS(worker_->protocol()); | 196 delegate_->FakeRegisterWithOS(worker_->protocol()); |
197 } | 197 } |
198 if (state != ShellIntegration::STATE_PROCESSING) { | 198 if (state != ShellIntegration::STATE_PROCESSING) { |
199 base::MessageLoop::current()->Quit(); | 199 base::MessageLoop::current()->Quit(); |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 private: | 203 private: |
204 FakeDelegate* delegate_; | 204 FakeDelegate* delegate_; |
205 }; | 205 }; |
206 | 206 |
207 class FakeProtocolClientWorker | 207 class FakeProtocolClientWorker |
208 : public ShellIntegration::DefaultProtocolClientWorker { | 208 : public ShellIntegration::DefaultProtocolClientWorker { |
209 public: | 209 public: |
210 FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer, | 210 FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer, |
211 const std::string& protocol, | 211 const std::string& protocol, |
212 bool force_failure) | 212 bool force_failure) |
213 : ShellIntegration::DefaultProtocolClientWorker(observer, protocol), | 213 : ShellIntegration::DefaultProtocolClientWorker(observer, protocol), |
214 force_failure_(force_failure) {} | 214 force_failure_(force_failure) {} |
215 | 215 |
216 private: | 216 private: |
217 virtual ~FakeProtocolClientWorker() {} | 217 virtual ~FakeProtocolClientWorker() {} |
218 | 218 |
219 virtual ShellIntegration::DefaultWebClientState CheckIsDefault() OVERRIDE { | 219 virtual ShellIntegration::DefaultWebClientState CheckIsDefault() override { |
220 if (force_failure_) { | 220 if (force_failure_) { |
221 return ShellIntegration::NOT_DEFAULT; | 221 return ShellIntegration::NOT_DEFAULT; |
222 } else { | 222 } else { |
223 return ShellIntegration::IS_DEFAULT; | 223 return ShellIntegration::IS_DEFAULT; |
224 } | 224 } |
225 } | 225 } |
226 | 226 |
227 virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE { | 227 virtual bool SetAsDefault(bool interactive_permitted) override { |
228 return true; | 228 return true; |
229 } | 229 } |
230 | 230 |
231 private: | 231 private: |
232 bool force_failure_; | 232 bool force_failure_; |
233 }; | 233 }; |
234 | 234 |
235 ProtocolHandlerRegistry::DefaultClientObserver* | 235 ProtocolHandlerRegistry::DefaultClientObserver* |
236 FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) { | 236 FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) { |
237 return new FakeClientObserver(registry, this); | 237 return new FakeClientObserver(registry, this); |
(...skipping 13 matching lines...) Expand all Loading... |
251 notification_registrar_.Add(this, | 251 notification_registrar_.Add(this, |
252 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | 252 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
253 content::Source<content::BrowserContext>(context)); | 253 content::Source<content::BrowserContext>(context)); |
254 } | 254 } |
255 | 255 |
256 int events() { return events_; } | 256 int events() { return events_; } |
257 bool notified() { return events_ > 0; } | 257 bool notified() { return events_ > 0; } |
258 void Clear() { events_ = 0; } | 258 void Clear() { events_ = 0; } |
259 virtual void Observe(int type, | 259 virtual void Observe(int type, |
260 const content::NotificationSource& source, | 260 const content::NotificationSource& source, |
261 const content::NotificationDetails& details) OVERRIDE { | 261 const content::NotificationDetails& details) override { |
262 ++events_; | 262 ++events_; |
263 } | 263 } |
264 | 264 |
265 int events_; | 265 int events_; |
266 content::NotificationRegistrar notification_registrar_; | 266 content::NotificationRegistrar notification_registrar_; |
267 }; | 267 }; |
268 | 268 |
269 class QueryProtocolHandlerOnChange | 269 class QueryProtocolHandlerOnChange |
270 : public content::NotificationObserver { | 270 : public content::NotificationObserver { |
271 public: | 271 public: |
272 QueryProtocolHandlerOnChange(content::BrowserContext* context, | 272 QueryProtocolHandlerOnChange(content::BrowserContext* context, |
273 ProtocolHandlerRegistry* registry) | 273 ProtocolHandlerRegistry* registry) |
274 : local_registry_(registry), | 274 : local_registry_(registry), |
275 called_(false), | 275 called_(false), |
276 notification_registrar_() { | 276 notification_registrar_() { |
277 notification_registrar_.Add(this, | 277 notification_registrar_.Add(this, |
278 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | 278 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
279 content::Source<content::BrowserContext>(context)); | 279 content::Source<content::BrowserContext>(context)); |
280 } | 280 } |
281 | 281 |
282 virtual void Observe(int type, | 282 virtual void Observe(int type, |
283 const content::NotificationSource& source, | 283 const content::NotificationSource& source, |
284 const content::NotificationDetails& details) OVERRIDE { | 284 const content::NotificationDetails& details) override { |
285 std::vector<std::string> output; | 285 std::vector<std::string> output; |
286 local_registry_->GetRegisteredProtocols(&output); | 286 local_registry_->GetRegisteredProtocols(&output); |
287 called_ = true; | 287 called_ = true; |
288 } | 288 } |
289 | 289 |
290 ProtocolHandlerRegistry* local_registry_; | 290 ProtocolHandlerRegistry* local_registry_; |
291 bool called_; | 291 bool called_; |
292 content::NotificationRegistrar notification_registrar_; | 292 content::NotificationRegistrar notification_registrar_; |
293 }; | 293 }; |
294 | 294 |
295 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because | 295 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because |
296 // it can't check the thread id (since net can't depend on content.) We want | 296 // it can't check the thread id (since net can't depend on content.) We want |
297 // to harness our tests so all threads use the same loop allowing us to | 297 // to harness our tests so all threads use the same loop allowing us to |
298 // guarantee all messages are processed.) By overriding the IsType method | 298 // guarantee all messages are processed.) By overriding the IsType method |
299 // we basically ignore the supplied message loop type, and instead infer | 299 // we basically ignore the supplied message loop type, and instead infer |
300 // our type based on the current thread. GO DEPENDENCY INJECTION! | 300 // our type based on the current thread. GO DEPENDENCY INJECTION! |
301 class TestMessageLoop : public base::MessageLoop { | 301 class TestMessageLoop : public base::MessageLoop { |
302 public: | 302 public: |
303 TestMessageLoop() {} | 303 TestMessageLoop() {} |
304 virtual ~TestMessageLoop() {} | 304 virtual ~TestMessageLoop() {} |
305 virtual bool IsType(base::MessageLoop::Type type) const OVERRIDE { | 305 virtual bool IsType(base::MessageLoop::Type type) const override { |
306 switch (type) { | 306 switch (type) { |
307 case base::MessageLoop::TYPE_UI: | 307 case base::MessageLoop::TYPE_UI: |
308 return BrowserThread::CurrentlyOn(BrowserThread::UI); | 308 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
309 case base::MessageLoop::TYPE_IO: | 309 case base::MessageLoop::TYPE_IO: |
310 return BrowserThread::CurrentlyOn(BrowserThread::IO); | 310 return BrowserThread::CurrentlyOn(BrowserThread::IO); |
311 #if defined(OS_ANDROID) | 311 #if defined(OS_ANDROID) |
312 case base::MessageLoop::TYPE_JAVA: // fall-through | 312 case base::MessageLoop::TYPE_JAVA: // fall-through |
313 #endif // defined(OS_ANDROID) | 313 #endif // defined(OS_ANDROID) |
314 case base::MessageLoop::TYPE_CUSTOM: | 314 case base::MessageLoop::TYPE_CUSTOM: |
315 case base::MessageLoop::TYPE_DEFAULT: | 315 case base::MessageLoop::TYPE_DEFAULT: |
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 // added to pref. | 1116 // added to pref. |
1117 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); | 1117 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); |
1118 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); | 1118 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
1119 | 1119 |
1120 registry()->RemoveIgnoredHandler(p2u1); | 1120 registry()->RemoveIgnoredHandler(p2u1); |
1121 | 1121 |
1122 // p2u1 installed by user and policy, so it is removed from pref alone. | 1122 // p2u1 installed by user and policy, so it is removed from pref alone. |
1123 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); | 1123 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); |
1124 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); | 1124 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); |
1125 } | 1125 } |
OLD | NEW |