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.h" | 10 #include "base/message_loop.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 ProtocolHandler* protocol_handler) OVERRIDE { | 63 ProtocolHandler* protocol_handler) OVERRIDE { |
64 return false; | 64 return false; |
65 } | 65 } |
66 virtual void AddInterceptor(Interceptor* interceptor) OVERRIDE { | 66 virtual void AddInterceptor(Interceptor* interceptor) OVERRIDE { |
67 } | 67 } |
68 virtual net::URLRequestJob* MaybeCreateJobWithInterceptor( | 68 virtual net::URLRequestJob* MaybeCreateJobWithInterceptor( |
69 net::URLRequest* request, | 69 net::URLRequest* request, |
70 net::NetworkDelegate* network_delegate) const OVERRIDE { | 70 net::NetworkDelegate* network_delegate) const OVERRIDE { |
71 return NULL; | 71 return NULL; |
72 } | 72 } |
73 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( | 73 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
74 const std::string& scheme, | 74 const std::string& scheme, |
75 net::URLRequest* request, | 75 net::URLRequest* request, |
76 net::NetworkDelegate* network_delegate) const OVERRIDE { | 76 net::NetworkDelegate* network_delegate) const OVERRIDE { |
77 return NULL; | 77 return NULL; |
78 } | 78 } |
79 net::URLRequestJob* MaybeInterceptRedirect( | 79 virtual net::URLRequestJob* MaybeInterceptRedirect( |
80 const GURL& location, | 80 const GURL& location, |
81 net::URLRequest* request, | 81 net::URLRequest* request, |
82 net::NetworkDelegate* network_delegate) const OVERRIDE { | 82 net::NetworkDelegate* network_delegate) const OVERRIDE { |
83 return NULL; | 83 return NULL; |
84 } | 84 } |
85 net::URLRequestJob* MaybeInterceptResponse( | 85 virtual net::URLRequestJob* MaybeInterceptResponse( |
86 net::URLRequest* request, | 86 net::URLRequest* request, |
87 net::NetworkDelegate* network_delegate) const OVERRIDE { | 87 net::NetworkDelegate* network_delegate) const OVERRIDE { |
88 return NULL; | 88 return NULL; |
89 } | 89 } |
90 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { | 90 virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
91 return false; | 91 return false; |
92 } | 92 } |
93 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { | 93 virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
94 return false; | 94 return false; |
95 } | 95 } |
(...skipping 21 matching lines...) Expand all Loading... |
117 scheme, | 117 scheme, |
118 expected, | 118 expected, |
119 base::Unretained(interceptor))); | 119 base::Unretained(interceptor))); |
120 MessageLoop::current()->RunUntilIdle(); | 120 MessageLoop::current()->RunUntilIdle(); |
121 } | 121 } |
122 | 122 |
123 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { | 123 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
124 public: | 124 public: |
125 FakeDelegate() : force_os_failure_(false) {} | 125 FakeDelegate() : force_os_failure_(false) {} |
126 virtual ~FakeDelegate() { } | 126 virtual ~FakeDelegate() { } |
127 virtual void RegisterExternalHandler(const std::string& protocol) { | 127 virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { |
128 ASSERT_TRUE( | 128 ASSERT_TRUE( |
129 registered_protocols_.find(protocol) == registered_protocols_.end()); | 129 registered_protocols_.find(protocol) == registered_protocols_.end()); |
130 registered_protocols_.insert(protocol); | 130 registered_protocols_.insert(protocol); |
131 } | 131 } |
132 | 132 |
133 virtual void DeregisterExternalHandler(const std::string& protocol) { | 133 virtual void DeregisterExternalHandler(const std::string& protocol) OVERRIDE { |
134 registered_protocols_.erase(protocol); | 134 registered_protocols_.erase(protocol); |
135 } | 135 } |
136 | 136 |
137 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( | 137 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( |
138 ShellIntegration::DefaultWebClientObserver* observer, | 138 ShellIntegration::DefaultWebClientObserver* observer, |
139 const std::string& protocol); | 139 const std::string& protocol) OVERRIDE; |
140 | 140 |
141 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( | 141 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( |
142 ProtocolHandlerRegistry* registry); | 142 ProtocolHandlerRegistry* registry) OVERRIDE; |
143 | 143 |
144 virtual void RegisterWithOSAsDefaultClient(const std::string& protocol, | 144 virtual void RegisterWithOSAsDefaultClient( |
145 ProtocolHandlerRegistry* reg) { | 145 const std::string& protocol, |
| 146 ProtocolHandlerRegistry* reg) OVERRIDE { |
146 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, | 147 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, |
147 reg); | 148 reg); |
148 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); | 149 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); |
149 } | 150 } |
150 | 151 |
151 virtual bool IsExternalHandlerRegistered(const std::string& protocol) { | 152 virtual bool IsExternalHandlerRegistered( |
| 153 const std::string& protocol) OVERRIDE { |
152 return registered_protocols_.find(protocol) != registered_protocols_.end(); | 154 return registered_protocols_.find(protocol) != registered_protocols_.end(); |
153 } | 155 } |
154 | 156 |
155 bool IsFakeRegisteredWithOS(const std::string& protocol) { | 157 bool IsFakeRegisteredWithOS(const std::string& protocol) { |
156 return os_registered_protocols_.find(protocol) != | 158 return os_registered_protocols_.find(protocol) != |
157 os_registered_protocols_.end(); | 159 os_registered_protocols_.end(); |
158 } | 160 } |
159 | 161 |
160 void FakeRegisterWithOS(const std::string& protocol) { | 162 void FakeRegisterWithOS(const std::string& protocol) { |
161 os_registered_protocols_.insert(protocol); | 163 os_registered_protocols_.insert(protocol); |
(...skipping 17 matching lines...) Expand all Loading... |
179 | 181 |
180 class FakeClientObserver | 182 class FakeClientObserver |
181 : public ProtocolHandlerRegistry::DefaultClientObserver { | 183 : public ProtocolHandlerRegistry::DefaultClientObserver { |
182 public: | 184 public: |
183 FakeClientObserver(ProtocolHandlerRegistry* registry, | 185 FakeClientObserver(ProtocolHandlerRegistry* registry, |
184 FakeDelegate* registry_delegate) | 186 FakeDelegate* registry_delegate) |
185 : ProtocolHandlerRegistry::DefaultClientObserver(registry), | 187 : ProtocolHandlerRegistry::DefaultClientObserver(registry), |
186 delegate_(registry_delegate) {} | 188 delegate_(registry_delegate) {} |
187 | 189 |
188 virtual void SetDefaultWebClientUIState( | 190 virtual void SetDefaultWebClientUIState( |
189 ShellIntegration::DefaultWebClientUIState state) { | 191 ShellIntegration::DefaultWebClientUIState state) OVERRIDE { |
190 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( | 192 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( |
191 state); | 193 state); |
192 if (state == ShellIntegration::STATE_IS_DEFAULT) { | 194 if (state == ShellIntegration::STATE_IS_DEFAULT) { |
193 delegate_->FakeRegisterWithOS(worker_->protocol()); | 195 delegate_->FakeRegisterWithOS(worker_->protocol()); |
194 } | 196 } |
195 if (state != ShellIntegration::STATE_PROCESSING) { | 197 if (state != ShellIntegration::STATE_PROCESSING) { |
196 MessageLoop::current()->Quit(); | 198 MessageLoop::current()->Quit(); |
197 } | 199 } |
198 } | 200 } |
199 | 201 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 notification_registrar_.Add(this, | 250 notification_registrar_.Add(this, |
249 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | 251 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
250 content::Source<Profile>(profile)); | 252 content::Source<Profile>(profile)); |
251 } | 253 } |
252 | 254 |
253 int events() { return events_; } | 255 int events() { return events_; } |
254 bool notified() { return events_ > 0; } | 256 bool notified() { return events_ > 0; } |
255 void Clear() { events_ = 0; } | 257 void Clear() { events_ = 0; } |
256 virtual void Observe(int type, | 258 virtual void Observe(int type, |
257 const content::NotificationSource& source, | 259 const content::NotificationSource& source, |
258 const content::NotificationDetails& details) { | 260 const content::NotificationDetails& details) OVERRIDE { |
259 ++events_; | 261 ++events_; |
260 } | 262 } |
261 | 263 |
262 int events_; | 264 int events_; |
263 content::NotificationRegistrar notification_registrar_; | 265 content::NotificationRegistrar notification_registrar_; |
264 }; | 266 }; |
265 | 267 |
266 class QueryProtocolHandlerOnChange | 268 class QueryProtocolHandlerOnChange |
267 : public content::NotificationObserver { | 269 : public content::NotificationObserver { |
268 public: | 270 public: |
269 QueryProtocolHandlerOnChange(Profile* profile, | 271 QueryProtocolHandlerOnChange(Profile* profile, |
270 ProtocolHandlerRegistry* registry) | 272 ProtocolHandlerRegistry* registry) |
271 : local_registry_(registry), | 273 : local_registry_(registry), |
272 called_(false), | 274 called_(false), |
273 notification_registrar_() { | 275 notification_registrar_() { |
274 notification_registrar_.Add(this, | 276 notification_registrar_.Add(this, |
275 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, | 277 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
276 content::Source<Profile>(profile)); | 278 content::Source<Profile>(profile)); |
277 } | 279 } |
278 | 280 |
279 virtual void Observe(int type, | 281 virtual void Observe(int type, |
280 const content::NotificationSource& source, | 282 const content::NotificationSource& source, |
281 const content::NotificationDetails& details) { | 283 const content::NotificationDetails& details) OVERRIDE { |
282 std::vector<std::string> output; | 284 std::vector<std::string> output; |
283 local_registry_->GetRegisteredProtocols(&output); | 285 local_registry_->GetRegisteredProtocols(&output); |
284 called_ = true; | 286 called_ = true; |
285 } | 287 } |
286 | 288 |
287 ProtocolHandlerRegistry* local_registry_; | 289 ProtocolHandlerRegistry* local_registry_; |
288 bool called_; | 290 bool called_; |
289 content::NotificationRegistrar notification_registrar_; | 291 content::NotificationRegistrar notification_registrar_; |
290 }; | 292 }; |
291 | 293 |
292 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because | 294 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because |
293 // it can't check the thread id (since net can't depend on content.) We want | 295 // it can't check the thread id (since net can't depend on content.) We want |
294 // to harness our tests so all threads use the same loop allowing us to | 296 // to harness our tests so all threads use the same loop allowing us to |
295 // guarantee all messages are processed.) By overriding the IsType method | 297 // guarantee all messages are processed.) By overriding the IsType method |
296 // we basically ignore the supplied message loop type, and instead infer | 298 // we basically ignore the supplied message loop type, and instead infer |
297 // our type based on the current thread. GO DEPENDENCY INJECTION! | 299 // our type based on the current thread. GO DEPENDENCY INJECTION! |
298 class TestMessageLoop : public MessageLoop { | 300 class TestMessageLoop : public MessageLoop { |
299 public: | 301 public: |
300 TestMessageLoop() : MessageLoop(MessageLoop::TYPE_DEFAULT) {} | 302 TestMessageLoop() : MessageLoop(MessageLoop::TYPE_DEFAULT) {} |
301 ~TestMessageLoop() {} | 303 virtual ~TestMessageLoop() {} |
302 virtual bool IsType(MessageLoop::Type type) const OVERRIDE { | 304 virtual bool IsType(MessageLoop::Type type) const OVERRIDE { |
303 switch (type) { | 305 switch (type) { |
304 case MessageLoop::TYPE_UI: | 306 case MessageLoop::TYPE_UI: |
305 return BrowserThread::CurrentlyOn(BrowserThread::UI); | 307 return BrowserThread::CurrentlyOn(BrowserThread::UI); |
306 case MessageLoop::TYPE_IO: | 308 case MessageLoop::TYPE_IO: |
307 return BrowserThread::CurrentlyOn(BrowserThread::IO); | 309 return BrowserThread::CurrentlyOn(BrowserThread::IO); |
308 case MessageLoop::TYPE_DEFAULT: | 310 case MessageLoop::TYPE_DEFAULT: |
309 return !BrowserThread::CurrentlyOn(BrowserThread::UI) && | 311 return !BrowserThread::CurrentlyOn(BrowserThread::UI) && |
310 !BrowserThread::CurrentlyOn(BrowserThread::IO); | 312 !BrowserThread::CurrentlyOn(BrowserThread::IO); |
311 } | 313 } |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
938 | 940 |
939 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { | 941 TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
940 RecreateRegistry(false); | 942 RecreateRegistry(false); |
941 registry()->AddPredefinedHandler(CreateProtocolHandler( | 943 registry()->AddPredefinedHandler(CreateProtocolHandler( |
942 "test", GURL("http://test.com/%s"), "Test")); | 944 "test", GURL("http://test.com/%s"), "Test")); |
943 registry()->InitProtocolSettings(); | 945 registry()->InitProtocolSettings(); |
944 std::vector<std::string> protocols; | 946 std::vector<std::string> protocols; |
945 registry()->GetRegisteredProtocols(&protocols); | 947 registry()->GetRegisteredProtocols(&protocols); |
946 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); | 948 ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
947 } | 949 } |
OLD | NEW |