| OLD | NEW |
| (Empty) |
| 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/recommended_apps.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "chrome/browser/extensions/extension_ui_util.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/app_list/recommended_apps_observer.h" | |
| 14 #include "extensions/browser/extension_prefs.h" | |
| 15 #include "extensions/browser/extension_registry.h" | |
| 16 #include "extensions/browser/pref_names.h" | |
| 17 #include "extensions/common/extension.h" | |
| 18 #include "extensions/common/extension_set.h" | |
| 19 | |
| 20 namespace app_list { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 struct AppSortInfo { | |
| 25 AppSortInfo() : app(NULL) {} | |
| 26 AppSortInfo(const extensions::Extension* app, | |
| 27 const base::Time& last_launch_time) | |
| 28 : app(app), last_launch_time(last_launch_time) {} | |
| 29 | |
| 30 const extensions::Extension* app; | |
| 31 base::Time last_launch_time; | |
| 32 }; | |
| 33 | |
| 34 bool AppLaunchedMoreRecent(const AppSortInfo& app1, const AppSortInfo& app2) { | |
| 35 return app1.last_launch_time > app2.last_launch_time; | |
| 36 } | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 RecommendedApps::RecommendedApps(Profile* profile) | |
| 41 : profile_(profile), extension_registry_observer_(this) { | |
| 42 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_); | |
| 43 pref_change_registrar_.Init(prefs->pref_service()); | |
| 44 pref_change_registrar_.Add(extensions::pref_names::kExtensions, | |
| 45 base::Bind(&RecommendedApps::Update, | |
| 46 base::Unretained(this))); | |
| 47 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile)); | |
| 48 Update(); | |
| 49 } | |
| 50 | |
| 51 RecommendedApps::~RecommendedApps() { | |
| 52 } | |
| 53 | |
| 54 void RecommendedApps::AddObserver(RecommendedAppsObserver* observer) { | |
| 55 observers_.AddObserver(observer); | |
| 56 } | |
| 57 | |
| 58 void RecommendedApps::RemoveObserver(RecommendedAppsObserver* observer) { | |
| 59 observers_.RemoveObserver(observer); | |
| 60 } | |
| 61 | |
| 62 void RecommendedApps::Update() { | |
| 63 extensions::ExtensionPrefs* prefs = extensions::ExtensionPrefs::Get(profile_); | |
| 64 | |
| 65 std::vector<AppSortInfo> sorted_apps; | |
| 66 const extensions::ExtensionSet& extensions = | |
| 67 extensions::ExtensionRegistry::Get(profile_)->enabled_extensions(); | |
| 68 for (extensions::ExtensionSet::const_iterator app = extensions.begin(); | |
| 69 app != extensions.end(); | |
| 70 ++app) { | |
| 71 if (!extensions::ui_util::ShouldDisplayInAppLauncher(app->get(), profile_)) | |
| 72 continue; | |
| 73 | |
| 74 sorted_apps.push_back( | |
| 75 AppSortInfo(app->get(), prefs->GetLastLaunchTime((*app)->id()))); | |
| 76 } | |
| 77 | |
| 78 std::sort(sorted_apps.begin(), sorted_apps.end(), &AppLaunchedMoreRecent); | |
| 79 | |
| 80 const size_t kMaxRecommendedApps = 4; | |
| 81 sorted_apps.resize(std::min(kMaxRecommendedApps, sorted_apps.size())); | |
| 82 | |
| 83 Apps new_recommends; | |
| 84 for (size_t i = 0; i < sorted_apps.size(); ++i) | |
| 85 new_recommends.push_back(sorted_apps[i].app); | |
| 86 | |
| 87 const bool changed = apps_.size() != new_recommends.size() || | |
| 88 !std::equal(apps_.begin(), apps_.end(), new_recommends.begin()); | |
| 89 if (changed) { | |
| 90 apps_.swap(new_recommends); | |
| 91 FOR_EACH_OBSERVER( | |
| 92 RecommendedAppsObserver, observers_, OnRecommendedAppsChanged()); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void RecommendedApps::OnExtensionWillBeInstalled( | |
| 97 content::BrowserContext* browser_context, | |
| 98 const extensions::Extension* extension, | |
| 99 bool is_update, | |
| 100 bool from_ephemeral, | |
| 101 const std::string& old_name) { | |
| 102 Update(); | |
| 103 } | |
| 104 | |
| 105 void RecommendedApps::OnExtensionLoaded( | |
| 106 content::BrowserContext* browser_context, | |
| 107 const extensions::Extension* extension) { | |
| 108 Update(); | |
| 109 } | |
| 110 | |
| 111 void RecommendedApps::OnExtensionUnloaded( | |
| 112 content::BrowserContext* browser_context, | |
| 113 const extensions::Extension* extension, | |
| 114 extensions::UnloadedExtensionInfo::Reason reason) { | |
| 115 Update(); | |
| 116 } | |
| 117 | |
| 118 void RecommendedApps::OnExtensionUninstalled( | |
| 119 content::BrowserContext* browser_context, | |
| 120 const extensions::Extension* extension, | |
| 121 extensions::UninstallReason reason) { | |
| 122 Update(); | |
| 123 } | |
| 124 | |
| 125 } // namespace app_list | |
| OLD | NEW |