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
3 // found in the LICENSE file.
5 #include "chrome/browser/autocomplete/extension_app_provider.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/history/history_service.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/history/url_database.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/extensions/application_launch.h"
20 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
21 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
22 #include "content/public/browser/notification_source.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/common/extension.h"
25 #include "extensions/common/extension_set.h"
26 #include "ui/base/l10n/l10n_util.h"
28 ExtensionAppProvider::ExtensionAppProvider(
29 AutocompleteProviderListener* listener,
31 : AutocompleteProvider(listener, profile,
32 AutocompleteProvider::TYPE_EXTENSION_APP) {
33 // Notifications of extensions loading and unloading always come from the
34 // non-incognito profile, but we need to see them regardless, as the incognito
35 // windows can be affected.
36 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
37 content::Source<Profile>(profile_->GetOriginalProfile()));
38 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
39 content::Source<Profile>(profile_->GetOriginalProfile()));
44 void ExtensionAppProvider::LaunchAppFromOmnibox(
45 const AutocompleteMatch& match,
47 WindowOpenDisposition disposition) {
48 ExtensionService* service =
49 extensions::ExtensionSystem::Get(profile)->extension_service();
50 const extensions::Extension* extension =
51 service->GetInstalledApp(match.destination_url);
52 // While the Omnibox popup is open, the extension can be updated, changing
53 // its URL and leaving us with no extension being found. In this case, we
54 // ignore the request.
58 CoreAppLauncherHandler::RecordAppLaunchType(
59 extension_misc::APP_LAUNCH_OMNIBOX_APP,
60 extension->GetType());
62 OpenApplication(AppLaunchParams(profile, extension, disposition));
65 void ExtensionAppProvider::AddExtensionAppForTesting(
66 const ExtensionApp& extension_app) {
67 extension_apps_.push_back(extension_app);
70 AutocompleteMatch ExtensionAppProvider::CreateAutocompleteMatch(
71 const AutocompleteInput& input,
72 const ExtensionApp& app,
73 size_t name_match_index,
74 size_t url_match_index) {
75 // TODO(finnur): Figure out what type to return here, might want to have
76 // the extension icon/a generic icon show up in the Omnibox.
77 AutocompleteMatch match(this, 0, false,
78 AutocompleteMatchType::EXTENSION_APP);
79 match.fill_into_edit =
80 app.should_match_against_launch_url ? app.launch_url : input.text();
81 match.destination_url = GURL(app.launch_url);
82 match.allowed_to_be_default_match = true;
83 match.contents = AutocompleteMatch::SanitizeString(app.name);
84 AutocompleteMatch::ClassifyLocationInString(name_match_index,
85 input.text().length(), app.name.length(), ACMatchClassification::NONE,
86 &match.contents_class);
87 if (app.should_match_against_launch_url) {
88 match.description = app.launch_url;
89 AutocompleteMatch::ClassifyLocationInString(url_match_index,
90 input.text().length(), app.launch_url.length(),
91 ACMatchClassification::URL, &match.description_class);
93 match.relevance = CalculateRelevance(
95 input.text().length(),
96 name_match_index != base::string16::npos ?
97 app.name.length() : app.launch_url.length(),
98 match.destination_url);
102 void ExtensionAppProvider::Start(const AutocompleteInput& input,
103 bool minimal_changes) {
106 if ((input.type() == AutocompleteInput::INVALID) ||
107 (input.type() == AutocompleteInput::FORCED_QUERY))
110 if (input.text().empty())
113 for (ExtensionApps::const_iterator app = extension_apps_.begin();
114 app != extension_apps_.end(); ++app) {
115 // See if the input matches this extension application.
116 const base::string16& name = app->name;
117 base::string16::const_iterator name_iter =
118 std::search(name.begin(), name.end(),
119 input.text().begin(), input.text().end(),
120 base::CaseInsensitiveCompare<base::char16>());
121 bool matches_name = name_iter != name.end();
122 size_t name_match_index = matches_name ?
123 static_cast<size_t>(name_iter - name.begin()) : base::string16::npos;
125 bool matches_url = false;
126 size_t url_match_index = base::string16::npos;
127 if (app->should_match_against_launch_url) {
128 const base::string16& url = app->launch_url;
129 base::string16::const_iterator url_iter =
130 std::search(url.begin(), url.end(),
131 input.text().begin(), input.text().end(),
132 base::CaseInsensitiveCompare<base::char16>());
133 matches_url = url_iter != url.end() &&
134 input.type() != AutocompleteInput::FORCED_QUERY;
135 url_match_index = matches_url ?
136 static_cast<size_t>(url_iter - url.begin()) : base::string16::npos;
139 if (matches_name || matches_url) {
140 // We have a match, might be a partial match.
141 matches_.push_back(CreateAutocompleteMatch(
142 input, *app, name_match_index, url_match_index));
147 ExtensionAppProvider::~ExtensionAppProvider() {
150 void ExtensionAppProvider::RefreshAppList() {
151 ExtensionService* extension_service =
152 extensions::ExtensionSystem::Get(profile_)->extension_service();
153 if (!extension_service)
154 return; // During testing, there is no extension service.
155 const extensions::ExtensionSet* extensions = extension_service->extensions();
156 extension_apps_.clear();
157 for (extensions::ExtensionSet::const_iterator iter = extensions->begin();
158 iter != extensions->end(); ++iter) {
159 const extensions::Extension* app = iter->get();
160 if (!app->ShouldDisplayInAppLauncher())
162 // Note: Apps that appear in the NTP only are not added here since this
163 // provider is currently only used in the app launcher.
165 if (profile_->IsOffTheRecord() &&
166 !extensions::util::CanLoadInIncognito(app, profile_))
169 GURL launch_url = app->is_platform_app() ?
170 app->url() : extensions::AppLaunchInfo::GetFullLaunchURL(app);
171 DCHECK(launch_url.is_valid());
173 ExtensionApp extension_app = {
174 base::UTF8ToUTF16(app->name()),
175 base::UTF8ToUTF16(launch_url.spec()),
176 // Only hosted apps have recognizable URLs that users might type in,
177 // packaged apps and hosted apps use chrome-extension:// URLs that are
178 // normally not shown to users.
181 extension_apps_.push_back(extension_app);
185 void ExtensionAppProvider::Observe(int type,
186 const content::NotificationSource& source,
187 const content::NotificationDetails& details) {
191 int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type,
195 // If you update the algorithm here, please remember to update the tables in
196 // autocomplete.h also.
197 const int kMaxRelevance = 1425;
199 if (input_length == target_length)
200 return kMaxRelevance;
202 // We give a boost proportionally based on how much of the input matches the
203 // app name, up to a maximum close to 200 (we can be close to, but we'll never
204 // reach 200 because the 100% match is taken care of above).
205 double fraction_boost = static_cast<double>(200) *
206 input_length / target_length;
208 // We also give a boost relative to how often the user has previously typed
209 // the Extension App URL/selected the Extension App suggestion from this
210 // provider (boost is between 200-400).
211 double type_count_boost = 0;
212 HistoryService* const history_service =
213 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
214 history::URLDatabase* url_db = history_service ?
215 history_service->InMemoryDatabase() : NULL;
217 history::URLRow info;
218 url_db->GetRowForURL(url, &info);
220 400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count())));
222 int relevance = 575 + static_cast<int>(type_count_boost) +
223 static_cast<int>(fraction_boost);
224 DCHECK_LE(relevance, kMaxRelevance);