Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / common / custom_handlers / protocol_handler.cc
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.
4
5 #include "chrome/common/custom_handlers/protocol_handler.h"
6
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "net/base/escape.h"
10
11
12 ProtocolHandler::ProtocolHandler(const std::string& protocol,
13                                  const GURL& url)
14     : protocol_(protocol),
15       url_(url) {
16 }
17
18 ProtocolHandler ProtocolHandler::CreateProtocolHandler(
19     const std::string& protocol,
20     const GURL& url) {
21   std::string lower_protocol = base::StringToLowerASCII(protocol);
22   return ProtocolHandler(lower_protocol, url);
23 }
24
25 ProtocolHandler::ProtocolHandler() {
26 }
27
28 bool ProtocolHandler::IsValidDict(const base::DictionaryValue* value) {
29   // Note that "title" parameter is ignored.
30   return value->HasKey("protocol") && value->HasKey("url");
31 }
32
33 bool ProtocolHandler::IsSameOrigin(
34     const ProtocolHandler& handler) const {
35   return handler.url().GetOrigin() == url_.GetOrigin();
36 }
37
38 const ProtocolHandler& ProtocolHandler::EmptyProtocolHandler() {
39   static const ProtocolHandler* const kEmpty = new ProtocolHandler();
40   return *kEmpty;
41 }
42
43 ProtocolHandler ProtocolHandler::CreateProtocolHandler(
44     const base::DictionaryValue* value) {
45   if (!IsValidDict(value)) {
46     return EmptyProtocolHandler();
47   }
48   std::string protocol, url;
49   value->GetString("protocol", &protocol);
50   value->GetString("url", &url);
51   return ProtocolHandler::CreateProtocolHandler(protocol, GURL(url));
52 }
53
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);
59 }
60
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;
66 }
67
68 #if !defined(NDEBUG)
69 std::string ProtocolHandler::ToString() const {
70   return "{ protocol=" + protocol_ +
71          ", url=" + url_.spec() +
72          " }";
73 }
74 #endif
75
76 bool ProtocolHandler::operator==(const ProtocolHandler& other) const {
77   return protocol_ == other.protocol_ && url_ == other.url_;
78 }
79
80 bool ProtocolHandler::IsEquivalent(const ProtocolHandler& other) const {
81   return protocol_ == other.protocol_ && url_ == other.url_;
82 }
83
84 bool ProtocolHandler::operator<(const ProtocolHandler& other) const {
85   return url_ < other.url_;
86 }