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/extension_app_provider.h" | 5 #include "chrome/browser/autocomplete/extension_app_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 : AutocompleteProvider(listener, profile, "ExtensionApps") { | 22 : AutocompleteProvider(listener, profile, "ExtensionApps") { |
23 RegisterForNotifications(); | 23 RegisterForNotifications(); |
24 RefreshAppList(); | 24 RefreshAppList(); |
25 } | 25 } |
26 | 26 |
27 void ExtensionAppProvider::AddExtensionAppForTesting(const string16& app_name, | 27 void ExtensionAppProvider::AddExtensionAppForTesting(const string16& app_name, |
28 const string16& url) { | 28 const string16& url) { |
29 extension_apps_.push_back(std::make_pair(app_name, url)); | 29 extension_apps_.push_back(std::make_pair(app_name, url)); |
30 } | 30 } |
31 | 31 |
| 32 AutocompleteMatch ExtensionAppProvider::CreateAutocompleteMatch( |
| 33 const AutocompleteInput& input, |
| 34 const string16& name, |
| 35 const string16& url, |
| 36 size_t name_match_index, |
| 37 size_t url_match_index) { |
| 38 // TODO(finnur): Figure out what type to return here, might want to have |
| 39 // the extension icon/a generic icon show up in the Omnibox. |
| 40 AutocompleteMatch match(this, 0, false, |
| 41 AutocompleteMatch::EXTENSION_APP); |
| 42 match.fill_into_edit = url; |
| 43 match.destination_url = GURL(url); |
| 44 match.inline_autocomplete_offset = string16::npos; |
| 45 match.contents = AutocompleteMatch::SanitizeString(name); |
| 46 AutocompleteMatch::ClassifyLocationInString(name_match_index, |
| 47 input.text().length(), name.length(), ACMatchClassification::NONE, |
| 48 &match.contents_class); |
| 49 match.description = url; |
| 50 AutocompleteMatch::ClassifyLocationInString(url_match_index, |
| 51 input.text().length(), url.length(), ACMatchClassification::URL, |
| 52 &match.description_class); |
| 53 match.relevance = CalculateRelevance(input.type(), input.text().length(), |
| 54 (name_match_index != string16::npos ? name.length() : url.length()), |
| 55 match.destination_url); |
| 56 return match; |
| 57 } |
| 58 |
32 void ExtensionAppProvider::Start(const AutocompleteInput& input, | 59 void ExtensionAppProvider::Start(const AutocompleteInput& input, |
33 bool minimal_changes) { | 60 bool minimal_changes) { |
34 matches_.clear(); | 61 matches_.clear(); |
35 | 62 |
36 if (input.type() == AutocompleteInput::INVALID) | 63 if (input.type() == AutocompleteInput::INVALID) |
37 return; | 64 return; |
38 | 65 |
39 if (!input.text().empty()) { | 66 if (!input.text().empty()) { |
40 for (ExtensionApps::const_iterator app = extension_apps_.begin(); | 67 for (ExtensionApps::const_iterator app = extension_apps_.begin(); |
41 app != extension_apps_.end(); ++app) { | 68 app != extension_apps_.end(); ++app) { |
42 // See if the input matches this extension application. | 69 // See if the input matches this extension application. |
43 const string16& name = app->first; | 70 const string16& name = app->first; |
44 string16::const_iterator name_iter = std::search(name.begin(), name.end(), | 71 string16::const_iterator name_iter = std::search(name.begin(), name.end(), |
45 input.text().begin(), input.text().end(), | 72 input.text().begin(), input.text().end(), |
46 base::CaseInsensitiveCompare<char16>()); | 73 base::CaseInsensitiveCompare<char16>()); |
47 bool matches_name = name_iter != name.end(); | 74 bool matches_name = name_iter != name.end(); |
48 const string16& url = app->second; | 75 const string16& url = app->second; |
49 string16::const_iterator url_iter = std::search(url.begin(), url.end(), | 76 string16::const_iterator url_iter = std::search(url.begin(), url.end(), |
50 input.text().begin(), input.text().end(), | 77 input.text().begin(), input.text().end(), |
51 base::CaseInsensitiveCompare<char16>()); | 78 base::CaseInsensitiveCompare<char16>()); |
52 bool matches_url = url_iter != url.end() && | 79 bool matches_url = url_iter != url.end() && |
53 input.type() != AutocompleteInput::FORCED_QUERY; | 80 input.type() != AutocompleteInput::FORCED_QUERY; |
54 | 81 |
55 if (matches_name || matches_url) { | 82 if (matches_name || matches_url) { |
56 // We have a match, might be a partial match. | 83 // We have a match, might be a partial match. |
57 // TODO(finnur): Figure out what type to return here, might want to have | 84 matches_.push_back(CreateAutocompleteMatch(input, name, url, |
58 // the extension icon/a generic icon show up in the Omnibox. | 85 matches_name ? |
59 AutocompleteMatch match(this, 0, false, | 86 static_cast<size_t>(name_iter - name.begin()) : string16::npos, |
60 AutocompleteMatch::EXTENSION_APP); | 87 matches_url ? |
61 match.fill_into_edit = url; | 88 static_cast<size_t>(url_iter - url.begin()) : string16::npos)); |
62 match.destination_url = GURL(url); | |
63 match.inline_autocomplete_offset = string16::npos; | |
64 match.contents = name; | |
65 AutocompleteMatch::ClassifyLocationInString(matches_name ? | |
66 static_cast<size_t>(name_iter - name.begin()) : string16::npos, | |
67 input.text().length(), name.length(), ACMatchClassification::NONE, | |
68 &match.contents_class); | |
69 match.description = url; | |
70 AutocompleteMatch::ClassifyLocationInString(matches_url ? | |
71 static_cast<size_t>(url_iter - url.begin()) : string16::npos, | |
72 input.text().length(), url.length(), ACMatchClassification::URL, | |
73 &match.description_class); | |
74 match.relevance = CalculateRelevance(input.type(), | |
75 input.text().length(), matches_name ? name.length() : url.length(), | |
76 match.destination_url); | |
77 matches_.push_back(match); | |
78 } | 89 } |
79 } | 90 } |
80 } | 91 } |
81 } | 92 } |
82 | 93 |
83 ExtensionAppProvider::~ExtensionAppProvider() { | 94 ExtensionAppProvider::~ExtensionAppProvider() { |
84 } | 95 } |
85 | 96 |
86 void ExtensionAppProvider::RefreshAppList() { | 97 void ExtensionAppProvider::RefreshAppList() { |
87 ExtensionService* extension_service = profile_->GetExtensionService(); | 98 ExtensionService* extension_service = profile_->GetExtensionService(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 history::URLRow info; | 159 history::URLRow info; |
149 url_db->GetRowForURL(url, &info); | 160 url_db->GetRowForURL(url, &info); |
150 type_count_boost = | 161 type_count_boost = |
151 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); | 162 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); |
152 } | 163 } |
153 int relevance = 575 + static_cast<int>(type_count_boost) + | 164 int relevance = 575 + static_cast<int>(type_count_boost) + |
154 static_cast<int>(fraction_boost); | 165 static_cast<int>(fraction_boost); |
155 DCHECK_LE(relevance, kMaxRelevance); | 166 DCHECK_LE(relevance, kMaxRelevance); |
156 return relevance; | 167 return relevance; |
157 } | 168 } |
OLD | NEW |