Chromium Code Reviews| 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, | |
|
Peter Kasting
2011/10/26 01:07:22
Nit: Indent all these 4.
| |
| 34 const string16& name, | |
| 35 const string16& url, | |
| 36 size_t name_match_index, | |
| 37 size_t url_match_index) { | |
| 38 // We have a match, might be a partial match. | |
|
Peter Kasting
2011/10/26 01:07:22
Nit: This comment belongs in the caller.
| |
| 39 // TODO(finnur): Figure out what type to return here, might want to have | |
| 40 // the extension icon/a generic icon show up in the Omnibox. | |
| 41 AutocompleteMatch match(this, 0, false, | |
| 42 AutocompleteMatch::EXTENSION_APP); | |
| 43 match.fill_into_edit = url; | |
| 44 match.destination_url = GURL(url); | |
| 45 match.inline_autocomplete_offset = string16::npos; | |
| 46 match.contents = AutocompleteMatch::SanitizeString(name); | |
| 47 AutocompleteMatch::ClassifyLocationInString(name_match_index, | |
| 48 input.text().length(), | |
| 49 name.length(), | |
| 50 ACMatchClassification::NONE, | |
| 51 &match.contents_class); | |
| 52 match.description = url; | |
| 53 AutocompleteMatch::ClassifyLocationInString(url_match_index, | |
| 54 input.text().length(), | |
| 55 url.length(), | |
| 56 ACMatchClassification::URL, | |
| 57 &match.description_class); | |
| 58 match.relevance = CalculateRelevance(input.type(), | |
| 59 input.text().length(), | |
| 60 name_match_index != string16::npos ? | |
| 61 name.length() : url.length(), | |
| 62 match.destination_url); | |
| 63 return match; | |
| 64 } | |
| 65 | |
| 32 void ExtensionAppProvider::Start(const AutocompleteInput& input, | 66 void ExtensionAppProvider::Start(const AutocompleteInput& input, |
| 33 bool minimal_changes) { | 67 bool minimal_changes) { |
| 34 matches_.clear(); | 68 matches_.clear(); |
| 35 | 69 |
| 36 if (input.type() == AutocompleteInput::INVALID) | 70 if (input.type() == AutocompleteInput::INVALID) |
| 37 return; | 71 return; |
| 38 | 72 |
| 39 if (!input.text().empty()) { | 73 if (!input.text().empty()) { |
| 40 for (ExtensionApps::const_iterator app = extension_apps_.begin(); | 74 for (ExtensionApps::const_iterator app = extension_apps_.begin(); |
| 41 app != extension_apps_.end(); ++app) { | 75 app != extension_apps_.end(); ++app) { |
| 42 // See if the input matches this extension application. | 76 // See if the input matches this extension application. |
| 43 const string16& name = app->first; | 77 const string16& name = app->first; |
| 44 string16::const_iterator name_iter = std::search(name.begin(), name.end(), | 78 string16::const_iterator name_iter = std::search(name.begin(), name.end(), |
| 45 input.text().begin(), input.text().end(), | 79 input.text().begin(), input.text().end(), |
| 46 base::CaseInsensitiveCompare<char16>()); | 80 base::CaseInsensitiveCompare<char16>()); |
| 47 bool matches_name = name_iter != name.end(); | 81 bool matches_name = name_iter != name.end(); |
| 48 const string16& url = app->second; | 82 const string16& url = app->second; |
| 49 string16::const_iterator url_iter = std::search(url.begin(), url.end(), | 83 string16::const_iterator url_iter = std::search(url.begin(), url.end(), |
| 50 input.text().begin(), input.text().end(), | 84 input.text().begin(), input.text().end(), |
| 51 base::CaseInsensitiveCompare<char16>()); | 85 base::CaseInsensitiveCompare<char16>()); |
| 52 bool matches_url = url_iter != url.end() && | 86 bool matches_url = url_iter != url.end() && |
| 53 input.type() != AutocompleteInput::FORCED_QUERY; | 87 input.type() != AutocompleteInput::FORCED_QUERY; |
| 54 | 88 |
| 55 if (matches_name || matches_url) { | 89 if (matches_name || matches_url) { |
| 56 // We have a match, might be a partial match. | 90 size_t name_match_index = matches_name ? |
| 57 // TODO(finnur): Figure out what type to return here, might want to have | 91 static_cast<size_t>(name_iter - name.begin()) : string16::npos; |
| 58 // the extension icon/a generic icon show up in the Omnibox. | 92 size_t url_match_index = matches_url ? |
| 59 AutocompleteMatch match(this, 0, false, | 93 static_cast<size_t>(url_iter - url.begin()) : string16::npos; |
| 60 AutocompleteMatch::EXTENSION_APP); | 94 matches_.push_back( |
| 61 match.fill_into_edit = url; | 95 CreateAutocompleteMatch(input, name, url, name_match_index, |
| 62 match.destination_url = GURL(url); | 96 url_match_index)); |
| 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 } | 97 } |
| 79 } | 98 } |
| 80 } | 99 } |
| 81 } | 100 } |
| 82 | 101 |
| 83 ExtensionAppProvider::~ExtensionAppProvider() { | 102 ExtensionAppProvider::~ExtensionAppProvider() { |
| 84 } | 103 } |
| 85 | 104 |
| 86 void ExtensionAppProvider::RefreshAppList() { | 105 void ExtensionAppProvider::RefreshAppList() { |
| 87 ExtensionService* extension_service = profile_->GetExtensionService(); | 106 ExtensionService* extension_service = profile_->GetExtensionService(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 history::URLRow info; | 167 history::URLRow info; |
| 149 url_db->GetRowForURL(url, &info); | 168 url_db->GetRowForURL(url, &info); |
| 150 type_count_boost = | 169 type_count_boost = |
| 151 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); | 170 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); |
| 152 } | 171 } |
| 153 int relevance = 575 + static_cast<int>(type_count_boost) + | 172 int relevance = 575 + static_cast<int>(type_count_boost) + |
| 154 static_cast<int>(fraction_boost); | 173 static_cast<int>(fraction_boost); |
| 155 DCHECK_LE(relevance, kMaxRelevance); | 174 DCHECK_LE(relevance, kMaxRelevance); |
| 156 return relevance; | 175 return relevance; |
| 157 } | 176 } |
| OLD | NEW |