Chromium Code Reviews| Index: chrome/browser/autocomplete/extension_app_provider.cc |
| =================================================================== |
| --- chrome/browser/autocomplete/extension_app_provider.cc (revision 0) |
| +++ chrome/browser/autocomplete/extension_app_provider.cc (revision 0) |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/autocomplete/extension_app_provider.h" |
| + |
| +#include <algorithm> |
| + |
| +#include "base/string16.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/autocomplete/autocomplete_match.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "content/common/notification_service.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +ExtensionAppProvider::ExtensionAppProvider(ACProviderListener* listener, |
| + Profile* profile) |
| + : AutocompleteProvider(listener, profile, "ExtensionApps") { |
| + RegisterForNotifications(); |
| + RefreshAppList(); |
| +} |
| + |
| +void ExtensionAppProvider::Start(const AutocompleteInput& input, |
| + bool minimal_changes) { |
| + matches_.clear(); |
| + |
| + if (input.type() == AutocompleteInput::INVALID || |
| + input.type() == AutocompleteInput::FORCED_QUERY) |
|
Peter Kasting
2011/04/04 19:35:32
You should probably handle FORCED_QUERY but maybe
Finnur
2011/04/05 15:50:24
Sound good. Done.
On 2011/04/04 19:35:32, Peter K
|
| + return; |
| + |
| + if (!input.text().empty()) { |
| + std::string input_utf8 = UTF16ToUTF8(input.text()); |
| + for (ExtensionApps::const_iterator app = extension_apps_.begin(); |
| + app != extension_apps_.end(); ++app) { |
| + // See if the input matches this extension application. |
| + const std::string& name = app->first; |
| + const std::string& url = app->second; |
| + std::string::const_iterator name_iter = |
| + std::search(name.begin(), |
| + name.end(), |
| + input_utf8.begin(), |
| + input_utf8.end(), |
| + base::CaseInsensitiveCompare<char>()); |
| + std::string::const_iterator url_iter = |
| + std::search(url.begin(), |
| + url.end(), |
| + input_utf8.begin(), |
| + input_utf8.end(), |
| + base::CaseInsensitiveCompare<char>()); |
| + |
| + bool matches_name = name_iter != name.end(); |
| + bool matches_url = url_iter != url.end(); |
| + if (matches_name || matches_url) { |
| + // We have a match, might be a partial match. |
| + // TODO(finnur): Figure out what type to return here, might want to have |
| + // the extension icon/a generic icon show up in the Omnibox. |
| + AutocompleteMatch match(this, 0, false, AutocompleteMatch::HISTORY_URL); |
| + match.fill_into_edit = UTF8ToUTF16(url); |
| + match.destination_url = GURL(url); |
| + match.inline_autocomplete_offset = string16::npos; |
| + match.contents = UTF8ToUTF16(name); |
| + |
| + size_t pos = name_iter - name.begin(); |
|
Peter Kasting
2011/04/04 19:35:32
Nit: This block of code is basically repeated verb
Finnur
2011/04/05 15:50:24
Good idea. Done.
On 2011/04/04 19:35:32, Peter Ka
|
| + if (!matches_name || pos > 0) { |
| + match.contents_class.push_back( |
| + ACMatchClassification(0, ACMatchClassification::DIM)); |
| + } |
| + if (matches_name) { |
| + match.contents_class.push_back( |
| + ACMatchClassification(pos, ACMatchClassification::MATCH)); |
| + if (pos + input.text().length() < name.length()) { |
| + match.contents_class.push_back( |
| + ACMatchClassification(pos + input.text().length(), |
| + ACMatchClassification::DIM)); |
| + } |
| + } |
| + |
| + match.description = UTF8ToUTF16(url); |
| + pos = url_iter - url.begin(); |
| + if (!matches_url || pos > 0) { |
| + match.description_class.push_back( |
| + ACMatchClassification(0, ACMatchClassification::DIM)); |
| + } |
| + if (matches_url) { |
| + match.description_class.push_back( |
| + ACMatchClassification(pos, ACMatchClassification::MATCH)); |
| + if (pos + input.text().length() < url.length()) { |
| + match.description_class.push_back( |
| + ACMatchClassification(pos + input.text().length(), |
| + ACMatchClassification::DIM)); |
| + } |
| + } |
| + |
| + match.relevance = CalculateRelevance(input.type(), |
| + input.text().length(), |
| + matches_name ? |
| + name.length() : url.length()); |
| + matches_.push_back(match); |
| + } |
| + } |
| + } |
| +} |
| + |
| +ExtensionAppProvider::~ExtensionAppProvider() { |
| +} |
| + |
| +void ExtensionAppProvider::RefreshAppList() { |
| + ExtensionService* extension_service = profile_->GetExtensionService(); |
| + if (!extension_service) |
| + return; // During testing, there is no extension service. |
| + const ExtensionList* extensions = extension_service->extensions(); |
| + extension_apps_.clear(); |
| + for (ExtensionList::const_iterator app = extensions->begin(); |
| + app != extensions->end(); ++app) { |
| + if ((*app)->is_app() && !(*app)->launch_web_url().empty()) { |
| + extension_apps_.push_back(std::make_pair((*app)->name(), |
| + (*app)->launch_web_url())); |
| + } |
| + } |
| +} |
| + |
| +void ExtensionAppProvider::RegisterForNotifications() { |
| + registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
| + NotificationService::AllSources()); |
| + registrar_.Add(this, NotificationType::EXTENSION_UNINSTALLED, |
| + NotificationService::AllSources()); |
| +} |
| + |
| +void ExtensionAppProvider::Observe(NotificationType type, |
| + const NotificationSource& source, |
| + const NotificationDetails& details) { |
| + switch (type.value) { |
|
Peter Kasting
2011/04/04 19:35:32
Nit: I don't think all this is useful. Just make
Finnur
2011/04/05 15:50:24
Done.
|
| + case NotificationType::EXTENSION_LOADED: |
| + case NotificationType::EXTENSION_UNINSTALLED: |
| + RefreshAppList(); |
| + break; |
| + default: |
| + NOTREACHED() << "Unhandled notification: " << type.value; |
| + break; |
| + } |
| +} |
| + |
| +int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type, |
| + int input_length, |
| + int target_length) { |
| + // If you update the algorithm here, please remember to update the tables in |
| + // autocomplete.h also. |
| + if (input_length == target_length) |
| + return 1425; |
| + |
| + // Sliding scale from 700+100 to 1175 based on how many characters you typed. |
|
Peter Kasting
2011/04/04 19:35:32
I suggest scaling from 400 up instead of 800, and
Finnur
2011/04/05 15:50:24
See? I knew there was a guideline hidden in there
|
| + return std::min(1175, 700 + (input_length * 75)); |
| +} |
| Property changes on: chrome\browser\autocomplete\extension_app_provider.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |