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

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

Issue 10546083: Convert ProtocolHandlerRegistry to be a ProfileKeyedService. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Respond to Will's comments. Created 8 years, 6 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h" 14 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h"
14 #include "chrome/browser/net/chrome_url_request_context.h" 15 #include "chrome/browser/net/chrome_url_request_context.h"
15 #include "chrome/browser/prefs/pref_service.h" 16 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/profiles/profile_io_data.h" 17 #include "chrome/browser/profiles/profile_io_data.h"
17 #include "chrome/common/chrome_notification_types.h" 18 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/chrome_switches.h" 19 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/custom_handlers/protocol_handler.h" 20 #include "chrome/common/custom_handlers/protocol_handler.h"
20 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
21 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/child_process_security_policy.h" 23 #include "content/public/browser/child_process_security_policy.h"
23 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
24 #include "net/base/network_delegate.h" 25 #include "net/base/network_delegate.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_job.h"
25 #include "net/url_request/url_request_redirect_job.h" 28 #include "net/url_request/url_request_redirect_job.h"
26 29
27 using content::BrowserThread; 30 using content::BrowserThread;
28 using content::ChildProcessSecurityPolicy; 31 using content::ChildProcessSecurityPolicy;
29 32
30 namespace { 33 namespace {
31 34
35 const ProtocolHandler& LookupHandler(
36 const ProtocolHandlerRegistry::ProtocolHandlerMap& handler_map,
37 const std::string& scheme) {
38 ProtocolHandlerRegistry::ProtocolHandlerMap::const_iterator p =
39 handler_map.find(scheme);
40 if (p != handler_map.end()) {
41 return p->second;
42 }
43 return ProtocolHandler::EmptyProtocolHandler();
44 }
45
32 // If true default protocol handlers will be removed if the OS level 46 // If true default protocol handlers will be removed if the OS level
33 // registration for a protocol is no longer Chrome. 47 // registration for a protocol is no longer Chrome.
34 bool ShouldRemoveHandlersNotInOS() { 48 bool ShouldRemoveHandlersNotInOS() {
35 #if defined(OS_LINUX) 49 #if defined(OS_LINUX)
36 // We don't do this on Linux as the OS registration there is not reliable, 50 // We don't do this on Linux as the OS registration there is not reliable,
37 // and Chrome OS doesn't have any notion of OS registration. 51 // and Chrome OS doesn't have any notion of OS registration.
38 // TODO(benwells): When Linux support is more reliable remove this 52 // TODO(benwells): When Linux support is more reliable remove this
39 // difference (http://crbug.com/88255). 53 // difference (http://crbug.com/88255).
40 return false; 54 return false;
41 #else 55 #else
42 return ShellIntegration::CanSetAsDefaultProtocolClient() != 56 return ShellIntegration::CanSetAsDefaultProtocolClient() !=
43 ShellIntegration::SET_DEFAULT_NOT_ALLOWED; 57 ShellIntegration::SET_DEFAULT_NOT_ALLOWED;
44 #endif 58 #endif
45 } 59 }
46 60
47 } // namespace 61 void InstallDefaultProtocolHandlers(ProtocolHandlerRegistry* registry) {
62 // only chromeos has default protocol handlers at this point.
63 #if defined(OS_CHROMEOS)
64 registry->AddPredefinedHandler(
65 ProtocolHandler::CreateProtocolHandler(
66 "mailto",
67 GURL(l10n_util::GetStringUTF8(IDS_GOOGLE_MAILTO_HANDLER_URL)),
68 l10n_util::GetStringUTF16(IDS_GOOGLE_MAILTO_HANDLER_NAME)));
69 registry->AddPredefinedHandler(
70 ProtocolHandler::CreateProtocolHandler(
71 "webcal",
72 GURL(l10n_util::GetStringUTF8(IDS_GOOGLE_WEBCAL_HANDLER_URL)),
73 l10n_util::GetStringUTF16(IDS_GOOGLE_WEBCAL_HANDLER_NAME)));
74 #endif
75 }
48 76
49 static const ProtocolHandler& LookupHandler( 77 } // namespace
50 const ProtocolHandlerRegistry::ProtocolHandlerMap& handler_map, 78
51 const std::string& scheme) { 79 // Core ------------------------------------------------------------------------
52 ProtocolHandlerRegistry::ProtocolHandlerMap::const_iterator p = 80
53 handler_map.find(scheme); 81 // Core is an IO thread specific object. Access to the class should all
54 if (p != handler_map.end()) { 82 // be done via the IO thread. The registry living on the UI thread makes
55 return p->second; 83 // a best effort to update the IO object after local updates are completed.
84 class ProtocolHandlerRegistry::Core
85 : public base::RefCountedThreadSafe<ProtocolHandlerRegistry::Core> {
86 public:
87 explicit Core(bool);
88
89 // Returns true if the protocol has a default protocol handler.
90 // Should be called only from the IO thread.
91 bool IsHandledProtocol(const std::string& scheme) const;
92
93 // Clears the default for the provided protocol.
94 // Should be called only from the IO thread.
95 void ClearDefault(const std::string& scheme);
96
97 // Makes this ProtocolHandler the default handler for its protocol.
98 // Should be called only from the IO thread.
99 void SetDefault(const ProtocolHandler& handler);
100
101 // Creates a URL request job for the given request if there is a matching
102 // protocol handler, returns NULL otherwise.
103 net::URLRequestJob* MaybeCreateJob(net::URLRequest* request) const;
104
105 // Indicate that the registry has been enabled in the IO thread's
106 // copy of the data.
107 void Enable() { enabled_ = true; }
108
109 // Indicate that the registry has been disabled in the IO thread's copy of
110 // the data.
111 void Disable() { enabled_ = false; }
112
113 private:
114 friend class base::RefCountedThreadSafe<Core>;
115 virtual ~Core();
116
117 // Copy of default_handlers_ that is only accessed on the IO thread.
118 ProtocolHandlerRegistry::ProtocolHandlerMap default_handlers_;
119 // Copy of enabled_ that is only accessed on the IO thread.
120 bool enabled_;
121
122 DISALLOW_COPY_AND_ASSIGN(Core);
123 };
124
125 ProtocolHandlerRegistry::Core::Core(bool) : enabled_(true) {}
126 ProtocolHandlerRegistry::Core::~Core() {}
127
128 bool ProtocolHandlerRegistry::Core::IsHandledProtocol(
129 const std::string& scheme) const {
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
131 return enabled_ && !LookupHandler(default_handlers_, scheme).IsEmpty();
132 }
133
134 void ProtocolHandlerRegistry::Core::ClearDefault(const std::string& scheme) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
136 default_handlers_.erase(scheme);
137 }
138
139 void ProtocolHandlerRegistry::Core::SetDefault(const ProtocolHandler& handler) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
141 ClearDefault(handler.protocol());
142 default_handlers_.insert(std::make_pair(handler.protocol(), handler));
143 }
144
145 net::URLRequestJob* ProtocolHandlerRegistry::Core::MaybeCreateJob(
146 net::URLRequest* request) const {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
148
149 ProtocolHandler handler = LookupHandler(default_handlers_,
150 request->url().scheme());
151 if (handler.IsEmpty()) {
152 return NULL;
56 } 153 }
57 return ProtocolHandler::EmptyProtocolHandler(); 154 GURL translated_url(handler.TranslateUrl(request->url()));
155 if (!translated_url.is_valid()) {
156 return NULL;
157 }
158 return new net::URLRequestRedirectJob(request, translated_url);
159 }
160
161 // IOURLInterceptor ------------------------------------------------------------
162
163 // Instances of this class are produced for ownership by the IO
164 // thread where it handler URL requests. We should never hold
165 // any pointers on this class, only produce them in response to
166 // requests via |ProtocolHandlerRegistry::CreateIOURLInterceptor|.
167 class ProtocolHandlerRegistry::IOURLInterceptor
168 : public net::URLRequestJobFactory::Interceptor {
169 public:
170 explicit IOURLInterceptor(Core* core);
171 virtual ~IOURLInterceptor();
172
173 virtual net::URLRequestJob* MaybeIntercept(
174 net::URLRequest* request) const OVERRIDE;
175
176 virtual bool WillHandleProtocol(const std::string& protocol) const OVERRIDE;
177
178 virtual net::URLRequestJob* MaybeInterceptRedirect(
179 const GURL& url, net::URLRequest* request) const OVERRIDE {
180 return NULL;
181 }
182
183 virtual net::URLRequestJob* MaybeInterceptResponse(
184 net::URLRequest* request) const OVERRIDE {
185 return NULL;
186 }
187
188 private:
189 scoped_refptr<Core> core_;
190 DISALLOW_COPY_AND_ASSIGN(IOURLInterceptor);
191 };
192
193 ProtocolHandlerRegistry::IOURLInterceptor::IOURLInterceptor(Core* core)
194 : core_(core) {
195 DCHECK(core_);
196 }
197
198 ProtocolHandlerRegistry::IOURLInterceptor::~IOURLInterceptor() {
199 // TODO(smckay): release what ever type of pointer we have
200 // on the C instance.
benwells 2012/06/22 23:31:27 I dont understand this todo. What is the C instanc
Steve McKay 2012/06/27 21:56:45 Obsolete. Nuked.
201 }
202
203 net::URLRequestJob* ProtocolHandlerRegistry::IOURLInterceptor::MaybeIntercept(
204 net::URLRequest* request) const {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
206
207 return core_->MaybeCreateJob(request);
208 }
209
210 bool ProtocolHandlerRegistry::IOURLInterceptor::WillHandleProtocol(
211 const std::string& protocol) const {
212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
213
214 return core_->IsHandledProtocol(protocol);
58 } 215 }
59 216
60 // DefaultClientObserver ------------------------------------------------------ 217 // DefaultClientObserver ------------------------------------------------------
61 218
62 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver( 219 ProtocolHandlerRegistry::DefaultClientObserver::DefaultClientObserver(
63 ProtocolHandlerRegistry* registry) 220 ProtocolHandlerRegistry* registry)
64 : worker_(NULL), 221 : worker_(NULL),
65 registry_(registry) { 222 registry_(registry) {
66 DCHECK(registry_); 223 DCHECK(registry_);
67 } 224 }
68 225
69 ProtocolHandlerRegistry::DefaultClientObserver::~DefaultClientObserver() { 226 ProtocolHandlerRegistry::DefaultClientObserver::~DefaultClientObserver() {
70 if (worker_) 227 if (worker_)
71 worker_->ObserverDestroyed(); 228 worker_->ObserverDestroyed();
72 229
73 DefaultClientObserverList::iterator iter = std::find( 230 DefaultClientObserverList::iterator iter = std::find(
74 registry_->default_client_observers_.begin(), 231 registry_->default_client_observers_.begin(),
75 registry_->default_client_observers_.end(), this); 232 registry_->default_client_observers_.end(), this);
76 registry_->default_client_observers_.erase(iter); 233 registry_->default_client_observers_.erase(iter);
77 } 234 }
78 235
79 void 236 void ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
80 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
81 ShellIntegration::DefaultWebClientUIState state) { 237 ShellIntegration::DefaultWebClientUIState state) {
82 if (worker_) { 238 if (worker_) {
83 if (ShouldRemoveHandlersNotInOS() && 239 if (ShouldRemoveHandlersNotInOS() &&
84 (state == ShellIntegration::STATE_NOT_DEFAULT)) { 240 (state == ShellIntegration::STATE_NOT_DEFAULT)) {
85 registry_->ClearDefault(worker_->protocol()); 241 registry_->ClearDefault(worker_->protocol());
86 } 242 }
87 } else { 243 } else {
88 NOTREACHED(); 244 NOTREACHED();
89 } 245 }
90 } 246 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 worker->StartSetAsDefault(); 300 worker->StartSetAsDefault();
145 } 301 }
146 302
147 // ProtocolHandlerRegistry ----------------------------------------------------- 303 // ProtocolHandlerRegistry -----------------------------------------------------
148 304
149 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile, 305 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile,
150 Delegate* delegate) 306 Delegate* delegate)
151 : profile_(profile), 307 : profile_(profile),
152 delegate_(delegate), 308 delegate_(delegate),
153 enabled_(true), 309 enabled_(true),
154 enabled_io_(enabled_),
155 is_loading_(false), 310 is_loading_(false),
156 is_loaded_(false) { 311 is_loaded_(false),
312 core_(new Core(enabled_)){
157 } 313 }
158 314
159 bool ProtocolHandlerRegistry::SilentlyHandleRegisterHandlerRequest( 315 bool ProtocolHandlerRegistry::SilentlyHandleRegisterHandlerRequest(
160 const ProtocolHandler& handler) { 316 const ProtocolHandler& handler) {
161 if (handler.IsEmpty() || !CanSchemeBeOverridden(handler.protocol())) 317 if (handler.IsEmpty() || !CanSchemeBeOverridden(handler.protocol()))
162 return true; 318 return true;
163 319
164 if (!enabled() || IsRegistered(handler) || HasIgnoredEquivalent(handler)) 320 if (!enabled() || IsRegistered(handler) || HasIgnoredEquivalent(handler))
165 return true; 321 return true;
166 322
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 p != handlers->end(); p++) { 382 p != handlers->end(); p++) {
227 if (handler.IsSameOrigin(*p)) { 383 if (handler.IsSameOrigin(*p)) {
228 replaced_handlers.push_back(*p); 384 replaced_handlers.push_back(*p);
229 } 385 }
230 } 386 }
231 return replaced_handlers; 387 return replaced_handlers;
232 } 388 }
233 389
234 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) { 390 void ProtocolHandlerRegistry::ClearDefault(const std::string& scheme) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 391 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
392
236 default_handlers_.erase(scheme); 393 default_handlers_.erase(scheme);
benwells 2012/06/22 23:31:27 This is just a thought, but could Core be used to
Steve McKay 2012/06/27 21:56:45 I'll be back in the code in the near future. Can w
benwells 2012/06/28 08:15:46 Certainly!
237 BrowserThread::PostTask( 394 BrowserThread::PostTask(
238 BrowserThread::IO, 395 BrowserThread::IO,
239 FROM_HERE, 396 FROM_HERE,
240 base::Bind(&ProtocolHandlerRegistry::ClearDefaultIO, this, scheme)); 397 base::Bind(&Core::ClearDefault, core_, scheme));
241 Save(); 398 Save();
242 NotifyChanged(); 399 NotifyChanged();
243 } 400 }
244 401
245 bool ProtocolHandlerRegistry::IsDefault( 402 bool ProtocolHandlerRegistry::IsDefault(
246 const ProtocolHandler& handler) const { 403 const ProtocolHandler& handler) const {
247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 return GetHandlerFor(handler.protocol()) == handler; 405 return GetHandlerFor(handler.protocol()) == handler;
249 } 406 }
250 407
251 void ProtocolHandlerRegistry::Load() { 408 void ProtocolHandlerRegistry::InitProtocolSettings() {
409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
410
411 // Install predefined protocol handlers.
412 InstallDefaultProtocolHandlers(this);
413
252 // Any further default additions to the table will get rejected from now on. 414 // Any further default additions to the table will get rejected from now on.
253 is_loaded_ = true; 415 is_loaded_ = true;
254 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
255 is_loading_ = true; 416 is_loading_ = true;
417
256 PrefService* prefs = profile_->GetPrefs(); 418 PrefService* prefs = profile_->GetPrefs();
257 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) { 419 if (prefs->HasPrefPath(prefs::kCustomHandlersEnabled)) {
258 enabled_ = prefs->GetBoolean(prefs::kCustomHandlersEnabled); 420 if (prefs->GetBoolean(prefs::kCustomHandlersEnabled)) {
259 BrowserThread::PostTask( 421 Enable();
260 BrowserThread::IO, 422 } else {
261 FROM_HERE, 423 Disable();
262 base::Bind(enabled_ ? &ProtocolHandlerRegistry::EnableIO : 424 }
263 &ProtocolHandlerRegistry::DisableIO, this));
264 } 425 }
265 std::vector<const DictionaryValue*> registered_handlers = 426 std::vector<const DictionaryValue*> registered_handlers =
266 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers); 427 GetHandlersFromPref(prefs::kRegisteredProtocolHandlers);
267 for (std::vector<const DictionaryValue*>::const_iterator p = 428 for (std::vector<const DictionaryValue*>::const_iterator p =
268 registered_handlers.begin(); 429 registered_handlers.begin();
269 p != registered_handlers.end(); ++p) { 430 p != registered_handlers.end(); ++p) {
270 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p); 431 ProtocolHandler handler = ProtocolHandler::CreateProtocolHandler(*p);
271 RegisterProtocolHandler(handler); 432 RegisterProtocolHandler(handler);
272 bool is_default = false; 433 bool is_default = false;
273 if ((*p)->GetBoolean("default", &is_default) && is_default) { 434 if ((*p)->GetBoolean("default", &is_default) && is_default) {
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 if (should_notify) 582 if (should_notify)
422 NotifyChanged(); 583 NotifyChanged();
423 } 584 }
424 585
425 bool ProtocolHandlerRegistry::IsHandledProtocol( 586 bool ProtocolHandlerRegistry::IsHandledProtocol(
426 const std::string& scheme) const { 587 const std::string& scheme) const {
427 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 588 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
428 return enabled_ && !GetHandlerFor(scheme).IsEmpty(); 589 return enabled_ && !GetHandlerFor(scheme).IsEmpty();
429 } 590 }
430 591
431 bool ProtocolHandlerRegistry::IsHandledProtocolIO(
432 const std::string& scheme) const {
433 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
434 return enabled_io_ && !LookupHandler(default_handlers_io_, scheme).IsEmpty();
435 }
436
437 void ProtocolHandlerRegistry::RemoveHandler( 592 void ProtocolHandlerRegistry::RemoveHandler(
438 const ProtocolHandler& handler) { 593 const ProtocolHandler& handler) {
439 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 594 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
440 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()]; 595 ProtocolHandlerList& handlers = protocol_handlers_[handler.protocol()];
441 ProtocolHandlerList::iterator p = 596 ProtocolHandlerList::iterator p =
442 std::find(handlers.begin(), handlers.end(), handler); 597 std::find(handlers.begin(), handlers.end(), handler);
443 if (p != handlers.end()) { 598 if (p != handlers.end()) {
444 handlers.erase(p); 599 handlers.erase(p);
445 } 600 }
446 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol()); 601 ProtocolHandlerMap::iterator q = default_handlers_.find(handler.protocol());
447 if (q != default_handlers_.end() && q->second == handler) { 602 if (q != default_handlers_.end() && q->second == handler) {
448 // Make the new top handler in the list the default. 603 // Make the new top handler in the list the default.
449 if (!handlers.empty()) { 604 if (!handlers.empty()) {
450 // NOTE We pass a copy because SetDefault() modifies handlers. 605 // NOTE We pass a copy because SetDefault() modifies handlers.
451 SetDefault(ProtocolHandler(handlers[0])); 606 SetDefault(ProtocolHandler(handlers[0]));
452 } else { 607 } else {
453 BrowserThread::PostTask( 608 BrowserThread::PostTask(
454 BrowserThread::IO, FROM_HERE, 609 BrowserThread::IO, FROM_HERE,
455 base::Bind(&ProtocolHandlerRegistry::ClearDefaultIO, this, 610 base::Bind(&Core::ClearDefault, core_, q->second.protocol()));
456 q->second.protocol())); 611
457 default_handlers_.erase(q); 612 default_handlers_.erase(q);
458 } 613 }
459 } 614 }
460 615
461 if (!IsHandledProtocol(handler.protocol())) { 616 if (!IsHandledProtocol(handler.protocol())) {
462 delegate_->DeregisterExternalHandler(handler.protocol()); 617 delegate_->DeregisterExternalHandler(handler.protocol());
463 } 618 }
464 Save(); 619 Save();
465 NotifyChanged(); 620 NotifyChanged();
466 } 621 }
467 622
468 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) { 623 void ProtocolHandlerRegistry::RemoveDefaultHandler(const std::string& scheme) {
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 624 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
470 ProtocolHandler current_default = GetHandlerFor(scheme); 625 ProtocolHandler current_default = GetHandlerFor(scheme);
471 if (!current_default.IsEmpty()) 626 if (!current_default.IsEmpty())
472 RemoveHandler(current_default); 627 RemoveHandler(current_default);
473 } 628 }
474 629
475 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor( 630 const ProtocolHandler& ProtocolHandlerRegistry::GetHandlerFor(
476 const std::string& scheme) const { 631 const std::string& scheme) const {
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 632 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
478 return LookupHandler(default_handlers_, scheme); 633 return LookupHandler(default_handlers_, scheme);
479 } 634 }
480 635
481 net::URLRequestJob* ProtocolHandlerRegistry::MaybeCreateJob(
482 net::URLRequest* request) const {
483 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
484 ProtocolHandler handler = LookupHandler(default_handlers_io_,
485 request->url().scheme());
486 if (handler.IsEmpty()) {
487 return NULL;
488 }
489 GURL translated_url(handler.TranslateUrl(request->url()));
490 if (!translated_url.is_valid()) {
491 return NULL;
492 }
493 return new net::URLRequestRedirectJob(request, translated_url);
494 }
495
496 void ProtocolHandlerRegistry::Enable() { 636 void ProtocolHandlerRegistry::Enable() {
497 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
498 if (enabled_) { 638 if (enabled_) {
499 return; 639 return;
500 } 640 }
501 enabled_ = true; 641 enabled_ = true;
502 BrowserThread::PostTask( 642 BrowserThread::PostTask(
503 BrowserThread::IO, 643 BrowserThread::IO,
504 FROM_HERE, 644 FROM_HERE,
505 base::Bind(&ProtocolHandlerRegistry::EnableIO, this)); 645 base::Bind(&Core::Enable, core_));
646
506 ProtocolHandlerMap::const_iterator p; 647 ProtocolHandlerMap::const_iterator p;
507 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) { 648 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) {
508 delegate_->RegisterExternalHandler(p->first); 649 delegate_->RegisterExternalHandler(p->first);
509 } 650 }
510 Save(); 651 Save();
511 NotifyChanged(); 652 NotifyChanged();
512 } 653 }
513 654
514 void ProtocolHandlerRegistry::Disable() { 655 void ProtocolHandlerRegistry::Disable() {
515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 656 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
516 if (!enabled_) { 657 if (!enabled_) {
517 return; 658 return;
518 } 659 }
519 enabled_ = false; 660 enabled_ = false;
520 BrowserThread::PostTask( 661 BrowserThread::PostTask(
521 BrowserThread::IO, 662 BrowserThread::IO,
522 FROM_HERE, 663 FROM_HERE,
523 base::Bind(&ProtocolHandlerRegistry::DisableIO, this)); 664 base::Bind(&Core::Disable, core_));
665
524 ProtocolHandlerMap::const_iterator p; 666 ProtocolHandlerMap::const_iterator p;
525 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) { 667 for (p = default_handlers_.begin(); p != default_handlers_.end(); ++p) {
526 delegate_->DeregisterExternalHandler(p->first); 668 delegate_->DeregisterExternalHandler(p->first);
527 } 669 }
528 Save(); 670 Save();
529 NotifyChanged(); 671 NotifyChanged();
530 } 672 }
531 673
532 void ProtocolHandlerRegistry::Finalize() { 674 void ProtocolHandlerRegistry::Shutdown() {
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
534 delegate_.reset(NULL); 676 delegate_.reset(NULL);
535 // We free these now in case there are any outstanding workers running. If 677 // We free these now in case there are any outstanding workers running. If
536 // we didn't free them they could respond to workers and try to update the 678 // we didn't free them they could respond to workers and try to update the
537 // protocol handler registry after it was deleted. 679 // protocol handler registry after it was deleted.
538 // Observers remove themselves from this list when they are deleted; so 680 // Observers remove themselves from this list when they are deleted; so
539 // we delete the last item until none are left in the list. 681 // we delete the last item until none are left in the list.
540 while (!default_client_observers_.empty()) { 682 while (!default_client_observers_.empty()) {
541 delete default_client_observers_.back(); 683 delete default_client_observers_.back();
542 } 684 }
543 } 685 }
544 686
545 // static 687 // static
546 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* pref_service) { 688 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* pref_service) {
547 pref_service->RegisterListPref(prefs::kRegisteredProtocolHandlers, 689 pref_service->RegisterListPref(prefs::kRegisteredProtocolHandlers,
548 PrefService::UNSYNCABLE_PREF); 690 PrefService::UNSYNCABLE_PREF);
549 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers, 691 pref_service->RegisterListPref(prefs::kIgnoredProtocolHandlers,
550 PrefService::UNSYNCABLE_PREF); 692 PrefService::UNSYNCABLE_PREF);
551 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true, 693 pref_service->RegisterBooleanPref(prefs::kCustomHandlersEnabled, true,
552 PrefService::UNSYNCABLE_PREF); 694 PrefService::UNSYNCABLE_PREF);
553 } 695 }
554 696
555 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() { 697 ProtocolHandlerRegistry::~ProtocolHandlerRegistry() {
556 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 698 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
557 DCHECK(default_client_observers_.empty()); 699 DCHECK(default_client_observers_.empty());
558 } 700 }
559 701
560 void ProtocolHandlerRegistry::PromoteHandler(const ProtocolHandler& handler) { 702 void ProtocolHandlerRegistry::PromoteHandler(const ProtocolHandler& handler) {
561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
562 DCHECK(IsRegistered(handler)); 704 DCHECK(IsRegistered(handler));
563 ProtocolHandlerMultiMap::iterator p = 705 ProtocolHandlerMultiMap::iterator p =
564 protocol_handlers_.find(handler.protocol()); 706 protocol_handlers_.find(handler.protocol());
565 ProtocolHandlerList& list = p->second; 707 ProtocolHandlerList& list = p->second;
566 list.erase(std::find(list.begin(), list.end(), handler)); 708 list.erase(std::find(list.begin(), list.end(), handler));
567 list.insert(list.begin(), handler); 709 list.insert(list.begin(), handler);
568 } 710 }
569 711
570 void ProtocolHandlerRegistry::ClearDefaultIO(const std::string& scheme) {
571 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
572 default_handlers_io_.erase(scheme);
573 }
574
575 void ProtocolHandlerRegistry::SetDefaultIO(const ProtocolHandler& handler) {
576 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
577 ClearDefaultIO(handler.protocol());
578 default_handlers_io_.insert(std::make_pair(handler.protocol(), handler));
579 }
580
581 void ProtocolHandlerRegistry::Save() { 712 void ProtocolHandlerRegistry::Save() {
582 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 713 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
583 if (is_loading_) { 714 if (is_loading_) {
584 return; 715 return;
585 } 716 }
586 scoped_ptr<Value> registered_protocol_handlers(EncodeRegisteredHandlers()); 717 scoped_ptr<Value> registered_protocol_handlers(EncodeRegisteredHandlers());
587 scoped_ptr<Value> ignored_protocol_handlers(EncodeIgnoredHandlers()); 718 scoped_ptr<Value> ignored_protocol_handlers(EncodeIgnoredHandlers());
588 scoped_ptr<Value> enabled(Value::CreateBooleanValue(enabled_)); 719 scoped_ptr<Value> enabled(Value::CreateBooleanValue(enabled_));
589 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, 720 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers,
590 *registered_protocol_handlers); 721 *registered_protocol_handlers);
(...skipping 20 matching lines...) Expand all
611 // If we're not loading, and we are setting a default for a new protocol, 742 // If we're not loading, and we are setting a default for a new protocol,
612 // register with the OS. 743 // register with the OS.
613 if (!is_loading_ && p == default_handlers_.end()) 744 if (!is_loading_ && p == default_handlers_.end())
614 delegate_->RegisterWithOSAsDefaultClient(handler.protocol(), this); 745 delegate_->RegisterWithOSAsDefaultClient(handler.protocol(), this);
615 default_handlers_.erase(handler.protocol()); 746 default_handlers_.erase(handler.protocol());
616 default_handlers_.insert(std::make_pair(handler.protocol(), handler)); 747 default_handlers_.insert(std::make_pair(handler.protocol(), handler));
617 PromoteHandler(handler); 748 PromoteHandler(handler);
618 BrowserThread::PostTask( 749 BrowserThread::PostTask(
619 BrowserThread::IO, 750 BrowserThread::IO,
620 FROM_HERE, 751 FROM_HERE,
621 base::Bind(&ProtocolHandlerRegistry::SetDefaultIO, this, handler)); 752 base::Bind(&Core::SetDefault, core_, handler));
622 } 753 }
623 754
624 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) { 755 void ProtocolHandlerRegistry::InsertHandler(const ProtocolHandler& handler) {
625 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
626 ProtocolHandlerMultiMap::iterator p = 757 ProtocolHandlerMultiMap::iterator p =
627 protocol_handlers_.find(handler.protocol()); 758 protocol_handlers_.find(handler.protocol());
628 759
629 if (p != protocol_handlers_.end()) { 760 if (p != protocol_handlers_.end()) {
630 p->second.push_back(handler); 761 p->second.push_back(handler);
631 return; 762 return;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 } 845 }
715 846
716 void ProtocolHandlerRegistry::AddPredefinedHandler( 847 void ProtocolHandlerRegistry::AddPredefinedHandler(
717 const ProtocolHandler& handler) { 848 const ProtocolHandler& handler) {
718 // If called after the load command was issued this function will fail. 849 // If called after the load command was issued this function will fail.
719 DCHECK(!is_loaded_); 850 DCHECK(!is_loaded_);
720 RegisterProtocolHandler(handler); 851 RegisterProtocolHandler(handler);
721 SetDefault(handler); 852 SetDefault(handler);
722 } 853 }
723 854
724 855 net::URLRequestJobFactory::Interceptor*
856 ProtocolHandlerRegistry::CreateIOURLInterceptor() {
857 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
858 // this is always created on the UI thread (in profile_io's
859 // InitializeOnUIThread. Any method calls must be done
860 // on the IO thread (this is checked).
861 return new IOURLInterceptor(core_);
862 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698