OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/autocomplete/autocomplete_match.h" | |
10 #include "chrome/browser/browser_about_handler.h" | 9 #include "chrome/browser/browser_about_handler.h" |
11 #include "chrome/browser/net/url_fixer_upper.h" | 10 #include "chrome/browser/net/url_fixer_upper.h" |
11 #include "chrome/common/url_constants.h" | |
12 | |
13 namespace { | |
14 | |
15 // This list should be kept in sync with chrome/common/url_constants.h. | |
16 const char* kChromeSettingsSubPages[] = { | |
17 chrome::kAdvancedOptionsSubPage, | |
18 chrome::kAutofillSubPage, | |
19 chrome::kBrowserOptionsSubPage, | |
20 chrome::kClearBrowserDataSubPage, | |
21 chrome::kContentSettingsSubPage, | |
22 chrome::kContentSettingsExceptionsSubPage, | |
23 chrome::kImportDataSubPage, | |
24 chrome::kInstantConfirmPage, | |
25 chrome::kLanguageOptionsSubPage, | |
26 chrome::kPersonalOptionsSubPage, | |
27 chrome::kPasswordManagerSubPage, | |
28 chrome::kSearchEnginesSubPage, | |
29 chrome::kSyncSetupSubPage, | |
30 #if defined(OS_CHROMEOS) | |
31 chrome::kAboutOptionsSubPage, | |
32 chrome::kInternetOptionsSubPage, | |
33 chrome::kSystemOptionsSubPage, | |
34 #endif | |
35 }; | |
36 | |
37 } // namespace | |
12 | 38 |
13 const int BuiltinProvider::kRelevance = 575; | 39 const int BuiltinProvider::kRelevance = 575; |
14 | 40 |
15 BuiltinProvider::BuiltinProvider(ACProviderListener* listener, | 41 BuiltinProvider::BuiltinProvider(ACProviderListener* listener, |
16 Profile* profile) | 42 Profile* profile) |
17 : AutocompleteProvider(listener, profile, "Builtin") { | 43 : AutocompleteProvider(listener, profile, "Builtin") { |
18 std::vector<std::string> builtins(ChromePaths()); | 44 std::vector<std::string> builtins(ChromePaths()); |
19 for (std::vector<std::string>::iterator i(builtins.begin()); | 45 for (std::vector<std::string>::iterator i(builtins.begin()); |
20 i != builtins.end(); ++i) | 46 i != builtins.end(); ++i) |
msw
2011/06/11 21:49:48
Outdented one space.
| |
21 builtins_.push_back(ASCIIToUTF16("about:") + ASCIIToUTF16(*i)); | 47 builtins_.push_back(ASCIIToUTF16(*i)); |
48 string16 settings(ASCIIToUTF16(chrome::kChromeUISettingsHost) + | |
49 ASCIIToUTF16("/")); | |
50 for (size_t i = 0; i < arraysize(kChromeSettingsSubPages); i++) | |
51 builtins_.push_back(settings + ASCIIToUTF16(kChromeSettingsSubPages[i])); | |
22 } | 52 } |
23 | 53 |
24 BuiltinProvider::~BuiltinProvider() {} | 54 BuiltinProvider::~BuiltinProvider() {} |
25 | 55 |
26 void BuiltinProvider::Start(const AutocompleteInput& input, | 56 void BuiltinProvider::Start(const AutocompleteInput& input, |
27 bool minimal_changes) { | 57 bool minimal_changes) { |
28 matches_.clear(); | 58 matches_.clear(); |
29 if ((input.type() == AutocompleteInput::INVALID) || | 59 if ((input.type() == AutocompleteInput::INVALID) || |
30 (input.type() == AutocompleteInput::FORCED_QUERY) || | 60 (input.type() == AutocompleteInput::FORCED_QUERY) || |
31 (input.type() == AutocompleteInput::QUERY) || | 61 (input.type() == AutocompleteInput::QUERY) || |
32 (input.matches_requested() == AutocompleteInput::BEST_MATCH)) | 62 (input.matches_requested() == AutocompleteInput::BEST_MATCH)) |
33 return; | 63 return; |
34 for (Builtins::const_iterator i(builtins_.begin()); | 64 |
35 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { | 65 static const string16 kAbout = ASCIIToUTF16(chrome::kAboutScheme) + |
36 if (StartsWith(*i, input.text(), false)) { | 66 ASCIIToUTF16(chrome::kStandardSchemeSeparator); |
37 AutocompleteMatch match(this, kRelevance, false, | 67 static const string16 kChrome = ASCIIToUTF16(chrome::kChromeUIScheme) + |
38 AutocompleteMatch::NAVSUGGEST); | 68 ASCIIToUTF16(chrome::kStandardSchemeSeparator); |
39 match.fill_into_edit = *i; | 69 |
40 match.destination_url = GURL(*i); | 70 static const int kUrl = ACMatchClassification::URL; |
41 match.contents = match.fill_into_edit; | 71 static const int kMatch = kUrl | ACMatchClassification::MATCH; |
42 match.contents_class.push_back(ACMatchClassification(0, | 72 |
43 ACMatchClassification::MATCH | ACMatchClassification::URL)); | 73 string16 text = input.text(); |
44 if (match.contents.length() > input.text().length()) { | 74 bool starting_chrome = StartsWith(kChrome, text, false); |
45 match.contents_class.push_back( | 75 if (starting_chrome || StartsWith(kAbout, text, false)) { |
46 ACMatchClassification(input.text().length(), | 76 ACMatchClassifications styles; |
47 ACMatchClassification::URL)); | 77 // Highlight the input portion matching "chrome://"; or if the user has |
78 // input "about:" (with optional slashes), highlight the whole "chrome://". | |
79 static const size_t kAboutLength = strlen(chrome::kAboutScheme); | |
Peter Kasting
2011/06/14 20:18:33
Nit: Naming this |kAboutSchemeLength| might help c
msw
2011/06/14 21:32:15
Done.
| |
80 bool highlight = starting_chrome || text.length() > kAboutLength; | |
81 styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl)); | |
82 size_t offset = starting_chrome ? text.length() : kChrome.length(); | |
83 styles.push_back(ACMatchClassification(offset, kUrl)); | |
Peter Kasting
2011/06/14 20:18:33
Nit: Seems like you should only do this when |high
msw
2011/06/14 21:32:15
Done.
| |
84 // Include some common builtin chrome URLs as the user types the scheme. | |
85 AddMatch(text, ASCIIToUTF16(chrome::kChromeUIChromeURLsHost), styles); | |
86 AddMatch(text, ASCIIToUTF16(chrome::kChromeUISettingsHost), styles); | |
87 AddMatch(text, ASCIIToUTF16(chrome::kChromeUIVersionHost), styles); | |
88 } else { | |
89 // Match input about: or chrome: URL input against builtin chrome URLs. | |
90 GURL url = URLFixerUpper::FixupURL(UTF16ToUTF8(text), std::string()); | |
91 if (url.SchemeIs(chrome::kChromeUIScheme) && url.has_host()) { | |
92 // Include the path for sub-pages (e.g. "chrome://settings/browser"). | |
93 string16 host_and_path = UTF8ToUTF16(url.host() + url.path()); | |
94 TrimString(host_and_path, ASCIIToUTF16("/").c_str(), &host_and_path); | |
95 for (Builtins::const_iterator i(builtins_.begin()); | |
96 (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) { | |
Peter Kasting
2011/06/14 20:18:33
Nit: Indent 2 more
msw
2011/06/14 21:32:15
Done.
| |
97 if (StartsWith(*i, host_and_path, false)) { | |
98 ACMatchClassifications styles; | |
99 // Highlight the "chrome://" scheme, even for input "about:foo". | |
100 styles.push_back(ACMatchClassification(0, kMatch)); | |
101 size_t match_length = kChrome.length() + host_and_path.length(); | |
Peter Kasting
2011/06/14 20:18:33
Nit: This temp can be hoisted above the loop too,
msw
2011/06/14 21:32:15
Done.
| |
102 if ((kChrome.length() + i->length()) > match_length) | |
103 styles.push_back(ACMatchClassification(match_length, kUrl)); | |
104 AddMatch(text, *i, styles); | |
105 } | |
48 } | 106 } |
49 matches_.push_back(match); | |
50 } | 107 } |
51 } | 108 } |
109 | |
52 for (size_t i = 0; i < matches_.size(); ++i) | 110 for (size_t i = 0; i < matches_.size(); ++i) |
53 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); | 111 matches_[i].relevance = kRelevance + matches_.size() - (i + 1); |
54 } | 112 } |
113 | |
114 void BuiltinProvider::AddMatch(const string16& input, | |
Peter Kasting
2011/06/14 20:18:33
Nit: This argument is never used.
Seems like |inp
msw
2011/06/14 21:32:15
Done.
| |
115 const string16& match_string, | |
116 const ACMatchClassifications& styles) { | |
117 AutocompleteMatch match(this, kRelevance, false, | |
118 AutocompleteMatch::NAVSUGGEST); | |
119 match.fill_into_edit = ASCIIToUTF16(chrome::kChromeUIScheme) + | |
120 ASCIIToUTF16(chrome::kStandardSchemeSeparator) + match_string; | |
121 match.destination_url = GURL(match.fill_into_edit); | |
122 match.contents = match.fill_into_edit; | |
123 match.contents_class = styles; | |
124 matches_.push_back(match); | |
125 } | |
OLD | NEW |