[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 | |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 7 | #include "base/basictypes.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 9 | #include "chrome/browser/extensions/bookmark_app_helper.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_sync_data.h" |
| 12 | #include "chrome/browser/extensions/extension_sync_service_factory.h" |
| 13 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | a9f74a6 | 2014-01-10 06:48:36 | [diff] [blame] | 14 | #include "chrome/browser/extensions/launch_util.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 15 | #include "chrome/browser/profiles/profile.h" |
| 16 | #include "chrome/browser/sync/glue/sync_start_util.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 17 | #include "chrome/common/extensions/extension_constants.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 18 | #include "chrome/common/extensions/sync_helper.h" |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 19 | #include "chrome/common/web_application_info.h" |
[email protected] | 34f5405 | 2014-03-20 21:33:40 | [diff] [blame] | 20 | #include "components/sync_driver/sync_prefs.h" |
[email protected] | 7447404 | 2013-11-21 12:03:54 | [diff] [blame] | 21 | #include "extensions/browser/app_sorting.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 22 | #include "extensions/browser/extension_prefs.h" |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 23 | #include "extensions/browser/extension_registry.h" |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 24 | #include "extensions/browser/extension_system.h" |
[email protected] | 411f8ae | 2014-05-22 11:12:23 | [diff] [blame] | 25 | #include "extensions/browser/extension_util.h" |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 26 | #include "extensions/browser/uninstall_reason.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 27 | #include "extensions/common/extension.h" |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 28 | #include "extensions/common/extension_set.h" |
benwells | d4b64a7 | 2014-12-11 12:38:27 | [diff] [blame] | 29 | #include "extensions/common/image_util.h" |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 30 | #include "sync/api/sync_change.h" |
| 31 | #include "sync/api/sync_error_factory.h" |
| 32 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 33 | using extensions::AppSorting; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 34 | using extensions::Extension; |
| 35 | using extensions::ExtensionPrefs; |
[email protected] | ca97594 | 2014-01-07 12:06:47 | [diff] [blame] | 36 | using extensions::ExtensionRegistry; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 37 | using extensions::ExtensionSet; |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 38 | using extensions::ExtensionSyncData; |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 39 | using extensions::ExtensionSystem; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 40 | using extensions::SyncBundle; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 41 | |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 42 | namespace { |
| 43 | |
| 44 | void OnWebApplicationInfoLoaded( |
| 45 | WebApplicationInfo synced_info, |
| 46 | base::WeakPtr<ExtensionService> extension_service, |
| 47 | const WebApplicationInfo& loaded_info) { |
| 48 | DCHECK_EQ(synced_info.app_url, loaded_info.app_url); |
| 49 | |
| 50 | if (!extension_service) |
| 51 | return; |
| 52 | |
| 53 | // Use the old icons if they exist. |
| 54 | synced_info.icons = loaded_info.icons; |
benwells | d4b64a7 | 2014-12-11 12:38:27 | [diff] [blame] | 55 | CreateOrUpdateBookmarkApp(extension_service.get(), &synced_info); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 56 | } |
| 57 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 58 | // Returns the pref value for "all urls enabled" for the given extension id. |
| 59 | ExtensionSyncData::OptionalBoolean GetAllowedOnAllUrlsOptionalBoolean( |
| 60 | const std::string& extension_id, |
| 61 | content::BrowserContext* context) { |
| 62 | bool allowed_on_all_urls = |
| 63 | extensions::util::AllowedScriptingOnAllUrls(extension_id, context); |
| 64 | // If the extension is not allowed on all urls (which is not the default), |
| 65 | // then we have to sync the preference. |
| 66 | if (!allowed_on_all_urls) |
| 67 | return ExtensionSyncData::BOOLEAN_FALSE; |
| 68 | |
| 69 | // If the user has explicitly set a value, then we sync it. |
| 70 | if (extensions::util::HasSetAllowedScriptingOnAllUrls(extension_id, context)) |
| 71 | return ExtensionSyncData::BOOLEAN_TRUE; |
| 72 | |
| 73 | // Otherwise, unset. |
| 74 | return ExtensionSyncData::BOOLEAN_UNSET; |
| 75 | } |
| 76 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 77 | // Returns true if the sync type of |extension| matches |type|. |
| 78 | bool IsCorrectSyncType(const Extension& extension, syncer::ModelType type) { |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 79 | return (type == syncer::EXTENSIONS && extension.is_extension()) || |
| 80 | (type == syncer::APPS && extension.is_app()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 81 | } |
| 82 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 83 | syncer::SyncDataList ToSyncerSyncDataList( |
| 84 | const std::vector<ExtensionSyncData>& data) { |
| 85 | syncer::SyncDataList result; |
| 86 | result.reserve(data.size()); |
| 87 | for (const ExtensionSyncData& item : data) |
| 88 | result.push_back(item.GetSyncData()); |
| 89 | return result; |
| 90 | } |
| 91 | |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 92 | } // namespace |
| 93 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 94 | ExtensionSyncService::ExtensionSyncService(Profile* profile) |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 95 | : profile_(profile), |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame^] | 96 | registry_observer_(this), |
| 97 | prefs_observer_(this), |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 98 | flare_(sync_start_util::GetFlareForSyncableService(profile->GetPath())) { |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame^] | 99 | registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 100 | prefs_observer_.Add(ExtensionPrefs::Get(profile_)); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 101 | } |
| 102 | |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame^] | 103 | ExtensionSyncService::~ExtensionSyncService() { |
| 104 | } |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 105 | |
| 106 | // static |
macourteau | 86b29d8 | 2015-01-23 13:24:45 | [diff] [blame] | 107 | ExtensionSyncService* ExtensionSyncService::Get( |
| 108 | content::BrowserContext* context) { |
| 109 | return ExtensionSyncServiceFactory::GetForBrowserContext(context); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 110 | } |
| 111 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 112 | void ExtensionSyncService::SyncExtensionChangeIfNeeded( |
| 113 | const Extension& extension) { |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 114 | if (!extensions::util::ShouldSync(&extension, profile_)) |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 115 | return; |
| 116 | |
| 117 | syncer::ModelType type = |
| 118 | extension.is_app() ? syncer::APPS : syncer::EXTENSIONS; |
| 119 | SyncBundle* bundle = GetSyncBundle(type); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 120 | if (bundle->IsSyncing()) { |
| 121 | bundle->PushSyncAddOrUpdate(extension.id(), |
| 122 | CreateSyncData(extension).GetSyncData()); |
| 123 | DCHECK(!ExtensionPrefs::Get(profile_)->NeedsSync(extension.id())); |
| 124 | } else { |
| 125 | ExtensionPrefs::Get(profile_)->SetNeedsSync(extension.id(), true); |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 126 | if (extension_service()->is_ready() && !flare_.is_null()) |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 127 | flare_.Run(type); // Tell sync to start ASAP. |
| 128 | } |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 131 | syncer::SyncMergeResult ExtensionSyncService::MergeDataAndStartSyncing( |
| 132 | syncer::ModelType type, |
| 133 | const syncer::SyncDataList& initial_sync_data, |
| 134 | scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
| 135 | scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) { |
| 136 | CHECK(sync_processor.get()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 137 | LOG_IF(FATAL, type != syncer::EXTENSIONS && type != syncer::APPS) |
| 138 | << "Got " << type << " ModelType"; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 139 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 140 | SyncBundle* bundle = GetSyncBundle(type); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 141 | bundle->StartSyncing(sync_processor.Pass()); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 142 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 143 | // Apply the initial sync data, filtering out any items where we have more |
| 144 | // recent local changes. Also tell the SyncBundle the extension IDs. |
| 145 | for (const syncer::SyncData& sync_data : initial_sync_data) { |
| 146 | scoped_ptr<ExtensionSyncData> extension_sync_data( |
| 147 | ExtensionSyncData::CreateFromSyncData(sync_data)); |
| 148 | // If the extension has local state that needs to be synced, ignore this |
| 149 | // change (we assume the local state is more recent). |
| 150 | if (extension_sync_data && |
| 151 | !ExtensionPrefs::Get(profile_)->NeedsSync(extension_sync_data->id())) { |
| 152 | ApplySyncData(*extension_sync_data); |
| 153 | } |
| 154 | } |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 155 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 156 | // Now push those local changes to sync. |
| 157 | // TODO(treib,kalman): We should only have to send out changes for extensions |
| 158 | // which have NeedsSync set (i.e. |GetLocalSyncDataList(type, false)|). That |
| 159 | // makes some sync_integration_tests fail though - figure out why and fix it! |
| 160 | std::vector<ExtensionSyncData> data_list = GetLocalSyncDataList(type, true); |
| 161 | bundle->PushSyncDataList(ToSyncerSyncDataList(data_list)); |
| 162 | for (const ExtensionSyncData& data : data_list) |
| 163 | ExtensionPrefs::Get(profile_)->SetNeedsSync(data.id(), false); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 164 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 165 | if (type == syncer::APPS) |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 166 | ExtensionSystem::Get(profile_)->app_sorting()->FixNTPOrdinalCollisions(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 167 | |
| 168 | return syncer::SyncMergeResult(type); |
| 169 | } |
| 170 | |
| 171 | void ExtensionSyncService::StopSyncing(syncer::ModelType type) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 172 | GetSyncBundle(type)->Reset(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | syncer::SyncDataList ExtensionSyncService::GetAllSyncData( |
| 176 | syncer::ModelType type) const { |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 177 | const SyncBundle* bundle = GetSyncBundle(type); |
| 178 | if (!bundle->IsSyncing()) |
| 179 | return syncer::SyncDataList(); |
| 180 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 181 | std::vector<ExtensionSyncData> sync_data_list = |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 182 | GetLocalSyncDataList(type, true); |
| 183 | |
| 184 | // Add pending data (where the local extension is either not installed yet or |
| 185 | // outdated). |
| 186 | std::vector<ExtensionSyncData> pending_extensions = bundle->GetPendingData(); |
| 187 | sync_data_list.insert(sync_data_list.begin(), |
| 188 | pending_extensions.begin(), |
| 189 | pending_extensions.end()); |
| 190 | |
| 191 | return ToSyncerSyncDataList(sync_data_list); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | syncer::SyncError ExtensionSyncService::ProcessSyncChanges( |
| 195 | const tracked_objects::Location& from_here, |
| 196 | const syncer::SyncChangeList& change_list) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 197 | for (const syncer::SyncChange& sync_change : change_list) { |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 198 | scoped_ptr<ExtensionSyncData> extension_sync_data( |
| 199 | ExtensionSyncData::CreateFromSyncChange(sync_change)); |
| 200 | if (extension_sync_data) |
| 201 | ApplySyncData(*extension_sync_data); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 202 | } |
| 203 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 204 | ExtensionSystem::Get(profile_)->app_sorting()->FixNTPOrdinalCollisions(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 205 | |
| 206 | return syncer::SyncError(); |
| 207 | } |
| 208 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 209 | ExtensionSyncData ExtensionSyncService::CreateSyncData( |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 210 | const Extension& extension) const { |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 211 | const ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile_); |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame^] | 212 | // Query the enabled state from ExtensionPrefs rather than |
| 213 | // ExtensionService::IsExtensionEnabled - the latter uses ExtensionRegistry |
| 214 | // which might not have been updated yet (ExtensionPrefs are updated first, |
| 215 | // and we're listening for changes to these). |
| 216 | bool enabled = !extension_prefs->IsExtensionDisabled(extension.id()); |
| 217 | enabled = enabled && |
| 218 | !extension_prefs->IsExternalExtensionUninstalled(extension.id()); |
| 219 | // Blacklisted extensions are not marked as disabled in ExtensionPrefs. |
| 220 | enabled = enabled && |
| 221 | extension_prefs->GetExtensionBlacklistState(extension.id()) == |
| 222 | extensions::NOT_BLACKLISTED; |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 223 | int disable_reasons = extension_prefs->GetDisableReasons(extension.id()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 224 | bool incognito_enabled = extensions::util::IsIncognitoEnabled(extension.id(), |
| 225 | profile_); |
| 226 | bool remote_install = |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 227 | extension_prefs->HasDisableReason(extension.id(), |
| 228 | Extension::DISABLE_REMOTE_INSTALL); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 229 | ExtensionSyncData::OptionalBoolean allowed_on_all_url = |
| 230 | GetAllowedOnAllUrlsOptionalBoolean(extension.id(), profile_); |
| 231 | if (extension.is_app()) { |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 232 | AppSorting* app_sorting = ExtensionSystem::Get(profile_)->app_sorting(); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 233 | return ExtensionSyncData( |
| 234 | extension, enabled, disable_reasons, incognito_enabled, remote_install, |
| 235 | allowed_on_all_url, |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 236 | app_sorting->GetAppLaunchOrdinal(extension.id()), |
| 237 | app_sorting->GetPageOrdinal(extension.id()), |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 238 | extensions::GetLaunchTypePrefValue(extension_prefs, extension.id())); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 239 | } |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 240 | return ExtensionSyncData( |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 241 | extension, enabled, disable_reasons, incognito_enabled, remote_install, |
| 242 | allowed_on_all_url); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 243 | } |
| 244 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 245 | bool ExtensionSyncService::ApplySyncData( |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 246 | const ExtensionSyncData& extension_sync_data) { |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 247 | syncer::ModelType type = extension_sync_data.is_app() ? syncer::APPS |
| 248 | : syncer::EXTENSIONS; |
| 249 | SyncBundle* bundle = GetSyncBundle(type); |
| 250 | bundle->ApplySyncData(extension_sync_data); |
| 251 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 252 | const std::string& id = extension_sync_data.id(); |
| 253 | |
| 254 | if (extension_sync_data.is_app()) { |
| 255 | if (extension_sync_data.app_launch_ordinal().IsValid() && |
| 256 | extension_sync_data.page_ordinal().IsValid()) { |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 257 | AppSorting* app_sorting = ExtensionSystem::Get(profile_)->app_sorting(); |
| 258 | app_sorting->SetAppLaunchOrdinal( |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 259 | id, |
| 260 | extension_sync_data.app_launch_ordinal()); |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 261 | app_sorting->SetPageOrdinal(id, extension_sync_data.page_ordinal()); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 262 | } |
| 263 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 264 | // The corresponding validation of this value during ExtensionSyncData |
| 265 | // population is in ExtensionSyncData::ToAppSpecifics. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 266 | if (extension_sync_data.launch_type() >= extensions::LAUNCH_TYPE_FIRST && |
| 267 | extension_sync_data.launch_type() < extensions::NUM_LAUNCH_TYPES) { |
| 268 | extensions::SetLaunchType( |
| 269 | profile_, id, extension_sync_data.launch_type()); |
| 270 | } |
| 271 | |
| 272 | if (!extension_sync_data.bookmark_app_url().empty()) |
| 273 | ApplyBookmarkAppSyncData(extension_sync_data); |
| 274 | } |
| 275 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 276 | if (!ApplyExtensionSyncDataHelper(extension_sync_data, type)) { |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 277 | bundle->AddPendingExtension(id, extension_sync_data); |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 278 | extension_service()->CheckForUpdatesSoon(); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | |
| 282 | return true; |
| 283 | } |
| 284 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 285 | void ExtensionSyncService::ApplyBookmarkAppSyncData( |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 286 | const ExtensionSyncData& extension_sync_data) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 287 | DCHECK(extension_sync_data.is_app()); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 288 | |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 289 | // Process bookmark app sync if necessary. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 290 | GURL bookmark_app_url(extension_sync_data.bookmark_app_url()); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 291 | if (!bookmark_app_url.is_valid() || |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 292 | extension_sync_data.uninstalled()) { |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 296 | const Extension* extension = |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 297 | extension_service()->GetInstalledExtension(extension_sync_data.id()); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 298 | |
| 299 | // Return if there are no bookmark app details that need updating. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 300 | if (extension && |
| 301 | extension->non_localized_name() == extension_sync_data.name() && |
| 302 | extension->description() == |
| 303 | extension_sync_data.bookmark_app_description()) { |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
| 307 | WebApplicationInfo web_app_info; |
| 308 | web_app_info.app_url = bookmark_app_url; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 309 | web_app_info.title = base::UTF8ToUTF16(extension_sync_data.name()); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 310 | web_app_info.description = |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 311 | base::UTF8ToUTF16(extension_sync_data.bookmark_app_description()); |
| 312 | if (!extension_sync_data.bookmark_app_icon_color().empty()) { |
benwells | d4b64a7 | 2014-12-11 12:38:27 | [diff] [blame] | 313 | extensions::image_util::ParseCSSColorString( |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 314 | extension_sync_data.bookmark_app_icon_color(), |
benwells | d4b64a7 | 2014-12-11 12:38:27 | [diff] [blame] | 315 | &web_app_info.generated_icon_color); |
| 316 | } |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 317 | for (const auto& icon : extension_sync_data.linked_icons()) { |
benwells | ed75d1b5 | 2015-04-29 02:39:19 | [diff] [blame] | 318 | WebApplicationInfo::IconInfo icon_info; |
| 319 | icon_info.url = icon.url; |
| 320 | icon_info.width = icon.size; |
| 321 | icon_info.height = icon.size; |
| 322 | web_app_info.icons.push_back(icon_info); |
| 323 | } |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 324 | |
| 325 | // If the bookmark app already exists, keep the old icons. |
| 326 | if (!extension) { |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 327 | CreateOrUpdateBookmarkApp(extension_service(), &web_app_info); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 328 | } else { |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 329 | GetWebApplicationInfoFromApp(profile_, |
| 330 | extension, |
| 331 | base::Bind(&OnWebApplicationInfoLoaded, |
| 332 | web_app_info, |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 333 | extension_service()->AsWeakPtr())); |
[email protected] | d93fcd513 | 2014-04-24 08:42:45 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 337 | void ExtensionSyncService::SetSyncStartFlareForTesting( |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 338 | const syncer::SyncableService::StartSyncFlare& flare) { |
| 339 | flare_ = flare; |
| 340 | } |
| 341 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 342 | ExtensionService* ExtensionSyncService::extension_service() const { |
| 343 | return ExtensionSystem::Get(profile_)->extension_service(); |
| 344 | } |
| 345 | |
treib | 8a6d989 | 2015-08-26 10:23:19 | [diff] [blame^] | 346 | void ExtensionSyncService::OnExtensionInstalled( |
| 347 | content::BrowserContext* browser_context, |
| 348 | const extensions::Extension* extension, |
| 349 | bool is_update) { |
| 350 | DCHECK_EQ(profile_, browser_context); |
| 351 | SyncExtensionChangeIfNeeded(*extension); |
| 352 | } |
| 353 | |
| 354 | void ExtensionSyncService::OnExtensionUninstalled( |
| 355 | content::BrowserContext* browser_context, |
| 356 | const extensions::Extension* extension, |
| 357 | extensions::UninstallReason reason) { |
| 358 | DCHECK_EQ(profile_, browser_context); |
| 359 | // Don't bother syncing if the extension will be re-installed momentarily. |
| 360 | if (reason == extensions::UNINSTALL_REASON_REINSTALL || |
| 361 | !extensions::util::ShouldSync(extension, profile_)) |
| 362 | return; |
| 363 | |
| 364 | // TODO(tim): If we get here and IsSyncing is false, this will cause |
| 365 | // "back from the dead" style bugs, because sync will add-back the extension |
| 366 | // that was uninstalled here when MergeDataAndStartSyncing is called. |
| 367 | // See crbug.com/256795. |
| 368 | // Possible fix: Set NeedsSync here, then in MergeDataAndStartSyncing, if |
| 369 | // NeedsSync is set but the extension isn't installed, send a sync deletion. |
| 370 | syncer::ModelType type = |
| 371 | extension->is_app() ? syncer::APPS : syncer::EXTENSIONS; |
| 372 | SyncBundle* bundle = GetSyncBundle(type); |
| 373 | if (bundle->IsSyncing()) { |
| 374 | bundle->PushSyncDeletion(extension->id(), |
| 375 | CreateSyncData(*extension).GetSyncData()); |
| 376 | } else if (extension_service()->is_ready() && !flare_.is_null()) { |
| 377 | flare_.Run(type); // Tell sync to start ASAP. |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void ExtensionSyncService::OnExtensionStateChanged( |
| 382 | const std::string& extension_id, |
| 383 | bool state) { |
| 384 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 385 | const Extension* extension = registry->GetInstalledExtension(extension_id); |
| 386 | // We can get pref change notifications for extensions that aren't installed |
| 387 | // (yet). In that case, we'll pick up the change later via ExtensionRegistry |
| 388 | // observation (in OnExtensionInstalled). |
| 389 | if (extension) |
| 390 | SyncExtensionChangeIfNeeded(*extension); |
| 391 | } |
| 392 | |
| 393 | void ExtensionSyncService::OnExtensionDisableReasonsChanged( |
| 394 | const std::string& extension_id, |
| 395 | int disabled_reasons) { |
| 396 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 397 | const Extension* extension = registry->GetInstalledExtension(extension_id); |
| 398 | // We can get pref change notifications for extensions that aren't installed |
| 399 | // (yet). In that case, we'll pick up the change later via ExtensionRegistry |
| 400 | // observation (in OnExtensionInstalled). |
| 401 | if (extension) |
| 402 | SyncExtensionChangeIfNeeded(*extension); |
| 403 | } |
| 404 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 405 | SyncBundle* ExtensionSyncService::GetSyncBundle(syncer::ModelType type) { |
| 406 | return const_cast<SyncBundle*>( |
| 407 | const_cast<const ExtensionSyncService&>(*this).GetSyncBundle(type)); |
| 408 | } |
| 409 | |
| 410 | const SyncBundle* ExtensionSyncService::GetSyncBundle( |
| 411 | syncer::ModelType type) const { |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 412 | return (type == syncer::APPS) ? &app_sync_bundle_ : &extension_sync_bundle_; |
| 413 | } |
| 414 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 415 | std::vector<ExtensionSyncData> ExtensionSyncService::GetLocalSyncDataList( |
| 416 | syncer::ModelType type, |
| 417 | bool include_everything) const { |
| 418 | // Collect the local state. FillSyncDataList will filter out extensions for |
| 419 | // which we have pending data that should be used instead. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 420 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 421 | std::vector<ExtensionSyncData> data; |
| 422 | // TODO(treib, kalman): Should we be including blacklisted/blocked extensions |
| 423 | // here? I.e. just calling registry->GeneratedInstalledExtensionsSet()? |
| 424 | // It would be more consistent, but the danger is that the black/blocklist |
| 425 | // hasn't been updated on all clients by the time sync has kicked in - |
| 426 | // so it's safest not to. Take care to add any other extension lists here |
| 427 | // in the future if they are added. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 428 | FillSyncDataList( |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 429 | registry->enabled_extensions(), type, include_everything, &data); |
| 430 | FillSyncDataList( |
| 431 | registry->disabled_extensions(), type, include_everything, &data); |
| 432 | FillSyncDataList( |
| 433 | registry->terminated_extensions(), type, include_everything, &data); |
| 434 | return data; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void ExtensionSyncService::FillSyncDataList( |
| 438 | const ExtensionSet& extensions, |
| 439 | syncer::ModelType type, |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 440 | bool include_everything, |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 441 | std::vector<ExtensionSyncData>* sync_data_list) const { |
| 442 | const SyncBundle* bundle = GetSyncBundle(type); |
| 443 | for (const scoped_refptr<const Extension>& extension : extensions) { |
| 444 | if (IsCorrectSyncType(*extension, type) && |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 445 | extensions::util::ShouldSync(extension.get(), profile_) && |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 446 | !bundle->HasPendingExtensionId(extension->id()) && |
| 447 | (include_everything || |
| 448 | ExtensionPrefs::Get(profile_)->NeedsSync(extension->id()))) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 449 | sync_data_list->push_back(CreateSyncData(*extension)); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | bool ExtensionSyncService::ApplyExtensionSyncDataHelper( |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 455 | const ExtensionSyncData& extension_sync_data, |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 456 | syncer::ModelType type) { |
| 457 | const std::string& id = extension_sync_data.id(); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 458 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); |
| 459 | const Extension* extension = registry->GetInstalledExtension(id); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 460 | |
| 461 | // TODO(bolms): we should really handle this better. The particularly bad |
| 462 | // case is where an app becomes an extension or vice versa, and we end up with |
| 463 | // a zombie extension that won't go away. |
| 464 | if (extension && !IsCorrectSyncType(*extension, type)) |
| 465 | return true; |
| 466 | |
| 467 | // Handle uninstalls first. |
| 468 | if (extension_sync_data.uninstalled()) { |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 469 | if (!ExtensionService::UninstallExtensionHelper( |
| 470 | extension_service(), id, extensions::UNINSTALL_REASON_SYNC)) { |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 471 | LOG(WARNING) << "Could not uninstall extension " << id << " for sync"; |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 472 | } |
| 473 | return true; |
| 474 | } |
| 475 | |
| 476 | // Extension from sync was uninstalled by the user as external extensions. |
| 477 | // Honor user choice and skip installation/enabling. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 478 | if (ExtensionPrefs::Get(profile_)->IsExternalExtensionUninstalled(id)) { |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 479 | LOG(WARNING) << "Extension with id " << id |
| 480 | << " from sync was uninstalled as external extension"; |
| 481 | return true; |
| 482 | } |
| 483 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 484 | int version_compare_result = extension ? |
| 485 | extension->version()->CompareTo(extension_sync_data.version()) : 0; |
| 486 | |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 487 | // Set user settings. |
[email protected] | 658eae5 | 2014-06-14 20:28:05 | [diff] [blame] | 488 | if (extension_sync_data.enabled()) { |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 489 | DCHECK(!extension_sync_data.disable_reasons()); |
| 490 | |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 491 | // Only grant permissions if the sync data explicitly sets the disable |
| 492 | // reasons to Extension::DISABLE_NONE (as opposed to the legacy (<M45) case |
| 493 | // where they're not set at all), and if the version from sync matches our |
| 494 | // local one. Otherwise we just enable it without granting permissions. If |
| 495 | // any permissions are missing, CheckPermissionsIncrease will soon disable |
| 496 | // it again. |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 497 | bool grant_permissions = |
| 498 | extension_sync_data.supports_disable_reasons() && |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 499 | extension && (version_compare_result == 0); |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 500 | if (grant_permissions) |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 501 | extension_service()->GrantPermissionsAndEnableExtension(extension); |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 502 | else |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 503 | extension_service()->EnableExtension(id); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 504 | } else { |
treib | c119232 | 2015-05-20 12:56:07 | [diff] [blame] | 505 | int disable_reasons = extension_sync_data.disable_reasons(); |
[email protected] | 21db9ef | 2014-05-16 02:06:27 | [diff] [blame] | 506 | if (extension_sync_data.remote_install()) { |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 507 | if (!(disable_reasons & Extension::DISABLE_REMOTE_INSTALL)) { |
| 508 | // In the non-legacy case (>=M45) where disable reasons are synced at |
| 509 | // all, DISABLE_REMOTE_INSTALL should be among them already. |
| 510 | DCHECK(!extension_sync_data.supports_disable_reasons()); |
| 511 | disable_reasons |= Extension::DISABLE_REMOTE_INSTALL; |
| 512 | } |
| 513 | } else if (!extension_sync_data.supports_disable_reasons()) { |
treib | c119232 | 2015-05-20 12:56:07 | [diff] [blame] | 514 | // Legacy case (<M45), from before we synced disable reasons (see |
| 515 | // crbug.com/484214). |
| 516 | disable_reasons = Extension::DISABLE_UNKNOWN_FROM_SYNC; |
| 517 | } |
| 518 | |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 519 | // In the non-legacy case (>=M45), clear any existing disable reasons first. |
| 520 | // Otherwise sync can't remove just some of them. |
| 521 | if (extension_sync_data.supports_disable_reasons()) |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 522 | ExtensionPrefs::Get(profile_)->ClearDisableReasons(id); |
treib | 231f2bb | 2015-06-09 12:46:24 | [diff] [blame] | 523 | |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 524 | extension_service()->DisableExtension(id, disable_reasons); |
[email protected] | 21db9ef | 2014-05-16 02:06:27 | [diff] [blame] | 525 | } |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 526 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 527 | // We need to cache some information here because setting the incognito flag |
| 528 | // invalidates the |extension| pointer (it reloads the extension). |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 529 | bool extension_installed = (extension != NULL); |
[email protected] | 760f743b | 2014-05-28 13:52:02 | [diff] [blame] | 530 | |
| 531 | // If the target extension has already been installed ephemerally, it can |
| 532 | // be promoted to a regular installed extension and downloading from the Web |
| 533 | // Store is not necessary. |
| 534 | if (extension && extensions::util::IsEphemeralApp(id, profile_)) |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 535 | extension_service()->PromoteEphemeralApp(extension, true); |
[email protected] | 760f743b | 2014-05-28 13:52:02 | [diff] [blame] | 536 | |
| 537 | // Update the incognito flag. |
[email protected] | 1d5cf414 | 2014-01-24 18:25:22 | [diff] [blame] | 538 | extensions::util::SetIsIncognitoEnabled( |
| 539 | id, profile_, extension_sync_data.incognito_enabled()); |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 540 | extension = NULL; // No longer safe to use. |
| 541 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 542 | // Update the all urls flag. |
| 543 | if (extension_sync_data.all_urls_enabled() != |
| 544 | ExtensionSyncData::BOOLEAN_UNSET) { |
| 545 | bool allowed = extension_sync_data.all_urls_enabled() == |
| 546 | ExtensionSyncData::BOOLEAN_TRUE; |
| 547 | extensions::util::SetAllowedScriptingOnAllUrls(id, profile_, allowed); |
| 548 | } |
| 549 | |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 550 | if (extension_installed) { |
| 551 | // If the extension is already installed, check if it's outdated. |
[email protected] | 760f743b | 2014-05-28 13:52:02 | [diff] [blame] | 552 | if (version_compare_result < 0) { |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 553 | // Extension is outdated. |
| 554 | return false; |
| 555 | } |
| 556 | } else { |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 557 | CHECK(type == syncer::EXTENSIONS || type == syncer::APPS); |
treib | 35f9ed0 | 2015-08-10 10:38:44 | [diff] [blame] | 558 | if (!extension_service()->pending_extension_manager()->AddFromSync( |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 559 | id, |
| 560 | extension_sync_data.update_url(), |
treib | c644a1c | 2015-07-13 08:37:04 | [diff] [blame] | 561 | extensions::sync_helper::IsSyncable, |
[email protected] | 6338fa3 | 2014-07-16 21:41:59 | [diff] [blame] | 562 | extension_sync_data.remote_install(), |
| 563 | extension_sync_data.installed_by_custodian())) { |
[email protected] | f8aefb13 | 2013-10-30 09:29:52 | [diff] [blame] | 564 | LOG(WARNING) << "Could not add pending extension for " << id; |
| 565 | // This means that the extension is already pending installation, with a |
| 566 | // non-INTERNAL location. Add to pending_sync_data, even though it will |
| 567 | // never be removed (we'll never install a syncable version of the |
| 568 | // extension), so that GetAllSyncData() continues to send it. |
| 569 | } |
| 570 | // Track pending extensions so that we can return them in GetAllSyncData(). |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | return true; |
| 575 | } |