OLD | NEW |
| (Empty) |
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/browser/net/browser_url_util.h" | |
6 | |
7 #include "base/string_util.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "chrome/common/url_constants.h" | |
10 #include "googleurl/src/gurl.h" | |
11 #include "googleurl/src/url_parse.h" | |
12 #include "net/base/escape.h" | |
13 #include "net/base/net_util.h" | |
14 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
15 | |
16 namespace chrome_browser_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 | |
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 | |
51 GURL AppendOrReplaceQueryParameter(const GURL& url, | |
52 const std::string& name, | |
53 const std::string& value) { | |
54 bool replaced = false; | |
55 std::string param_name = net::EscapeQueryParamValue(name, true); | |
56 std::string param_value = net::EscapeQueryParamValue(value, true); | |
57 | |
58 const std::string input = url.query(); | |
59 url_parse::Component cursor(0, input.size()); | |
60 std::string output; | |
61 url_parse::Component key_range, value_range; | |
62 while (url_parse::ExtractQueryKeyValue( | |
63 input.data(), &cursor, &key_range, &value_range)) { | |
64 const base::StringPiece key( | |
65 input.data() + key_range.begin, key_range.len); | |
66 const base::StringPiece value( | |
67 input.data() + value_range.begin, value_range.len); | |
68 std::string key_value_pair; | |
69 // Check |replaced| as only the first pair should be replaced. | |
70 if (!replaced && key == param_name) { | |
71 replaced = true; | |
72 key_value_pair = (param_name + "=" + param_value); | |
73 } else { | |
74 key_value_pair.assign(input.data(), | |
75 key_range.begin, | |
76 value_range.end() - key_range.begin); | |
77 } | |
78 if (!output.empty()) | |
79 output += "&"; | |
80 | |
81 output += key_value_pair; | |
82 } | |
83 if (!replaced) { | |
84 if (!output.empty()) | |
85 output += "&"; | |
86 | |
87 output += (param_name + "=" + param_value); | |
88 } | |
89 GURL::Replacements replacements; | |
90 replacements.SetQueryStr(output); | |
91 return url.ReplaceComponents(replacements); | |
92 } | |
93 | |
94 } // namespace chrome_browser_net | |
OLD | NEW |