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 "chrome/browser/custom_handlers/protocol_handler_registry.h" | 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "chrome/browser/custom_handlers/protocol_handler.h" | 10 #include "chrome/browser/custom_handlers/protocol_handler.h" |
11 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h" | 11 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h" |
12 #include "chrome/browser/net/chrome_url_request_context.h" | 12 #include "chrome/browser/net/chrome_url_request_context.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile_io_data.h" | 14 #include "chrome/browser/profiles/profile_io_data.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "content/browser/browser_thread.h" | |
16 #include "content/browser/child_process_security_policy.h" | 17 #include "content/browser/child_process_security_policy.h" |
18 #include "content/common/notification_service.h" | |
17 #include "net/base/network_delegate.h" | 19 #include "net/base/network_delegate.h" |
18 #include "net/url_request/url_request_redirect_job.h" | 20 #include "net/url_request/url_request_redirect_job.h" |
19 | 21 |
20 | |
21 // ProtocolHandlerRegistry ----------------------------------------------------- | 22 // ProtocolHandlerRegistry ----------------------------------------------------- |
22 | 23 |
23 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile, | 24 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile, |
24 Delegate* delegate) | 25 Delegate* delegate) |
25 : profile_(profile), | 26 : profile_(profile), |
26 delegate_(delegate), | 27 delegate_(delegate), |
27 enabled_(true) { | 28 enabled_(true), |
29 in_unit_test_(false) { | |
28 } | 30 } |
29 | 31 |
30 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() { | 32 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() { |
31 } | 33 } |
32 | 34 |
33 ProtocolHandlerRegistry::ProtocolHandlerList& | 35 const ProtocolHandlerRegistry::ProtocolHandlerList* |
34 ProtocolHandlerRegistry::GetHandlerListFor(const std::string& scheme) { | 36 ProtocolHandlerRegistry::GetHandlersFor( |
35 ProtocolHandlerMultiMap::iterator p = protocol_handlers_.find(scheme); | 37 const std::string& scheme) const { |
38 ProtocolHandlerMultiMap::const_iterator p = protocol_handlers_.find(scheme); | |
36 if (p == protocol_handlers_.end()) { | 39 if (p == protocol_handlers_.end()) { |
37 protocol_handlers_[scheme] = ProtocolHandlerList(); | 40 return NULL; |
38 } | 41 } |
39 return protocol_handlers_[scheme]; | 42 return &p->second; |
40 } | 43 } |
41 | 44 |
42 void ProtocolHandlerRegistry::RegisterProtocolHandler( | 45 void ProtocolHandlerRegistry::RegisterProtocolHandler( |
43 const ProtocolHandler& handler) { | 46 const ProtocolHandler& handler) { |
44 DCHECK(CanSchemeBeOverridden(handler.protocol())); | 47 DCHECK(CanSchemeBeOverridden(handler.protocol())); |
45 DCHECK(!handler.IsEmpty()); | 48 DCHECK(!handler.IsEmpty()); |
46 if (IsRegistered(handler)) { | 49 if (IsRegistered(handler)) { |
47 return; | 50 return; |
48 } | 51 } |
49 if (enabled_ && !delegate_->IsExternalHandlerRegistered(handler.protocol())) { | 52 if (enabled_ && !delegate_->IsExternalHandlerRegistered(handler.protocol())) { |
50 delegate_->RegisterExternalHandler(handler.protocol()); | 53 delegate_->RegisterExternalHandler(handler.protocol()); |
51 } | 54 } |
52 GetHandlerListFor(handler.protocol()).push_back(handler); | 55 InsertHandler(handler); |
56 NotifyChanged(); | |
57 } | |
58 | |
59 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) { | |
60 ProtocolHandlerMultiMap::iterator p = | |
61 protocol_handlers_.find(handler.protocol()); | |
62 | |
63 if (p != protocol_handlers_.end()) { | |
64 p->second.push_back(handler); | |
65 return; | |
66 } | |
67 | |
68 ProtocolHandlerList new_list; | |
69 new_list.push_back(handler); | |
70 protocol_handlers_[handler.protocol()] = new_list; | |
53 } | 71 } |
54 | 72 |
55 void ProtocolHandlerRegistry::IgnoreProtocolHandler( | 73 void ProtocolHandlerRegistry::IgnoreProtocolHandler( |
56 const ProtocolHandler& handler) { | 74 const ProtocolHandler& handler) { |
57 ignored_protocol_handlers_.push_back(handler); | 75 ignored_protocol_handlers_.push_back(handler); |
58 } | 76 } |
59 | 77 |
60 void ProtocolHandlerRegistry::Enable() { | 78 void ProtocolHandlerRegistry::Enable() { |
61 if (enabled_) { | 79 if (enabled_) { |
62 return; | 80 return; |
63 } | 81 } |
64 enabled_ = true; | 82 enabled_ = true; |
65 for (ProtocolHandlerMultiMap::const_iterator p = protocol_handlers_.begin(); | 83 for (ProtocolHandlerMultiMap::const_iterator p = protocol_handlers_.begin(); |
66 p != protocol_handlers_.end(); ++p) { | 84 p != protocol_handlers_.end(); ++p) { |
67 delegate_->RegisterExternalHandler(p->first); | 85 delegate_->RegisterExternalHandler(p->first); |
68 } | 86 } |
87 NotifyChanged(); | |
69 } | 88 } |
70 | 89 |
71 void ProtocolHandlerRegistry::Disable() { | 90 void ProtocolHandlerRegistry::Disable() { |
72 if (!enabled_) { | 91 if (!enabled_) { |
73 return; | 92 return; |
74 } | 93 } |
75 enabled_ = false; | 94 enabled_ = false; |
76 for (ProtocolHandlerMultiMap::const_iterator p = protocol_handlers_.begin(); | 95 for (ProtocolHandlerMultiMap::const_iterator p = protocol_handlers_.begin(); |
77 p != protocol_handlers_.end(); ++p) { | 96 p != protocol_handlers_.end(); ++p) { |
78 delegate_->DeregisterExternalHandler(p->first); | 97 delegate_->DeregisterExternalHandler(p->first); |
79 } | 98 } |
99 NotifyChanged(); | |
80 } | 100 } |
81 | 101 |
82 std::vector<const DictionaryValue*> | 102 std::vector<const DictionaryValue*> |
83 ProtocolHandlerRegistry::GetHandlersFromPref(const char* pref_name) const { | 103 ProtocolHandlerRegistry::GetHandlersFromPref(const char* pref_name) const { |
84 std::vector<const DictionaryValue*> result; | 104 std::vector<const DictionaryValue*> result; |
85 PrefService* prefs = profile_->GetPrefs(); | 105 PrefService* prefs = profile_->GetPrefs(); |
86 if (!prefs->HasPrefPath(pref_name)) { | 106 if (!prefs->HasPrefPath(pref_name)) { |
87 return result; | 107 return result; |
88 } | 108 } |
89 | 109 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 bool ProtocolHandlerRegistry::CanSchemeBeOverridden( | 174 bool ProtocolHandlerRegistry::CanSchemeBeOverridden( |
155 const std::string& scheme) const { | 175 const std::string& scheme) const { |
156 return IsHandledProtocol(scheme) || | 176 return IsHandledProtocol(scheme) || |
157 !delegate_->IsExternalHandlerRegistered(scheme); | 177 !delegate_->IsExternalHandlerRegistered(scheme); |
158 } | 178 } |
159 | 179 |
160 void ProtocolHandlerRegistry::GetHandledProtocols( | 180 void ProtocolHandlerRegistry::GetHandledProtocols( |
161 std::vector<std::string>* output) const { | 181 std::vector<std::string>* output) const { |
162 ProtocolHandlerMultiMap::const_iterator p; | 182 ProtocolHandlerMultiMap::const_iterator p; |
163 for (p = protocol_handlers_.begin(); p != protocol_handlers_.end(); ++p) { | 183 for (p = protocol_handlers_.begin(); p != protocol_handlers_.end(); ++p) { |
164 output->push_back(p->first); | 184 if (!p->second.empty()) { |
185 output->push_back(p->first); | |
186 } | |
165 } | 187 } |
166 } | 188 } |
167 | 189 |
168 void ProtocolHandlerRegistry::RemoveIgnoredHandler( | 190 void ProtocolHandlerRegistry::RemoveIgnoredHandler( |
169 const ProtocolHandler& handler) { | 191 const ProtocolHandler& handler) { |
170 for (ProtocolHandlerList::iterator p = ignored_protocol_handlers_.begin(); | 192 ProtocolHandlerList::iterator p = |
171 p != ignored_protocol_handlers_.end(); ++p) { | 193 std::find(ignored_protocol_handlers_.begin(), |
172 if (handler == *p) { | 194 ignored_protocol_handlers_.end(), handler); |
173 ignored_protocol_handlers_.erase(p); | 195 if (p != ignored_protocol_handlers_.end()) { |
174 break; | 196 ignored_protocol_handlers_.erase(p); |
175 } | 197 Save(); |
198 NotifyChanged(); | |
176 } | 199 } |
177 } | 200 } |
178 | 201 |
179 bool ProtocolHandlerRegistry::IsRegistered(const ProtocolHandler& handler) { | 202 bool ProtocolHandlerRegistry::IsRegistered( |
180 ProtocolHandlerList& handlers(GetHandlerListFor(handler.protocol())); | 203 const ProtocolHandler& handler) const { |
181 return std::find(handlers.begin(), handlers.end(), handler) != handlers.end(); | 204 const ProtocolHandlerList* handlers = GetHandlersFor(handler.protocol()); |
205 if (!handlers) { | |
206 return false; | |
207 } | |
208 return std::find(handlers->begin(), handlers->end(), handler) != | |
209 handlers->end(); | |
182 } | 210 } |
183 | 211 |
184 bool ProtocolHandlerRegistry::IsIgnored(const ProtocolHandler& handler) const { | 212 bool ProtocolHandlerRegistry::IsIgnored(const ProtocolHandler& handler) const { |
185 ProtocolHandlerList::const_iterator i; | 213 ProtocolHandlerList::const_iterator i; |
186 for (i = ignored_protocol_handlers_.begin(); | 214 for (i = ignored_protocol_handlers_.begin(); |
187 i != ignored_protocol_handlers_.end(); ++i) { | 215 i != ignored_protocol_handlers_.end(); ++i) { |
188 if (*i == handler) { | 216 if (*i == handler) { |
189 return true; | 217 return true; |
190 } | 218 } |
191 } | 219 } |
192 return false; | 220 return false; |
193 } | 221 } |
194 | 222 |
195 bool ProtocolHandlerRegistry::IsHandledProtocol( | 223 bool ProtocolHandlerRegistry::IsHandledProtocol( |
196 const std::string& scheme) const { | 224 const std::string& scheme) const { |
197 return protocol_handlers_.find(scheme) != protocol_handlers_.end(); | 225 return !GetHandlerFor(scheme).IsEmpty(); |
198 } | 226 } |
199 | 227 |
200 void ProtocolHandlerRegistry::RemoveHandler(const ProtocolHandler& handler) { | 228 void ProtocolHandlerRegistry::RemoveHandler(const ProtocolHandler& handler) { |
201 ProtocolHandlerList& handlers(GetHandlerListFor(handler.protocol())); | 229 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()]; |
202 ProtocolHandlerList::iterator p = | 230 ProtocolHandlerList::iterator p = |
203 std::find(handlers.begin(), handlers.end(), handler); | 231 std::find(handlers.begin(), handlers.end(), handler); |
204 if (p != handlers.end()) { | 232 if (p != handlers.end()) { |
205 handlers.erase(p); | 233 handlers.erase(p); |
206 } | 234 } |
207 | 235 |
208 ProtocolHandlerMap::iterator it = default_handlers_.find(handler.protocol()); | 236 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol()); |
209 if (it != default_handlers_.end() && it->second == handler) { | 237 if (q != default_handlers_.end() && q->second == handler) { |
210 default_handlers_.erase(it); | 238 default_handlers_.erase(q); |
211 } | 239 } |
240 | |
241 if (!IsHandledProtocol(handler.protocol())) { | |
242 delegate_->DeregisterExternalHandler(handler.protocol()); | |
243 } | |
244 Save(); | |
245 NotifyChanged(); | |
212 } | 246 } |
213 | 247 |
214 net::URLRequestJob* ProtocolHandlerRegistry::MaybeCreateJob( | 248 net::URLRequestJob* ProtocolHandlerRegistry::MaybeCreateJob( |
215 net::URLRequest* request) const { | 249 net::URLRequest* request) const { |
216 ProtocolHandler handler = GetHandlerFor(request->url().scheme()); | 250 ProtocolHandler handler = GetHandlerFor(request->url().scheme()); |
217 | 251 |
218 if (handler.IsEmpty()) { | 252 if (handler.IsEmpty()) { |
219 return NULL; | 253 return NULL; |
220 } | 254 } |
221 | 255 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers, | 311 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers, |
278 PrefService::UNSYNCABLE_PREF); | 312 PrefService::UNSYNCABLE_PREF); |
279 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true, | 313 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true, |
280 PrefService::UNSYNCABLE_PREF); | 314 PrefService::UNSYNCABLE_PREF); |
281 } | 315 } |
282 | 316 |
283 void ProtocolHandlerRegistry::SetDefault(const ProtocolHandler& handler) { | 317 void ProtocolHandlerRegistry::SetDefault(const ProtocolHandler& handler) { |
284 default_handlers_.erase(handler.protocol()); | 318 default_handlers_.erase(handler.protocol()); |
285 default_handlers_.insert(std::make_pair(handler.protocol(), handler)); | 319 default_handlers_.insert(std::make_pair(handler.protocol(), handler)); |
286 Save(); | 320 Save(); |
321 NotifyChanged(); | |
287 } | 322 } |
288 | 323 |
289 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) { | 324 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) { |
290 default_handlers_.erase(scheme); | 325 default_handlers_.erase(scheme); |
291 Save(); | 326 Save(); |
327 NotifyChanged(); | |
292 } | 328 } |
293 | 329 |
294 bool ProtocolHandlerRegistry::IsDefault(const ProtocolHandler& handler) const { | 330 bool ProtocolHandlerRegistry::IsDefault(const ProtocolHandler& handler) const { |
295 return GetHandlerFor(handler.protocol()) == handler; | 331 return GetHandlerFor(handler.protocol()) == handler; |
296 } | 332 } |
297 | 333 |
298 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor( | 334 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor( |
299 const std::string& scheme) const { | 335 const std::string& scheme) const { |
300 ProtocolHandlerMap::const_iterator p = default_handlers_.find(scheme); | 336 ProtocolHandlerMap::const_iterator p = default_handlers_.find(scheme); |
301 if (p != default_handlers_.end()) { | 337 if (p != default_handlers_.end()) { |
302 return p->second; | 338 return p->second; |
303 } | 339 } |
304 ProtocolHandlerMultiMap::const_iterator q = | 340 ProtocolHandlerMultiMap::const_iterator q = protocol_handlers_.find(scheme); |
305 protocol_handlers_.find(scheme); | 341 if (q == protocol_handlers_.end() || q->second.empty()) { |
306 if (q != protocol_handlers_.end() && q->second.size() == 1) { | 342 return ProtocolHandler::EmptyProtocolHandler(); |
307 return q->second[0]; | |
308 } | 343 } |
309 return ProtocolHandler::EmptyProtocolHandler(); | 344 return q->second.back(); |
345 } | |
346 | |
347 int ProtocolHandlerRegistry::GetHandlerIndex(const std::string& scheme) const { | |
348 const ProtocolHandler& handler = GetHandlerFor(scheme); | |
349 const ProtocolHandlerList* handlers = GetHandlersFor(scheme); | |
350 if (!handlers) { | |
351 return -1; | |
352 } | |
353 | |
354 ProtocolHandlerList::const_iterator p; | |
355 int i; | |
356 for (i = 0, p = handlers->begin(); p != handlers->end(); ++p, ++i) { | |
357 if (*p == handler) { | |
358 return i; | |
359 } | |
360 } | |
361 return -1; | |
310 } | 362 } |
311 | 363 |
312 bool ProtocolHandlerRegistry::HasDefault( | 364 bool ProtocolHandlerRegistry::HasDefault( |
313 const std::string& scheme) const { | 365 const std::string& scheme) const { |
314 return !GetHandlerFor(scheme).IsEmpty(); | 366 return !GetHandlerFor(scheme).IsEmpty(); |
315 } | 367 } |
316 | 368 |
317 bool ProtocolHandlerRegistry::HasHandler(const std::string& scheme) { | 369 void ProtocolHandlerRegistry::NotifyChanged() { |
318 return !GetHandlerListFor(scheme).empty(); | 370 if (is_loading_) |
371 return; | |
372 | |
373 if (!in_unit_test_) | |
jam
2011/05/25 16:18:30
you don't need to change the code for this. a lot
koz (OOO until 15th September)
2011/05/25 21:32:31
Thanks for the tip! Done.
| |
374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
375 NotificationService::current()->Notify( | |
376 NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED, | |
377 Source<Profile>(profile_), | |
378 NotificationService::NoDetails()); | |
319 } | 379 } |
320 | 380 |
321 // Delegate -------------------------------------------------------------------- | 381 // Delegate -------------------------------------------------------------------- |
322 | 382 |
323 ProtocolHandlerRegistry::Delegate::~Delegate() { | 383 ProtocolHandlerRegistry::Delegate::~Delegate() { |
324 } | 384 } |
325 | 385 |
326 void ProtocolHandlerRegistry::Delegate::RegisterExternalHandler( | 386 void ProtocolHandlerRegistry::Delegate::RegisterExternalHandler( |
327 const std::string& protocol) { | 387 const std::string& protocol) { |
328 ChildProcessSecurityPolicy* policy = | 388 ChildProcessSecurityPolicy* policy = |
329 ChildProcessSecurityPolicy::GetInstance(); | 389 ChildProcessSecurityPolicy::GetInstance(); |
330 if (!policy->IsWebSafeScheme(protocol)) { | 390 if (!policy->IsWebSafeScheme(protocol)) { |
331 policy->RegisterWebSafeScheme(protocol); | 391 policy->RegisterWebSafeScheme(protocol); |
332 } | 392 } |
333 } | 393 } |
334 | 394 |
335 void ProtocolHandlerRegistry::Delegate::DeregisterExternalHandler( | 395 void ProtocolHandlerRegistry::Delegate::DeregisterExternalHandler( |
336 const std::string& protocol) { | 396 const std::string& protocol) { |
337 } | 397 } |
338 | 398 |
339 bool ProtocolHandlerRegistry::Delegate::IsExternalHandlerRegistered( | 399 bool ProtocolHandlerRegistry::Delegate::IsExternalHandlerRegistered( |
340 const std::string& protocol) { | 400 const std::string& protocol) { |
341 return ProfileIOData::IsHandledProtocol(protocol); | 401 return ProfileIOData::IsHandledProtocol(protocol); |
342 } | 402 } |
OLD | NEW |