OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/base/url_util.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_piece.h" |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/common/url_constants.h" | |
10 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
11 #include "googleurl/src/url_parse.h" | |
12 #include "net/base/escape.h" | 9 #include "net/base/escape.h" |
13 #include "net/base/net_util.h" | |
14 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
15 | 10 |
16 namespace chrome_common_net { | 11 namespace net { |
17 | |
18 void WriteURLToClipboard(const GURL& url, | |
19 const std::string& languages, | |
20 ui::Clipboard *clipboard) { | |
21 if (url.is_empty() || !url.is_valid() || !clipboard) | |
22 return; | |
23 | |
24 // 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. | |
26 string16 text = url.SchemeIs(chrome::kMailToScheme) ? | |
27 ASCIIToUTF16(url.path()) : | |
28 net::FormatUrl(url, languages, net::kFormatUrlOmitNothing, | |
29 net::UnescapeRule::NONE, NULL, NULL, NULL); | |
30 | |
31 ui::ScopedClipboardWriter scw(clipboard, ui::Clipboard::BUFFER_STANDARD); | |
32 scw.WriteURL(text); | |
33 } | |
34 | 12 |
35 GURL AppendQueryParameter(const GURL& url, | 13 GURL AppendQueryParameter(const GURL& url, |
36 const std::string& name, | 14 const std::string& name, |
37 const std::string& value) { | 15 const std::string& value) { |
38 std::string query(url.query()); | 16 std::string query(url.query()); |
39 | 17 |
40 if (!query.empty()) | 18 if (!query.empty()) |
41 query += "&"; | 19 query += "&"; |
42 | 20 |
43 query += (net::EscapeQueryParamValue(name, true) + "=" + | 21 query += (EscapeQueryParamValue(name, true) + "=" + |
44 net::EscapeQueryParamValue(value, true)); | 22 EscapeQueryParamValue(value, true)); |
45 GURL::Replacements replacements; | 23 GURL::Replacements replacements; |
46 replacements.SetQueryStr(query); | 24 replacements.SetQueryStr(query); |
47 return url.ReplaceComponents(replacements); | 25 return url.ReplaceComponents(replacements); |
48 } | 26 } |
49 | 27 |
50 GURL AppendOrReplaceQueryParameter(const GURL& url, | 28 GURL AppendOrReplaceQueryParameter(const GURL& url, |
51 const std::string& name, | 29 const std::string& name, |
52 const std::string& value) { | 30 const std::string& value) { |
53 bool replaced = false; | 31 bool replaced = false; |
54 std::string param_name = net::EscapeQueryParamValue(name, true); | 32 std::string param_name = EscapeQueryParamValue(name, true); |
55 std::string param_value = net::EscapeQueryParamValue(value, true); | 33 std::string param_value = EscapeQueryParamValue(value, true); |
56 | 34 |
57 const std::string input = url.query(); | 35 const std::string input = url.query(); |
58 url_parse::Component cursor(0, input.size()); | 36 url_parse::Component cursor(0, input.size()); |
59 std::string output; | 37 std::string output; |
60 url_parse::Component key_range, value_range; | 38 url_parse::Component key_range, value_range; |
61 while (url_parse::ExtractQueryKeyValue( | 39 while (url_parse::ExtractQueryKeyValue( |
62 input.data(), &cursor, &key_range, &value_range)) { | 40 input.data(), &cursor, &key_range, &value_range)) { |
63 const base::StringPiece key( | 41 const base::StringPiece key( |
64 input.data() + key_range.begin, key_range.len); | 42 input.data() + key_range.begin, key_range.len); |
65 const base::StringPiece value( | 43 const base::StringPiece value( |
(...skipping 28 matching lines...) Expand all Loading... | |
94 const std::string& search_key, | 72 const std::string& search_key, |
95 std::string* out_value) { | 73 std::string* out_value) { |
96 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | 74 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; |
97 url_parse::Component key, value; | 75 url_parse::Component key, value; |
98 while (url_parse::ExtractQueryKeyValue( | 76 while (url_parse::ExtractQueryKeyValue( |
99 url.spec().c_str(), &query, &key, &value)) { | 77 url.spec().c_str(), &query, &key, &value)) { |
100 if (key.is_nonempty()) { | 78 if (key.is_nonempty()) { |
101 std::string key_string = url.spec().substr(key.begin, key.len); | 79 std::string key_string = url.spec().substr(key.begin, key.len); |
102 if (key_string == search_key) { | 80 if (key_string == search_key) { |
103 if (value.is_nonempty()) { | 81 if (value.is_nonempty()) { |
104 *out_value = net::UnescapeURLComponent( | 82 *out_value = UnescapeURLComponent( |
105 url.spec().substr(value.begin, value.len), | 83 url.spec().substr(value.begin, value.len), |
106 net::UnescapeRule::SPACES | | 84 UnescapeRule::SPACES | |
107 net::UnescapeRule::URL_SPECIAL_CHARS | | 85 UnescapeRule::URL_SPECIAL_CHARS | |
108 net::UnescapeRule::REPLACE_PLUS_WITH_SPACE); | 86 UnescapeRule::REPLACE_PLUS_WITH_SPACE); |
mmenke
2013/01/28 16:01:02
Did you deliberately reduce the indent here?
tfarina
2013/01/28 16:04:10
Yes. I don't mind either style, but I prefer this
mmenke
2013/01/28 16:05:35
I prefer the old style, to make it clear they are
| |
109 } else { | 87 } else { |
110 *out_value = ""; | 88 *out_value = ""; |
111 } | 89 } |
112 return true; | 90 return true; |
113 } | 91 } |
114 } | 92 } |
115 } | 93 } |
116 return false; | 94 return false; |
117 } | 95 } |
118 | 96 |
119 } // namespace chrome_common_net | 97 } // namespace net |
OLD | NEW |