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