Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / common / custom_handlers / protocol_handler.cc
blob75fc2744310eb19e86b97f580b724ff5ca2dc328
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
3 // found in the LICENSE file.
5 #include "chrome/common/custom_handlers/protocol_handler.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "net/base/escape.h"
12 ProtocolHandler::ProtocolHandler(const std::string& protocol,
13 const GURL& url)
14 : protocol_(protocol),
15 url_(url) {
18 ProtocolHandler ProtocolHandler::CreateProtocolHandler(
19 const std::string& protocol,
20 const GURL& url) {
21 std::string lower_protocol = base::ToLowerASCII(protocol);
22 return ProtocolHandler(lower_protocol, url);
25 ProtocolHandler::ProtocolHandler() {
28 bool ProtocolHandler::IsValidDict(const base::DictionaryValue* value) {
29 // Note that "title" parameter is ignored.
30 return value->HasKey("protocol") && value->HasKey("url");
33 bool ProtocolHandler::IsSameOrigin(
34 const ProtocolHandler& handler) const {
35 return handler.url().GetOrigin() == url_.GetOrigin();
38 const ProtocolHandler& ProtocolHandler::EmptyProtocolHandler() {
39 static const ProtocolHandler* const kEmpty = new ProtocolHandler();
40 return *kEmpty;
43 ProtocolHandler ProtocolHandler::CreateProtocolHandler(
44 const base::DictionaryValue* value) {
45 if (!IsValidDict(value)) {
46 return EmptyProtocolHandler();
48 std::string protocol, url;
49 value->GetString("protocol", &protocol);
50 value->GetString("url", &url);
51 return ProtocolHandler::CreateProtocolHandler(protocol, GURL(url));
54 GURL ProtocolHandler::TranslateUrl(const GURL& url) const {
55 std::string translatedUrlSpec(url_.spec());
56 base::ReplaceSubstringsAfterOffset(&translatedUrlSpec, 0, "%s",
57 net::EscapeQueryParamValue(url.spec(), true));
58 return GURL(translatedUrlSpec);
61 base::DictionaryValue* ProtocolHandler::Encode() const {
62 base::DictionaryValue* d = new base::DictionaryValue();
63 d->Set("protocol", new base::StringValue(protocol_));
64 d->Set("url", new base::StringValue(url_.spec()));
65 return d;
68 #if !defined(NDEBUG)
69 std::string ProtocolHandler::ToString() const {
70 return "{ protocol=" + protocol_ +
71 ", url=" + url_.spec() +
72 " }";
74 #endif
76 bool ProtocolHandler::operator==(const ProtocolHandler& other) const {
77 return protocol_ == other.protocol_ && url_ == other.url_;
80 bool ProtocolHandler::IsEquivalent(const ProtocolHandler& other) const {
81 return protocol_ == other.protocol_ && url_ == other.url_;
84 bool ProtocolHandler::operator<(const ProtocolHandler& other) const {
85 return url_ < other.url_;