1// Copyright (c) 2011 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/browser/custom_handlers/protocol_handler.h"
6
7#include "base/string_util.h"
8#include "net/base/escape.h"
9
10ProtocolHandler::ProtocolHandler(const std::string& protocol,
11                                 const GURL& url,
12                                 const string16& title)
13  : protocol_(protocol),
14    url_(url),
15    title_(title) {
16}
17
18ProtocolHandler* ProtocolHandler::CreateProtocolHandler(
19    const std::string& protocol,
20    const GURL& url,
21    const string16& title) {
22  std::string lower_protocol(protocol);
23  lower_protocol = StringToLowerASCII(protocol);
24  return new ProtocolHandler(lower_protocol, url, title);
25}
26
27ProtocolHandler* ProtocolHandler::CreateProtocolHandler(
28    const DictionaryValue* value) {
29  std::string protocol, url;
30  string16 title;
31  value->GetString("protocol", &protocol);
32  value->GetString("url", &url);
33  value->GetString("title", &title);
34  return ProtocolHandler::CreateProtocolHandler(protocol, GURL(url), title);
35}
36
37GURL ProtocolHandler::TranslateUrl(const GURL& url) {
38  std::string translatedUrlSpec(url_.spec());
39  ReplaceSubstringsAfterOffset(&translatedUrlSpec, 0, "%s",
40      EscapeQueryParamValue(url.spec(), true));
41  return GURL(translatedUrlSpec);
42}
43
44Value* ProtocolHandler::Encode() {
45  DictionaryValue* d = new DictionaryValue();
46  d->Set("protocol", Value::CreateStringValue(protocol_));
47  d->Set("url", Value::CreateStringValue(url_.spec()));
48  d->Set("title", Value::CreateStringValue(title_));
49  return d;
50}
51
52bool ProtocolHandler::operator==(const ProtocolHandler &other) const {
53  return protocol_ == other.protocol_ &&
54    url_ == other.url_ &&
55    title_ == other.title_;
56}
57
58