OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
| 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "chrome/browser/child_process_security_policy.h" |
| 9 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg
ate.h" |
| 10 #include "chrome/browser/net/chrome_url_request_context.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "net/url_request/url_request_redirect_job.h" |
| 13 |
| 14 |
| 15 // ProtocolHandlerRegistry ----------------------------------------------------- |
| 16 |
| 17 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile) |
| 18 :profile_(profile) { |
| 19 } |
| 20 |
| 21 void ProtocolHandlerRegistry::RegisterProtocolHandler( |
| 22 ProtocolHandler* handler) { |
| 23 if (protocolHandlers_.find(handler->protocol()) == protocolHandlers_.end()) { |
| 24 ChildProcessSecurityPolicy* policy = |
| 25 ChildProcessSecurityPolicy::GetInstance(); |
| 26 if (!policy->IsWebSafeScheme(handler->protocol())) { |
| 27 policy->RegisterWebSafeScheme(handler->protocol()); |
| 28 } |
| 29 net::URLRequest::RegisterProtocolFactory(handler->protocol(), |
| 30 &ProtocolHandlerRegistry::Factory); |
| 31 } |
| 32 protocolHandlers_[handler->protocol()] = handler; |
| 33 } |
| 34 |
| 35 void ProtocolHandlerRegistry::Load() { |
| 36 PrefService* prefs = profile_->GetPrefs(); |
| 37 if (!prefs->HasPrefPath(prefs::kRegisteredProtocolHandlers)) { |
| 38 return; |
| 39 } |
| 40 const ListValue* protocolHandlers = |
| 41 prefs->GetList(prefs::kRegisteredProtocolHandlers); |
| 42 |
| 43 for (size_t i = 0; i < protocolHandlers->GetSize(); i++) { |
| 44 DictionaryValue* dict; |
| 45 protocolHandlers->GetDictionary(i, &dict); |
| 46 RegisterHandlerFromValue(dict); |
| 47 } |
| 48 } |
| 49 |
| 50 void ProtocolHandlerRegistry::RegisterHandlerFromValue( |
| 51 const DictionaryValue* value) { |
| 52 ProtocolHandler* handler = ProtocolHandler::CreateProtocolHandler(value); |
| 53 if (handler) { |
| 54 RegisterProtocolHandler(handler); |
| 55 } |
| 56 } |
| 57 |
| 58 void ProtocolHandlerRegistry::Save() { |
| 59 scoped_ptr<Value> value(Encode()); |
| 60 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, *value); |
| 61 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 62 } |
| 63 |
| 64 ProtocolHandler* ProtocolHandlerRegistry::GetHandlerFor( |
| 65 const std::string& scheme) const { |
| 66 ProtocolHandlerMap::const_iterator i = protocolHandlers_.find(scheme); |
| 67 return i == protocolHandlers_.end() ? NULL : i->second; |
| 68 } |
| 69 |
| 70 bool ProtocolHandlerRegistry::CanSchemeBeOverridden( |
| 71 const std::string& scheme) const { |
| 72 return GetHandlerFor(scheme) != NULL || |
| 73 !net::URLRequest::IsHandledProtocol(scheme); |
| 74 } |
| 75 |
| 76 bool ProtocolHandlerRegistry::IsAlreadyRegistered( |
| 77 const ProtocolHandler* handler) const { |
| 78 ProtocolHandler* currentHandler = GetHandlerFor(handler->protocol()); |
| 79 return currentHandler && *currentHandler == *handler; |
| 80 } |
| 81 |
| 82 net::URLRequestJob* ProtocolHandlerRegistry::CreateJob( |
| 83 net::URLRequest* request, |
| 84 const std::string& scheme) const { |
| 85 ProtocolHandler* handler = GetHandlerFor(scheme); |
| 86 |
| 87 if (!handler) { |
| 88 return NULL; |
| 89 } |
| 90 |
| 91 GURL translatedUrl(handler->TranslateUrl(request->url())); |
| 92 |
| 93 if (!translatedUrl.is_valid()) { |
| 94 return NULL; |
| 95 } |
| 96 |
| 97 return new net::URLRequestRedirectJob(request, translatedUrl); |
| 98 } |
| 99 |
| 100 net::URLRequestJob* ProtocolHandlerRegistry::Factory(net::URLRequest* request, |
| 101 const std::string& scheme) { |
| 102 ChromeURLRequestContext* context = |
| 103 static_cast<ChromeURLRequestContext*>(request->context()); |
| 104 return context->protocol_handler_registry()->CreateJob(request, scheme); |
| 105 } |
| 106 |
| 107 Value* ProtocolHandlerRegistry::Encode() { |
| 108 ListValue* protocolHandlers = new ListValue(); |
| 109 |
| 110 for (ProtocolHandlerMap::iterator i = protocolHandlers_.begin(); |
| 111 i != protocolHandlers_.end(); ++i) { |
| 112 protocolHandlers->Append(i->second->Encode()); |
| 113 } |
| 114 return protocolHandlers; |
| 115 } |
| 116 |
| 117 void ProtocolHandlerRegistry::OnAcceptRegisterProtocolHandler( |
| 118 ProtocolHandler* handler) { |
| 119 RegisterProtocolHandler(handler); |
| 120 Save(); |
| 121 } |
| 122 |
| 123 void ProtocolHandlerRegistry::OnDenyRegisterProtocolHandler( |
| 124 ProtocolHandler* handler) { |
| 125 } |
| 126 |
| 127 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* prefService) { |
| 128 prefService->RegisterListPref(prefs::kRegisteredProtocolHandlers); |
| 129 } |
OLD | NEW |