1aac79f3561df4c1fdac4ccc3e7716efe0490a1e
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / builtin_provider.cc
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/autocomplete/builtin_provider.h"
6
7 #include <algorithm>
8
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/autocomplete/autocomplete_input.h"
12 #include "chrome/common/net/url_fixer_upper.h"
13 #include "chrome/common/url_constants.h"
14
15 namespace {
16
17 // This list should be kept in sync with chrome/common/url_constants.h.
18 // Only include useful sub-pages, confirmation alerts are not useful.
19 const char* const kChromeSettingsSubPages[] = {
20   chrome::kAutofillSubPage,
21   chrome::kClearBrowserDataSubPage,
22   chrome::kContentSettingsSubPage,
23   chrome::kContentSettingsExceptionsSubPage,
24   chrome::kImportDataSubPage,
25   chrome::kLanguageOptionsSubPage,
26   chrome::kPasswordManagerSubPage,
27   chrome::kResetProfileSettingsSubPage,
28   chrome::kSearchEnginesSubPage,
29   chrome::kSyncSetupSubPage,
30 #if defined(OS_CHROMEOS)
31   chrome::kInternetOptionsSubPage,
32 #endif
33 };
34
35 }  // namespace
36
37 const int BuiltinProvider::kRelevance = 860;
38
39 BuiltinProvider::BuiltinProvider(AutocompleteProviderListener* listener,
40                                  Profile* profile)
41     : AutocompleteProvider(listener, profile,
42           AutocompleteProvider::TYPE_BUILTIN) {
43   std::vector<std::string> builtins(
44       chrome::kChromeHostURLs,
45       chrome::kChromeHostURLs + chrome::kNumberOfChromeHostURLs);
46   std::sort(builtins.begin(), builtins.end());
47   for (std::vector<std::string>::iterator i(builtins.begin());
48        i != builtins.end(); ++i)
49     builtins_.push_back(base::ASCIIToUTF16(*i));
50   base::string16 settings(base::ASCIIToUTF16(chrome::kChromeUISettingsHost) +
51                           base::ASCIIToUTF16("/"));
52   for (size_t i = 0; i < arraysize(kChromeSettingsSubPages); i++) {
53     builtins_.push_back(
54         settings + base::ASCIIToUTF16(kChromeSettingsSubPages[i]));
55   }
56 }
57
58 void BuiltinProvider::Start(const AutocompleteInput& input,
59                             bool minimal_changes) {
60   matches_.clear();
61   if ((input.type() == AutocompleteInput::INVALID) ||
62       (input.type() == AutocompleteInput::FORCED_QUERY) ||
63       (input.type() == AutocompleteInput::QUERY))
64     return;
65
66   const base::string16 kAbout = base::ASCIIToUTF16(chrome::kAboutScheme) +
67       base::ASCIIToUTF16(content::kStandardSchemeSeparator);
68   const base::string16 kChrome = base::ASCIIToUTF16(chrome::kChromeUIScheme) +
69       base::ASCIIToUTF16(content::kStandardSchemeSeparator);
70
71   const int kUrl = ACMatchClassification::URL;
72   const int kMatch = kUrl | ACMatchClassification::MATCH;
73
74   base::string16 text = input.text();
75   bool starting_chrome = StartsWith(kChrome, text, false);
76   if (starting_chrome || StartsWith(kAbout, text, false)) {
77     ACMatchClassifications styles;
78     // Highlight the input portion matching "chrome://"; or if the user has
79     // input "about:" (with optional slashes), highlight the whole "chrome://".
80     const size_t kAboutSchemeLength = strlen(chrome::kAboutScheme);
81     bool highlight = starting_chrome || text.length() > kAboutSchemeLength;
82     styles.push_back(ACMatchClassification(0, highlight ? kMatch : kUrl));
83     size_t offset = starting_chrome ? text.length() : kChrome.length();
84     if (highlight)
85       styles.push_back(ACMatchClassification(offset, kUrl));
86     // Include some common builtin chrome URLs as the user types the scheme.
87     AddMatch(base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL),
88              base::string16(), styles);
89     AddMatch(base::ASCIIToUTF16(chrome::kChromeUISettingsURL),
90              base::string16(), styles);
91     AddMatch(base::ASCIIToUTF16(chrome::kChromeUIVersionURL),
92              base::string16(), styles);
93   } else {
94     // Match input about: or chrome: URL input against builtin chrome URLs.
95     GURL url = URLFixerUpper::FixupURL(base::UTF16ToUTF8(text), std::string());
96     // BuiltinProvider doesn't know how to suggest valid ?query or #fragment
97     // extensions to chrome: URLs.
98     if (url.SchemeIs(chrome::kChromeUIScheme) && url.has_host() &&
99         !url.has_query() && !url.has_ref()) {
100       // Include the path for sub-pages (e.g. "chrome://settings/browser").
101       base::string16 host_and_path = base::UTF8ToUTF16(url.host() + url.path());
102       base::TrimString(host_and_path, base::ASCIIToUTF16("/").c_str(),
103                        &host_and_path);
104       size_t match_length = kChrome.length() + host_and_path.length();
105       for (Builtins::const_iterator i(builtins_.begin());
106           (i != builtins_.end()) && (matches_.size() < kMaxMatches); ++i) {
107         if (StartsWith(*i, host_and_path, false)) {
108           ACMatchClassifications styles;
109           // Highlight the "chrome://" scheme, even for input "about:foo".
110           styles.push_back(ACMatchClassification(0, kMatch));
111           base::string16 match_string = kChrome + *i;
112           if (match_string.length() > match_length)
113             styles.push_back(ACMatchClassification(match_length, kUrl));
114           AddMatch(match_string, match_string.substr(match_length), styles);
115         }
116       }
117     }
118   }
119
120   for (size_t i = 0; i < matches_.size(); ++i)
121     matches_[i].relevance = kRelevance + matches_.size() - (i + 1);
122   if (!input.prevent_inline_autocomplete() && (matches_.size() == 1)) {
123     // If there's only one possible completion of the user's input and
124     // allowing completions is okay, give the match a high enough score to
125     // allow it to beat url-what-you-typed and be inlined.
126     matches_[0].relevance = 1250;
127     matches_[0].allowed_to_be_default_match = true;
128   }
129 }
130
131 BuiltinProvider::~BuiltinProvider() {}
132
133 void BuiltinProvider::AddMatch(const base::string16& match_string,
134                                const base::string16& inline_completion,
135                                const ACMatchClassifications& styles) {
136   AutocompleteMatch match(this, kRelevance, false,
137                           AutocompleteMatchType::NAVSUGGEST);
138   match.fill_into_edit = match_string;
139   match.inline_autocompletion = inline_completion;
140   match.destination_url = GURL(match_string);
141   match.contents = match_string;
142   match.contents_class = styles;
143   matches_.push_back(match);
144 }