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 "base/string_util.h" |
| 9 #include "chrome/browser/child_process_security_policy.h" |
| 10 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg
ate.h" |
| 11 #include "chrome/browser/net/chrome_url_request_context.h" |
| 12 #include "chrome/common/pref_names.h" |
| 13 #include "net/base/escape.h" |
| 14 #include "net/url_request/url_request_redirect_job.h" |
| 15 |
| 16 // ProtocolHandler ------------------------------------------------------------- |
| 17 |
| 18 ProtocolHandler::ProtocolHandler(const std::string& protocol, |
| 19 const GURL& url, |
| 20 const string16& title) |
| 21 : protocol_(protocol), |
| 22 url_(url), |
| 23 title_(title) { |
| 24 } |
| 25 |
| 26 ProtocolHandler* ProtocolHandler::CreateProtocolHandler( |
| 27 const std::string& protocol, |
| 28 const GURL& url, |
| 29 const string16& title) { |
| 30 std::string lower_protocol(protocol); |
| 31 lower_protocol = StringToLowerASCII(protocol); |
| 32 return new ProtocolHandler(lower_protocol, url, title); |
| 33 } |
| 34 |
| 35 ProtocolHandler* ProtocolHandler::CreateProtocolHandler( |
| 36 const DictionaryValue* value) { |
| 37 std::string protocol, url; |
| 38 string16 title; |
| 39 value->GetString("protocol", &protocol); |
| 40 value->GetString("url", &url); |
| 41 value->GetString("title", &title); |
| 42 return ProtocolHandler::CreateProtocolHandler(protocol, GURL(url), title); |
| 43 } |
| 44 |
| 45 GURL ProtocolHandler::TranslateUrl(const GURL& url) { |
| 46 std::string translatedUrlSpec(url_.spec()); |
| 47 ReplaceSubstringsAfterOffset(&translatedUrlSpec, 0, "%s", |
| 48 EscapeQueryParamValue(url.spec(), true)); |
| 49 return GURL(translatedUrlSpec); |
| 50 } |
| 51 |
| 52 Value* ProtocolHandler::Encode() { |
| 53 DictionaryValue* d = new DictionaryValue(); |
| 54 d->Set("protocol", Value::CreateStringValue(protocol_)); |
| 55 d->Set("url", Value::CreateStringValue(url_.spec())); |
| 56 d->Set("title", Value::CreateStringValue(title_)); |
| 57 return d; |
| 58 } |
| 59 |
| 60 bool ProtocolHandler::operator==(const ProtocolHandler &other) const { |
| 61 return protocol_ == other.protocol_ && |
| 62 url_ == other.url_ && |
| 63 title_ == other.title_; |
| 64 } |
| 65 |
| 66 // ProtocolHandlerRegistry ----------------------------------------------------- |
| 67 |
| 68 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile) |
| 69 :profile_(profile) { |
| 70 } |
| 71 |
| 72 void ProtocolHandlerRegistry::RegisterProtocolHandler( |
| 73 ProtocolHandler* handler) { |
| 74 if (protocolHandlers_.find(handler->protocol()) == protocolHandlers_.end()) { |
| 75 ChildProcessSecurityPolicy* policy = |
| 76 ChildProcessSecurityPolicy::GetInstance(); |
| 77 if (!policy->IsWebSafeScheme(handler->protocol())) { |
| 78 policy->RegisterWebSafeScheme(handler->protocol()); |
| 79 } |
| 80 net::URLRequest::RegisterProtocolFactory(handler->protocol(), |
| 81 &ProtocolHandlerRegistry::Factory); |
| 82 } |
| 83 protocolHandlers_[handler->protocol()] = handler; |
| 84 } |
| 85 |
| 86 void ProtocolHandlerRegistry::Load() { |
| 87 PrefService* prefs = profile_->GetPrefs(); |
| 88 if (!prefs->HasPrefPath(prefs::kRegisteredProtocolHandlers)) { |
| 89 return; |
| 90 } |
| 91 const ListValue* protocolHandlers = |
| 92 prefs->GetList(prefs::kRegisteredProtocolHandlers); |
| 93 |
| 94 for (size_t i = 0; i < protocolHandlers->GetSize(); i++) { |
| 95 DictionaryValue* dict; |
| 96 protocolHandlers->GetDictionary(i, &dict); |
| 97 RegisterHandlerFromValue(dict); |
| 98 } |
| 99 } |
| 100 |
| 101 void ProtocolHandlerRegistry::RegisterHandlerFromValue( |
| 102 const DictionaryValue* value) { |
| 103 ProtocolHandler* handler = ProtocolHandler::CreateProtocolHandler(value); |
| 104 if (handler) { |
| 105 RegisterProtocolHandler(handler); |
| 106 } |
| 107 } |
| 108 |
| 109 void ProtocolHandlerRegistry::Save() { |
| 110 scoped_ptr<Value> value(Encode()); |
| 111 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, *value); |
| 112 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
| 113 } |
| 114 |
| 115 ProtocolHandler* ProtocolHandlerRegistry::GetHandlerFor( |
| 116 const std::string& scheme) const { |
| 117 ProtocolHandlerMap::const_iterator i = protocolHandlers_.find(scheme); |
| 118 return i == protocolHandlers_.end() ? NULL : i->second; |
| 119 } |
| 120 |
| 121 bool ProtocolHandlerRegistry::CanSchemeBeOverridden( |
| 122 const std::string& scheme) const { |
| 123 return GetHandlerFor(scheme) != NULL || |
| 124 !net::URLRequest::IsHandledProtocol(scheme); |
| 125 } |
| 126 |
| 127 bool ProtocolHandlerRegistry::IsAlreadyRegistered( |
| 128 const ProtocolHandler* handler) const { |
| 129 ProtocolHandler* currentHandler = GetHandlerFor(handler->protocol()); |
| 130 return currentHandler && *currentHandler == *handler; |
| 131 } |
| 132 |
| 133 net::URLRequestJob* ProtocolHandlerRegistry::CreateJob( |
| 134 net::URLRequest* request, |
| 135 const std::string& scheme) const { |
| 136 ProtocolHandler* handler = GetHandlerFor(scheme); |
| 137 |
| 138 if (!handler) { |
| 139 return NULL; |
| 140 } |
| 141 |
| 142 GURL translatedUrl(handler->TranslateUrl(request->url())); |
| 143 |
| 144 if (!translatedUrl.is_valid()) { |
| 145 return NULL; |
| 146 } |
| 147 |
| 148 return new net::URLRequestRedirectJob(request, translatedUrl); |
| 149 } |
| 150 |
| 151 net::URLRequestJob* ProtocolHandlerRegistry::Factory(net::URLRequest* request, |
| 152 const std::string& scheme) { |
| 153 ChromeURLRequestContext* context = |
| 154 static_cast<ChromeURLRequestContext*>(request->context()); |
| 155 return context->protocol_handler_registry()->CreateJob(request, scheme); |
| 156 } |
| 157 |
| 158 Value* ProtocolHandlerRegistry::Encode() { |
| 159 ListValue* protocolHandlers = new ListValue(); |
| 160 |
| 161 for (ProtocolHandlerMap::iterator i = protocolHandlers_.begin(); |
| 162 i != protocolHandlers_.end(); ++i) { |
| 163 protocolHandlers->Append(i->second->Encode()); |
| 164 } |
| 165 return protocolHandlers; |
| 166 } |
| 167 |
| 168 void ProtocolHandlerRegistry::OnAcceptRegisterProtocolHandler( |
| 169 ProtocolHandler* handler) { |
| 170 RegisterProtocolHandler(handler); |
| 171 Save(); |
| 172 } |
| 173 |
| 174 void ProtocolHandlerRegistry::OnDenyRegisterProtocolHandler( |
| 175 ProtocolHandler* handler) { |
| 176 } |
| 177 |
| 178 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* prefService) { |
| 179 prefService->RegisterListPref(prefs::kRegisteredProtocolHandlers); |
| 180 } |
OLD | NEW |