| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 <algorithm> | |
| 6 | |
| 7 #include "chrome/browser/extensions/default_apps.h" | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/metrics/histogram.h" | |
| 11 #include "chrome/browser/prefs/pref_service.h" | |
| 12 #include "chrome/common/chrome_switches.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 | |
| 15 const int DefaultApps::kAppsPromoCounterMax = 10; | |
| 16 | |
| 17 // static | |
| 18 void DefaultApps::RegisterUserPrefs(PrefService* prefs) { | |
| 19 prefs->RegisterBooleanPref(prefs::kDefaultAppsInstalled, false); | |
| 20 prefs->RegisterIntegerPref(prefs::kAppsPromoCounter, 0); | |
| 21 } | |
| 22 | |
| 23 DefaultApps::DefaultApps(PrefService* prefs, | |
| 24 const std::string& application_locale) | |
| 25 : prefs_(prefs), application_locale_(application_locale) { | |
| 26 // Poppit, Entanglement | |
| 27 ids_.insert("mcbkbpnkkkipelfledbfocopglifcfmi"); | |
| 28 ids_.insert("aciahcmjmecflokailenpkdchphgkefd"); | |
| 29 } | |
| 30 | |
| 31 DefaultApps::~DefaultApps() {} | |
| 32 | |
| 33 const ExtensionIdSet& DefaultApps::default_apps() const { | |
| 34 return ids_; | |
| 35 } | |
| 36 | |
| 37 bool DefaultApps::DefaultAppSupported() { | |
| 38 // On Chrome OS the default apps are installed via a different mechanism. | |
| 39 #if defined(OS_CHROMEOS) | |
| 40 return false; | |
| 41 #else | |
| 42 return DefaultAppsSupportedForLanguage(); | |
| 43 #endif | |
| 44 } | |
| 45 | |
| 46 bool DefaultApps::DefaultAppsSupportedForLanguage() { | |
| 47 return application_locale_ == "en-US"; | |
| 48 } | |
| 49 | |
| 50 bool DefaultApps::ShouldInstallDefaultApps( | |
| 51 const ExtensionIdSet& installed_ids) { | |
| 52 if (!DefaultAppSupported()) | |
| 53 return false; | |
| 54 | |
| 55 if (GetDefaultAppsInstalled()) | |
| 56 return false; | |
| 57 | |
| 58 // If there are any non-default apps installed, we should never try to install | |
| 59 // the default apps again, even if the non-default apps are later removed. | |
| 60 if (NonDefaultAppIsInstalled(installed_ids)) { | |
| 61 SetDefaultAppsInstalled(true); | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 bool DefaultApps::ShouldShowAppLauncher(const ExtensionIdSet& installed_ids) { | |
| 69 // On Chrome OS the default apps are installed via a separate mechanism that | |
| 70 // is always enabled. Therefore we always show the launcher. | |
| 71 #if defined(OS_CHROME) | |
| 72 return true; | |
| 73 #else | |
| 74 // The app store only supports en-us at the moment, so we don't show the apps | |
| 75 // section by default for users in other locales. But we do show it if a user | |
| 76 // from a non-supported locale somehow installs an app (eg by navigating | |
| 77 // directly to the store). | |
| 78 if (!DefaultAppsSupportedForLanguage()) | |
| 79 return !installed_ids.empty(); | |
| 80 | |
| 81 // For supported locales, we need to wait for all the default apps to be | |
| 82 // installed before showing the apps section. We also show it if any | |
| 83 // non-default app is installed (eg because the user installed the app in a | |
| 84 // previous version of Chrome). | |
| 85 if (GetDefaultAppsInstalled() || NonDefaultAppIsInstalled(installed_ids)) | |
| 86 return true; | |
| 87 else | |
| 88 return false; | |
| 89 #endif | |
| 90 } | |
| 91 | |
| 92 bool DefaultApps::ShouldShowPromo(const ExtensionIdSet& installed_ids, | |
| 93 bool* just_expired) { | |
| 94 *just_expired = false; | |
| 95 | |
| 96 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 97 switches::kForceAppsPromoVisible)) { | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 if (!DefaultAppSupported()) | |
| 102 return false; | |
| 103 | |
| 104 if (!GetDefaultAppsInstalled()) | |
| 105 return false; | |
| 106 | |
| 107 int promo_counter = GetPromoCounter(); | |
| 108 if (promo_counter <= kAppsPromoCounterMax) { | |
| 109 // If we have the exact set of default apps, show the promo. If we don't | |
| 110 // have the exact set of default apps, this means that the user manually | |
| 111 // installed or uninstalled one. The promo doesn't make sense if it shows | |
| 112 // apps the user manually installed, so expire it immediately in that | |
| 113 // situation. | |
| 114 if (ids_ != installed_ids) { | |
| 115 SetPromoHidden(); | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 if (promo_counter == kAppsPromoCounterMax) { | |
| 120 *just_expired = true; | |
| 121 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, | |
| 122 extension_misc::PROMO_EXPIRE, | |
| 123 extension_misc::PROMO_BUCKET_BOUNDARY); | |
| 124 SetPromoCounter(++promo_counter); | |
| 125 return false; | |
| 126 } else { | |
| 127 UMA_HISTOGRAM_ENUMERATION(extension_misc::kAppsPromoHistogram, | |
| 128 extension_misc::PROMO_SEEN, | |
| 129 extension_misc::PROMO_BUCKET_BOUNDARY); | |
| 130 SetPromoCounter(++promo_counter); | |
| 131 return true; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 return false; | |
| 136 } | |
| 137 | |
| 138 void DefaultApps::DidInstallApp(const ExtensionIdSet& installed_ids) { | |
| 139 // If all the default apps have been installed, stop trying to install them. | |
| 140 // Note that we use std::includes here instead of == because apps might have | |
| 141 // been manually installed while the the default apps were installing and we | |
| 142 // wouldn't want to keep trying to install them in that case. | |
| 143 if (!GetDefaultAppsInstalled() && | |
| 144 std::includes(installed_ids.begin(), installed_ids.end(), | |
| 145 ids_.begin(), ids_.end())) { | |
| 146 SetDefaultAppsInstalled(true); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 bool DefaultApps::NonDefaultAppIsInstalled( | |
| 151 const ExtensionIdSet& installed_ids) const { | |
| 152 for (ExtensionIdSet::const_iterator iter = installed_ids.begin(); | |
| 153 iter != installed_ids.end(); ++iter) { | |
| 154 if (ids_.find(*iter) == ids_.end()) | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 return false; | |
| 159 } | |
| 160 | |
| 161 void DefaultApps::SetPromoHidden() { | |
| 162 SetPromoCounter(kAppsPromoCounterMax + 1); | |
| 163 } | |
| 164 | |
| 165 int DefaultApps::GetPromoCounter() const { | |
| 166 return prefs_->GetInteger(prefs::kAppsPromoCounter); | |
| 167 } | |
| 168 | |
| 169 void DefaultApps::SetPromoCounter(int val) { | |
| 170 prefs_->SetInteger(prefs::kAppsPromoCounter, val); | |
| 171 prefs_->ScheduleSavePersistentPrefs(); | |
| 172 } | |
| 173 | |
| 174 bool DefaultApps::GetDefaultAppsInstalled() const { | |
| 175 return prefs_->GetBoolean(prefs::kDefaultAppsInstalled); | |
| 176 } | |
| 177 | |
| 178 void DefaultApps::SetDefaultAppsInstalled(bool val) { | |
| 179 prefs_->SetBoolean(prefs::kDefaultAppsInstalled, val); | |
| 180 prefs_->ScheduleSavePersistentPrefs(); | |
| 181 } | |
| OLD | NEW |