[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 | |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 9 | #include <memory> |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
| 12 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 13 | #include "base/command_line.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 14 | #include "base/stl_util.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] | 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] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 19 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 20 | #include "chrome/common/pref_names.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 21 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 22 | #include "components/prefs/pref_service.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 23 | #include "components/version_info/version_info.h" |
Michael Giuffrida | e84fe6de | 2018-08-30 07:19:52 | [diff] [blame] | 24 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 25 | #include "extensions/common/extension.h" |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 26 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 27 | namespace { |
| 28 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 29 | // Returns true if the app was a default app in Chrome 22 |
| 30 | bool IsOldDefaultApp(const std::string& extension_id) { |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 31 | return extension_id == extension_misc::kGmailAppId || |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 32 | extension_id == extension_misc::kYoutubeAppId; |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | bool IsLocaleSupported() { |
| 36 | // Don't bother installing default apps in locales where it is known that |
| 37 | // they don't work. |
| 38 | // TODO(rogerta): Do this check dynamically once the webstore can expose |
| 39 | // an API. See http://crbug.com/101357 |
| 40 | const std::string& locale = g_browser_process->GetApplicationLocale(); |
thestig | 4b36dd3 | 2014-10-31 20:30:19 | [diff] [blame] | 41 | static const char* const unsupported_locales[] = {"CN", "TR", "IR"}; |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame] | 42 | for (size_t i = 0; i < base::size(unsupported_locales); ++i) { |
brettw | a7ff1b29 | 2015-07-16 17:49:29 | [diff] [blame] | 43 | if (base::EndsWith(locale, unsupported_locales[i], |
| 44 | base::CompareCase::INSENSITIVE_ASCII)) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
[email protected] | b809655 | 2013-05-04 15:48:11 | [diff] [blame] | 51 | } // namespace |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 52 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 53 | namespace default_apps { |
| 54 | |
[email protected] | 37ca3fe0 | 2013-07-05 15:32:44 | [diff] [blame] | 55 | void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
raymes | aa60872 | 2015-04-27 03:00:25 | [diff] [blame] | 56 | registry->RegisterIntegerPref(prefs::kDefaultAppsInstallState, kUnknown); |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool Provider::ShouldInstallInProfile() { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 60 | // We decide to install or not install default apps based on the following |
| 61 | // criteria, from highest priority to lowest priority: |
| 62 | // |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 63 | // - The command line option. Tests use this option to disable installation |
| 64 | // of default apps in some cases. |
| 65 | // - If the locale is not compatible with the defaults, don't install them. |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 66 | // - The kDefaultApps preferences value in the profile. This value is |
| 67 | // usually set in the master_preferences file. |
| 68 | bool install_apps = |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 69 | profile_->GetPrefs()->GetString(prefs::kDefaultApps) == "install"; |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 70 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 71 | InstallState state = |
| 72 | static_cast<InstallState>(profile_->GetPrefs()->GetInteger( |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 73 | prefs::kDefaultAppsInstallState)); |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 74 | |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 75 | is_migration_ = (state == kProvideLegacyDefaultApps); |
| 76 | |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 77 | switch (state) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 78 | case kUnknown: { |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 79 | bool is_new_profile = profile_->WasCreatedByVersionOrLater( |
| 80 | version_info::GetVersionNumber()); |
David Roger | 8b4b8b56 | 2019-05-17 15:59:04 | [diff] [blame] | 81 | if (!is_new_profile) |
[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] | 9bf21b9 | 2012-10-04 21:01:39 | [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 | |
[email protected] | 910f72ce | 2012-08-24 01:38:35 | [diff] [blame] | 108 | // Default apps are only installed on profile creation or a new chrome |
| 109 | // download. |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 110 | if (state == kUnknown) { |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 111 | if (install_apps) { |
[email protected] | 9bf21b9 | 2012-10-04 21:01:39 | [diff] [blame] | 112 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 113 | kAlreadyInstalledDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 114 | } else { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 115 | profile_->GetPrefs()->SetInteger(prefs::kDefaultAppsInstallState, |
| 116 | kNeverInstallDefaultApps); |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 117 | } |
[email protected] | 86c6b9e3 | 2011-10-25 17:09:10 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return install_apps; |
| 121 | } |
| 122 | |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 123 | Provider::Provider(Profile* profile, |
| 124 | VisitorInterface* service, |
Matthew Denton | ef83a62 | 2019-08-30 02:07:00 | [diff] [blame] | 125 | scoped_refptr<extensions::ExternalLoader> loader, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 126 | extensions::Manifest::Location crx_location, |
| 127 | extensions::Manifest::Location download_location, |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 128 | int creation_flags) |
Matthew Denton | ef83a62 | 2019-08-30 02:07:00 | [diff] [blame] | 129 | : extensions::ExternalProviderImpl(service, |
| 130 | std::move(loader), |
| 131 | profile, |
| 132 | crx_location, |
| 133 | download_location, |
| 134 | creation_flags), |
[email protected] | e9a30af | 2012-10-04 01:56:25 | [diff] [blame] | 135 | profile_(profile), |
| 136 | is_migration_(false) { |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 137 | DCHECK(profile); |
[email protected] | 47fc70c | 2011-12-06 07:29:51 | [diff] [blame] | 138 | set_auto_acknowledge(true); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void Provider::VisitRegisteredExtension() { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 142 | if (!profile_ || !ShouldInstallInProfile()) { |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 143 | SetPrefs(std::make_unique<base::DictionaryValue>()); |
[email protected] | d190cef | 2011-11-09 02:09:24 | [diff] [blame] | 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 | |
Istiaque Ahmed | a7431b3 | 2017-08-20 18:33:37 | [diff] [blame] | 150 | void Provider::SetPrefs(std::unique_ptr<base::DictionaryValue> prefs) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 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. |
jdoerrie | 13cd648c8 | 2018-10-02 21:21:02 | [diff] [blame] | 158 | for (auto it = new_default_apps.begin(); it != new_default_apps.end(); |
| 159 | ++it) { |
[email protected] | 937cf9d | 2012-08-30 03:51:55 | [diff] [blame] | 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 | |
Istiaque Ahmed | a7431b3 | 2017-08-20 18:33:37 | [diff] [blame] | 164 | ExternalProviderImpl::SetPrefs(std::move(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 |