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" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/history/history.h" | 13 #include "chrome/browser/history/history.h" |
| 14 #include "chrome/browser/history/url_database.h" | 14 #include "chrome/browser/history/url_database.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "content/common/notification_service.h" | 16 #include "content/common/notification_service.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 18 | 18 |
| 19 ExtensionAppProvider::ExtensionAppProvider(ACProviderListener* listener, | 19 ExtensionAppProvider::ExtensionAppProvider(ACProviderListener* listener, |
| 20 Profile* profile) | 20 Profile* profile) |
| 21 : AutocompleteProvider(listener, profile, "ExtensionApps") { | 21 : AutocompleteProvider(listener, profile, "ExtensionApps") { |
| 22 RegisterForNotifications(); | 22 RegisterForNotifications(); |
| 23 RefreshAppList(); | 23 RefreshAppList(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void ExtensionAppProvider::AddExtensionAppForTesting( | 26 void ExtensionAppProvider::AddExtensionAppForTesting( |
| 27 const std::string& app_name, | 27 const string16& app_name, |
|
Finnur
2011/05/25 22:26:05
Nit: This no longer needs to be on a separate line
| |
| 28 const std::string 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 void ExtensionAppProvider::Start(const AutocompleteInput& input, | 32 void ExtensionAppProvider::Start(const AutocompleteInput& input, |
| 33 bool minimal_changes) { | 33 bool minimal_changes) { |
| 34 matches_.clear(); | 34 matches_.clear(); |
| 35 | 35 |
| 36 if (input.type() == AutocompleteInput::INVALID) | 36 if (input.type() == AutocompleteInput::INVALID) |
| 37 return; | 37 return; |
| 38 | 38 |
| 39 if (!input.text().empty()) { | 39 if (!input.text().empty()) { |
| 40 std::string input_utf8 = UTF16ToUTF8(input.text()); | |
| 41 for (ExtensionApps::const_iterator app = extension_apps_.begin(); | 40 for (ExtensionApps::const_iterator app = extension_apps_.begin(); |
| 42 app != extension_apps_.end(); ++app) { | 41 app != extension_apps_.end(); ++app) { |
| 43 // See if the input matches this extension application. | 42 // See if the input matches this extension application. |
| 44 const std::string& name = app->first; | 43 const string16& name = app->first; |
| 45 const std::string& url = app->second; | 44 string16::const_iterator name_iter = std::search(name.begin(), name.end(), |
| 46 std::string::const_iterator name_iter = | 45 input.text().begin(), input.text().end(), |
| 47 std::search(name.begin(), | 46 base::CaseInsensitiveCompare<char16>()); |
| 48 name.end(), | |
| 49 input_utf8.begin(), | |
| 50 input_utf8.end(), | |
| 51 base::CaseInsensitiveCompare<char>()); | |
| 52 std::string::const_iterator url_iter = | |
| 53 std::search(url.begin(), | |
| 54 url.end(), | |
| 55 input_utf8.begin(), | |
| 56 input_utf8.end(), | |
| 57 base::CaseInsensitiveCompare<char>()); | |
| 58 | |
| 59 bool matches_name = name_iter != name.end(); | 47 bool matches_name = name_iter != name.end(); |
| 48 const string16& url = app->second; | |
| 49 string16::const_iterator url_iter = std::search(url.begin(), url.end(), | |
| 50 input.text().begin(), input.text().end(), | |
| 51 base::CaseInsensitiveCompare<char16>()); | |
| 60 bool matches_url = url_iter != url.end() && | 52 bool matches_url = url_iter != url.end() && |
| 61 input.type() != AutocompleteInput::FORCED_QUERY; | 53 input.type() != AutocompleteInput::FORCED_QUERY; |
| 54 | |
| 62 if (matches_name || matches_url) { | 55 if (matches_name || matches_url) { |
| 63 // We have a match, might be a partial match. | 56 // We have a match, might be a partial match. |
| 64 // TODO(finnur): Figure out what type to return here, might want to have | 57 // TODO(finnur): Figure out what type to return here, might want to have |
| 65 // the extension icon/a generic icon show up in the Omnibox. | 58 // the extension icon/a generic icon show up in the Omnibox. |
| 66 AutocompleteMatch match(this, 0, false, | 59 AutocompleteMatch match(this, 0, false, |
| 67 AutocompleteMatch::EXTENSION_APP); | 60 AutocompleteMatch::EXTENSION_APP); |
| 68 match.fill_into_edit = UTF8ToUTF16(url); | 61 match.fill_into_edit = url; |
| 69 match.destination_url = GURL(url); | 62 match.destination_url = GURL(url); |
| 70 match.inline_autocomplete_offset = string16::npos; | 63 match.inline_autocomplete_offset = string16::npos; |
| 71 match.contents = UTF8ToUTF16(name); | 64 match.contents = name; |
| 72 HighlightMatch(input, &match.contents_class, name_iter, name); | 65 AutocompleteMatch::ClassifyLocationInString( |
| 73 match.description = UTF8ToUTF16(url); | 66 matches_name ? (name_iter - name.begin()) : string16::npos, |
| 74 HighlightMatch(input, &match.description_class, url_iter, url); | 67 input.text().length(), name.length(), ACMatchClassification::NONE, |
| 68 &match.contents_class); | |
| 69 match.description = url; | |
| 70 AutocompleteMatch::ClassifyLocationInString( | |
| 71 matches_url ? (url_iter - url.begin()) : string16::npos, | |
| 72 input.text().length(), url.length(), ACMatchClassification::URL, | |
| 73 &match.description_class); | |
| 75 match.relevance = CalculateRelevance(input.type(), | 74 match.relevance = CalculateRelevance(input.type(), |
| 76 input.text().length(), | 75 input.text().length(), matches_name ? name.length() : url.length(), |
| 77 matches_name ? | 76 match.destination_url); |
| 78 name.length() : url.length(), | |
| 79 GURL(url)); | |
| 80 matches_.push_back(match); | 77 matches_.push_back(match); |
| 81 } | 78 } |
| 82 } | 79 } |
| 83 } | 80 } |
| 84 } | 81 } |
| 85 | 82 |
| 86 ExtensionAppProvider::~ExtensionAppProvider() { | 83 ExtensionAppProvider::~ExtensionAppProvider() { |
| 87 } | 84 } |
| 88 | 85 |
| 89 void ExtensionAppProvider::RefreshAppList() { | 86 void ExtensionAppProvider::RefreshAppList() { |
| 90 ExtensionService* extension_service = profile_->GetExtensionService(); | 87 ExtensionService* extension_service = profile_->GetExtensionService(); |
| 91 if (!extension_service) | 88 if (!extension_service) |
| 92 return; // During testing, there is no extension service. | 89 return; // During testing, there is no extension service. |
| 93 const ExtensionList* extensions = extension_service->extensions(); | 90 const ExtensionList* extensions = extension_service->extensions(); |
| 94 extension_apps_.clear(); | 91 extension_apps_.clear(); |
| 95 for (ExtensionList::const_iterator app = extensions->begin(); | 92 for (ExtensionList::const_iterator app = extensions->begin(); |
| 96 app != extensions->end(); ++app) { | 93 app != extensions->end(); ++app) { |
| 97 if ((*app)->is_app() && (*app)->GetFullLaunchURL().is_valid()) { | 94 if ((*app)->is_app() && (*app)->GetFullLaunchURL().is_valid()) { |
| 98 if (profile_->IsOffTheRecord() && | 95 if (profile_->IsOffTheRecord() && |
| 99 !extension_service->CanLoadInIncognito((*app))) | 96 !extension_service->CanLoadInIncognito((*app))) |
| 100 continue; | 97 continue; |
| 101 | 98 |
| 102 extension_apps_.push_back( | 99 extension_apps_.push_back( |
| 103 std::make_pair((*app)->name(), | 100 std::make_pair(UTF8ToUTF16((*app)->name()), |
| 104 (*app)->GetFullLaunchURL().spec())); | 101 UTF8ToUTF16((*app)->GetFullLaunchURL().spec()))); |
| 105 } | 102 } |
| 106 } | 103 } |
| 107 } | 104 } |
| 108 | 105 |
| 109 void ExtensionAppProvider::RegisterForNotifications() { | 106 void ExtensionAppProvider::RegisterForNotifications() { |
| 110 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 107 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
| 111 NotificationService::AllSources()); | 108 NotificationService::AllSources()); |
| 112 registrar_.Add(this, NotificationType::EXTENSION_UNINSTALLED, | 109 registrar_.Add(this, NotificationType::EXTENSION_UNINSTALLED, |
| 113 NotificationService::AllSources()); | 110 NotificationService::AllSources()); |
| 114 } | 111 } |
| 115 | 112 |
| 116 void ExtensionAppProvider::Observe(NotificationType type, | 113 void ExtensionAppProvider::Observe(NotificationType type, |
| 117 const NotificationSource& source, | 114 const NotificationSource& source, |
| 118 const NotificationDetails& details) { | 115 const NotificationDetails& details) { |
| 119 RefreshAppList(); | 116 RefreshAppList(); |
| 120 } | 117 } |
| 121 | 118 |
| 122 void ExtensionAppProvider::HighlightMatch(const AutocompleteInput& input, | |
| 123 ACMatchClassifications* match_class, | |
| 124 std::string::const_iterator iter, | |
| 125 const std::string& match_string) { | |
| 126 size_t pos = iter - match_string.begin(); | |
| 127 bool match_found = iter != match_string.end(); | |
| 128 if (!match_found || pos > 0) { | |
| 129 match_class->push_back( | |
| 130 ACMatchClassification(0, ACMatchClassification::DIM)); | |
| 131 } | |
| 132 if (match_found) { | |
| 133 match_class->push_back( | |
| 134 ACMatchClassification(pos, ACMatchClassification::MATCH)); | |
| 135 if (pos + input.text().length() < match_string.length()) { | |
| 136 match_class->push_back(ACMatchClassification(pos + input.text().length(), | |
| 137 ACMatchClassification::DIM)); | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type, | 119 int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type, |
| 143 int input_length, | 120 int input_length, |
| 144 int target_length, | 121 int target_length, |
| 145 const GURL& url) { | 122 const GURL& url) { |
| 146 // If you update the algorithm here, please remember to update the tables in | 123 // If you update the algorithm here, please remember to update the tables in |
| 147 // autocomplete.h also. | 124 // autocomplete.h also. |
| 148 const int kMaxRelevance = 1425; | 125 const int kMaxRelevance = 1425; |
| 149 | 126 |
| 150 if (input_length == target_length) | 127 if (input_length == target_length) |
| 151 return kMaxRelevance; | 128 return kMaxRelevance; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 168 history::URLRow info; | 145 history::URLRow info; |
| 169 url_db->GetRowForURL(url, &info); | 146 url_db->GetRowForURL(url, &info); |
| 170 type_count_boost = | 147 type_count_boost = |
| 171 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); | 148 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count()))); |
| 172 } | 149 } |
| 173 int relevance = 575 + static_cast<int>(type_count_boost) + | 150 int relevance = 575 + static_cast<int>(type_count_boost) + |
| 174 static_cast<int>(fraction_boost); | 151 static_cast<int>(fraction_boost); |
| 175 DCHECK_LE(relevance, kMaxRelevance); | 152 DCHECK_LE(relevance, kMaxRelevance); |
| 176 return relevance; | 153 return relevance; |
| 177 } | 154 } |
| OLD | NEW |