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/browser/autocomplete/builtin_provider.h" | 5 #include "chrome/browser/autocomplete/builtin_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
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 29 matching lines...) Expand all Loading... |
40 Profile* profile) | 40 Profile* profile) |
41 : AutocompleteProvider(listener, profile, | 41 : AutocompleteProvider(listener, profile, |
42 AutocompleteProvider::TYPE_BUILTIN) { | 42 AutocompleteProvider::TYPE_BUILTIN) { |
43 std::vector<std::string> builtins( | 43 std::vector<std::string> builtins( |
44 chrome::kChromeHostURLs, | 44 chrome::kChromeHostURLs, |
45 chrome::kChromeHostURLs + chrome::kNumberOfChromeHostURLs); | 45 chrome::kChromeHostURLs + chrome::kNumberOfChromeHostURLs); |
46 std::sort(builtins.begin(), builtins.end()); | 46 std::sort(builtins.begin(), builtins.end()); |
47 for (std::vector<std::string>::iterator i(builtins.begin()); | 47 for (std::vector<std::string>::iterator i(builtins.begin()); |
48 i != builtins.end(); ++i) | 48 i != builtins.end(); ++i) |
49 builtins_.push_back(ASCIIToUTF16(*i)); | 49 builtins_.push_back(ASCIIToUTF16(*i)); |
50 string16 settings(ASCIIToUTF16(chrome::kChromeUISettingsHost) + | 50 base::string16 settings(ASCIIToUTF16(chrome::kChromeUISettingsHost) + |
51 ASCIIToUTF16("/")); | 51 ASCIIToUTF16("/")); |
52 for (size_t i = 0; i < arraysize(kChromeSettingsSubPages); i++) | 52 for (size_t i = 0; i < arraysize(kChromeSettingsSubPages); i++) |
53 builtins_.push_back(settings + ASCIIToUTF16(kChromeSettingsSubPages[i])); | 53 builtins_.push_back(settings + ASCIIToUTF16(kChromeSettingsSubPages[i])); |
54 } | 54 } |
55 | 55 |
56 void BuiltinProvider::Start(const AutocompleteInput& input, | 56 void BuiltinProvider::Start(const AutocompleteInput& input, |
57 bool minimal_changes) { | 57 bool minimal_changes) { |
58 matches_.clear(); | 58 matches_.clear(); |
59 if ((input.type() == AutocompleteInput::INVALID) || | 59 if ((input.type() == AutocompleteInput::INVALID) || |
60 (input.type() == AutocompleteInput::FORCED_QUERY) || | 60 (input.type() == AutocompleteInput::FORCED_QUERY) || |
61 (input.type() == AutocompleteInput::QUERY)) | 61 (input.type() == AutocompleteInput::QUERY)) |
62 return; | 62 return; |
63 | 63 |
64 const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme) + | 64 const base::string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme) + |
65 ASCIIToUTF16(content::kStandardSchemeSeparator); | 65 ASCIIToUTF16(content::kStandardSchemeSeparator); |
66 const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme) + | 66 const base::string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme) + |
67 ASCIIToUTF16(content::kStandardSchemeSeparator); | 67 ASCIIToUTF16(content::kStandardSchemeSeparator); |
68 | 68 |
69 const int kUrl = ACMatchClassification::URL; | 69 const int kUrl = ACMatchClassification::URL; |
70 const int kMatch = kUrl | ACMatchClassification::MATCH; | 70 const int kMatch = kUrl | ACMatchClassification::MATCH; |
71 | 71 |
72 string16 text = input.text(); | 72 base::string16 text = input.text(); |
73 bool starting_chrome = StartsWith(kChrome, text, false); | 73 bool starting_chrome = StartsWith(kChrome, text, false); |
74 if (starting_chrome || StartsWith(kAbout, text, false)) { | 74 if (starting_chrome || StartsWith(kAbout, text, false)) { |
75 ACMatchClassifications styles; | 75 ACMatchClassifications styles; |
76 // Highlight the input portion matching "chrome://"; or if the user has | 76 // Highlight the input portion matching "chrome://"; or if the user has |
77 // input "about:" (with optional slashes), highlight the whole "chrome://". | 77 // input "about:" (with optional slashes), highlight the whole "chrome://". |
78 const size_t kAboutSchemeLength = strlen(chrome::kAboutScheme); | 78 const size_t kAboutSchemeLength = strlen(chrome::kAboutScheme); |
79 bool highlight = starting_chrome || text.length() > kAboutSchemeLength; | 79 bool highlight = starting_chrome || text.length() > kAboutSchemeLength; |
80 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); | 80 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); |
81 size_t offset = starting_chrome ? text.length() : kChrome.length(); | 81 size_t offset = starting_chrome ? text.length() : kChrome.length(); |
82 if (highlight) | 82 if (highlight) |
83 styles.push_back(ACMatchClassification(offset, kUrl)); | 83 styles.push_back(ACMatchClassification(offset, kUrl)); |
84 // Include some common builtin chrome URLs as the user types the scheme. | 84 // Include some common builtin chrome URLs as the user types the scheme. |
85 AddMatch(ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), string16(), styles); | 85 AddMatch(ASCIIToUTF16(chrome::kChromeUIChromeURLsURL), base::string16(), |
86 AddMatch(ASCIIToUTF16(chrome::kChromeUISettingsURL), string16(), styles); | 86 styles); |
87 AddMatch(ASCIIToUTF16(chrome::kChromeUIVersionURL), string16(), styles); | 87 AddMatch(ASCIIToUTF16(chrome::kChromeUISettingsURL), base::string16(), |
| 88 styles); |
| 89 AddMatch(ASCIIToUTF16(chrome::kChromeUIVersionURL), base::string16(), |
| 90 styles); |
88 } else { | 91 } else { |
89 // Match input about: or chrome: URL input against builtin chrome URLs. | 92 // Match input about: or chrome: URL input against builtin chrome URLs. |
90 GURL url = URLFixerUpper::FixupURL(UTF16ToUTF8(text), std::string()); | 93 GURL url = URLFixerUpper::FixupURL(UTF16ToUTF8(text), std::string()); |
91 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment | 94 // BuiltinProvider doesn't know how to suggest valid ?query or #fragment |
92 // extensions to chrome: URLs. | 95 // extensions to chrome: URLs. |
93 if (url.SchemeIs(chrome::kChromeUIScheme) && url.has_host() && | 96 if (url.SchemeIs(chrome::kChromeUIScheme) && url.has_host() && |
94 !url.has_query() && !url.has_ref()) { | 97 !url.has_query() && !url.has_ref()) { |
95 // Include the path for sub-pages (e.g. "chrome://settings/browser"). | 98 // Include the path for sub-pages (e.g. "chrome://settings/browser"). |
96 string16 host_and_path = UTF8ToUTF16(url.host() + url.path()); | 99 base::string16 host_and_path = UTF8ToUTF16(url.host() + url.path()); |
97 base::TrimString(host_and_path, ASCIIToUTF16("/").c_str(), | 100 base::TrimString(host_and_path, ASCIIToUTF16("/").c_str(), |
98 &host_and_path); | 101 &host_and_path); |
99 size_t match_length = kChrome.length() + host_and_path.length(); | 102 size_t match_length = kChrome.length() + host_and_path.length(); |
100 for (Builtins::const_iterator i(builtins_.begin()); | 103 for (Builtins::const_iterator i(builtins_.begin()); |
101 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { | 104 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { |
102 if (StartsWith(*i, host_and_path, false)) { | 105 if (StartsWith(*i, host_and_path, false)) { |
103 ACMatchClassifications styles; | 106 ACMatchClassifications styles; |
104 // Highlight the "chrome://" scheme, even for input "about:foo". | 107 // Highlight the "chrome://" scheme, even for input "about:foo". |
105 styles.push_back(ACMatchClassification(0, kMatch)); | 108 styles.push_back(ACMatchClassification(0, kMatch)); |
106 string16 match_string = kChrome + *i; | 109 base::string16 match_string = kChrome + *i; |
107 if (match_string.length() > match_length) | 110 if (match_string.length() > match_length) |
108 styles.push_back(ACMatchClassification(match_length, kUrl)); | 111 styles.push_back(ACMatchClassification(match_length, kUrl)); |
109 AddMatch(match_string, match_string.substr(match_length), styles); | 112 AddMatch(match_string, match_string.substr(match_length), styles); |
110 } | 113 } |
111 } | 114 } |
112 } | 115 } |
113 } | 116 } |
114 | 117 |
115 for (size_t i = 0; i < matches_.size(); ++i) | 118 for (size_t i = 0; i < matches_.size(); ++i) |
116 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); | 119 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); |
117 if (!input.prevent_inline_autocomplete() && (matches_.size() == 1)) { | 120 if (!input.prevent_inline_autocomplete() && (matches_.size() == 1)) { |
118 // If there's only one possible completion of the user's input and | 121 // If there's only one possible completion of the user's input and |
119 // allowing completions is okay, give the match a high enough score to | 122 // allowing completions is okay, give the match a high enough score to |
120 // allow it to beat url-what-you-typed and be inlined. | 123 // allow it to beat url-what-you-typed and be inlined. |
121 matches_[0].relevance = 1250; | 124 matches_[0].relevance = 1250; |
122 matches_[0].allowed_to_be_default_match = true; | 125 matches_[0].allowed_to_be_default_match = true; |
123 } | 126 } |
124 } | 127 } |
125 | 128 |
126 BuiltinProvider::~BuiltinProvider() {} | 129 BuiltinProvider::~BuiltinProvider() {} |
127 | 130 |
128 void BuiltinProvider::AddMatch(const string16& match_string, | 131 void BuiltinProvider::AddMatch(const base::string16& match_string, |
129 const string16& inline_completion, | 132 const base::string16& inline_completion, |
130 const ACMatchClassifications& styles) { | 133 const ACMatchClassifications& styles) { |
131 AutocompleteMatch match(this, kRelevance, false, | 134 AutocompleteMatch match(this, kRelevance, false, |
132 AutocompleteMatchType::NAVSUGGEST); | 135 AutocompleteMatchType::NAVSUGGEST); |
133 match.fill_into_edit = match_string; | 136 match.fill_into_edit = match_string; |
134 match.inline_autocompletion = inline_completion; | 137 match.inline_autocompletion = inline_completion; |
135 match.destination_url = GURL(match_string); | 138 match.destination_url = GURL(match_string); |
136 match.contents = match_string; | 139 match.contents = match_string; |
137 match.contents_class = styles; | 140 match.contents_class = styles; |
138 matches_.push_back(match); | 141 matches_.push_back(match); |
139 } | 142 } |
OLD | NEW |