Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / app_search_provider.cc
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.
4
5 #include "chrome/browser/ui/app_list/search/app_search_provider.h"
6
7 #include <string>
8
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"
23
24 using extensions::ExtensionRegistry;
25
26 namespace app_list {
27
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())) {
33   }
34   ~App() {}
35
36   const std::string& app_id() const { return app_id_; }
37   const TokenizedString& indexed_name() const { return indexed_name_; }
38
39  private:
40   const std::string app_id_;
41   TokenizedString indexed_name_;
42
43   DISALLOW_COPY_AND_ASSIGN(App);
44 };
45
46 AppSearchProvider::AppSearchProvider(
47     Profile* profile,
48     AppListControllerDelegate* list_controller)
49     : profile_(profile),
50       list_controller_(list_controller) {
51   registrar_.Add(this,
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()));
56   RefreshApps();
57 }
58
59 AppSearchProvider::~AppSearchProvider() {}
60
61 void AppSearchProvider::Start(const base::string16& query) {
62   const TokenizedString query_terms(query);
63
64   ClearResults();
65
66   TokenizedStringMatch match;
67   for (Apps::const_iterator app_it = apps_.begin();
68        app_it != apps_.end();
69        ++app_it) {
70     if (!match.Calculate(query_terms, (*app_it)->indexed_name()))
71       continue;
72
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>());
77   }
78 }
79
80 void AppSearchProvider::Stop() {}
81
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();
86
87     if (!app->ShouldDisplayInAppLauncher())
88       continue;
89
90     if (profile_->IsOffTheRecord() &&
91         !extensions::util::CanLoadInIncognito(app, profile_))
92       continue;
93     apps_.push_back(new App(app));
94   }
95 }
96
97 void AppSearchProvider::RefreshApps() {
98   apps_.clear();
99   ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
100   AddApps(registry->enabled_extensions());
101   AddApps(registry->disabled_extensions());
102   AddApps(registry->terminated_extensions());
103 }
104
105 void AppSearchProvider::Observe(int type,
106                                 const content::NotificationSource& source,
107                                 const content::NotificationDetails& detaila) {
108   RefreshApps();
109 }
110
111 }  // namespace app_list