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 // TODO(koz) Check for bad protocol and url here. | |
tony
2011/02/14 23:40:07
We should probably make sure we have this check be
koz (OOO until 15th September)
2011/02/15 03:37:27
I talked with Ojan earlier about validating input
| |
31 std::string lower_protocol(protocol); | |
32 lower_protocol = StringToLowerASCII(protocol); | |
33 return new ProtocolHandler(lower_protocol, url, title); | |
34 } | |
35 | |
36 ProtocolHandler* ProtocolHandler::CreateProtocolHandler(const DictionaryValue* v alue) { | |
tony
2011/02/14 23:40:07
80 cols here and in lots of other places. Please
koz (OOO until 15th September)
2011/02/15 03:37:27
Done.
| |
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(ProtocolHandler* handler) { | |
73 if (protocolHandlers_.find(handler->protocol()) == protocolHandlers_.end()) { | |
74 ChildProcessSecurityPolicy* policy = ChildProcessSecurityPolicy::GetInstance (); | |
75 if (!policy->IsWebSafeScheme(handler->protocol())) { | |
76 policy->RegisterWebSafeScheme(handler->protocol()); | |
77 } | |
78 net::URLRequest::RegisterProtocolFactory(handler->protocol(), | |
79 &ProtocolHandlerRegistry::Factory); | |
80 } | |
81 protocolHandlers_[handler->protocol()] = handler; | |
82 } | |
83 | |
84 void ProtocolHandlerRegistry::Load() { | |
85 PrefService* prefs = profile_->GetPrefs(); | |
86 if (!prefs->HasPrefPath(prefs::kRegisteredProtocolHandlers)) { | |
87 return; | |
88 } | |
89 const ListValue* protocolHandlers = prefs->GetList(prefs::kRegisteredProtocolH andlers); | |
90 | |
91 for (size_t i = 0; i < protocolHandlers->GetSize(); i++) { | |
92 DictionaryValue* dict; | |
93 protocolHandlers->GetDictionary(i, &dict); | |
94 RegisterHandlerFromValue(dict); | |
95 } | |
96 } | |
97 | |
98 void ProtocolHandlerRegistry::RegisterHandlerFromValue(const DictionaryValue* va lue) { | |
99 ProtocolHandler* handler = ProtocolHandler::CreateProtocolHandler(value); | |
100 if (handler) { | |
101 RegisterProtocolHandler(handler); | |
102 } else { | |
103 } | |
tony
2011/02/14 23:40:07
Remove empty else?
koz (OOO until 15th September)
2011/02/15 03:37:27
Done.
| |
104 } | |
105 | |
106 void ProtocolHandlerRegistry::Save() { | |
107 scoped_ptr<Value> value(Encode()); | |
108 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, *value); | |
109 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | |
110 } | |
111 | |
112 ProtocolHandler* ProtocolHandlerRegistry::GetHandlerFor(const std::string& schem e) const { | |
113 ProtocolHandlerMap::const_iterator i = protocolHandlers_.find(scheme); | |
114 return i == protocolHandlers_.end() ? NULL : i->second; | |
115 } | |
116 | |
117 bool ProtocolHandlerRegistry::IsAlreadyRegistered(const ProtocolHandler* handler ) const { | |
118 ProtocolHandler* currentHandler = GetHandlerFor(handler->protocol()); | |
119 return currentHandler && *currentHandler == *handler; | |
120 } | |
121 | |
122 net::URLRequestJob* ProtocolHandlerRegistry::CreateJob(net::URLRequest* request, | |
123 const std::string& scheme ) const { | |
124 ProtocolHandler* handler = GetHandlerFor(scheme); | |
125 | |
126 if (!handler) { | |
127 return NULL; | |
128 } | |
129 | |
130 GURL translatedUrl(handler->TranslateUrl(request->url())); | |
131 | |
132 if (!translatedUrl.is_valid()) { | |
133 return NULL; | |
134 } | |
135 | |
136 return new net::URLRequestRedirectJob(request, translatedUrl); | |
137 } | |
138 | |
139 net::URLRequestJob* ProtocolHandlerRegistry::Factory(net::URLRequest* request, | |
140 const std::string& scheme) { | |
141 ChromeURLRequestContext* context = | |
142 reinterpret_cast<ChromeURLRequestContext*>(request->context()); | |
tony
2011/02/14 23:40:07
I think this should be a static_cast.
koz (OOO until 15th September)
2011/02/15 03:37:27
Done.
| |
143 return context->protocol_handler_registry()->CreateJob(request, scheme); | |
144 } | |
145 | |
146 Value* ProtocolHandlerRegistry::Encode() { | |
147 ListValue* protocolHandlers = new ListValue(); | |
148 | |
149 for (ProtocolHandlerMap::iterator i = protocolHandlers_.begin(); | |
150 i != protocolHandlers_.end(); ++i) { | |
151 protocolHandlers->Append(i->second->Encode()); | |
152 } | |
153 return protocolHandlers; | |
154 } | |
155 | |
156 void ProtocolHandlerRegistry::OnAcceptRegisterProtocolHandler(ProtocolHandler* h andler) { | |
157 RegisterProtocolHandler(handler); | |
158 Save(); | |
159 } | |
160 | |
161 void ProtocolHandlerRegistry::OnDenyRegisterProtocolHandler(ProtocolHandler* han dler) { | |
162 | |
163 } | |
164 | |
165 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* prefService) { | |
166 prefService->RegisterListPref(prefs::kRegisteredProtocolHandlers); | |
167 } | |
OLD | NEW |