Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: chrome/browser/ui/app_list/search/app_search_provider.cc

Issue 379333005: Allow AppSearchProvider to provide recommendations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/app_list/search/app_search_provider.h" 5 #include "chrome/browser/ui/app_list/search/app_search_provider.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/extension_service.h" 10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_ui_util.h" 11 #include "chrome/browser/extensions/extension_ui_util.h"
12 #include "chrome/browser/extensions/extension_util.h" 12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/app_list/search/app_result.h" 14 #include "chrome/browser/ui/app_list/search/app_result.h"
15 #include "chrome/browser/ui/app_list/search/tokenized_string.h" 15 #include "chrome/browser/ui/app_list/search/tokenized_string.h"
16 #include "chrome/browser/ui/app_list/search/tokenized_string_match.h" 16 #include "chrome/browser/ui/app_list/search/tokenized_string_match.h"
17 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_system.h" 19 #include "extensions/browser/extension_system.h"
19 #include "extensions/common/extension.h" 20 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_set.h" 21 #include "extensions/common/extension_set.h"
21 22
22 using extensions::ExtensionRegistry; 23 using extensions::ExtensionRegistry;
23 24
24 namespace app_list { 25 namespace app_list {
25 26
26 class AppSearchProvider::App { 27 class AppSearchProvider::App {
27 public: 28 public:
28 explicit App(const extensions::Extension* extension) 29 explicit App(const extensions::Extension* extension,
30 const base::Time& last_launch_time)
29 : app_id_(extension->id()), 31 : app_id_(extension->id()),
30 indexed_name_(base::UTF8ToUTF16(extension->name())) { 32 indexed_name_(base::UTF8ToUTF16(extension->name())),
31 } 33 last_launch_time_(last_launch_time) {}
32 ~App() {} 34 ~App() {}
33 35
34 const std::string& app_id() const { return app_id_; } 36 const std::string& app_id() const { return app_id_; }
35 const TokenizedString& indexed_name() const { return indexed_name_; } 37 const TokenizedString& indexed_name() const { return indexed_name_; }
38 const base::Time& last_launch_time() const { return last_launch_time_; }
39
40 static bool AppLaunchedMoreRecent(App* const app1, App* const app2) {
41 return app1->last_launch_time() > app2->last_launch_time();
42 }
36 43
37 private: 44 private:
38 const std::string app_id_; 45 const std::string app_id_;
39 TokenizedString indexed_name_; 46 TokenizedString indexed_name_;
47 base::Time last_launch_time_;
40 48
41 DISALLOW_COPY_AND_ASSIGN(App); 49 DISALLOW_COPY_AND_ASSIGN(App);
42 }; 50 };
43 51
44 AppSearchProvider::AppSearchProvider(Profile* profile, 52 AppSearchProvider::AppSearchProvider(Profile* profile,
45 AppListControllerDelegate* list_controller) 53 AppListControllerDelegate* list_controller)
46 : profile_(profile), 54 : profile_(profile),
47 list_controller_(list_controller), 55 list_controller_(list_controller),
48 extension_registry_observer_(this) { 56 extension_registry_observer_(this) {
49 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); 57 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
50 RefreshApps(); 58 RefreshApps();
51 } 59 }
52 60
53 AppSearchProvider::~AppSearchProvider() {} 61 AppSearchProvider::~AppSearchProvider() {}
54 62
63 void AppSearchProvider::FetchRecommendations() {
64 ClearResults();
65
66 // Reload apps to ensure we have the latest launch time information.
Matt Giuca 2014/07/22 04:31:07 nit: Don't use the word "reload"; it implies you a
calamity 2014/07/22 07:24:04 Done.
67 RefreshApps();
68
69 for (Apps::const_iterator app_it = apps_.begin(); app_it != apps_.end();
70 ++app_it) {
71 scoped_ptr<AppResult> result(
72 new AppResult(profile_, (*app_it)->app_id(), list_controller_));
73 result->set_title((*app_it)->indexed_name().text());
74 result->UpdateFromLastLaunched((*app_it)->last_launch_time());
75 Add(result.PassAs<SearchResult>());
76 }
77 }
78
55 void AppSearchProvider::Start(const base::string16& query) { 79 void AppSearchProvider::Start(const base::string16& query) {
56 const TokenizedString query_terms(query); 80 const TokenizedString query_terms(query);
57 81
58 ClearResults(); 82 ClearResults();
59 83
60 TokenizedStringMatch match; 84 TokenizedStringMatch match;
61 for (Apps::const_iterator app_it = apps_.begin(); 85 for (Apps::const_iterator app_it = apps_.begin();
62 app_it != apps_.end(); 86 app_it != apps_.end();
63 ++app_it) { 87 ++app_it) {
64 if (!match.Calculate(query_terms, (*app_it)->indexed_name())) 88 if (!match.Calculate(query_terms, (*app_it)->indexed_name()))
65 continue; 89 continue;
66 90
67 scoped_ptr<AppResult> result( 91 scoped_ptr<AppResult> result(
68 new AppResult(profile_, (*app_it)->app_id(), list_controller_)); 92 new AppResult(profile_, (*app_it)->app_id(), list_controller_));
69 result->UpdateFromMatch((*app_it)->indexed_name(), match); 93 result->UpdateFromMatch((*app_it)->indexed_name(), match);
70 Add(result.PassAs<SearchResult>()); 94 Add(result.PassAs<SearchResult>());
71 } 95 }
72 } 96 }
73 97
74 void AppSearchProvider::Stop() {} 98 void AppSearchProvider::Stop() {}
75 99
76 void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) { 100 void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) {
101 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_);
77 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); 102 for (extensions::ExtensionSet::const_iterator iter = extensions.begin();
78 iter != extensions.end(); ++iter) { 103 iter != extensions.end(); ++iter) {
79 const extensions::Extension* app = iter->get(); 104 const extensions::Extension* app = iter->get();
80 105
81 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app, profile_)) 106 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app, profile_))
82 continue; 107 continue;
83 108
84 if (profile_->IsOffTheRecord() && 109 if (profile_->IsOffTheRecord() &&
85 !extensions::util::CanLoadInIncognito(app, profile_)) 110 !extensions::util::CanLoadInIncognito(app, profile_))
86 continue; 111 continue;
87 apps_.push_back(new App(app)); 112
113 apps_.push_back(new App(app, prefs->GetLastLaunchTime(app->id())));
88 } 114 }
89 } 115 }
90 116
91 void AppSearchProvider::RefreshApps() { 117 void AppSearchProvider::RefreshApps() {
92 apps_.clear(); 118 apps_.clear();
93 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); 119 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
94 AddApps(registry->enabled_extensions()); 120 AddApps(registry->enabled_extensions());
95 AddApps(registry->disabled_extensions()); 121 AddApps(registry->disabled_extensions());
96 AddApps(registry->terminated_extensions()); 122 AddApps(registry->terminated_extensions());
123 std::sort(apps_.begin(), apps_.end(), &App::AppLaunchedMoreRecent);
97 } 124 }
98 125
99 void AppSearchProvider::OnExtensionLoaded( 126 void AppSearchProvider::OnExtensionLoaded(
100 content::BrowserContext* browser_context, 127 content::BrowserContext* browser_context,
101 const extensions::Extension* extension) { 128 const extensions::Extension* extension) {
102 RefreshApps(); 129 RefreshApps();
103 } 130 }
104 131
105 void AppSearchProvider::OnExtensionUninstalled( 132 void AppSearchProvider::OnExtensionUninstalled(
106 content::BrowserContext* browser_context, 133 content::BrowserContext* browser_context,
107 const extensions::Extension* extension) { 134 const extensions::Extension* extension) {
108 RefreshApps(); 135 RefreshApps();
109 } 136 }
110 137
111 } // namespace app_list 138 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698