| 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"; | |
| 166 } | 140 } |
| 167 return std::string(); | 141 return std::string(); |
| 168 } | 142 } |
| 169 | 143 |
| 170 // static | 144 // static |
| 171 metrics::OmniboxInputType::Type AutocompleteInput::Parse( | 145 metrics::OmniboxInputType::Type AutocompleteInput::Parse( |
| 172 const base::string16& text, | 146 const base::string16& text, |
| 173 const std::string& desired_tld, | 147 const std::string& desired_tld, |
| 174 const AutocompleteSchemeClassifier& scheme_classifier, | 148 const AutocompleteSchemeClassifier& scheme_classifier, |
| 175 url::Parsed* parts, | 149 url::Parsed* parts, |
| 176 base::string16* scheme, | 150 base::string16* scheme, |
| 177 GURL* canonicalized_url) { | 151 GURL* canonicalized_url) { |
| 178 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); | 152 size_t first_non_white = text.find_first_not_of(base::kWhitespaceUTF16, 0); |
| 179 if (first_non_white == base::string16::npos) | 153 if (first_non_white == base::string16::npos) |
| 180 return metrics::OmniboxInputType::INVALID; // All whitespace. | 154 return metrics::OmniboxInputType::INVALID; // All whitespace. |
| 181 | 155 |
| 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 | 156 // 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 | 157 // 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 | 158 // consider a scheme. For example, we shouldn't consider www.google.com:80 |
| 191 // to have a scheme. | 159 // to have a scheme. |
| 192 url::Parsed local_parts; | 160 url::Parsed local_parts; |
| 193 if (!parts) | 161 if (!parts) |
| 194 parts = &local_parts; | 162 parts = &local_parts; |
| 195 const base::string16 parsed_scheme(url_formatter::SegmentURL(text, parts)); | 163 const base::string16 parsed_scheme(url_formatter::SegmentURL(text, parts)); |
| 196 if (scheme) | 164 if (scheme) |
| 197 *scheme = parsed_scheme; | 165 *scheme = parsed_scheme; |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 parts_ = url::Parsed(); | 536 parts_ = url::Parsed(); |
| 569 scheme_.clear(); | 537 scheme_.clear(); |
| 570 canonicalized_url_ = GURL(); | 538 canonicalized_url_ = GURL(); |
| 571 prevent_inline_autocomplete_ = false; | 539 prevent_inline_autocomplete_ = false; |
| 572 prefer_keyword_ = false; | 540 prefer_keyword_ = false; |
| 573 allow_exact_keyword_match_ = false; | 541 allow_exact_keyword_match_ = false; |
| 574 want_asynchronous_matches_ = true; | 542 want_asynchronous_matches_ = true; |
| 575 from_omnibox_focus_ = false; | 543 from_omnibox_focus_ = false; |
| 576 terms_prefixed_by_http_or_https_.clear(); | 544 terms_prefixed_by_http_or_https_.clear(); |
| 577 } | 545 } |
| OLD | NEW |