Chromium Code Reviews| OLD | NEW |
|---|---|
| 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) {} |
|
Matt Giuca
2014/07/23 01:53:18
nit: Is this ClangFormatted? I believe you should
calamity
2014/07/28 02:55:26
Yeah, git cl format wants this apparently.
Matt Giuca
2014/07/28 02:59:35
Acknowledged.
| |
| 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) { | |
|
Matt Giuca
2014/07/23 01:53:18
"App* const"? Did you mean "const App*"?
("App* c
calamity
2014/07/28 02:55:27
Removed.
| |
| 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 |
| 55 void AppSearchProvider::Start(const base::string16& query) { | 63 void AppSearchProvider::Start(const base::string16& query) { |
| 64 StartImpl(base::Time::Now(), query); | |
| 65 } | |
| 66 | |
| 67 void AppSearchProvider::Stop() { | |
| 68 } | |
| 69 | |
| 70 void AppSearchProvider::StartImpl(const base::Time& current_time, | |
| 71 const base::string16& query) { | |
| 56 const TokenizedString query_terms(query); | 72 const TokenizedString query_terms(query); |
| 57 | 73 |
| 58 ClearResults(); | 74 ClearResults(); |
| 59 | 75 |
| 60 TokenizedStringMatch match; | 76 bool show_recommendations = query.empty(); |
| 77 // Refresh list of apps to ensure we have the latest launch time information. | |
| 78 if (show_recommendations) | |
| 79 RefreshApps(); | |
| 80 | |
| 61 for (Apps::const_iterator app_it = apps_.begin(); | 81 for (Apps::const_iterator app_it = apps_.begin(); |
| 62 app_it != apps_.end(); | 82 app_it != apps_.end(); |
| 63 ++app_it) { | 83 ++app_it) { |
| 64 if (!match.Calculate(query_terms, (*app_it)->indexed_name())) | |
| 65 continue; | |
| 66 | |
| 67 scoped_ptr<AppResult> result( | 84 scoped_ptr<AppResult> result( |
| 68 new AppResult(profile_, (*app_it)->app_id(), list_controller_)); | 85 new AppResult(profile_, (*app_it)->app_id(), list_controller_)); |
| 69 result->UpdateFromMatch((*app_it)->indexed_name(), match); | 86 if (show_recommendations) { |
| 87 result->set_title((*app_it)->indexed_name().text()); | |
| 88 result->UpdateFromLastLaunched(current_time, | |
| 89 (*app_it)->last_launch_time()); | |
| 90 } else { | |
| 91 TokenizedStringMatch match; | |
| 92 if (!match.Calculate(query_terms, (*app_it)->indexed_name())) | |
| 93 continue; | |
| 94 | |
| 95 result->UpdateFromMatch((*app_it)->indexed_name(), match); | |
| 96 } | |
| 70 Add(result.PassAs<SearchResult>()); | 97 Add(result.PassAs<SearchResult>()); |
| 71 } | 98 } |
| 72 } | 99 } |
| 73 | 100 |
| 74 void AppSearchProvider::Stop() {} | |
| 75 | |
| 76 void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) { | 101 void AppSearchProvider::AddApps(const extensions::ExtensionSet& extensions) { |
| 102 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_); | |
| 77 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); | 103 for (extensions::ExtensionSet::const_iterator iter = extensions.begin(); |
| 78 iter != extensions.end(); ++iter) { | 104 iter != extensions.end(); ++iter) { |
| 79 const extensions::Extension* app = iter->get(); | 105 const extensions::Extension* app = iter->get(); |
| 80 | 106 |
| 81 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app, profile_)) | 107 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app, profile_)) |
| 82 continue; | 108 continue; |
| 83 | 109 |
| 84 if (profile_->IsOffTheRecord() && | 110 if (profile_->IsOffTheRecord() && |
| 85 !extensions::util::CanLoadInIncognito(app, profile_)) | 111 !extensions::util::CanLoadInIncognito(app, profile_)) |
| 86 continue; | 112 continue; |
| 87 apps_.push_back(new App(app)); | 113 |
| 114 apps_.push_back(new App(app, prefs->GetLastLaunchTime(app->id()))); | |
| 88 } | 115 } |
| 89 } | 116 } |
| 90 | 117 |
| 91 void AppSearchProvider::RefreshApps() { | 118 void AppSearchProvider::RefreshApps() { |
| 92 apps_.clear(); | 119 apps_.clear(); |
| 93 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); | 120 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 94 AddApps(registry->enabled_extensions()); | 121 AddApps(registry->enabled_extensions()); |
| 95 AddApps(registry->disabled_extensions()); | 122 AddApps(registry->disabled_extensions()); |
| 96 AddApps(registry->terminated_extensions()); | 123 AddApps(registry->terminated_extensions()); |
| 124 std::sort(apps_.begin(), apps_.end(), &App::AppLaunchedMoreRecent); | |
|
Matt Giuca
2014/07/23 01:53:18
Question: Does this actually need to be sorted her
calamity
2014/07/28 02:55:27
You are indeed correct. This should have been remo
| |
| 97 } | 125 } |
| 98 | 126 |
| 99 void AppSearchProvider::OnExtensionLoaded( | 127 void AppSearchProvider::OnExtensionLoaded( |
| 100 content::BrowserContext* browser_context, | 128 content::BrowserContext* browser_context, |
| 101 const extensions::Extension* extension) { | 129 const extensions::Extension* extension) { |
| 102 RefreshApps(); | 130 RefreshApps(); |
| 103 } | 131 } |
| 104 | 132 |
| 105 void AppSearchProvider::OnExtensionUninstalled( | 133 void AppSearchProvider::OnExtensionUninstalled( |
| 106 content::BrowserContext* browser_context, | 134 content::BrowserContext* browser_context, |
| 107 const extensions::Extension* extension) { | 135 const extensions::Extension* extension) { |
| 108 RefreshApps(); | 136 RefreshApps(); |
| 109 } | 137 } |
| 110 | 138 |
| 111 } // namespace app_list | 139 } // namespace app_list |
| OLD | NEW |