- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / extension_app_provider.cc
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/extension.h"
23 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
24 #include "content/public/browser/notification_source.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 ExtensionAppProvider::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.
43 void 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
65 void ExtensionAppProvider::AddExtensionAppForTesting(
66     const ExtensionApp& extension_app) {
67   extension_apps_.push_back(extension_app);
68 }
69
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);
92   }
93   match.relevance = CalculateRelevance(
94       input.type(),
95       input.text().length(),
96       name_match_index != string16::npos ?
97           app.name.length() : app.launch_url.length(),
98       match.destination_url);
99   return match;
100 }
101
102 void 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 string16& name = app->name;
117     string16::const_iterator name_iter = std::search(name.begin(), name.end(),
118         input.text().begin(), input.text().end(),
119         base::CaseInsensitiveCompare<char16>());
120     bool matches_name = name_iter != name.end();
121     size_t name_match_index = matches_name ?
122         static_cast<size_t>(name_iter - name.begin()) : string16::npos;
123
124     bool matches_url = false;
125     size_t url_match_index = string16::npos;
126     if (app->should_match_against_launch_url) {
127       const string16& url = app->launch_url;
128       string16::const_iterator url_iter = std::search(url.begin(), url.end(),
129           input.text().begin(), input.text().end(),
130           base::CaseInsensitiveCompare<char16>());
131       matches_url = url_iter != url.end() &&
132           input.type() != AutocompleteInput::FORCED_QUERY;
133       url_match_index = matches_url ?
134           static_cast<size_t>(url_iter - url.begin()) : string16::npos;
135     }
136
137     if (matches_name || matches_url) {
138       // We have a match, might be a partial match.
139       matches_.push_back(CreateAutocompleteMatch(
140           input, *app, name_match_index, url_match_index));
141     }
142   }
143 }
144
145 ExtensionAppProvider::~ExtensionAppProvider() {
146 }
147
148 void ExtensionAppProvider::RefreshAppList() {
149   ExtensionService* extension_service =
150       extensions::ExtensionSystemFactory::GetForProfile(profile_)->
151       extension_service();
152   if (!extension_service)
153     return;  // During testing, there is no extension service.
154   const ExtensionSet* extensions = extension_service->extensions();
155   extension_apps_.clear();
156   for (ExtensionSet::const_iterator iter = extensions->begin();
157        iter != extensions->end(); ++iter) {
158     const extensions::Extension* app = iter->get();
159     if (!app->ShouldDisplayInAppLauncher())
160       continue;
161     // Note: Apps that appear in the NTP only are not added here since this
162     // provider is currently only used in the app launcher.
163
164     if (profile_->IsOffTheRecord() &&
165         !extension_util::CanLoadInIncognito(app, extension_service))
166       continue;
167
168     GURL launch_url = app->is_platform_app() ?
169         app->url() : extensions::AppLaunchInfo::GetFullLaunchURL(app);
170     DCHECK(launch_url.is_valid());
171
172     ExtensionApp extension_app = {
173         UTF8ToUTF16(app->name()),
174         UTF8ToUTF16(launch_url.spec()),
175         // Only hosted apps have recognizable URLs that users might type in,
176         // packaged apps and hosted apps use chrome-extension:// URLs that are
177         // normally not shown to users.
178         app->is_hosted_app()
179     };
180     extension_apps_.push_back(extension_app);
181   }
182 }
183
184 void ExtensionAppProvider::Observe(int type,
185                                    const content::NotificationSource& source,
186                                    const content::NotificationDetails& details) {
187   RefreshAppList();
188 }
189
190 int ExtensionAppProvider::CalculateRelevance(AutocompleteInput::Type type,
191                                              int input_length,
192                                              int target_length,
193                                              const GURL& url) {
194   // If you update the algorithm here, please remember to update the tables in
195   // autocomplete.h also.
196   const int kMaxRelevance = 1425;
197
198   if (input_length == target_length)
199     return kMaxRelevance;
200
201   // We give a boost proportionally based on how much of the input matches the
202   // app name, up to a maximum close to 200 (we can be close to, but we'll never
203   // reach 200 because the 100% match is taken care of above).
204   double fraction_boost = static_cast<double>(200) *
205                           input_length / target_length;
206
207   // We also give a boost relative to how often the user has previously typed
208   // the Extension App URL/selected the Extension App suggestion from this
209   // provider (boost is between 200-400).
210   double type_count_boost = 0;
211   HistoryService* const history_service =
212       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
213   history::URLDatabase* url_db = history_service ?
214       history_service->InMemoryDatabase() : NULL;
215   if (url_db) {
216     history::URLRow info;
217     url_db->GetRowForURL(url, &info);
218     type_count_boost =
219         400 * (1.0 - (std::pow(static_cast<double>(2), -info.typed_count())));
220   }
221   int relevance = 575 + static_cast<int>(type_count_boost) +
222                         static_cast<int>(fraction_boost);
223   DCHECK_LE(relevance, kMaxRelevance);
224   return relevance;
225 }