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_system.h"
13 #include "chrome/browser/extensions/extension_util.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/app_list/search/app_result.h"
16 #include "chrome/browser/ui/app_list/search/tokenized_string.h"
17 #include "chrome/browser/ui/app_list/search/tokenized_string_match.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h"
20 #include "extensions/browser/extension_registry.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
) {
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()));
58 AppSearchProvider::~AppSearchProvider() {}
60 void AppSearchProvider::Start(const base::string16
& query
) {
61 const TokenizedString
query_terms(query
);
65 TokenizedStringMatch match
;
66 for (Apps::const_iterator app_it
= apps_
.begin();
67 app_it
!= apps_
.end();
69 if (!match
.Calculate(query_terms
, (*app_it
)->indexed_name()))
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 ExtensionService
* service
) {
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 !extension_util::CanLoadInIncognito(app
, service
))
93 apps_
.push_back(new App(app
));
97 void AppSearchProvider::RefreshApps() {
98 ExtensionService
* extension_service
=
99 extensions::ExtensionSystem::Get(profile_
)->extension_service();
100 if (!extension_service
)
101 return; // During testing, there is no extension service.
105 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile_
);
106 AddApps(registry
->enabled_extensions(), extension_service
);
107 AddApps(registry
->disabled_extensions(), extension_service
);
108 AddApps(registry
->terminated_extensions(), extension_service
);
111 void AppSearchProvider::Observe(int type
,
112 const content::NotificationSource
& source
,
113 const content::NotificationDetails
& detaila
) {
117 } // namespace app_list