| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/field_trial.h" | 8 #include "base/metrics/field_trial.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/first_run/first_run.h" | 10 #include "chrome/browser/first_run/first_run.h" |
| 11 #include "chrome/browser/extensions/default_apps_trial.h" | 11 #include "chrome/browser/extensions/default_apps_trial.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
| 16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 | 19 |
| 20 namespace { |
| 21 |
| 22 const char kGmailId[] = "pjkljhegncpnkpknbcohdijeoejaedia"; |
| 23 const char kSearchId[] = "coobgpohoikkiipiblmjeljniedjpjpf"; |
| 24 const char kYoutubeId[] = "blpcfgokakmgnkcojhhkbfbldkacnbeo"; |
| 25 |
| 26 // Returns true if the app was a default app in Chrome 22 |
| 27 bool IsOldDefaultApp(const std::string& extension_id) { |
| 28 return extension_id == kGmailId || extension_id == kSearchId |
| 29 || extension_id == kYoutubeId; |
| 30 } |
| 31 |
| 32 bool IsLocaleSupported() { |
| 33 // Don't bother installing default apps in locales where it is known that |
| 34 // they don't work. |
| 35 // TODO(rogerta): Do this check dynamically once the webstore can expose |
| 36 // an API. See http://crbug.com/101357 |
| 37 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 38 static const char* unsupported_locales[] = {"CN", "TR", "IR"}; |
| 39 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { |
| 40 if (EndsWith(locale, unsupported_locales[i], false)) { |
| 41 return false; |
| 42 } |
| 43 } |
| 44 return true; |
| 45 } |
| 46 |
| 47 } // namespace |
| 48 |
| 20 namespace default_apps { | 49 namespace default_apps { |
| 21 | 50 |
| 22 bool ShouldInstallInProfile(Profile* profile) { | 51 void RegisterUserPrefs(PrefService* prefs) { |
| 52 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, |
| 53 PrefService::UNSYNCABLE_PREF); |
| 54 } |
| 55 |
| 56 bool Provider::ShouldInstallInProfile() { |
| 23 // We decide to install or not install default apps based on the following | 57 // We decide to install or not install default apps based on the following |
| 24 // criteria, from highest priority to lowest priority: | 58 // criteria, from highest priority to lowest priority: |
| 25 // | 59 // |
| 26 // - If this instance of chrome is participating in the default apps | 60 // - If this instance of chrome is participating in the default apps |
| 27 // field trial, then install apps based on the group. | 61 // field trial, then install apps based on the group. |
| 28 // - The command line option. Tests use this option to disable installation | 62 // - The command line option. Tests use this option to disable installation |
| 29 // of default apps in some cases. | 63 // of default apps in some cases. |
| 30 // - If the locale is not compatible with the defaults, don't install them. | 64 // - If the locale is not compatible with the defaults, don't install them. |
| 31 // - The kDefaultApps preferences value in the profile. This value is | 65 // - The kDefaultApps preferences value in the profile. This value is |
| 32 // usually set in the master_preferences file. | 66 // usually set in the master_preferences file. |
| 33 bool install_apps = | 67 bool install_apps = |
| 34 profile->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; | 68 profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; |
| 35 | 69 |
| 36 default_apps::InstallState state = | 70 InstallState state = |
| 37 static_cast<default_apps::InstallState>(profile->GetPrefs()->GetInteger( | 71 static_cast<InstallState>(profile_->GetPrefs()->GetInteger( |
| 38 prefs::kDefaultAppsInstallState)); | 72 prefs::kDefaultAppsInstallState)); |
| 39 | 73 |
| 74 is_migration_ = (state == kProvideLegacyDefaultApps); |
| 75 |
| 40 switch (state) { | 76 switch (state) { |
| 41 case default_apps::kUnknown: { | 77 case kUnknown: { |
| 42 // This is the first time the default apps feature runs on this profile. | 78 // This is the first time the default apps feature runs on this profile. |
| 43 // Determine if we want to install them or not. | 79 // Determine if we want to install them or not. |
| 44 chrome::VersionInfo version_info; | 80 chrome::VersionInfo version_info; |
| 45 if (!profile->WasCreatedByVersionOrLater(version_info.Version().c_str())) | 81 if (!profile_->WasCreatedByVersionOrLater(version_info.Version().c_str())) |
| 46 install_apps = false; | 82 install_apps = false; |
| 47 break; | 83 break; |
| 48 } | 84 } |
| 49 | 85 |
| 50 // The old default apps were provided as external extensions and were | 86 // The old default apps were provided as external extensions and were |
| 51 // installed everytime Chrome was run. Thus, changing the list of default | 87 // installed everytime Chrome was run. Thus, changing the list of default |
| 52 // apps affected all users. Migrate old default apps to new mechanism where | 88 // apps affected all users. Migrate old default apps to new mechanism where |
| 53 // they are installed only once as INTERNAL. | 89 // they are installed only once as INTERNAL. |
| 54 // TODO(grv) : remove after Q1-2013. | 90 // TODO(grv) : remove after Q1-2013. |
| 55 case default_apps::kProvideLegacyDefaultApps: | 91 case kProvideLegacyDefaultApps: |
| 56 profile->GetPrefs()->SetInteger( | 92 profile_->GetPrefs()->SetInteger( |
| 57 prefs::kDefaultAppsInstallState, | 93 prefs::kDefaultAppsInstallState, |
| 58 default_apps::kAlreadyInstalledDefaultApps); | 94 kAlreadyInstalledDefaultApps); |
| 59 break; | 95 break; |
| 60 | 96 |
| 61 case default_apps::kAlreadyInstalledDefaultApps: | 97 case kAlreadyInstalledDefaultApps: |
| 62 case default_apps::kNeverInstallDefaultApps: | 98 case kNeverInstallDefaultApps: |
| 63 install_apps = false; | 99 install_apps = false; |
| 64 break; | 100 break; |
| 65 default: | 101 default: |
| 66 NOTREACHED(); | 102 NOTREACHED(); |
| 67 } | 103 } |
| 68 | 104 |
| 69 if (install_apps && !isLocaleSupported()) { | 105 if (install_apps && !IsLocaleSupported()) { |
| 70 install_apps = false; | 106 install_apps = false; |
| 71 } | 107 } |
| 72 | 108 |
| 73 if (CommandLine::ForCurrentProcess()->HasSwitch( | 109 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 74 switches::kDisableDefaultApps)) { | 110 switches::kDisableDefaultApps)) { |
| 75 install_apps = false; | 111 install_apps = false; |
| 76 } | 112 } |
| 77 | 113 |
| 78 if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { | 114 if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { |
| 79 install_apps = base::FieldTrialList::Find( | 115 install_apps = base::FieldTrialList::Find( |
| 80 kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; | 116 kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; |
| 81 } | 117 } |
| 82 | 118 |
| 83 // Default apps are only installed on profile creation or a new chrome | 119 // Default apps are only installed on profile creation or a new chrome |
| 84 // download. | 120 // download. |
| 85 if (state == default_apps::kUnknown) { | 121 if (state == kUnknown) { |
| 86 if (install_apps) { | 122 if (install_apps) { |
| 87 profile->GetPrefs()->SetInteger( | 123 profile_->GetPrefs()->SetInteger( |
| 88 prefs::kDefaultAppsInstallState, | 124 prefs::kDefaultAppsInstallState, |
| 89 default_apps::kAlreadyInstalledDefaultApps); | 125 kAlreadyInstalledDefaultApps); |
| 90 } else { | 126 } else { |
| 91 profile->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, | 127 profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 92 default_apps::kNeverInstallDefaultApps); | 128 kNeverInstallDefaultApps); |
| 93 } | 129 } |
| 94 } | 130 } |
| 95 | 131 |
| 96 return install_apps; | 132 return install_apps; |
| 97 } | 133 } |
| 98 | 134 |
| 99 void RegisterUserPrefs(PrefService* prefs) { | |
| 100 prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, | |
| 101 PrefService::UNSYNCABLE_PREF); | |
| 102 } | |
| 103 | |
| 104 Provider::Provider(Profile* profile, | 135 Provider::Provider(Profile* profile, |
| 105 VisitorInterface* service, | 136 VisitorInterface* service, |
| 106 extensions::ExternalLoader* loader, | 137 extensions::ExternalLoader* loader, |
| 107 extensions::Extension::Location crx_location, | 138 extensions::Extension::Location crx_location, |
| 108 extensions::Extension::Location download_location, | 139 extensions::Extension::Location download_location, |
| 109 int creation_flags) | 140 int creation_flags) |
| 110 : extensions::ExternalProviderImpl(service, loader, crx_location, | 141 : extensions::ExternalProviderImpl(service, loader, crx_location, |
| 111 download_location, creation_flags), | 142 download_location, creation_flags), |
| 112 profile_(profile) { | 143 profile_(profile) { |
| 113 DCHECK(profile); | 144 DCHECK(profile); |
| 114 set_auto_acknowledge(true); | 145 set_auto_acknowledge(true); |
| 115 } | 146 } |
| 116 | 147 |
| 117 void Provider::VisitRegisteredExtension() { | 148 void Provider::VisitRegisteredExtension() { |
| 118 if (!profile_ || !ShouldInstallInProfile(profile_)) { | 149 if (!profile_ || !ShouldInstallInProfile()) { |
| 119 base::DictionaryValue* prefs = new base::DictionaryValue; | 150 base::DictionaryValue* prefs = new base::DictionaryValue; |
| 120 SetPrefs(prefs); | 151 SetPrefs(prefs); |
| 121 return; | 152 return; |
| 122 } | 153 } |
| 123 | 154 |
| 124 extensions::ExternalProviderImpl::VisitRegisteredExtension(); | 155 extensions::ExternalProviderImpl::VisitRegisteredExtension(); |
| 125 } | 156 } |
| 126 | 157 |
| 127 bool isLocaleSupported() { | 158 void Provider::SetPrefs(base::DictionaryValue* prefs) { |
| 128 // Don't bother installing default apps in locales where it is known that | 159 if (is_migration_) { |
| 129 // they don't work. | 160 std::set<std::string> new_default_apps; |
| 130 // TODO(rogerta): Do this check dynamically once the webstore can expose | 161 for (base::DictionaryValue::key_iterator i = prefs->begin_keys(); |
| 131 // an API. See http://crbug.com/101357 | 162 i != prefs->end_keys(); ++i) { |
| 132 const std::string& locale = g_browser_process->GetApplicationLocale(); | 163 if (!IsOldDefaultApp(*i)) { |
| 133 static const char* unsupported_locales[] = {"CN", "TR", "IR"}; | 164 new_default_apps.insert(*i); |
| 134 for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { | 165 } |
| 135 if (EndsWith(locale, unsupported_locales[i], false)) { | 166 } |
| 136 return false; | 167 // Filter out the new default apps for migrating users. |
| 168 for (std::set<std::string>::iterator it = new_default_apps.begin(); |
| 169 it != new_default_apps.end(); ++it) { |
| 170 prefs->Remove(*it, NULL); |
| 137 } | 171 } |
| 138 } | 172 } |
| 139 return true; | 173 |
| 174 ExternalProviderImpl::SetPrefs(prefs); |
| 140 } | 175 } |
| 141 | 176 |
| 142 } // namespace default_apps | 177 } // namespace default_apps |
| OLD | NEW |