Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include "base/string_util.h" | |
| 2 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | |
| 3 #include "chrome/browser/custom_handlers/register_protocol_handler_infobar_deleg ate.h" | |
| 4 #include "chrome/browser/child_process_security_policy.h" | |
| 5 #include "chrome/browser/net/chrome_url_request_context.h" | |
| 6 #include "chrome/common/pref_names.h" | |
| 7 #include "net/url_request/url_request_redirect_job.h" | |
| 8 #include "stdio.h" | |
| 9 #include <iostream> | |
| 10 | |
| 11 // ProtocolHandler ------------------------------------------------------------- | |
| 12 | |
| 13 ProtocolHandler::ProtocolHandler(const std::string& protocol, const std::string& url, const std::string& title) | |
| 14 :protocol_(protocol), | |
| 15 url_(url), | |
| 16 title_(title) { | |
| 17 } | |
| 18 | |
| 19 ProtocolHandler* ProtocolHandler::CreateProtocolHandler( | |
| 20 const std::string& protocol, const std::string& url, | |
| 21 const std::string& title) { | |
| 22 // FIXME(koz) Check for bad protocol and url here. | |
|
tony
2011/02/07 20:51:45
TODO(koz) is more common in chromium code.
koz (OOO until 15th September)
2011/02/13 22:33:48
Done.
| |
| 23 std::string lowerProtocol(protocol); | |
|
tony
2011/02/07 20:51:45
Nit: lower_protocol
koz (OOO until 15th September)
2011/02/13 22:33:48
Done.
| |
| 24 lowerProtocol = StringToLowerASCII(protocol); | |
| 25 std::cout << "from " << protocol << " to " << lowerProtocol << std::endl; | |
|
tony
2011/02/07 20:51:45
remove debugging code here and other places. you
koz (OOO until 15th September)
2011/02/13 22:33:48
Done.
| |
| 26 return new ProtocolHandler(lowerProtocol, url, title); | |
| 27 } | |
| 28 | |
| 29 ProtocolHandler* ProtocolHandler::CreateProtocolHandler(const DictionaryValue* v alue) { | |
| 30 std::string protocol, url, title; | |
| 31 value->GetString("protocol", &protocol); | |
| 32 value->GetString("url", &url); | |
| 33 value->GetString("title", &title); | |
| 34 return ProtocolHandler::CreateProtocolHandler(protocol, url, title); | |
| 35 } | |
| 36 | |
| 37 GURL ProtocolHandler::TranslateUrl(const GURL& url) { | |
| 38 std::string urlToMangle = url.spec(); | |
| 39 int offset = urlToMangle.find("://"); | |
| 40 std::string remainder = urlToMangle.substr(offset + 3); | |
|
tony
2011/02/07 20:51:45
You can use url_cannon::Replacements to change the
koz (OOO until 15th September)
2011/02/13 22:33:48
Right - we'd like to clear the scheme, but it is e
| |
| 41 | |
| 42 return GURL(SpliceInto(url_, remainder)); | |
| 43 } | |
| 44 | |
| 45 Value* ProtocolHandler::Encode() { | |
| 46 DictionaryValue* d = new DictionaryValue(); | |
| 47 d->Set("protocol", Value::CreateStringValue(protocol_)); | |
| 48 d->Set("url", Value::CreateStringValue(url_)); | |
| 49 d->Set("title", Value::CreateStringValue(title_)); | |
| 50 return d; | |
| 51 } | |
| 52 | |
| 53 std::string ProtocolHandler::SpliceInto(const std::string& templ, const std::str ing& value) { | |
| 54 std::string result(templ); | |
| 55 result.replace(result.find("%s"), 2, value); | |
|
tony
2011/02/07 20:51:45
Can we share this code with the code for search en
koz (OOO until 15th September)
2011/02/13 22:33:48
The search engine code just calls ReplaceSubstring
| |
| 56 return result; | |
| 57 } | |
| 58 | |
| 59 // ProtocolHandlerRegistry ----------------------------------------------------- -------- | |
| 60 | |
| 61 ProtocolHandlerRegistry::ProtocolHandlerRegistry(Profile* profile) | |
| 62 :profile_(profile) { | |
| 63 printf("New protocol handler registry!\n"); | |
| 64 } | |
| 65 | |
| 66 void ProtocolHandlerRegistry::RegisterProtocolHandler(ProtocolHandler* handler) { | |
| 67 if (protocolHandlers_.find(handler->protocol()) == protocolHandlers_.end()) { | |
| 68 ChildProcessSecurityPolicy* policy = ChildProcessSecurityPolicy::GetInstance (); | |
| 69 if (!policy->IsWebSafeScheme(handler->protocol())) { | |
| 70 policy->RegisterWebSafeScheme(handler->protocol()); | |
| 71 } | |
| 72 net::URLRequest::RegisterProtocolFactory(handler->protocol(), &ProtocolHandl erRegistry::Factory); | |
| 73 } | |
| 74 protocolHandlers_[handler->protocol()] = handler; | |
| 75 } | |
| 76 | |
| 77 void ProtocolHandlerRegistry::Load() { | |
| 78 printf("Loading from profile.\n"); | |
| 79 PrefService* prefs = profile_->GetPrefs(); | |
| 80 if (!prefs->HasPrefPath(prefs::kRegisteredProtocolHandlers)) { | |
| 81 printf("Don't have a profile, cancelling load.\n"); | |
| 82 return; | |
| 83 } | |
| 84 const DictionaryValue* dict = prefs->GetDictionary(prefs::kRegisteredProtocolH andlers); | |
| 85 | |
| 86 for (DictionaryValue::key_iterator i = dict->begin_keys(); i != dict->end_keys (); ++i) { | |
| 87 DictionaryValue* value; | |
| 88 dict->GetDictionary(*i, &value); | |
| 89 RegisterHandlerFromValue(value); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void ProtocolHandlerRegistry::RegisterHandlerFromValue(const DictionaryValue* va lue) { | |
| 94 ProtocolHandler* handler = ProtocolHandler::CreateProtocolHandler(value); | |
| 95 if (handler) { | |
| 96 RegisterProtocolHandler(handler); | |
| 97 } else { | |
| 98 printf("Bad PH!\n"); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void ProtocolHandlerRegistry::Save() { | |
| 103 printf("Saving to profile.\n"); | |
| 104 Value* value = Encode(); | |
|
tony
2011/02/07 20:51:45
Can we use a scoped_ptr here?
koz (OOO until 15th September)
2011/02/13 22:33:48
Done.
| |
| 105 profile_->GetPrefs()->Set(prefs::kRegisteredProtocolHandlers, *value); | |
| 106 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | |
| 107 delete value; | |
| 108 } | |
| 109 | |
| 110 net::URLRequestJob* ProtocolHandlerRegistry::CreateJob(net::URLRequest* request, const std::string& scheme) const { | |
| 111 ProtocolHandlerMap::const_iterator i = protocolHandlers_.find(scheme); | |
| 112 if (i == protocolHandlers_.end()) { | |
| 113 return NULL; | |
| 114 } | |
| 115 | |
| 116 return new net::URLRequestRedirectJob(request, i->second->TranslateUrl(request ->url())); | |
| 117 } | |
| 118 | |
| 119 net::URLRequestJob* ProtocolHandlerRegistry::Factory(net::URLRequest* request, | |
| 120 const std::string& scheme) { | |
| 121 ChromeURLRequestContext* context = reinterpret_cast<ChromeURLRequestContext*>( request->context()); | |
| 122 return context->protocol_handler_registry()->CreateJob(request, scheme); | |
| 123 } | |
| 124 | |
| 125 Value* ProtocolHandlerRegistry::Encode() { | |
| 126 DictionaryValue* schemes = new DictionaryValue(); | |
| 127 | |
| 128 for (ProtocolHandlerMap::iterator i = protocolHandlers_.begin(); i != protocol Handlers_.end(); ++i) { | |
| 129 schemes->Set(i->first, i->second->Encode()); | |
| 130 } | |
| 131 return schemes; | |
| 132 } | |
| 133 | |
| 134 void ProtocolHandlerRegistry::OnAcceptRegisterProtocolHandler(ProtocolHandler* h andler) { | |
| 135 RegisterProtocolHandler(handler); | |
| 136 Save(); | |
| 137 } | |
| 138 | |
| 139 void ProtocolHandlerRegistry::OnDenyRegisterProtocolHandler(ProtocolHandler* han dler) { | |
| 140 | |
| 141 } | |
| 142 | |
| 143 void ProtocolHandlerRegistry::RegisterPrefs(PrefService* prefService) { | |
| 144 prefService->RegisterDictionaryPref(prefs::kRegisteredProtocolHandlers); | |
| 145 } | |
| OLD | NEW |