[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 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame^] | 7 | #include <stddef.h> |
| 8 | |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 12 | #include "base/command_line.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame^] | 13 | #include "base/macros.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 14 | #include "base/prefs/pref_service.h" |
[email protected] | d4f5204 | 2014-08-08 22:43:59 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 16 | #include "chrome/browser/browser_process.h" |
[email protected] | 13e062e | 2014-08-09 10:21:55 | [diff] [blame] | 17 | #include "chrome/browser/first_run/first_run.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile.h" |
| 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 21 | #include "chrome/common/pref_names.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 22 | #include "components/pref_registry/pref_registry_syncable.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 23 | #include "components/version_info/version_info.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 24 | #include "extensions/common/extension.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 25 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 28 | // Returns true if the app was a default app in Chrome 22 |
| 29 | bool IsOldDefaultApp(const std::string& extension_id) { |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 30 | return extension_id == extension_misc::kGmailAppId || |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 31 | extension_id == extension_misc::kYoutubeAppId; |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | bool IsLocaleSupported() { |
| 35 | // Don't bother installing default apps in locales where it is known that |
| 36 | // they don't work. |
| 37 | // TODO(rogerta): Do this check dynamically once the webstore can expose |
| 38 | // an API. See http://crbug.com/101357 |
| 39 | const std::string& locale = g_browser_process->GetApplicationLocale(); |
thestig | 4b36dd3 | 2014-10-31 20:30:19 | [diff] [blame] | 40 | static const char* const unsupported_locales[] = {"CN", "TR", "IR"}; |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 41 | for (size_t i = 0; i < arraysize(unsupported_locales); ++i) { |
brettw | a7ff1b29 | 2015-07-16 17:49:29 | [diff] [blame] | 42 | if (base::EndsWith(locale, unsupported_locales[i], |
| 43 | base::CompareCase::INSENSITIVE_ASCII)) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | return true; |
| 48 | } |
| 49 | |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 50 | } // namespace |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 51 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 52 | namespace default_apps { |
| 53 | |
[email protected] | 37ca3fe0 | 2013-07-05 15:32:44 | [diff] [blame] | 54 | void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
raymes | aa60872 | 2015-04-27 03:00:25 | [diff] [blame] | 55 | registry->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool Provider::ShouldInstallInProfile() { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 59 | // We decide to install or not install default apps based on the following |
| 60 | // criteria, from highest priority to lowest priority: |
| 61 | // |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 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] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 78 | // Only new installations and profiles get default apps. In theory the |
| 79 | // new profile checks should catch new installations, but that is not |
| 80 | // always the case (http:/crbug.com/145351). |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 81 | bool is_new_profile = profile_->WasCreatedByVersionOrLater( |
| 82 | version_info::GetVersionNumber()); |
[email protected] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 83 | bool is_first_run = first_run::IsChromeFirstRun(); |
[email protected] | 2eef64f | 2012-08-31 23:09:12 | [diff] [blame] | 84 | if (!is_first_run && !is_new_profile) |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 85 | install_apps = false; |
| 86 | break; |
| 87 | } |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 88 | |
| 89 | // The old default apps were provided as external extensions and were |
| 90 | // installed everytime Chrome was run. Thus, changing the list of default |
| 91 | // apps affected all users. Migrate old default apps to new mechanism where |
| 92 | // they are installed only once as INTERNAL. |
| 93 | // TODO(grv) : remove after Q1-2013. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 94 | case kProvideLegacyDefaultApps: |
| 95 | profile_->GetPrefs()->SetInteger( |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 96 | prefs::kDefaultAppsInstallState, |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 97 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 98 | break; |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 99 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 100 | case kAlreadyInstalledDefaultApps: |
| 101 | case kNeverInstallDefaultApps: |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 102 | install_apps = false; |
| 103 | break; |
| 104 | default: |
| 105 | NOTREACHED(); |
| 106 | } |
| 107 | |
[email protected] | 9bf21b9 | 2012-10-04 21:01:39 | [diff] [blame] | 108 | if (install_apps && !IsLocaleSupported()) |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 109 | install_apps = false; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 110 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 111 | // Default apps are only installed on profile creation or a new chrome |
| 112 | // download. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 113 | if (state == kUnknown) { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 114 | if (install_apps) { |
[email protected] | 9bf21b9 | 2012-10-04 21:01:39 | [diff] [blame] | 115 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 116 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 117 | } else { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 118 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 119 | kNeverInstallDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 120 | } |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return install_apps; |
| 124 | } |
| 125 | |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 126 | Provider::Provider(Profile* profile, |
| 127 | VisitorInterface* service, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 128 | extensions::ExternalLoader* loader, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 129 | extensions::Manifest::Location crx_location, |
| 130 | extensions::Manifest::Location download_location, |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 131 | int creation_flags) |
[email protected] | 19eac6d | 2013-05-30 06:51:03 | [diff] [blame] | 132 | : extensions::ExternalProviderImpl(service, loader, profile, crx_location, |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 133 | download_location, creation_flags), |
[email protected] | e9a30af | 2012-10-04 01:56:25 | [diff] [blame] | 134 | profile_(profile), |
| 135 | is_migration_(false) { |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 136 | DCHECK(profile); |
[email protected] | 47fc70c | 2011-12-06 07:29:51 | [diff] [blame] | 137 | set_auto_acknowledge(true); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void Provider::VisitRegisteredExtension() { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 141 | if (!profile_ || !ShouldInstallInProfile()) { |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 142 | base::DictionaryValue* prefs = new base::DictionaryValue; |
| 143 | SetPrefs(prefs); |
| 144 | return; |
| 145 | } |
| 146 | |
[email protected] | 5df038b | 2012-07-16 19:03:27 | [diff] [blame] | 147 | extensions::ExternalProviderImpl::VisitRegisteredExtension(); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 150 | void Provider::SetPrefs(base::DictionaryValue* prefs) { |
| 151 | if (is_migration_) { |
| 152 | std::set<std::string> new_default_apps; |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 153 | for (base::DictionaryValue::Iterator i(*prefs); !i.IsAtEnd(); i.Advance()) { |
[email protected] | 02d9b27 | 2013-03-06 12:54:56 | [diff] [blame] | 154 | if (!IsOldDefaultApp(i.key())) |
| 155 | new_default_apps.insert(i.key()); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 156 | } |
| 157 | // Filter out the new default apps for migrating users. |
| 158 | for (std::set<std::string>::iterator it = new_default_apps.begin(); |
| 159 | it != new_default_apps.end(); ++it) { |
| 160 | prefs->Remove(*it, NULL); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 161 | } |
| 162 | } |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 163 | |
| 164 | ExternalProviderImpl::SetPrefs(prefs); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 167 | } // namespace default_apps |