OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/omnibox/browser/autocomplete_input.h" | 5 #include "components/omnibox/browser/autocomplete_input.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 if (type_ == metrics::OmniboxInputType::INVALID) | 113 if (type_ == metrics::OmniboxInputType::INVALID) |
114 return; | 114 return; |
115 | 115 |
116 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || | 116 if (((type_ == metrics::OmniboxInputType::UNKNOWN) || |
117 (type_ == metrics::OmniboxInputType::URL)) && | 117 (type_ == metrics::OmniboxInputType::URL)) && |
118 canonicalized_url.is_valid() && | 118 canonicalized_url.is_valid() && |
119 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || | 119 (!canonicalized_url.IsStandard() || canonicalized_url.SchemeIsFile() || |
120 canonicalized_url.SchemeIsFileSystem() || | 120 canonicalized_url.SchemeIsFileSystem() || |
121 !canonicalized_url.host().empty())) | 121 !canonicalized_url.host().empty())) |
122 canonicalized_url_ = canonicalized_url; | 122 canonicalized_url_ = canonicalized_url; |
123 | |
124 size_t chars_removed = RemoveForcedQueryStringIfNecessary(type_, &text_); | |
125 AdjustCursorPositionIfNecessary(chars_removed, &cursor_position_); | |
126 if (chars_removed) { | |
127 // Remove spaces between opening question mark and first actual character. | |
128 base::string16 trimmed_text; | |
129 if ((base::TrimWhitespace(text_, base::TRIM_LEADING, &trimmed_text) & | |
130 base::TRIM_LEADING) != 0) { | |
131 AdjustCursorPositionIfNecessary(text_.length() - trimmed_text.length(), | |
132 &cursor_position_); | |
133 text_ = trimmed_text; | |
134 } | |
135 } | |
136 } | 123 } |
137 | 124 |
138 AutocompleteInput::AutocompleteInput(const AutocompleteInput& other) = default; | 125 AutocompleteInput::AutocompleteInput(const AutocompleteInput& other) = default; |
139 | 126 |
140 AutocompleteInput::~AutocompleteInput() { | 127 AutocompleteInput::~AutocompleteInput() { |
141 } | 128 } |
142 | 129 |
143 // static | 130 // static |
144 size_t AutocompleteInput::RemoveForcedQueryStringIfNecessary( | |
145 metrics::OmniboxInputType::Type type, | |
146 base::string16* text) { | |
147 if ((type != metrics::OmniboxInputType::FORCED_QUERY) || text->empty() || | |
148 (*text)[0] != L'?') | |
149 return 0; | |
150 // Drop the leading '?'. | |
151 text->erase(0, 1); | |
152 return 1; | |
153 } | |
154 | |
155 // static | |
156 std::string AutocompleteInput::TypeToString( | 131 std::string AutocompleteInput::TypeToString( |
157 metrics::OmniboxInputType::Type type) { | 132 metrics::OmniboxInputType::Type type) { |
158 switch (type) { | 133 switch (type) { |
159 case metrics::OmniboxInputType::INVALID: return "invalid"; | 134 case metrics::OmniboxInputType::INVALID: return "invalid"; |
160 case metrics::OmniboxInputType::UNKNOWN: return "unknown"; | 135 case metrics::OmniboxInputType::UNKNOWN: return "unknown"; |
161 case metrics::OmniboxInputType::DEPRECATED_REQUESTED_URL: | 136 case metrics::OmniboxInputType::DEPRECATED_REQUESTED_URL: |
162 return "deprecated-requested-url"; | 137 return "deprecated-requested-url"; |
163 case metrics::OmniboxInputType::URL: return "url"; | 138 case metrics::OmniboxInputType::URL: return "url"; |
164 case metrics::OmniboxInputType::QUERY: return "query"; | 139 case metrics::OmniboxInputType::QUERY: return "query"; |
165 case metrics::OmniboxInputType::FORCED_QUERY: return "forced-query"; | 140 case metrics::OmniboxInputType::DEPRECATED_FORCED_QUERY: |
| 141 return "deprecated-forced-query"; |
166 } | 142 } |
167 return std::string(); | 143 return std::string(); |
168 } | 144 } |
169 | 145 |
170 // static | 146 // static |
171 metrics::OmniboxInputType::Type AutocompleteInput::Parse( | 147 metrics::OmniboxInputType::Type AutocompleteInput::Parse( |
172 const base::string16& text, | 148 const base::string16& text, |
173 const std::string& desired_tld, | 149 const std::string& desired_tld, |
174 const AutocompleteSchemeClassifier& scheme_classifier, | 150 const AutocompleteSchemeClassifier& scheme_classifier, |
175 url::Parsed* parts, | 151 url::Parsed* parts, |
176 base::string16* scheme, | 152 base::string16* scheme, |
177 GURL* canonicalized_url) { | 153 GURL* canonicalized_url) { |
178 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); | 154 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); |
179 if (first_non_white == base::string16::npos) | 155 if (first_non_white == base::string16::npos) |
180 return metrics::OmniboxInputType::INVALID; // All whitespace. | 156 return metrics::OmniboxInputType::INVALID; // All whitespace. |
181 | 157 |
182 if (text[first_non_white] == L'?') { | |
183 // If the first non-whitespace character is a '?', we magically treat this | |
184 // as a query. | |
185 return metrics::OmniboxInputType::FORCED_QUERY; | |
186 } | |
187 | |
188 // Ask our parsing back-end to help us understand what the user typed. We | 158 // Ask our parsing back-end to help us understand what the user typed. We |
189 // use the URLFixerUpper here because we want to be smart about what we | 159 // use the URLFixerUpper here because we want to be smart about what we |
190 // consider a scheme. For example, we shouldn't consider www.google.com:80 | 160 // consider a scheme. For example, we shouldn't consider www.google.com:80 |
191 // to have a scheme. | 161 // to have a scheme. |
192 url::Parsed local_parts; | 162 url::Parsed local_parts; |
193 if (!parts) | 163 if (!parts) |
194 parts = &local_parts; | 164 parts = &local_parts; |
195 const base::string16 parsed_scheme(url_formatter::SegmentURL(text, parts)); | 165 const base::string16 parsed_scheme(url_formatter::SegmentURL(text, parts)); |
196 if (scheme) | 166 if (scheme) |
197 *scheme = parsed_scheme; | 167 *scheme = parsed_scheme; |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 parts_ = url::Parsed(); | 538 parts_ = url::Parsed(); |
569 scheme_.clear(); | 539 scheme_.clear(); |
570 canonicalized_url_ = GURL(); | 540 canonicalized_url_ = GURL(); |
571 prevent_inline_autocomplete_ = false; | 541 prevent_inline_autocomplete_ = false; |
572 prefer_keyword_ = false; | 542 prefer_keyword_ = false; |
573 allow_exact_keyword_match_ = false; | 543 allow_exact_keyword_match_ = false; |
574 want_asynchronous_matches_ = true; | 544 want_asynchronous_matches_ = true; |
575 from_omnibox_focus_ = false; | 545 from_omnibox_focus_ = false; |
576 terms_prefixed_by_http_or_https_.clear(); | 546 terms_prefixed_by_http_or_https_.clear(); |
577 } | 547 } |
OLD | NEW |