1 // Copyright 2013 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/ui/app_list/search/app_search_provider.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/app_list/search/app_result.h"
15 #include "chrome/browser/ui/app_list/search/tokenized_string.h"
16 #include "chrome/browser/ui/app_list/search/tokenized_string_match.h"
17 #include "content/public/browser/notification_details.h"
18 #include "content/public/browser/notification_source.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_set.h"
24 using extensions::ExtensionRegistry;
28 class AppSearchProvider::App {
30 explicit App(const extensions::Extension* extension)
31 : app_id_(extension->id()),
32 indexed_name_(base::UTF8ToUTF16(extension->name())) {
36 const std::string& app_id() const { return app_id_; }
37 const TokenizedString& indexed_name() const { return indexed_name_; }
40 const std::string app_id_;
41 TokenizedString indexed_name_;
43 DISALLOW_COPY_AND_ASSIGN(App);
46 AppSearchProvider::AppSearchProvider(
48 AppListControllerDelegate* list_controller)
50 list_controller_(list_controller) {
52 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
53 content::Source<Profile>(profile_->GetOriginalProfile()));
54 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
55 content::Source<Profile>(profile_->GetOriginalProfile()));
59 AppSearchProvider::~AppSearchProvider() {}
61 void AppSearchProvider::Start(const base::string16& query) {
62 const TokenizedString query_terms(query);
66 TokenizedStringMatch match;
67 for (Apps::const_iterator app_it = apps_.begin();
68 app_it != apps_.end();
70 if (!match.Calculate(query_terms, (*app_it)->indexed_name()))
73 scoped_ptr<AppResult> result(
74 new AppResult(profile_, (*app_it)->app_id(), list_controller_));
75 result->UpdateFromMatch((*app_it)->indexed_name(), match);
76 Add(result.PassAs<ChromeSearchResult>());
80 void AppSearchProvider::Stop() {}
82 void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) {
83 for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
84 iter != extensions.end(); ++iter) {
85 const extensions::Extension* app = iter->get();
87 if (!app->ShouldDisplayInAppLauncher())
90 if (profile_->IsOffTheRecord() &&
91 !extensions::util::CanLoadInIncognito(app, profile_))
93 apps_.push_back(new App(app));
97 void AppSearchProvider::RefreshApps() {
99 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
100 AddApps(registry->enabled_extensions());
101 AddApps(registry->disabled_extensions());
102 AddApps(registry->terminated_extensions());
105 void AppSearchProvider::Observe(int type,
106 const content::NotificationSource& source,
107 const content::NotificationDetails& detaila) {
111 } // namespace app_list