[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 | |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame^] | 7 | #include <set> |
| 8 | #include <string> |
| 9 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | 75fee37 | 2013-03-06 00:42:44 | [diff] [blame] | 12 | #include "components/user_prefs/pref_registry_syncable.h" |
[email protected] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 13 | #if !defined(OS_ANDROID) |
[email protected] | 2276832 | 2011-12-21 22:28:23 | [diff] [blame] | 14 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 15 | #endif |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 16 | #include "base/prefs/pref_service.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
| 18 | #include "chrome/common/chrome_switches.h" |
[email protected] | 7328595 | 2012-05-25 20:46:40 | [diff] [blame] | 19 | #include "chrome/common/chrome_version_info.h" |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 21 | #include "chrome/common/pref_names.h" |
| 22 | #include "ui/base/l10n/l10n_util.h" |
| 23 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 26 | // Returns true if the app was a default app in Chrome 22 |
| 27 | bool IsOldDefaultApp(const std::string& extension_id) { |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame^] | 28 | return extension_id == extension_misc::kGmailAppId || |
| 29 | extension_id == extension_misc::kGoogleSearchAppId || |
| 30 | extension_id == extension_misc::kYoutubeAppId; |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | bool IsLocaleSupported() { |
| 34 | // Don't bother installing default apps in locales where it is known that |
| 35 | // they don't work. |
| 36 | // TODO(rogerta): Do this check dynamically once the webstore can expose |
| 37 | // an API. See http://crbug.com/101357 |
| 38 | const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 39 | static const char* unsupported_locales[] = {"CN", "TR", "IR"}; |
| 40 | for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { |
| 41 | if (EndsWith(locale, unsupported_locales[i], false)) { |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame^] | 48 | } // namespace |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 49 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 50 | namespace default_apps { |
| 51 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 52 | void RegisterUserPrefs(PrefRegistrySyncable* registry) { |
| 53 | registry->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown, |
| 54 | PrefRegistrySyncable::UNSYNCABLE_PREF); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | bool Provider::ShouldInstallInProfile() { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 58 | // We decide to install or not install default apps based on the following |
| 59 | // criteria, from highest priority to lowest priority: |
| 60 | // |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 61 | // - The command line option. Tests use this option to disable installation |
| 62 | // of default apps in some cases. |
| 63 | // - If the locale is not compatible with the defaults, don't install them. |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 64 | // - The kDefaultApps preferences value in the profile. This value is |
| 65 | // usually set in the master_preferences file. |
| 66 | bool install_apps = |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 67 | profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 68 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 69 | InstallState state = |
| 70 | static_cast<InstallState>(profile_->GetPrefs()->GetInteger( |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 71 | prefs::kDefaultAppsInstallState)); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 72 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 73 | is_migration_ = (state == kProvideLegacyDefaultApps); |
| 74 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 75 | switch (state) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 76 | case kUnknown: { |
[email protected] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 77 | // Only new installations and profiles get default apps. In theory the |
| 78 | // new profile checks should catch new installations, but that is not |
| 79 | // always the case (http:/crbug.com/145351). |
[email protected] | 7328595 | 2012-05-25 20:46:40 | [diff] [blame] | 80 | chrome::VersionInfo version_info; |
[email protected] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 81 | bool is_new_profile = |
| 82 | profile_->WasCreatedByVersionOrLater(version_info.Version().c_str()); |
| 83 | // Android excludes most of the first run code, so it can't determine |
| 84 | // if this is a first run. That's OK though, because Android doesn't |
| 85 | // use default apps in general. |
| 86 | #if defined(OS_ANDROID) |
| 87 | bool is_first_run = false; |
| 88 | #else |
| 89 | bool is_first_run = first_run::IsChromeFirstRun(); |
| 90 | #endif |
| 91 | if (!is_first_run && !is_new_profile) |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 92 | install_apps = false; |
| 93 | break; |
| 94 | } |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 95 | |
| 96 | // The old default apps were provided as external extensions and were |
| 97 | // installed everytime Chrome was run. Thus, changing the list of default |
| 98 | // apps affected all users. Migrate old default apps to new mechanism where |
| 99 | // they are installed only once as INTERNAL. |
| 100 | // TODO(grv) : remove after Q1-2013. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 101 | case kProvideLegacyDefaultApps: |
| 102 | profile_->GetPrefs()->SetInteger( |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 103 | prefs::kDefaultAppsInstallState, |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 104 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 105 | break; |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 106 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 107 | case kAlreadyInstalledDefaultApps: |
| 108 | case kNeverInstallDefaultApps: |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 109 | install_apps = false; |
| 110 | break; |
| 111 | default: |
| 112 | NOTREACHED(); |
| 113 | } |
| 114 | |
[email protected] | 9bf21b9 | 2012-10-04 21:01:39 | [diff] [blame] | 115 | if (install_apps && !IsLocaleSupported()) |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 116 | install_apps = false; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 117 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 118 | // Default apps are only installed on profile creation or a new chrome |
| 119 | // download. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 120 | if (state == kUnknown) { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 121 | if (install_apps) { |
[email protected] | 9bf21b9 | 2012-10-04 21:01:39 | [diff] [blame] | 122 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 123 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 124 | } else { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 125 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 126 | kNeverInstallDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 127 | } |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return install_apps; |
| 131 | } |
| 132 | |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 133 | Provider::Provider(Profile* profile, |
| 134 | VisitorInterface* service, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 135 | extensions::ExternalLoader* loader, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 136 | extensions::Manifest::Location crx_location, |
| 137 | extensions::Manifest::Location download_location, |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 138 | int creation_flags) |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 139 | : extensions::ExternalProviderImpl(service, loader, crx_location, |
| 140 | download_location, creation_flags), |
[email protected] | e9a30af | 2012-10-04 01:56:25 | [diff] [blame] | 141 | profile_(profile), |
| 142 | is_migration_(false) { |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 143 | DCHECK(profile); |
[email protected] | 47fc70c | 2011-12-06 07:29:51 | [diff] [blame] | 144 | set_auto_acknowledge(true); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void Provider::VisitRegisteredExtension() { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 148 | if (!profile_ || !ShouldInstallInProfile()) { |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 149 | base::DictionaryValue* prefs = new base::DictionaryValue; |
| 150 | SetPrefs(prefs); |
| 151 | return; |
| 152 | } |
| 153 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 154 | extensions::ExternalProviderImpl::VisitRegisteredExtension(); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 157 | void Provider::SetPrefs(base::DictionaryValue* prefs) { |
| 158 | if (is_migration_) { |
| 159 | std::set<std::string> new_default_apps; |
[email protected] | 02d9b27 | 2013-03-06 12:54:56 | [diff] [blame] | 160 | for (DictionaryValue::Iterator i(*prefs); !i.IsAtEnd(); i.Advance()) { |
| 161 | if (!IsOldDefaultApp(i.key())) |
| 162 | new_default_apps.insert(i.key()); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 163 | } |
| 164 | // Filter out the new default apps for migrating users. |
| 165 | for (std::set<std::string>::iterator it = new_default_apps.begin(); |
| 166 | it != new_default_apps.end(); ++it) { |
| 167 | prefs->Remove(*it, NULL); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 168 | } |
| 169 | } |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 170 | |
| 171 | ExternalProviderImpl::SetPrefs(prefs); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 174 | } // namespace default_apps |