NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / app_list / search / app_search_provider.cc
blob085d1a3b4fcf3f905c6f101ba0cbeb5dd59d107d
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"
7 #include <string>
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;
26 namespace app_list {
28 class AppSearchProvider::App {
29 public:
30 explicit App(const extensions::Extension* extension)
31 : app_id_(extension->id()),
32 indexed_name_(base::UTF8ToUTF16(extension->name())) {
34 ~App() {}
36 const std::string& app_id() const { return app_id_; }
37 const TokenizedString& indexed_name() const { return indexed_name_; }
39 private:
40 const std::string app_id_;
41 TokenizedString indexed_name_;
43 DISALLOW_COPY_AND_ASSIGN(App);
46 AppSearchProvider::AppSearchProvider(
47 Profile* profile,
48 AppListControllerDelegate* list_controller)
49 : profile_(profile),
50 list_controller_(list_controller) {
51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
52 content::Source<Profile>(profile_->GetOriginalProfile()));
53 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
54 content::Source<Profile>(profile_->GetOriginalProfile()));
55 RefreshApps();
58 AppSearchProvider::~AppSearchProvider() {}
60 void AppSearchProvider::Start(const base::string16& query) {
61 const TokenizedString query_terms(query);
63 ClearResults();
65 TokenizedStringMatch match;
66 for (Apps::const_iterator app_it = apps_.begin();
67 app_it != apps_.end();
68 ++app_it) {
69 if (!match.Calculate(query_terms, (*app_it)->indexed_name()))
70 continue;
72 scoped_ptr<AppResult> result(
73 new AppResult(profile_, (*app_it)->app_id(), list_controller_));
74 result->UpdateFromMatch((*app_it)->indexed_name(), match);
75 Add(result.PassAs<ChromeSearchResult>());
79 void AppSearchProvider::Stop() {}
81 void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) {
82 for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
83 iter != extensions.end(); ++iter) {
84 const extensions::Extension* app = iter->get();
86 if (!app->ShouldDisplayInAppLauncher())
87 continue;
89 if (profile_->IsOffTheRecord() &&
90 !extensions::util::CanLoadInIncognito(app, profile_))
91 continue;
92 apps_.push_back(new App(app));
96 void AppSearchProvider::RefreshApps() {
97 apps_.clear();
98 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
99 AddApps(registry->enabled_extensions());
100 AddApps(registry->disabled_extensions());
101 AddApps(registry->terminated_extensions());
104 void AppSearchProvider::Observe(int type,
105 const content::NotificationSource& source,
106 const content::NotificationDetails& detaila) {
107 RefreshApps();
110 } // namespace app_list