Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry.cc

Issue 7033018: Handler settings page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Responded to comments Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/child_process_security_policy.h" 16 #include "content/browser/child_process_security_policy.h"
17 #include "content/common/notification_service.h"
17 #include "net/base/network_delegate.h" 18 #include "net/base/network_delegate.h"
18 #include "net/url_request/url_request_redirect_job.h" 19 #include "net/url_request/url_request_redirect_job.h"
19 20
20 21
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 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
willchan no longer on Chromium 2011/05/21 00:06:05 Which thread does this happen on? Is this the UI t
koz (OOO until 15th September) 2011/05/24 08:47:49 It will be called on Load(), but in that case it d
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 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
willchan no longer on Chromium 2011/05/21 00:06:05 Looks like this is probably the UI thread, right?
koz (OOO until 15th September) 2011/05/24 08:47:49 Yep, it's called when the user toggles the checkbo
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 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
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 29 matching lines...) Expand all
119 } 138 }
120 } 139 }
121 std::vector<const DictionaryValue*> ignored_handlers = 140 std::vector<const DictionaryValue*> ignored_handlers =
122 GetHandlersFromPref(prefs::kIgnoredProtocolHandlers); 141 GetHandlersFromPref(prefs::kIgnoredProtocolHandlers);
123 for (std::vector<const DictionaryValue*>::const_iterator p = 142 for (std::vector<const DictionaryValue*>::const_iterator p =
124 ignored_handlers.begin(); 143 ignored_handlers.begin();
125 p != ignored_handlers.end(); ++p) { 144 p != ignored_handlers.end(); ++p) {
126 IgnoreProtocolHandler(ProtocolHandler::CreateProtocolHandler(*p)); 145 IgnoreProtocolHandler(ProtocolHandler::CreateProtocolHandler(*p));
127 } 146 }
128 is_loading_ = false; 147 is_loading_ = false;
148 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_LOADED);
129 } 149 }
130 150
131 void ProtocolHandlerRegistry::RegisterHandlerFromValue( 151 void ProtocolHandlerRegistry::RegisterHandlerFromValue(
132 const DictionaryValue* value) { 152 const DictionaryValue* value) {
133 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(value); 153 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(value);
134 if (!handler.IsEmpty()) { 154 if (!handler.IsEmpty()) {
135 RegisterProtocolHandler(handler); 155 RegisterProtocolHandler(handler);
136 } 156 }
137 } 157 }
138 158
(...skipping 15 matching lines...) Expand all
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 }
176 } 197 }
198 Save();
199 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
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 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
willchan no longer on Chromium 2011/05/21 00:06:05 What thread is this handled on?
koz (OOO until 15th September) 2011/05/24 08:47:49 The UI thread - it's called when the user clicks t
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
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 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
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 Notify(NotificationType::PROTOCOL_HANDLER_REGISTRY_CHANGED);
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) {
318 return !GetHandlerListFor(scheme).empty();
319 }
320
321 // Delegate -------------------------------------------------------------------- 369 // Delegate --------------------------------------------------------------------
322 370
323 ProtocolHandlerRegistry::Delegate::~Delegate() { 371 ProtocolHandlerRegistry::Delegate::~Delegate() {
324 } 372 }
325 373
326 void ProtocolHandlerRegistry::Delegate::RegisterExternalHandler( 374 void ProtocolHandlerRegistry::Delegate::RegisterExternalHandler(
327 const std::string& protocol) { 375 const std::string& protocol) {
328 ChildProcessSecurityPolicy* policy = 376 ChildProcessSecurityPolicy* policy =
329 ChildProcessSecurityPolicy::GetInstance(); 377 ChildProcessSecurityPolicy::GetInstance();
330 if (!policy->IsWebSafeScheme(protocol)) { 378 if (!policy->IsWebSafeScheme(protocol)) {
331 policy->RegisterWebSafeScheme(protocol); 379 policy->RegisterWebSafeScheme(protocol);
332 } 380 }
333 } 381 }
334 382
335 void ProtocolHandlerRegistry::Delegate::DeregisterExternalHandler( 383 void ProtocolHandlerRegistry::Delegate::DeregisterExternalHandler(
336 const std::string& protocol) { 384 const std::string& protocol) {
337 } 385 }
338 386
339 bool ProtocolHandlerRegistry::Delegate::IsExternalHandlerRegistered( 387 bool ProtocolHandlerRegistry::Delegate::IsExternalHandlerRegistered(
340 const std::string& protocol) { 388 const std::string& protocol) {
341 return ProfileIOData::IsHandledProtocol(protocol); 389 return ProfileIOData::IsHandledProtocol(protocol);
342 } 390 }
391
392 void ProtocolHandlerRegistry::Notify(NotificationType type) {
willchan no longer on Chromium 2011/05/20 22:48:50 What thread is Notify() being called on? That my m
koz (OOO until 15th September) 2011/05/20 23:18:57 Ah, I wasn't aware that the thread that Notify() i
393 NotificationService::current()->Notify(type,
394 Source<Profile>(profile_),
395 NotificationService::NoDetails());
396 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698