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