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
,
14 : protocol_(protocol
),
18 ProtocolHandler
ProtocolHandler::CreateProtocolHandler(
19 const std::string
& protocol
,
21 std::string lower_protocol
= StringToLowerASCII(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();
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 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()));
69 std::string
ProtocolHandler::ToString() const {
70 return "{ protocol=" + protocol_
+
71 ", url=" + url_
.spec() +
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_
;