[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 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/extension_sync_service.h" |
| 6 | |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
treib | b794dd5 | 2015-12-01 18:47:14 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 11 | #include "chrome/browser/extensions/bookmark_app_helper.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_sync_data.h" |
| 14 | #include "chrome/browser/extensions/extension_sync_service_factory.h" |
| 15 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | a9f74a6 | 2014-01-10 06:48:36 | [diff] [blame] | 16 | #include "chrome/browser/extensions/launch_util.h" |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 17 | #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 18 | #include "chrome/browser/profiles/profile.h" |
| 19 | #include "chrome/browser/sync/glue/sync_start_util.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 21 | #include "chrome/common/extensions/sync_helper.h" |
brettw | 9e85ef4 | 2016-11-01 21:01:24 | [diff] [blame] | 22 | #include "chrome/common/features.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 23 | #include "chrome/common/web_application_info.h" |
skym | 7160384 | 2016-10-10 18:17:31 | [diff] [blame] | 24 | #include "components/sync/model/sync_change.h" |
| 25 | #include "components/sync/model/sync_error_factory.h" |
[email protected] | 7447404 | 2013-11-21 12:03:54 | [diff] [blame] | 26 | #include "extensions/browser/app_sorting.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 27 | #include "extensions/browser/extension_prefs.h" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 28 | #include "extensions/browser/extension_registry.h" |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 29 | #include "extensions/browser/extension_system.h" |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 30 | #include "extensions/browser/extension_util.h" |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 31 | #include "extensions/browser/uninstall_reason.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 32 | #include "extensions/common/extension.h" |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 33 | #include "extensions/common/extension_set.h" |
benwells | d4b64a7 | 2014-12-11 12:38:27 | [diff] [blame] | 34 | #include "extensions/common/image_util.h" |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 35 | #include "extensions/common/permissions/permission_message_provider.h" |
| 36 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 37 | |
brettw | 9e85ef4 | 2016-11-01 21:01:24 | [diff] [blame] | 38 | #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
treib | 40d3ad9 | 2015-10-20 18:15:42 | [diff] [blame] | 39 | #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 40 | #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
| 41 | #endif |
| 42 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 43 | using extensions::AppSorting; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 44 | using extensions::Extension; |
| 45 | using extensions::ExtensionPrefs; |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 46 | using extensions::ExtensionRegistry; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 47 | using extensions::ExtensionSet; |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 48 | using extensions::ExtensionSyncData; |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 49 | using extensions::ExtensionSystem; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 50 | using extensions::SyncBundle; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 51 | |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 52 | namespace { |
| 53 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 54 | // Returns the pref value for "all urls enabled" for the given extension id. |
| 55 | ExtensionSyncData::OptionalBoolean GetAllowedOnAllUrlsOptionalBoolean( |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 56 | const Extension& extension, |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 57 | content::BrowserContext* context) { |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 58 | extensions::ScriptingPermissionsModifier permissions_modifier(context, |
| 59 | &extension); |
| 60 | bool allowed_on_all_urls = permissions_modifier.IsAllowedOnAllUrls(); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 61 | // If the extension is not allowed on all urls (which is not the default), |
| 62 | // then we have to sync the preference. |
| 63 | if (!allowed_on_all_urls) |
| 64 | return ExtensionSyncData::BOOLEAN_FALSE; |
| 65 | |
| 66 | // If the user has explicitly set a value, then we sync it. |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 67 | if (permissions_modifier.HasSetAllowedOnAllUrls()) |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 68 | return ExtensionSyncData::BOOLEAN_TRUE; |
| 69 | |
| 70 | // Otherwise, unset. |
| 71 | return ExtensionSyncData::BOOLEAN_UNSET; |
| 72 | } |
| 73 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 74 | // Returns true if the sync type of |extension| matches |type|. |
| 75 | bool IsCorrectSyncType(const Extension& extension, syncer::ModelType type) { |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 76 | return (type == syncer::EXTENSIONS && extension.is_extension()) || |
| 77 | (type == syncer::APPS && extension.is_app()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 78 | } |
| 79 | |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 80 | // Predicate for PendingExtensionManager. |
| 81 | // TODO(treib,devlin): The !is_theme check shouldn't be necessary anymore after |
| 82 | // all the bad data from crbug.com/558299 has been cleaned up, after M52 or so. |
| 83 | bool ShouldAllowInstall(const Extension* extension) { |
| 84 | return !extension->is_theme() && |
| 85 | extensions::sync_helper::IsSyncable(extension); |
| 86 | } |
| 87 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 88 | syncer::SyncDataList ToSyncerSyncDataList( |
| 89 | const std::vector<ExtensionSyncData>& data) { |
| 90 | syncer::SyncDataList result; |
| 91 | result.reserve(data.size()); |
| 92 | for (const ExtensionSyncData& item : data) |
| 93 | result.push_back(item.GetSyncData()); |
| 94 | return result; |
| 95 | } |
| 96 | |
Karan Bhatia | 2a11723 | 2017-08-23 00:24:56 | [diff] [blame] | 97 | static_assert(extensions::disable_reason::DISABLE_REASON_LAST == (1LL << 17), |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 98 | "Please consider whether your new disable reason should be" |
| 99 | " syncable, and if so update this bitmask accordingly!"); |
| 100 | const int kKnownSyncableDisableReasons = |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 101 | extensions::disable_reason::DISABLE_USER_ACTION | |
| 102 | extensions::disable_reason::DISABLE_PERMISSIONS_INCREASE | |
| 103 | extensions::disable_reason::DISABLE_SIDELOAD_WIPEOUT | |
| 104 | extensions::disable_reason::DISABLE_GREYLIST | |
| 105 | extensions::disable_reason::DISABLE_REMOTE_INSTALL; |
| 106 | const int kAllKnownDisableReasons = |
| 107 | extensions::disable_reason::DISABLE_REASON_LAST - 1; |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 108 | // We also include any future bits for newer clients that added another disable |
| 109 | // reason. |
| 110 | const int kSyncableDisableReasons = |
| 111 | kKnownSyncableDisableReasons | ~kAllKnownDisableReasons; |
| 112 | |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 113 | } // namespace |
| 114 | |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 115 | struct ExtensionSyncService::PendingUpdate { |
| 116 | PendingUpdate() : grant_permissions_and_reenable(false) {} |
| 117 | PendingUpdate(const base::Version& version, |
| 118 | bool grant_permissions_and_reenable) |
| 119 | : version(version), |
| 120 | grant_permissions_and_reenable(grant_permissions_and_reenable) {} |
| 121 | |
| 122 | base::Version version; |
| 123 | bool grant_permissions_and_reenable; |
| 124 | }; |
| 125 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 126 | ExtensionSyncService::ExtensionSyncService(Profile* profile) |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 127 | : profile_(profile), |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 128 | registry_observer_(this), |
| 129 | prefs_observer_(this), |
treib | b794dd5 | 2015-12-01 18:47:14 | [diff] [blame] | 130 | ignore_updates_(false), |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 131 | flare_(sync_start_util::GetFlareForSyncableService(profile->GetPath())) { |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 132 | registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 133 | prefs_observer_.Add(ExtensionPrefs::Get(profile_)); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 134 | } |
| 135 | |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 136 | ExtensionSyncService::~ExtensionSyncService() { |
| 137 | } |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 138 | |
| 139 | // static |
macourteau | 86b29d8 | 2015-01-23 13:24:45 | [diff] [blame] | 140 | ExtensionSyncService* ExtensionSyncService::Get( |
| 141 | content::BrowserContext* context) { |
| 142 | return ExtensionSyncServiceFactory::GetForBrowserContext(context); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 143 | } |
| 144 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 145 | void ExtensionSyncService::SyncExtensionChangeIfNeeded( |
| 146 | const Extension& extension) { |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 147 | if (ignore_updates_ || !ShouldSync(extension)) |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 148 | return; |
| 149 | |
| 150 | syncer::ModelType type = |
| 151 | extension.is_app() ? syncer::APPS : syncer::EXTENSIONS; |
| 152 | SyncBundle* bundle = GetSyncBundle(type); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 153 | if (bundle->IsSyncing()) { |
| 154 | bundle->PushSyncAddOrUpdate(extension.id(), |
| 155 | CreateSyncData(extension).GetSyncData()); |
| 156 | DCHECK(!ExtensionPrefs::Get(profile_)->NeedsSync(extension.id())); |
| 157 | } else { |
| 158 | ExtensionPrefs::Get(profile_)->SetNeedsSync(extension.id(), true); |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 159 | if (extension_service()->is_ready() && !flare_.is_null()) |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 160 | flare_.Run(type); // Tell sync to start ASAP. |
| 161 | } |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 162 | } |
| 163 | |
treib | 40d3ad9 | 2015-10-20 18:15:42 | [diff] [blame] | 164 | bool ExtensionSyncService::HasPendingReenable( |
| 165 | const std::string& id, |
| 166 | const base::Version& version) const { |
| 167 | auto it = pending_updates_.find(id); |
| 168 | if (it == pending_updates_.end()) |
| 169 | return false; |
| 170 | const PendingUpdate& pending = it->second; |
robpercival | dcd8b10 | 2016-01-25 19:39:00 | [diff] [blame] | 171 | return pending.version == version && |
treib | 40d3ad9 | 2015-10-20 18:15:42 | [diff] [blame] | 172 | pending.grant_permissions_and_reenable; |
| 173 | } |
| 174 | |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 175 | syncer::SyncMergeResult ExtensionSyncService::MergeDataAndStartSyncing( |
| 176 | syncer::ModelType type, |
| 177 | const syncer::SyncDataList& initial_sync_data, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 178 | std::unique_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 179 | std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) { |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 180 | CHECK(sync_processor.get()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 181 | LOG_IF(FATAL, type != syncer::EXTENSIONS && type != syncer::APPS) |
| 182 | << "Got " << type << " ModelType"; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 183 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 184 | SyncBundle* bundle = GetSyncBundle(type); |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 185 | bundle->StartSyncing(std::move(sync_processor)); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 186 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 187 | // Apply the initial sync data, filtering out any items where we have more |
| 188 | // recent local changes. Also tell the SyncBundle the extension IDs. |
| 189 | for (const syncer::SyncData& sync_data : initial_sync_data) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 190 | std::unique_ptr<ExtensionSyncData> extension_sync_data( |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 191 | ExtensionSyncData::CreateFromSyncData(sync_data)); |
| 192 | // If the extension has local state that needs to be synced, ignore this |
| 193 | // change (we assume the local state is more recent). |
| 194 | if (extension_sync_data && |
| 195 | !ExtensionPrefs::Get(profile_)->NeedsSync(extension_sync_data->id())) { |
| 196 | ApplySyncData(*extension_sync_data); |
| 197 | } |
| 198 | } |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 199 | |
treib | 0feb828 | 2016-01-18 10:18:15 | [diff] [blame] | 200 | // Now push the local state to sync. |
| 201 | // Note: We'd like to only send out changes for extensions which have |
| 202 | // NeedsSync set. However, we can't tell if our changes ever made it to the |
| 203 | // sync server (they might not e.g. when there's a temporary auth error), so |
| 204 | // we couldn't safely clear the flag. So just send out everything and let the |
| 205 | // sync client handle no-op changes. |
| 206 | std::vector<ExtensionSyncData> data_list = GetLocalSyncDataList(type); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 207 | bundle->PushSyncDataList(ToSyncerSyncDataList(data_list)); |
treib | 0feb828 | 2016-01-18 10:18:15 | [diff] [blame] | 208 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 209 | for (const ExtensionSyncData& data : data_list) |
| 210 | ExtensionPrefs::Get(profile_)->SetNeedsSync(data.id(), false); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 211 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 212 | if (type == syncer::APPS) |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 213 | ExtensionSystem::Get(profile_)->app_sorting()->FixNTPOrdinalCollisions(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 214 | |
| 215 | return syncer::SyncMergeResult(type); |
| 216 | } |
| 217 | |
| 218 | void ExtensionSyncService::StopSyncing(syncer::ModelType type) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 219 | GetSyncBundle(type)->Reset(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | syncer::SyncDataList ExtensionSyncService::GetAllSyncData( |
| 223 | syncer::ModelType type) const { |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 224 | const SyncBundle* bundle = GetSyncBundle(type); |
| 225 | if (!bundle->IsSyncing()) |
| 226 | return syncer::SyncDataList(); |
| 227 | |
treib | 0feb828 | 2016-01-18 10:18:15 | [diff] [blame] | 228 | std::vector<ExtensionSyncData> sync_data_list = GetLocalSyncDataList(type); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 229 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 230 | // Add pending data (where the local extension is not installed yet). |
| 231 | std::vector<ExtensionSyncData> pending_extensions = |
| 232 | bundle->GetPendingExtensionData(); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 233 | sync_data_list.insert(sync_data_list.begin(), |
| 234 | pending_extensions.begin(), |
| 235 | pending_extensions.end()); |
| 236 | |
| 237 | return ToSyncerSyncDataList(sync_data_list); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | syncer::SyncError ExtensionSyncService::ProcessSyncChanges( |
Brett Wilson | e1a7042 | 2017-09-12 05:10:09 | [diff] [blame] | 241 | const base::Location& from_here, |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 242 | const syncer::SyncChangeList& change_list) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 243 | for (const syncer::SyncChange& sync_change : change_list) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 244 | std::unique_ptr<ExtensionSyncData> extension_sync_data( |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 245 | ExtensionSyncData::CreateFromSyncChange(sync_change)); |
| 246 | if (extension_sync_data) |
| 247 | ApplySyncData(*extension_sync_data); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 248 | } |
| 249 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 250 | ExtensionSystem::Get(profile_)->app_sorting()->FixNTPOrdinalCollisions(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 251 | |
| 252 | return syncer::SyncError(); |
| 253 | } |
| 254 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 255 | ExtensionSyncData ExtensionSyncService::CreateSyncData( |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 256 | const Extension& extension) const { |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 257 | const std::string& id = extension.id(); |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 258 | const ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 259 | int disable_reasons = |
| 260 | extension_prefs->GetDisableReasons(id) & kSyncableDisableReasons; |
| 261 | // Note that we're ignoring the enabled state during ApplySyncData (we check |
| 262 | // for the existence of disable reasons instead), we're just setting it here |
| 263 | // for older Chrome versions (<M48). |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 264 | bool enabled = (disable_reasons == extensions::disable_reason::DISABLE_NONE); |
proberge | 901ecab | 2017-08-31 19:24:28 | [diff] [blame] | 265 | if (extension_prefs->GetExtensionBlacklistState(extension.id()) == |
| 266 | extensions::BLACKLISTED_MALWARE) { |
| 267 | enabled = false; |
| 268 | NOTREACHED() << "Blacklisted extensions should not be getting synced."; |
| 269 | } |
| 270 | |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 271 | bool incognito_enabled = extensions::util::IsIncognitoEnabled(id, profile_); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 272 | bool remote_install = extension_prefs->HasDisableReason( |
| 273 | id, extensions::disable_reason::DISABLE_REMOTE_INSTALL); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 274 | ExtensionSyncData::OptionalBoolean allowed_on_all_url = |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 275 | GetAllowedOnAllUrlsOptionalBoolean(extension, profile_); |
mamir | 192d788 | 2016-06-22 17:10:16 | [diff] [blame] | 276 | bool installed_by_custodian = |
| 277 | extensions::util::WasInstalledByCustodian(id, profile_); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 278 | AppSorting* app_sorting = ExtensionSystem::Get(profile_)->app_sorting(); |
| 279 | |
| 280 | ExtensionSyncData result = extension.is_app() |
| 281 | ? ExtensionSyncData( |
| 282 | extension, enabled, disable_reasons, incognito_enabled, |
| 283 | remote_install, allowed_on_all_url, |
mamir | 192d788 | 2016-06-22 17:10:16 | [diff] [blame] | 284 | installed_by_custodian, |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 285 | app_sorting->GetAppLaunchOrdinal(id), |
| 286 | app_sorting->GetPageOrdinal(id), |
| 287 | extensions::GetLaunchTypePrefValue(extension_prefs, id)) |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 288 | : ExtensionSyncData( |
| 289 | extension, enabled, disable_reasons, incognito_enabled, |
mamir | 192d788 | 2016-06-22 17:10:16 | [diff] [blame] | 290 | remote_install, allowed_on_all_url, installed_by_custodian); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 291 | |
| 292 | // If there's a pending update, send the new version to sync instead of the |
| 293 | // installed one. |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 294 | auto it = pending_updates_.find(id); |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 295 | if (it != pending_updates_.end()) { |
| 296 | const base::Version& version = it->second.version; |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 297 | // If we have a pending version, it should be newer than the installed one. |
Devlin Cronin | 03bf2d2 | 2017-12-20 08:21:05 | [diff] [blame^] | 298 | DCHECK_EQ(-1, extension.version().CompareTo(version)); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 299 | result.set_version(version); |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 300 | // If we'll re-enable the extension once it's updated, also send that back |
| 301 | // to sync. |
| 302 | if (it->second.grant_permissions_and_reenable) |
| 303 | result.set_enabled(true); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 304 | } |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 305 | return result; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 306 | } |
| 307 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 308 | void ExtensionSyncService::ApplySyncData( |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 309 | const ExtensionSyncData& extension_sync_data) { |
rdevlin.cronin | 2f1ed4c | 2017-06-13 16:22:13 | [diff] [blame] | 310 | const std::string& id = extension_sync_data.id(); |
| 311 | // Note: |extension| may be null if it hasn't been installed yet. |
| 312 | const Extension* extension = |
| 313 | ExtensionRegistry::Get(profile_)->GetInstalledExtension(id); |
| 314 | // If there is an existing extension that shouldn't be sync'd, don't |
| 315 | // apply this sync data. This can happen if the local version of an |
| 316 | // extension is default-installed, but the sync server has data from another |
| 317 | // (non-default-installed) installation. We can't apply the sync data because |
| 318 | // it would always override the local state (which would never get sync'd). |
| 319 | // See crbug.com/731824. |
| 320 | if (extension && !ShouldSync(*extension)) |
| 321 | return; |
| 322 | |
treib | b794dd5 | 2015-12-01 18:47:14 | [diff] [blame] | 323 | // Ignore any pref change notifications etc. while we're applying incoming |
| 324 | // sync data, so that we don't end up notifying ourselves. |
| 325 | base::AutoReset<bool> ignore_updates(&ignore_updates_, true); |
| 326 | |
mamir | 192d788 | 2016-06-22 17:10:16 | [diff] [blame] | 327 | // Note: this may cause an existing version of the extension to be reloaded. |
| 328 | extensions::util::SetWasInstalledByCustodian( |
| 329 | extension_sync_data.id(), profile_, |
| 330 | extension_sync_data.installed_by_custodian()); |
| 331 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 332 | syncer::ModelType type = extension_sync_data.is_app() ? syncer::APPS |
| 333 | : syncer::EXTENSIONS; |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 334 | SyncBundle* bundle = GetSyncBundle(type); |
asargent | e48ab75 | 2016-03-12 00:59:20 | [diff] [blame] | 335 | DCHECK(bundle->IsSyncing()); |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 336 | if (extension && !IsCorrectSyncType(*extension, type)) { |
asargent | e48ab75 | 2016-03-12 00:59:20 | [diff] [blame] | 337 | // The installed item isn't the same type as the sync data item, so we need |
| 338 | // to remove the sync data item; otherwise it will be a zombie that will |
| 339 | // keep coming back even if the installed item with this id is uninstalled. |
| 340 | // First tell the bundle about the extension, so that it won't just ignore |
| 341 | // the deletion, then push the deletion. |
| 342 | bundle->ApplySyncData(extension_sync_data); |
| 343 | bundle->PushSyncDeletion(id, extension_sync_data.GetSyncData()); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 344 | return; |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 345 | } |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 346 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 347 | // Forward to the bundle. This will just update the list of synced extensions. |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 348 | bundle->ApplySyncData(extension_sync_data); |
| 349 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 350 | // Handle uninstalls first. |
| 351 | if (extension_sync_data.uninstalled()) { |
Devlin Cronin | 6fd1cd6 | 2017-12-05 19:13:57 | [diff] [blame] | 352 | base::string16 error; |
| 353 | bool uninstalled = true; |
| 354 | if (!extension) { |
| 355 | error = base::ASCIIToUTF16("Unknown extension"); |
| 356 | uninstalled = false; |
| 357 | } else { |
| 358 | uninstalled = extension_service()->UninstallExtension( |
| 359 | id, extensions::UNINSTALL_REASON_SYNC, &error); |
| 360 | } |
| 361 | |
| 362 | if (!uninstalled) { |
| 363 | LOG(WARNING) << "Failed to uninstall extension with id '" << id |
| 364 | << "' from sync: " << error; |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 365 | } |
| 366 | return; |
| 367 | } |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 368 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 369 | // Extension from sync was uninstalled by the user as an external extension. |
| 370 | // Honor user choice and skip installation/enabling. |
| 371 | // TODO(treib): Should we still apply pref changes? |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 372 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
| 373 | if (extension_prefs->IsExternalExtensionUninstalled(id)) { |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 374 | LOG(WARNING) << "Extension with id " << id |
| 375 | << " from sync was uninstalled as external extension"; |
| 376 | return; |
| 377 | } |
| 378 | |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 379 | enum { |
| 380 | NOT_INSTALLED, |
| 381 | INSTALLED_OUTDATED, |
| 382 | INSTALLED_MATCHING, |
| 383 | INSTALLED_NEWER, |
| 384 | } state = NOT_INSTALLED; |
| 385 | if (extension) { |
Devlin Cronin | 03bf2d2 | 2017-12-20 08:21:05 | [diff] [blame^] | 386 | switch (extension->version().CompareTo(extension_sync_data.version())) { |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 387 | case -1: state = INSTALLED_OUTDATED; break; |
| 388 | case 0: state = INSTALLED_MATCHING; break; |
| 389 | case 1: state = INSTALLED_NEWER; break; |
| 390 | default: NOTREACHED(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Figure out the resulting set of disable reasons. |
| 395 | int disable_reasons = extension_prefs->GetDisableReasons(id); |
| 396 | |
| 397 | // Chrome versions M37-M44 used |extension_sync_data.remote_install()| to tag |
| 398 | // not-yet-approved remote installs. It's redundant now that disable reasons |
| 399 | // are synced (DISABLE_REMOTE_INSTALL should be among them already), but some |
| 400 | // old sync data may still be around, and it doesn't hurt to add the reason. |
| 401 | // TODO(treib,devlin): Deprecate and eventually remove |remote_install|? |
| 402 | if (extension_sync_data.remote_install()) |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 403 | disable_reasons |= extensions::disable_reason::DISABLE_REMOTE_INSTALL; |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 404 | |
| 405 | // Add/remove disable reasons based on the incoming sync data. |
| 406 | int incoming_disable_reasons = extension_sync_data.disable_reasons(); |
| 407 | if (!!incoming_disable_reasons == extension_sync_data.enabled()) { |
| 408 | // The enabled flag disagrees with the presence of disable reasons. This |
treib | b794dd5 | 2015-12-01 18:47:14 | [diff] [blame] | 409 | // must either come from an old (<M45) client which doesn't sync disable |
| 410 | // reasons, or the extension is blacklisted (which doesn't have a |
| 411 | // corresponding disable reason). |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 412 | // Update |disable_reasons| based on the enabled flag. |
| 413 | if (extension_sync_data.enabled()) |
| 414 | disable_reasons &= ~kKnownSyncableDisableReasons; |
| 415 | else // Assume the extension was likely disabled by the user. |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 416 | disable_reasons |= extensions::disable_reason::DISABLE_USER_ACTION; |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 417 | } else { |
| 418 | // Replace the syncable disable reasons: |
| 419 | // 1. Remove any syncable disable reasons we might have. |
| 420 | disable_reasons &= ~kSyncableDisableReasons; |
| 421 | // 2. Add the incoming reasons. Mask with |kSyncableDisableReasons|, because |
| 422 | // Chrome M45-M47 also wrote local disable reasons to sync, and we don't |
| 423 | // want those. |
| 424 | disable_reasons |= incoming_disable_reasons & kSyncableDisableReasons; |
| 425 | } |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 426 | |
| 427 | // Enable/disable the extension. |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 428 | bool should_be_enabled = |
| 429 | (disable_reasons == extensions::disable_reason::DISABLE_NONE); |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 430 | bool reenable_after_update = false; |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 431 | if (should_be_enabled && !extension_service()->IsExtensionEnabled(id)) { |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 432 | if (extension) { |
| 433 | // Only grant permissions if the sync data explicitly sets the disable |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 434 | // reasons to extensions::disable_reason::DISABLE_NONE (as opposed to the |
| 435 | // legacy |
| 436 | // (<M45) case where they're not set at all), and if the version from sync |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 437 | // matches our local one. |
| 438 | bool grant_permissions = extension_sync_data.supports_disable_reasons() && |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 439 | (state == INSTALLED_MATCHING); |
| 440 | if (grant_permissions) |
| 441 | extension_service()->GrantPermissions(extension); |
| 442 | |
| 443 | // Only enable if the extension has all required permissions. |
| 444 | // (Even if the version doesn't match - if the new version needs more |
| 445 | // permissions, it'll get disabled after the update.) |
| 446 | bool has_all_permissions = |
| 447 | grant_permissions || |
| 448 | !extensions::PermissionMessageProvider::Get()->IsPrivilegeIncrease( |
| 449 | *extension_prefs->GetGrantedPermissions(id), |
| 450 | extension->permissions_data()->active_permissions(), |
| 451 | extension->GetType()); |
| 452 | if (has_all_permissions) |
| 453 | extension_service()->EnableExtension(id); |
| 454 | else if (extension_sync_data.supports_disable_reasons()) |
| 455 | reenable_after_update = true; |
treib | 40d3ad9 | 2015-10-20 18:15:42 | [diff] [blame] | 456 | |
brettw | 9e85ef4 | 2016-11-01 21:01:24 | [diff] [blame] | 457 | #if BUILDFLAG(ENABLE_SUPERVISED_USERS) |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 458 | if (!has_all_permissions && (state == INSTALLED_NEWER) && |
| 459 | extensions::util::IsExtensionSupervised(extension, profile_)) { |
| 460 | SupervisedUserServiceFactory::GetForProfile(profile_) |
Devlin Cronin | 03bf2d2 | 2017-12-20 08:21:05 | [diff] [blame^] | 461 | ->AddExtensionUpdateRequest(id, extension->version()); |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 462 | } |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 463 | #endif |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 464 | } else { |
| 465 | // The extension is not installed yet. Set it to enabled; we'll check for |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 466 | // permission increase (more accurately, for a version change) when it's |
| 467 | // actually installed. |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 468 | extension_service()->EnableExtension(id); |
treib | 8668a30 | 2015-10-05 16:21:53 | [diff] [blame] | 469 | } |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 470 | } else if (!should_be_enabled) { |
| 471 | // Note that |disable_reasons| includes any pre-existing reasons that |
| 472 | // weren't explicitly removed above. |
| 473 | if (extension_service()->IsExtensionEnabled(id)) |
| 474 | extension_service()->DisableExtension(id, disable_reasons); |
| 475 | else // Already disabled, just replace the disable reasons. |
| 476 | extension_prefs->ReplaceDisableReasons(id, disable_reasons); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 477 | } |
| 478 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 479 | // Update the incognito flag. |
| 480 | extensions::util::SetIsIncognitoEnabled( |
| 481 | id, profile_, extension_sync_data.incognito_enabled()); |
| 482 | extension = nullptr; // No longer safe to use. |
| 483 | |
| 484 | // Update the all urls flag. |
| 485 | if (extension_sync_data.all_urls_enabled() != |
| 486 | ExtensionSyncData::BOOLEAN_UNSET) { |
| 487 | bool allowed = extension_sync_data.all_urls_enabled() == |
rdevlin.cronin | d01837b | 2016-08-17 01:37:18 | [diff] [blame] | 488 | ExtensionSyncData::BOOLEAN_TRUE; |
| 489 | extensions::ScriptingPermissionsModifier::SetAllowedOnAllUrlsForSync( |
| 490 | allowed, profile_, id); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Set app-specific data. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 494 | if (extension_sync_data.is_app()) { |
| 495 | if (extension_sync_data.app_launch_ordinal().IsValid() && |
| 496 | extension_sync_data.page_ordinal().IsValid()) { |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 497 | AppSorting* app_sorting = ExtensionSystem::Get(profile_)->app_sorting(); |
| 498 | app_sorting->SetAppLaunchOrdinal( |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 499 | id, |
| 500 | extension_sync_data.app_launch_ordinal()); |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 501 | app_sorting->SetPageOrdinal(id, extension_sync_data.page_ordinal()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 502 | } |
| 503 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 504 | // The corresponding validation of this value during ExtensionSyncData |
| 505 | // population is in ExtensionSyncData::ToAppSpecifics. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 506 | if (extension_sync_data.launch_type() >= extensions::LAUNCH_TYPE_FIRST && |
| 507 | extension_sync_data.launch_type() < extensions::NUM_LAUNCH_TYPES) { |
| 508 | extensions::SetLaunchType( |
| 509 | profile_, id, extension_sync_data.launch_type()); |
| 510 | } |
| 511 | |
Giovanni Ortuño Urquidi | b7d775d | 2017-07-18 03:20:16 | [diff] [blame] | 512 | if (!extension_sync_data.bookmark_app_url().empty()) { |
| 513 | // Handles creating and updating the bookmark app. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 514 | ApplyBookmarkAppSyncData(extension_sync_data); |
Giovanni Ortuño Urquidi | b7d775d | 2017-07-18 03:20:16 | [diff] [blame] | 515 | return; |
| 516 | } |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 517 | } |
| 518 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 519 | // Finally, trigger installation/update as required. |
| 520 | bool check_for_updates = false; |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 521 | if (state == INSTALLED_OUTDATED) { |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 522 | // If the extension is installed but outdated, store the new version. |
treib | 29e1b9b1 | 2015-11-11 08:50:56 | [diff] [blame] | 523 | pending_updates_[id] = |
| 524 | PendingUpdate(extension_sync_data.version(), reenable_after_update); |
| 525 | check_for_updates = true; |
| 526 | } else if (state == NOT_INSTALLED) { |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 527 | if (!extension_service()->pending_extension_manager()->AddFromSync( |
| 528 | id, |
| 529 | extension_sync_data.update_url(), |
treib | e960e28 | 2015-09-11 10:38:08 | [diff] [blame] | 530 | extension_sync_data.version(), |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 531 | ShouldAllowInstall, |
mamir | 192d788 | 2016-06-22 17:10:16 | [diff] [blame] | 532 | extension_sync_data.remote_install())) { |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 533 | LOG(WARNING) << "Could not add pending extension for " << id; |
| 534 | // This means that the extension is already pending installation, with a |
| 535 | // non-INTERNAL location. Add to pending_sync_data, even though it will |
| 536 | // never be removed (we'll never install a syncable version of the |
| 537 | // extension), so that GetAllSyncData() continues to send it. |
| 538 | } |
| 539 | // Track pending extensions so that we can return them in GetAllSyncData(). |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 540 | bundle->AddPendingExtensionData(extension_sync_data); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 541 | check_for_updates = true; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 542 | } |
| 543 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 544 | if (check_for_updates) |
| 545 | extension_service()->CheckForUpdatesSoon(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 546 | } |
| 547 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 548 | void ExtensionSyncService::ApplyBookmarkAppSyncData( |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 549 | const ExtensionSyncData& extension_sync_data) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 550 | DCHECK(extension_sync_data.is_app()); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 551 | |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 552 | // Process bookmark app sync if necessary. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 553 | GURL bookmark_app_url(extension_sync_data.bookmark_app_url()); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 554 | if (!bookmark_app_url.is_valid() || |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 555 | extension_sync_data.uninstalled()) { |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 559 | const Extension* extension = |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 560 | extension_service()->GetInstalledExtension(extension_sync_data.id()); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 561 | |
| 562 | // Return if there are no bookmark app details that need updating. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 563 | if (extension && |
| 564 | extension->non_localized_name() == extension_sync_data.name() && |
| 565 | extension->description() == |
| 566 | extension_sync_data.bookmark_app_description()) { |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 567 | return; |
| 568 | } |
| 569 | |
| 570 | WebApplicationInfo web_app_info; |
| 571 | web_app_info.app_url = bookmark_app_url; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 572 | web_app_info.title = base::UTF8ToUTF16(extension_sync_data.name()); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 573 | web_app_info.description = |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 574 | base::UTF8ToUTF16(extension_sync_data.bookmark_app_description()); |
Giovanni Ortuño Urquidi | e7e79d45 | 2017-08-03 10:16:15 | [diff] [blame] | 575 | web_app_info.scope = GURL(extension_sync_data.bookmark_app_scope()); |
Christopher Lam | cec8c4f | 2017-10-16 01:38:43 | [diff] [blame] | 576 | web_app_info.theme_color = extension_sync_data.bookmark_app_theme_color(); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 577 | if (!extension_sync_data.bookmark_app_icon_color().empty()) { |
limasdf | 3aa01fa | 2015-11-01 19:39:14 | [diff] [blame] | 578 | extensions::image_util::ParseHexColorString( |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 579 | extension_sync_data.bookmark_app_icon_color(), |
benwells | d4b64a7 | 2014-12-11 12:38:27 | [diff] [blame] | 580 | &web_app_info.generated_icon_color); |
| 581 | } |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 582 | for (const auto& icon : extension_sync_data.linked_icons()) { |
benwells | ed75d1b5 | 2015-04-29 02:39:19 | [diff] [blame] | 583 | WebApplicationInfo::IconInfo icon_info; |
| 584 | icon_info.url = icon.url; |
| 585 | icon_info.width = icon.size; |
| 586 | icon_info.height = icon.size; |
| 587 | web_app_info.icons.push_back(icon_info); |
| 588 | } |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 589 | |
Christopher Lam | 780f0e1 | 2017-10-09 11:01:16 | [diff] [blame] | 590 | CreateOrUpdateBookmarkApp(extension_service(), &web_app_info); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 591 | } |
| 592 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 593 | void ExtensionSyncService::SetSyncStartFlareForTesting( |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 594 | const syncer::SyncableService::StartSyncFlare& flare) { |
| 595 | flare_ = flare; |
| 596 | } |
| 597 | |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 598 | void ExtensionSyncService::DeleteThemeDoNotUse(const Extension& theme) { |
| 599 | DCHECK(theme.is_theme()); |
| 600 | GetSyncBundle(syncer::EXTENSIONS)->PushSyncDeletion( |
| 601 | theme.id(), CreateSyncData(theme).GetSyncData()); |
| 602 | } |
| 603 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 604 | ExtensionService* ExtensionSyncService::extension_service() const { |
| 605 | return ExtensionSystem::Get(profile_)->extension_service(); |
| 606 | } |
| 607 | |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 608 | void ExtensionSyncService::OnExtensionInstalled( |
| 609 | content::BrowserContext* browser_context, |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 610 | const Extension* extension, |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 611 | bool is_update) { |
| 612 | DCHECK_EQ(profile_, browser_context); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 613 | // Clear pending version if the installed one has caught up. |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 614 | auto it = pending_updates_.find(extension->id()); |
| 615 | if (it != pending_updates_.end()) { |
Devlin Cronin | 03bf2d2 | 2017-12-20 08:21:05 | [diff] [blame^] | 616 | int compare_result = extension->version().CompareTo(it->second.version); |
asargent | e48ab75 | 2016-03-12 00:59:20 | [diff] [blame] | 617 | if (compare_result == 0 && it->second.grant_permissions_and_reenable) { |
| 618 | // The call to SyncExtensionChangeIfNeeded below will take care of syncing |
| 619 | // changes to this extension, so we don't want to trigger sync activity |
| 620 | // from the call to GrantPermissionsAndEnableExtension. |
| 621 | base::AutoReset<bool> ignore_updates(&ignore_updates_, true); |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 622 | extension_service()->GrantPermissionsAndEnableExtension(extension); |
asargent | e48ab75 | 2016-03-12 00:59:20 | [diff] [blame] | 623 | } |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 624 | if (compare_result >= 0) |
| 625 | pending_updates_.erase(it); |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 626 | } |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 627 | SyncExtensionChangeIfNeeded(*extension); |
| 628 | } |
| 629 | |
| 630 | void ExtensionSyncService::OnExtensionUninstalled( |
| 631 | content::BrowserContext* browser_context, |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 632 | const Extension* extension, |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 633 | extensions::UninstallReason reason) { |
| 634 | DCHECK_EQ(profile_, browser_context); |
| 635 | // Don't bother syncing if the extension will be re-installed momentarily. |
| 636 | if (reason == extensions::UNINSTALL_REASON_REINSTALL || |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 637 | !ShouldSync(*extension)) { |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 638 | return; |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 639 | } |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 640 | |
| 641 | // TODO(tim): If we get here and IsSyncing is false, this will cause |
| 642 | // "back from the dead" style bugs, because sync will add-back the extension |
| 643 | // that was uninstalled here when MergeDataAndStartSyncing is called. |
| 644 | // See crbug.com/256795. |
| 645 | // Possible fix: Set NeedsSync here, then in MergeDataAndStartSyncing, if |
| 646 | // NeedsSync is set but the extension isn't installed, send a sync deletion. |
treib | b794dd5 | 2015-12-01 18:47:14 | [diff] [blame] | 647 | if (!ignore_updates_) { |
| 648 | syncer::ModelType type = |
| 649 | extension->is_app() ? syncer::APPS : syncer::EXTENSIONS; |
| 650 | SyncBundle* bundle = GetSyncBundle(type); |
| 651 | if (bundle->IsSyncing()) { |
| 652 | bundle->PushSyncDeletion(extension->id(), |
| 653 | CreateSyncData(*extension).GetSyncData()); |
| 654 | } else if (extension_service()->is_ready() && !flare_.is_null()) { |
| 655 | flare_.Run(type); // Tell sync to start ASAP. |
| 656 | } |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 657 | } |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 658 | |
treib | 95e800c | 2015-10-13 15:48:31 | [diff] [blame] | 659 | pending_updates_.erase(extension->id()); |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | void ExtensionSyncService::OnExtensionStateChanged( |
| 663 | const std::string& extension_id, |
| 664 | bool state) { |
| 665 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 666 | const Extension* extension = registry->GetInstalledExtension(extension_id); |
| 667 | // We can get pref change notifications for extensions that aren't installed |
| 668 | // (yet). In that case, we'll pick up the change later via ExtensionRegistry |
| 669 | // observation (in OnExtensionInstalled). |
| 670 | if (extension) |
| 671 | SyncExtensionChangeIfNeeded(*extension); |
| 672 | } |
| 673 | |
| 674 | void ExtensionSyncService::OnExtensionDisableReasonsChanged( |
| 675 | const std::string& extension_id, |
| 676 | int disabled_reasons) { |
| 677 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 678 | const Extension* extension = registry->GetInstalledExtension(extension_id); |
| 679 | // We can get pref change notifications for extensions that aren't installed |
| 680 | // (yet). In that case, we'll pick up the change later via ExtensionRegistry |
| 681 | // observation (in OnExtensionInstalled). |
| 682 | if (extension) |
| 683 | SyncExtensionChangeIfNeeded(*extension); |
| 684 | } |
| 685 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 686 | SyncBundle* ExtensionSyncService::GetSyncBundle(syncer::ModelType type) { |
| 687 | return const_cast<SyncBundle*>( |
| 688 | const_cast<const ExtensionSyncService&>(*this).GetSyncBundle(type)); |
| 689 | } |
| 690 | |
| 691 | const SyncBundle* ExtensionSyncService::GetSyncBundle( |
| 692 | syncer::ModelType type) const { |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 693 | return (type == syncer::APPS) ? &app_sync_bundle_ : &extension_sync_bundle_; |
| 694 | } |
| 695 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 696 | std::vector<ExtensionSyncData> ExtensionSyncService::GetLocalSyncDataList( |
treib | 0feb828 | 2016-01-18 10:18:15 | [diff] [blame] | 697 | syncer::ModelType type) const { |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 698 | // Collect the local state. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 699 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 700 | std::vector<ExtensionSyncData> data; |
| 701 | // TODO(treib, kalman): Should we be including blacklisted/blocked extensions |
| 702 | // here? I.e. just calling registry->GeneratedInstalledExtensionsSet()? |
| 703 | // It would be more consistent, but the danger is that the black/blocklist |
| 704 | // hasn't been updated on all clients by the time sync has kicked in - |
| 705 | // so it's safest not to. Take care to add any other extension lists here |
| 706 | // in the future if they are added. |
treib | 0feb828 | 2016-01-18 10:18:15 | [diff] [blame] | 707 | FillSyncDataList(registry->enabled_extensions(), type, &data); |
| 708 | FillSyncDataList(registry->disabled_extensions(), type, &data); |
| 709 | FillSyncDataList(registry->terminated_extensions(), type, &data); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 710 | return data; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | void ExtensionSyncService::FillSyncDataList( |
| 714 | const ExtensionSet& extensions, |
| 715 | syncer::ModelType type, |
| 716 | std::vector<ExtensionSyncData>* sync_data_list) const { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 717 | for (const scoped_refptr<const Extension>& extension : extensions) { |
treib | 0feb828 | 2016-01-18 10:18:15 | [diff] [blame] | 718 | if (IsCorrectSyncType(*extension, type) && ShouldSync(*extension)) { |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 719 | // We should never have pending data for an installed extension. |
| 720 | DCHECK(!GetSyncBundle(type)->HasPendingExtensionData(extension->id())); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 721 | sync_data_list->push_back(CreateSyncData(*extension)); |
| 722 | } |
| 723 | } |
| 724 | } |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 725 | |
| 726 | bool ExtensionSyncService::ShouldSync(const Extension& extension) const { |
| 727 | // Themes are handled by the ThemeSyncableService. |
| 728 | return extensions::util::ShouldSync(&extension, profile_) && |
| 729 | !extension.is_theme(); |
| 730 | } |