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