| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/net/url_util.h" | 5 #include "chrome/common/net/url_util.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | |
| 8 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/common/url_constants.h" | 8 #include "chrome/common/url_constants.h" |
| 10 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 11 #include "googleurl/src/url_parse.h" | |
| 12 #include "net/base/escape.h" | 10 #include "net/base/escape.h" |
| 13 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| 14 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 12 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 15 | 13 |
| 16 namespace chrome_common_net { | 14 namespace chrome_common_net { |
| 17 | 15 |
| 18 void WriteURLToClipboard(const GURL& url, | 16 void WriteURLToClipboard(const GURL& url, |
| 19 const std::string& languages, | 17 const std::string& languages, |
| 20 ui::Clipboard *clipboard) { | 18 ui::Clipboard *clipboard) { |
| 21 if (url.is_empty() || !url.is_valid() || !clipboard) | 19 if (url.is_empty() || !url.is_valid() || !clipboard) |
| 22 return; | 20 return; |
| 23 | 21 |
| 24 // Unescaping path and query is not a good idea because other applications | 22 // Unescaping path and query is not a good idea because other applications |
| 25 // may not encode non-ASCII characters in UTF-8. See crbug.com/2820. | 23 // may not encode non-ASCII characters in UTF-8. See crbug.com/2820. |
| 26 string16 text = url.SchemeIs(chrome::kMailToScheme) ? | 24 string16 text = url.SchemeIs(chrome::kMailToScheme) ? |
| 27 ASCIIToUTF16(url.path()) : | 25 ASCIIToUTF16(url.path()) : |
| 28 net::FormatUrl(url, languages, net::kFormatUrlOmitNothing, | 26 net::FormatUrl(url, languages, net::kFormatUrlOmitNothing, |
| 29 net::UnescapeRule::NONE, NULL, NULL, NULL); | 27 net::UnescapeRule::NONE, NULL, NULL, NULL); |
| 30 | 28 |
| 31 ui::ScopedClipboardWriter scw(clipboard, ui::Clipboard::BUFFER_STANDARD); | 29 ui::ScopedClipboardWriter scw(clipboard, ui::Clipboard::BUFFER_STANDARD); |
| 32 scw.WriteURL(text); | 30 scw.WriteURL(text); |
| 33 } | 31 } |
| 34 | 32 |
| 35 GURL AppendQueryParameter(const GURL& url, | |
| 36 const std::string& name, | |
| 37 const std::string& value) { | |
| 38 std::string query(url.query()); | |
| 39 | |
| 40 if (!query.empty()) | |
| 41 query += "&"; | |
| 42 | |
| 43 query += (net::EscapeQueryParamValue(name, true) + "=" + | |
| 44 net::EscapeQueryParamValue(value, true)); | |
| 45 GURL::Replacements replacements; | |
| 46 replacements.SetQueryStr(query); | |
| 47 return url.ReplaceComponents(replacements); | |
| 48 } | |
| 49 | |
| 50 GURL AppendOrReplaceQueryParameter(const GURL& url, | |
| 51 const std::string& name, | |
| 52 const std::string& value) { | |
| 53 bool replaced = false; | |
| 54 std::string param_name = net::EscapeQueryParamValue(name, true); | |
| 55 std::string param_value = net::EscapeQueryParamValue(value, true); | |
| 56 | |
| 57 const std::string input = url.query(); | |
| 58 url_parse::Component cursor(0, input.size()); | |
| 59 std::string output; | |
| 60 url_parse::Component key_range, value_range; | |
| 61 while (url_parse::ExtractQueryKeyValue( | |
| 62 input.data(), &cursor, &key_range, &value_range)) { | |
| 63 const base::StringPiece key( | |
| 64 input.data() + key_range.begin, key_range.len); | |
| 65 const base::StringPiece value( | |
| 66 input.data() + value_range.begin, value_range.len); | |
| 67 std::string key_value_pair; | |
| 68 // Check |replaced| as only the first pair should be replaced. | |
| 69 if (!replaced && key == param_name) { | |
| 70 replaced = true; | |
| 71 key_value_pair = (param_name + "=" + param_value); | |
| 72 } else { | |
| 73 key_value_pair.assign(input.data(), | |
| 74 key_range.begin, | |
| 75 value_range.end() - key_range.begin); | |
| 76 } | |
| 77 if (!output.empty()) | |
| 78 output += "&"; | |
| 79 | |
| 80 output += key_value_pair; | |
| 81 } | |
| 82 if (!replaced) { | |
| 83 if (!output.empty()) | |
| 84 output += "&"; | |
| 85 | |
| 86 output += (param_name + "=" + param_value); | |
| 87 } | |
| 88 GURL::Replacements replacements; | |
| 89 replacements.SetQueryStr(output); | |
| 90 return url.ReplaceComponents(replacements); | |
| 91 } | |
| 92 | |
| 93 bool GetValueForKeyInQuery(const GURL& url, | |
| 94 const std::string& search_key, | |
| 95 std::string* out_value) { | |
| 96 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | |
| 97 url_parse::Component key, value; | |
| 98 while (url_parse::ExtractQueryKeyValue( | |
| 99 url.spec().c_str(), &query, &key, &value)) { | |
| 100 if (key.is_nonempty()) { | |
| 101 std::string key_string = url.spec().substr(key.begin, key.len); | |
| 102 if (key_string == search_key) { | |
| 103 if (value.is_nonempty()) { | |
| 104 *out_value = net::UnescapeURLComponent( | |
| 105 url.spec().substr(value.begin, value.len), | |
| 106 net::UnescapeRule::SPACES | | |
| 107 net::UnescapeRule::URL_SPECIAL_CHARS | | |
| 108 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE); | |
| 109 } else { | |
| 110 *out_value = ""; | |
| 111 } | |
| 112 return true; | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 } // namespace chrome_common_net | 33 } // namespace chrome_common_net |
| OLD | NEW |