Chromium Code Reviews| 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/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/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 14 #include "chrome/browser/extensions/extension_ui_util.h" | 14 #include "chrome/browser/extensions/extension_ui_util.h" |
| 15 #include "chrome/browser/extensions/extension_util.h" | 15 #include "chrome/browser/extensions/extension_util.h" |
| 16 #include "chrome/browser/history/history_service.h" | 16 #include "chrome/browser/history/history_service.h" |
| 17 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
| 18 #include "chrome/browser/history/url_database.h" | 18 #include "chrome/browser/history/url_database.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/extensions/application_launch.h" | 20 #include "chrome/browser/ui/extensions/application_launch.h" |
| 21 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" | 21 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h" |
| 22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 23 #include "components/metrics/proto/omnibox_input_type.pb.h" | |
| 23 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 24 #include "extensions/browser/extension_registry.h" | 25 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/browser/extension_system.h" | 26 #include "extensions/browser/extension_system.h" |
| 26 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
| 27 #include "extensions/common/extension_set.h" | 28 #include "extensions/common/extension_set.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 29 | 30 |
| 30 ExtensionAppProvider::ExtensionAppProvider( | 31 ExtensionAppProvider::ExtensionAppProvider( |
| 31 AutocompleteProviderListener* listener, | 32 AutocompleteProviderListener* listener, |
| 32 Profile* profile) | 33 Profile* profile) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 name_match_index != base::string16::npos ? | 100 name_match_index != base::string16::npos ? |
| 100 app.name.length() : app.launch_url.length(), | 101 app.name.length() : app.launch_url.length(), |
| 101 match.destination_url); | 102 match.destination_url); |
| 102 return match; | 103 return match; |
| 103 } | 104 } |
| 104 | 105 |
| 105 void ExtensionAppProvider::Start(const AutocompleteInput& input, | 106 void ExtensionAppProvider::Start(const AutocompleteInput& input, |
| 106 bool minimal_changes) { | 107 bool minimal_changes) { |
| 107 matches_.clear(); | 108 matches_.clear(); |
| 108 | 109 |
| 109 if ((input.type() == AutocompleteInput::INVALID) || | 110 if ((input.type() == metrics::OmniboxInputType::INVALID) || |
| 110 (input.type() == AutocompleteInput::FORCED_QUERY)) | 111 (input.type() == metrics::OmniboxInputType::FORCED_QUERY)) |
| 111 return; | 112 return; |
| 112 | 113 |
| 113 if (input.text().empty()) | 114 if (input.text().empty()) |
| 114 return; | 115 return; |
| 115 | 116 |
| 116 for (ExtensionApps::const_iterator app = extension_apps_.begin(); | 117 for (ExtensionApps::const_iterator app = extension_apps_.begin(); |
| 117 app != extension_apps_.end(); ++app) { | 118 app != extension_apps_.end(); ++app) { |
| 118 // See if the input matches this extension application. | 119 // See if the input matches this extension application. |
| 119 const base::string16& name = app->name; | 120 const base::string16& name = app->name; |
| 120 base::string16::const_iterator name_iter = | 121 base::string16::const_iterator name_iter = |
| 121 std::search(name.begin(), name.end(), | 122 std::search(name.begin(), name.end(), |
| 122 input.text().begin(), input.text().end(), | 123 input.text().begin(), input.text().end(), |
| 123 base::CaseInsensitiveCompare<base::char16>()); | 124 base::CaseInsensitiveCompare<base::char16>()); |
| 124 bool matches_name = name_iter != name.end(); | 125 bool matches_name = name_iter != name.end(); |
| 125 size_t name_match_index = matches_name ? | 126 size_t name_match_index = matches_name ? |
| 126 static_cast<size_t>(name_iter - name.begin()) : base::string16::npos; | 127 static_cast<size_t>(name_iter - name.begin()) : base::string16::npos; |
| 127 | 128 |
| 128 bool matches_url = false; | 129 bool matches_url = false; |
| 129 size_t url_match_index = base::string16::npos; | 130 size_t url_match_index = base::string16::npos; |
| 130 if (app->should_match_against_launch_url) { | 131 if (app->should_match_against_launch_url) { |
| 131 const base::string16& url = app->launch_url; | 132 const base::string16& url = app->launch_url; |
| 132 base::string16::const_iterator url_iter = | 133 base::string16::const_iterator url_iter = |
| 133 std::search(url.begin(), url.end(), | 134 std::search(url.begin(), url.end(), |
| 134 input.text().begin(), input.text().end(), | 135 input.text().begin(), input.text().end(), |
| 135 base::CaseInsensitiveCompare<base::char16>()); | 136 base::CaseInsensitiveCompare<base::char16>()); |
| 136 matches_url = url_iter != url.end() && | 137 matches_url = (url_iter != url.end()) && |
| 137 input.type() != AutocompleteInput::FORCED_QUERY; | 138 input.type() != metrics::OmniboxInputType::FORCED_QUERY; |
|
Peter Kasting
2014/06/06 20:42:33
Nit: Put parens on both subexprs if you're going t
Mark P
2014/06/06 21:24:51
Done. (put parens on both)
| |
| 138 url_match_index = matches_url ? | 139 url_match_index = matches_url ? |
| 139 static_cast<size_t>(url_iter - url.begin()) : base::string16::npos; | 140 static_cast<size_t>(url_iter - url.begin()) : base::string16::npos; |
| 140 } | 141 } |
| 141 | 142 |
| 142 if (matches_name || matches_url) { | 143 if (matches_name || matches_url) { |
| 143 // We have a match, might be a partial match. | 144 // We have a match, might be a partial match. |
| 144 matches_.push_back(CreateAutocompleteMatch( | 145 matches_.push_back(CreateAutocompleteMatch( |
| 145 input, *app, name_match_index, url_match_index)); | 146 input, *app, name_match_index, url_match_index)); |
| 146 } | 147 } |
| 147 } | 148 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 extension_apps_.push_back(extension_app); | 185 extension_apps_.push_back(extension_app); |
| 185 } | 186 } |
| 186 } | 187 } |
| 187 | 188 |
| 188 void ExtensionAppProvider::Observe(int type, | 189 void ExtensionAppProvider::Observe(int type, |
| 189 const content::NotificationSource& source, | 190 const content::NotificationSource& source, |
| 190 const content::NotificationDetails& details) { | 191 const content::NotificationDetails& details) { |
| 191 RefreshAppList(); | 192 RefreshAppList(); |
| 192 } | 193 } |
| 193 | 194 |
| 194 int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type, | 195 int ExtensionAppProvider::CalculateRelevance( |
| 195 int input_length, | 196 metrics::OmniboxInputType::Type type, |
| 196 int target_length, | 197 int input_length, |
| 197 const GURL& url) { | 198 int target_length, |
| 199 const GURL& url) { | |
| 198 // If you update the algorithm here, please remember to update the tables in | 200 // If you update the algorithm here, please remember to update the tables in |
| 199 // autocomplete.h also. | 201 // autocomplete.h also. |
| 200 const int kMaxRelevance = 1425; | 202 const int kMaxRelevance = 1425; |
| 201 | 203 |
| 202 if (input_length == target_length) | 204 if (input_length == target_length) |
| 203 return kMaxRelevance; | 205 return kMaxRelevance; |
| 204 | 206 |
| 205 // We give a boost proportionally based on how much of the input matches the | 207 // We give a boost proportionally based on how much of the input matches the |
| 206 // app name, up to a maximum close to 200 (we can be close to, but we'll never | 208 // app name, up to a maximum close to 200 (we can be close to, but we'll never |
| 207 // reach 200 because the 100% match is taken care of above). | 209 // reach 200 because the 100% match is taken care of above). |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 220 history::URLRow info; | 222 history::URLRow info; |
| 221 url_db->GetRowForURL(url, &info); | 223 url_db->GetRowForURL(url, &info); |
| 222 type_count_boost = | 224 type_count_boost = |
| 223 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); | 225 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); |
| 224 } | 226 } |
| 225 int relevance = 575 + static_cast<int>(type_count_boost) + | 227 int relevance = 575 + static_cast<int>(type_count_boost) + |
| 226 static_cast<int>(fraction_boost); | 228 static_cast<int>(fraction_boost); |
| 227 DCHECK_LE(relevance, kMaxRelevance); | 229 DCHECK_LE(relevance, kMaxRelevance); |
| 228 return relevance; | 230 return relevance; |
| 229 } | 231 } |
| OLD | NEW |