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