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.
4
5#include "chrome/browser/autocomplete/extension_app_provider.h"
6
7#include <algorithm>
8#include <cmath>
9
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_system_factory.h"
15#include "chrome/browser/extensions/extension_util.h"
16#include "chrome/browser/history/history_service.h"
17#include "chrome/browser/history/history_service_factory.h"
18#include "chrome/browser/history/url_database.h"
19#include "chrome/browser/profiles/profile.h"
20#include "chrome/browser/ui/extensions/application_launch.h"
21#include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
22#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
23#include "content/public/browser/notification_source.h"
24#include "extensions/common/extension.h"
25#include "ui/base/l10n/l10n_util.h"
26
27ExtensionAppProvider::ExtensionAppProvider(
28    AutocompleteProviderListener* listener,
29    Profile* profile)
30    : AutocompleteProvider(listener, profile,
31          AutocompleteProvider::TYPE_EXTENSION_APP) {
32  // Notifications of extensions loading and unloading always come from the
33  // non-incognito profile, but we need to see them regardless, as the incognito
34  // windows can be affected.
35  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
36                 content::Source<Profile>(profile_->GetOriginalProfile()));
37  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
38                 content::Source<Profile>(profile_->GetOriginalProfile()));
39  RefreshAppList();
40}
41
42// static.
43void ExtensionAppProvider::LaunchAppFromOmnibox(
44    const AutocompleteMatch& match,
45    Profile* profile,
46    WindowOpenDisposition disposition) {
47  ExtensionService* service =
48      extensions::ExtensionSystemFactory::GetForProfile(profile)->
49      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.
55  if (!extension)
56    return;
57
58  CoreAppLauncherHandler::RecordAppLaunchType(
59      extension_misc::APP_LAUNCH_OMNIBOX_APP,
60      extension->GetType());
61
62  OpenApplication(AppLaunchParams(profile, extension, disposition));
63}
64
65void ExtensionAppProvider::AddExtensionAppForTesting(
66    const ExtensionApp& extension_app) {
67  extension_apps_.push_back(extension_app);
68}
69
70AutocompleteMatch 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);
92  }
93  match.relevance = CalculateRelevance(
94      input.type(),
95      input.text().length(),
96      name_match_index != base::string16::npos ?
97          app.name.length() : app.launch_url.length(),
98      match.destination_url);
99  return match;
100}
101
102void ExtensionAppProvider::Start(const AutocompleteInput& input,
103                                 bool minimal_changes) {
104  matches_.clear();
105
106  if ((input.type() == AutocompleteInput::INVALID) ||
107      (input.type() == AutocompleteInput::FORCED_QUERY))
108    return;
109
110  if (input.text().empty())
111    return;
112
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<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;
124
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<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;
137    }
138
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));
143    }
144  }
145}
146
147ExtensionAppProvider::~ExtensionAppProvider() {
148}
149
150void ExtensionAppProvider::RefreshAppList() {
151  ExtensionService* extension_service =
152      extensions::ExtensionSystemFactory::GetForProfile(profile_)->
153      extension_service();
154  if (!extension_service)
155    return;  // During testing, there is no extension service.
156  const ExtensionSet* extensions = extension_service->extensions();
157  extension_apps_.clear();
158  for (ExtensionSet::const_iterator iter = extensions->begin();
159       iter != extensions->end(); ++iter) {
160    const extensions::Extension* app = iter->get();
161    if (!app->ShouldDisplayInAppLauncher())
162      continue;
163    // Note: Apps that appear in the NTP only are not added here since this
164    // provider is currently only used in the app launcher.
165
166    if (profile_->IsOffTheRecord() &&
167        !extension_util::CanLoadInIncognito(app, extension_service))
168      continue;
169
170    GURL launch_url = app->is_platform_app() ?
171        app->url() : extensions::AppLaunchInfo::GetFullLaunchURL(app);
172    DCHECK(launch_url.is_valid());
173
174    ExtensionApp extension_app = {
175        UTF8ToUTF16(app->name()),
176        UTF8ToUTF16(launch_url.spec()),
177        // Only hosted apps have recognizable URLs that users might type in,
178        // packaged apps and hosted apps use chrome-extension:// URLs that are
179        // normally not shown to users.
180        app->is_hosted_app()
181    };
182    extension_apps_.push_back(extension_app);
183  }
184}
185
186void ExtensionAppProvider::Observe(int type,
187                                   const content::NotificationSource& source,
188                                   const content::NotificationDetails& details) {
189  RefreshAppList();
190}
191
192int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type,
193                                             int input_length,
194                                             int target_length,
195                                             const GURL& url) {
196  // If you update the algorithm here, please remember to update the tables in
197  // autocomplete.h also.
198  const int kMaxRelevance = 1425;
199
200  if (input_length == target_length)
201    return kMaxRelevance;
202
203  // We give a boost proportionally based on how much of the input matches the
204  // app name, up to a maximum close to 200 (we can be close to, but we'll never
205  // reach 200 because the 100% match is taken care of above).
206  double fraction_boost = static_cast<double>(200) *
207                          input_length / target_length;
208
209  // We also give a boost relative to how often the user has previously typed
210  // the Extension App URL/selected the Extension App suggestion from this
211  // provider (boost is between 200-400).
212  double type_count_boost = 0;
213  HistoryService* const history_service =
214      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
215  history::URLDatabase* url_db = history_service ?
216      history_service->InMemoryDatabase() : NULL;
217  if (url_db) {
218    history::URLRow info;
219    url_db->GetRowForURL(url, &info);
220    type_count_boost =
221        400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count())));
222  }
223  int relevance = 575 + static_cast<int>(type_count_boost) +
224                        static_cast<int>(fraction_boost);
225  DCHECK_LE(relevance, kMaxRelevance);
226  return relevance;
227}
228