[email protected] | 7328595 | 2012-05-25 20:46:40 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 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/extensions/default_apps.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/metrics/field_trial.h" |
| 9 | #include "chrome/browser/browser_process.h" |
[email protected] | 2276832 | 2011-12-21 22:28:23 | [diff] [blame] | 10 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 11 | #include "chrome/browser/extensions/default_apps_trial.h" |
[email protected] | 2276832 | 2011-12-21 22:28:23 | [diff] [blame] | 12 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
| 14 | #include "chrome/common/chrome_switches.h" |
[email protected] | 7328595 | 2012-05-25 20:46:40 | [diff] [blame] | 15 | #include "chrome/common/chrome_version_info.h" |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 16 | #include "chrome/common/extensions/extension.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 17 | #include "chrome/common/pref_names.h" |
| 18 | #include "ui/base/l10n/l10n_util.h" |
| 19 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 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 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 49 | namespace default_apps { |
| 50 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 51 | void RegisterUserPrefs(PrefService* prefs) { |
| 52 | prefs->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, |
| 53 | PrefService::UNSYNCABLE_PREF); |
| 54 | } |
| 55 | |
| 56 | bool Provider::ShouldInstallInProfile() { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 57 | // We decide to install or not install default apps based on the following |
| 58 | // criteria, from highest priority to lowest priority: |
| 59 | // |
| 60 | // - If this instance of chrome is participating in the default apps |
| 61 | // field trial, then install apps based on the group. |
| 62 | // - The command line option. Tests use this option to disable installation |
| 63 | // of default apps in some cases. |
| 64 | // - If the locale is not compatible with the defaults, don't install them. |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 65 | // - The kDefaultApps preferences value in the profile. This value is |
| 66 | // usually set in the master_preferences file. |
| 67 | bool install_apps = |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 68 | profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 69 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 70 | InstallState state = |
| 71 | static_cast<InstallState>(profile_->GetPrefs()->GetInteger( |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 72 | prefs::kDefaultAppsInstallState)); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 73 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 74 | is_migration_ = (state == kProvideLegacyDefaultApps); |
| 75 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 76 | switch (state) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 77 | case kUnknown: { |
[email protected] | 2276832 | 2011-12-21 22:28:23 | [diff] [blame] | 78 | // This is the first time the default apps feature runs on this profile. |
[email protected] | 7328595 | 2012-05-25 20:46:40 | [diff] [blame] | 79 | // Determine if we want to install them or not. |
| 80 | chrome::VersionInfo version_info; |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 81 | if (!profile_->WasCreatedByVersionOrLater(version_info.Version().c_str())) |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 82 | install_apps = false; |
| 83 | break; |
| 84 | } |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 85 | |
| 86 | // The old default apps were provided as external extensions and were |
| 87 | // installed everytime Chrome was run. Thus, changing the list of default |
| 88 | // apps affected all users. Migrate old default apps to new mechanism where |
| 89 | // they are installed only once as INTERNAL. |
| 90 | // TODO(grv) : remove after Q1-2013. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 91 | case kProvideLegacyDefaultApps: |
| 92 | profile_->GetPrefs()->SetInteger( |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 93 | prefs::kDefaultAppsInstallState, |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 94 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 95 | break; |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 96 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 97 | case kAlreadyInstalledDefaultApps: |
| 98 | case kNeverInstallDefaultApps: |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 99 | install_apps = false; |
| 100 | break; |
| 101 | default: |
| 102 | NOTREACHED(); |
| 103 | } |
| 104 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 105 | if (install_apps && !IsLocaleSupported()) { |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 106 | install_apps = false; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 110 | switches::kDisableDefaultApps)) { |
| 111 | install_apps = false; |
| 112 | } |
| 113 | |
[email protected] | 568c331 | 2011-12-06 15:36:04 | [diff] [blame] | 114 | if (base::FieldTrialList::TrialExists(kDefaultAppsTrialName)) { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 115 | install_apps = base::FieldTrialList::Find( |
[email protected] | 568c331 | 2011-12-06 15:36:04 | [diff] [blame] | 116 | kDefaultAppsTrialName)->group_name() != kDefaultAppsTrialNoAppsGroup; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 119 | // Default apps are only installed on profile creation or a new chrome |
| 120 | // download. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 121 | if (state == kUnknown) { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 122 | if (install_apps) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 123 | profile_->GetPrefs()->SetInteger( |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 124 | prefs::kDefaultAppsInstallState, |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 125 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 126 | } else { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 127 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 128 | kNeverInstallDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 129 | } |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | return install_apps; |
| 133 | } |
| 134 | |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 135 | Provider::Provider(Profile* profile, |
| 136 | VisitorInterface* service, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 137 | extensions::ExternalLoader* loader, |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 138 | extensions::Extension::Location crx_location, |
| 139 | extensions::Extension::Location download_location, |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 140 | int creation_flags) |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 141 | : extensions::ExternalProviderImpl(service, loader, crx_location, |
| 142 | download_location, creation_flags), |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 143 | profile_(profile) { |
| 144 | DCHECK(profile); |
[email protected] | 47fc70c | 2011-12-06 07:29:51 | [diff] [blame] | 145 | set_auto_acknowledge(true); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void Provider::VisitRegisteredExtension() { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 149 | if (!profile_ || !ShouldInstallInProfile()) { |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 150 | base::DictionaryValue* prefs = new base::DictionaryValue; |
| 151 | SetPrefs(prefs); |
| 152 | return; |
| 153 | } |
| 154 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 155 | extensions::ExternalProviderImpl::VisitRegisteredExtension(); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 158 | void Provider::SetPrefs(base::DictionaryValue* prefs) { |
| 159 | if (is_migration_) { |
| 160 | std::set<std::string> new_default_apps; |
| 161 | for (base::DictionaryValue::key_iterator i = prefs->begin_keys(); |
| 162 | i != prefs->end_keys(); ++i) { |
| 163 | if (!IsOldDefaultApp(*i)) { |
| 164 | new_default_apps.insert(*i); |
| 165 | } |
| 166 | } |
| 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); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 171 | } |
| 172 | } |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame^] | 173 | |
| 174 | ExternalProviderImpl::SetPrefs(prefs); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 177 | } // namespace default_apps |