Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_sync_service.cc
blob905ab7b1be2b04a14555e149ef30830d0aa49179
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.
5 #include "chrome/browser/extensions/extension_sync_service.h"
7 #include "base/basictypes.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/threading/thread_restrictions.h"
10 #include "chrome/browser/extensions/bookmark_app_helper.h"
11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_sync_data.h"
13 #include "chrome/browser/extensions/extension_sync_service_factory.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/extensions/launch_util.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/sync/glue/sync_start_util.h"
18 #include "chrome/common/extensions/extension_constants.h"
19 #include "chrome/common/extensions/sync_helper.h"
20 #include "chrome/common/web_application_info.h"
21 #include "components/sync_driver/sync_prefs.h"
22 #include "extensions/browser/app_sorting.h"
23 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_registry.h"
25 #include "extensions/browser/extension_util.h"
26 #include "extensions/browser/uninstall_reason.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_set.h"
29 #include "extensions/common/image_util.h"
30 #include "sync/api/sync_change.h"
31 #include "sync/api/sync_error_factory.h"
33 using extensions::Extension;
34 using extensions::ExtensionPrefs;
35 using extensions::ExtensionRegistry;
36 using extensions::ExtensionSet;
37 using extensions::ExtensionSyncData;
38 using extensions::SyncBundle;
40 namespace {
42 void OnWebApplicationInfoLoaded(
43 WebApplicationInfo synced_info,
44 base::WeakPtr<ExtensionService> extension_service,
45 const WebApplicationInfo& loaded_info) {
46 DCHECK_EQ(synced_info.app_url, loaded_info.app_url);
48 if (!extension_service)
49 return;
51 // Use the old icons if they exist.
52 synced_info.icons = loaded_info.icons;
53 CreateOrUpdateBookmarkApp(extension_service.get(), &synced_info);
56 // Returns the pref value for "all urls enabled" for the given extension id.
57 ExtensionSyncData::OptionalBoolean GetAllowedOnAllUrlsOptionalBoolean(
58 const std::string& extension_id,
59 content::BrowserContext* context) {
60 bool allowed_on_all_urls =
61 extensions::util::AllowedScriptingOnAllUrls(extension_id, context);
62 // If the extension is not allowed on all urls (which is not the default),
63 // then we have to sync the preference.
64 if (!allowed_on_all_urls)
65 return ExtensionSyncData::BOOLEAN_FALSE;
67 // If the user has explicitly set a value, then we sync it.
68 if (extensions::util::HasSetAllowedScriptingOnAllUrls(extension_id, context))
69 return ExtensionSyncData::BOOLEAN_TRUE;
71 // Otherwise, unset.
72 return ExtensionSyncData::BOOLEAN_UNSET;
75 // Returns true if the sync type of |extension| matches |type|.
76 bool IsCorrectSyncType(const Extension& extension, syncer::ModelType type) {
77 return (type == syncer::EXTENSIONS && extension.is_extension()) ||
78 (type == syncer::APPS && extension.is_app());
81 syncer::SyncDataList ToSyncerSyncDataList(
82 const std::vector<ExtensionSyncData>& data) {
83 syncer::SyncDataList result;
84 result.reserve(data.size());
85 for (const ExtensionSyncData& item : data)
86 result.push_back(item.GetSyncData());
87 return result;
90 } // namespace
92 ExtensionSyncService::ExtensionSyncService(Profile* profile,
93 ExtensionPrefs* extension_prefs,
94 ExtensionService* extension_service)
95 : profile_(profile),
96 extension_prefs_(extension_prefs),
97 extension_service_(extension_service) {
98 SetSyncStartFlare(sync_start_util::GetFlareForSyncableService(
99 profile_->GetPath()));
102 ExtensionSyncService::~ExtensionSyncService() {}
104 // static
105 ExtensionSyncService* ExtensionSyncService::Get(
106 content::BrowserContext* context) {
107 return ExtensionSyncServiceFactory::GetForBrowserContext(context);
110 void ExtensionSyncService::SyncUninstallExtension(
111 const extensions::Extension& extension) {
112 if (!extensions::util::ShouldSync(&extension, profile_))
113 return;
115 // TODO(tim): If we get here and IsSyncing is false, this will cause
116 // "back from the dead" style bugs, because sync will add-back the extension
117 // that was uninstalled here when MergeDataAndStartSyncing is called.
118 // See crbug.com/256795.
119 syncer::ModelType type =
120 extension.is_app() ? syncer::APPS : syncer::EXTENSIONS;
121 SyncBundle* bundle = GetSyncBundle(type);
122 if (bundle->IsSyncing()) {
123 bundle->PushSyncDeletion(extension.id(),
124 CreateSyncData(extension).GetSyncData());
125 } else if (extension_service_->is_ready() && !flare_.is_null()) {
126 flare_.Run(type); // Tell sync to start ASAP.
130 void ExtensionSyncService::SyncExtensionChangeIfNeeded(
131 const Extension& extension) {
132 if (!extensions::util::ShouldSync(&extension, profile_))
133 return;
135 syncer::ModelType type =
136 extension.is_app() ? syncer::APPS : syncer::EXTENSIONS;
137 SyncBundle* bundle = GetSyncBundle(type);
138 if (bundle->IsSyncing()) {
139 bundle->PushSyncAddOrUpdate(extension.id(),
140 CreateSyncData(extension).GetSyncData());
141 DCHECK(!ExtensionPrefs::Get(profile_)->NeedsSync(extension.id()));
142 } else {
143 ExtensionPrefs::Get(profile_)->SetNeedsSync(extension.id(), true);
144 if (extension_service_->is_ready() && !flare_.is_null())
145 flare_.Run(type); // Tell sync to start ASAP.
149 syncer::SyncMergeResult ExtensionSyncService::MergeDataAndStartSyncing(
150 syncer::ModelType type,
151 const syncer::SyncDataList& initial_sync_data,
152 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
153 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) {
154 CHECK(sync_processor.get());
155 LOG_IF(FATAL, type != syncer::EXTENSIONS && type != syncer::APPS)
156 << "Got " << type << " ModelType";
158 SyncBundle* bundle = GetSyncBundle(type);
159 bundle->StartSyncing(sync_processor.Pass());
161 // Apply the initial sync data, filtering out any items where we have more
162 // recent local changes. Also tell the SyncBundle the extension IDs.
163 for (const syncer::SyncData& sync_data : initial_sync_data) {
164 scoped_ptr<ExtensionSyncData> extension_sync_data(
165 ExtensionSyncData::CreateFromSyncData(sync_data));
166 // If the extension has local state that needs to be synced, ignore this
167 // change (we assume the local state is more recent).
168 if (extension_sync_data &&
169 !ExtensionPrefs::Get(profile_)->NeedsSync(extension_sync_data->id())) {
170 ApplySyncData(*extension_sync_data);
174 // Now push those local changes to sync.
175 // TODO(treib,kalman): We should only have to send out changes for extensions
176 // which have NeedsSync set (i.e. |GetLocalSyncDataList(type, false)|). That
177 // makes some sync_integration_tests fail though - figure out why and fix it!
178 std::vector<ExtensionSyncData> data_list = GetLocalSyncDataList(type, true);
179 bundle->PushSyncDataList(ToSyncerSyncDataList(data_list));
180 for (const ExtensionSyncData& data : data_list)
181 ExtensionPrefs::Get(profile_)->SetNeedsSync(data.id(), false);
183 if (type == syncer::APPS)
184 extension_prefs_->app_sorting()->FixNTPOrdinalCollisions();
186 return syncer::SyncMergeResult(type);
189 void ExtensionSyncService::StopSyncing(syncer::ModelType type) {
190 GetSyncBundle(type)->Reset();
193 syncer::SyncDataList ExtensionSyncService::GetAllSyncData(
194 syncer::ModelType type) const {
195 const SyncBundle* bundle = GetSyncBundle(type);
196 if (!bundle->IsSyncing())
197 return syncer::SyncDataList();
199 std::vector<extensions::ExtensionSyncData> sync_data_list =
200 GetLocalSyncDataList(type, true);
202 // Add pending data (where the local extension is either not installed yet or
203 // outdated).
204 std::vector<ExtensionSyncData> pending_extensions = bundle->GetPendingData();
205 sync_data_list.insert(sync_data_list.begin(),
206 pending_extensions.begin(),
207 pending_extensions.end());
209 return ToSyncerSyncDataList(sync_data_list);
212 syncer::SyncError ExtensionSyncService::ProcessSyncChanges(
213 const tracked_objects::Location& from_here,
214 const syncer::SyncChangeList& change_list) {
215 for (const syncer::SyncChange& sync_change : change_list) {
216 scoped_ptr<ExtensionSyncData> extension_sync_data(
217 ExtensionSyncData::CreateFromSyncChange(sync_change));
218 if (extension_sync_data)
219 ApplySyncData(*extension_sync_data);
222 extension_prefs_->app_sorting()->FixNTPOrdinalCollisions();
224 return syncer::SyncError();
227 ExtensionSyncData ExtensionSyncService::CreateSyncData(
228 const extensions::Extension& extension) const {
229 bool enabled = extension_service_->IsExtensionEnabled(extension.id());
230 int disable_reasons = extension_prefs_->GetDisableReasons(extension.id());
231 bool incognito_enabled = extensions::util::IsIncognitoEnabled(extension.id(),
232 profile_);
233 bool remote_install =
234 extension_prefs_->HasDisableReason(extension.id(),
235 Extension::DISABLE_REMOTE_INSTALL);
236 ExtensionSyncData::OptionalBoolean allowed_on_all_url =
237 GetAllowedOnAllUrlsOptionalBoolean(extension.id(), profile_);
238 if (extension.is_app()) {
239 return ExtensionSyncData(
240 extension, enabled, disable_reasons, incognito_enabled, remote_install,
241 allowed_on_all_url,
242 extension_prefs_->app_sorting()->GetAppLaunchOrdinal(extension.id()),
243 extension_prefs_->app_sorting()->GetPageOrdinal(extension.id()),
244 extensions::GetLaunchTypePrefValue(extension_prefs_, extension.id()));
246 return ExtensionSyncData(
247 extension, enabled, disable_reasons, incognito_enabled, remote_install,
248 allowed_on_all_url);
251 bool ExtensionSyncService::ApplySyncData(
252 const ExtensionSyncData& extension_sync_data) {
253 syncer::ModelType type = extension_sync_data.is_app() ? syncer::APPS
254 : syncer::EXTENSIONS;
255 SyncBundle* bundle = GetSyncBundle(type);
256 bundle->ApplySyncData(extension_sync_data);
258 const std::string& id = extension_sync_data.id();
260 if (extension_sync_data.is_app()) {
261 if (extension_sync_data.app_launch_ordinal().IsValid() &&
262 extension_sync_data.page_ordinal().IsValid()) {
263 extension_prefs_->app_sorting()->SetAppLaunchOrdinal(
265 extension_sync_data.app_launch_ordinal());
266 extension_prefs_->app_sorting()->SetPageOrdinal(
268 extension_sync_data.page_ordinal());
271 // The corresponding validation of this value during ExtensionSyncData
272 // population is in ExtensionSyncData::ToAppSpecifics.
273 if (extension_sync_data.launch_type() >= extensions::LAUNCH_TYPE_FIRST &&
274 extension_sync_data.launch_type() < extensions::NUM_LAUNCH_TYPES) {
275 extensions::SetLaunchType(
276 profile_, id, extension_sync_data.launch_type());
279 if (!extension_sync_data.bookmark_app_url().empty())
280 ApplyBookmarkAppSyncData(extension_sync_data);
283 if (!ApplyExtensionSyncDataHelper(extension_sync_data, type)) {
284 bundle->AddPendingExtension(id, extension_sync_data);
285 extension_service_->CheckForUpdatesSoon();
286 return false;
289 return true;
292 void ExtensionSyncService::ApplyBookmarkAppSyncData(
293 const extensions::ExtensionSyncData& extension_sync_data) {
294 DCHECK(extension_sync_data.is_app());
296 // Process bookmark app sync if necessary.
297 GURL bookmark_app_url(extension_sync_data.bookmark_app_url());
298 if (!bookmark_app_url.is_valid() ||
299 extension_sync_data.uninstalled()) {
300 return;
303 const Extension* extension =
304 extension_service_->GetInstalledExtension(extension_sync_data.id());
306 // Return if there are no bookmark app details that need updating.
307 if (extension &&
308 extension->non_localized_name() == extension_sync_data.name() &&
309 extension->description() ==
310 extension_sync_data.bookmark_app_description()) {
311 return;
314 WebApplicationInfo web_app_info;
315 web_app_info.app_url = bookmark_app_url;
316 web_app_info.title = base::UTF8ToUTF16(extension_sync_data.name());
317 web_app_info.description =
318 base::UTF8ToUTF16(extension_sync_data.bookmark_app_description());
319 if (!extension_sync_data.bookmark_app_icon_color().empty()) {
320 extensions::image_util::ParseCSSColorString(
321 extension_sync_data.bookmark_app_icon_color(),
322 &web_app_info.generated_icon_color);
324 for (const auto& icon : extension_sync_data.linked_icons()) {
325 WebApplicationInfo::IconInfo icon_info;
326 icon_info.url = icon.url;
327 icon_info.width = icon.size;
328 icon_info.height = icon.size;
329 web_app_info.icons.push_back(icon_info);
332 // If the bookmark app already exists, keep the old icons.
333 if (!extension) {
334 CreateOrUpdateBookmarkApp(extension_service_, &web_app_info);
335 } else {
336 GetWebApplicationInfoFromApp(profile_,
337 extension,
338 base::Bind(&OnWebApplicationInfoLoaded,
339 web_app_info,
340 extension_service_->AsWeakPtr()));
344 void ExtensionSyncService::SetSyncStartFlare(
345 const syncer::SyncableService::StartSyncFlare& flare) {
346 flare_ = flare;
349 SyncBundle* ExtensionSyncService::GetSyncBundle(syncer::ModelType type) {
350 return const_cast<SyncBundle*>(
351 const_cast<const ExtensionSyncService&>(*this).GetSyncBundle(type));
354 const SyncBundle* ExtensionSyncService::GetSyncBundle(
355 syncer::ModelType type) const {
356 return (type == syncer::APPS) ? &app_sync_bundle_ : &extension_sync_bundle_;
359 std::vector<ExtensionSyncData> ExtensionSyncService::GetLocalSyncDataList(
360 syncer::ModelType type,
361 bool include_everything) const {
362 // Collect the local state. FillSyncDataList will filter out extensions for
363 // which we have pending data that should be used instead.
364 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
365 std::vector<ExtensionSyncData> data;
366 // TODO(treib, kalman): Should we be including blacklisted/blocked extensions
367 // here? I.e. just calling registry->GeneratedInstalledExtensionsSet()?
368 // It would be more consistent, but the danger is that the black/blocklist
369 // hasn't been updated on all clients by the time sync has kicked in -
370 // so it's safest not to. Take care to add any other extension lists here
371 // in the future if they are added.
372 FillSyncDataList(
373 registry->enabled_extensions(), type, include_everything, &data);
374 FillSyncDataList(
375 registry->disabled_extensions(), type, include_everything, &data);
376 FillSyncDataList(
377 registry->terminated_extensions(), type, include_everything, &data);
378 return data;
381 void ExtensionSyncService::FillSyncDataList(
382 const ExtensionSet& extensions,
383 syncer::ModelType type,
384 bool include_everything,
385 std::vector<ExtensionSyncData>* sync_data_list) const {
386 const SyncBundle* bundle = GetSyncBundle(type);
387 for (const scoped_refptr<const Extension>& extension : extensions) {
388 if (IsCorrectSyncType(*extension, type) &&
389 extensions::util::ShouldSync(extension.get(), profile_) &&
390 !bundle->HasPendingExtensionId(extension->id()) &&
391 (include_everything ||
392 ExtensionPrefs::Get(profile_)->NeedsSync(extension->id()))) {
393 sync_data_list->push_back(CreateSyncData(*extension));
398 bool ExtensionSyncService::ApplyExtensionSyncDataHelper(
399 const ExtensionSyncData& extension_sync_data,
400 syncer::ModelType type) {
401 const std::string& id = extension_sync_data.id();
402 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
403 const Extension* extension = registry->GetInstalledExtension(id);
405 // TODO(bolms): we should really handle this better. The particularly bad
406 // case is where an app becomes an extension or vice versa, and we end up with
407 // a zombie extension that won't go away.
408 if (extension && !IsCorrectSyncType(*extension, type))
409 return true;
411 // Handle uninstalls first.
412 if (extension_sync_data.uninstalled()) {
413 if (!extension_service_->UninstallExtensionHelper(
414 extension_service_, id, extensions::UNINSTALL_REASON_SYNC)) {
415 LOG(WARNING) << "Could not uninstall extension " << id << " for sync";
417 return true;
420 // Extension from sync was uninstalled by the user as external extensions.
421 // Honor user choice and skip installation/enabling.
422 if (ExtensionPrefs::Get(profile_)->IsExternalExtensionUninstalled(id)) {
423 LOG(WARNING) << "Extension with id " << id
424 << " from sync was uninstalled as external extension";
425 return true;
428 int version_compare_result = extension ?
429 extension->version()->CompareTo(extension_sync_data.version()) : 0;
431 // Set user settings.
432 if (extension_sync_data.enabled()) {
433 DCHECK(!extension_sync_data.disable_reasons());
435 // Only grant permissions if the sync data explicitly sets the disable
436 // reasons to Extension::DISABLE_NONE (as opposed to the legacy (<M45) case
437 // where they're not set at all), and if the version from sync matches our
438 // local one. Otherwise we just enable it without granting permissions. If
439 // any permissions are missing, CheckPermissionsIncrease will soon disable
440 // it again.
441 bool grant_permissions =
442 extension_sync_data.supports_disable_reasons() &&
443 extension && (version_compare_result == 0);
444 if (grant_permissions)
445 extension_service_->GrantPermissionsAndEnableExtension(extension);
446 else
447 extension_service_->EnableExtension(id);
448 } else {
449 int disable_reasons = extension_sync_data.disable_reasons();
450 if (extension_sync_data.remote_install()) {
451 if (!(disable_reasons & Extension::DISABLE_REMOTE_INSTALL)) {
452 // In the non-legacy case (>=M45) where disable reasons are synced at
453 // all, DISABLE_REMOTE_INSTALL should be among them already.
454 DCHECK(!extension_sync_data.supports_disable_reasons());
455 disable_reasons |= Extension::DISABLE_REMOTE_INSTALL;
457 } else if (!extension_sync_data.supports_disable_reasons()) {
458 // Legacy case (<M45), from before we synced disable reasons (see
459 // crbug.com/484214).
460 disable_reasons = Extension::DISABLE_UNKNOWN_FROM_SYNC;
463 // In the non-legacy case (>=M45), clear any existing disable reasons first.
464 // Otherwise sync can't remove just some of them.
465 if (extension_sync_data.supports_disable_reasons())
466 ExtensionPrefs::Get(profile_)->ClearDisableReasons(id);
468 extension_service_->DisableExtension(id, disable_reasons);
471 // We need to cache some information here because setting the incognito flag
472 // invalidates the |extension| pointer (it reloads the extension).
473 bool extension_installed = (extension != NULL);
475 // If the target extension has already been installed ephemerally, it can
476 // be promoted to a regular installed extension and downloading from the Web
477 // Store is not necessary.
478 if (extension && extensions::util::IsEphemeralApp(id, profile_))
479 extension_service_->PromoteEphemeralApp(extension, true);
481 // Update the incognito flag.
482 extensions::util::SetIsIncognitoEnabled(
483 id, profile_, extension_sync_data.incognito_enabled());
484 extension = NULL; // No longer safe to use.
486 // Update the all urls flag.
487 if (extension_sync_data.all_urls_enabled() !=
488 ExtensionSyncData::BOOLEAN_UNSET) {
489 bool allowed = extension_sync_data.all_urls_enabled() ==
490 ExtensionSyncData::BOOLEAN_TRUE;
491 extensions::util::SetAllowedScriptingOnAllUrls(id, profile_, allowed);
494 if (extension_installed) {
495 // If the extension is already installed, check if it's outdated.
496 if (version_compare_result < 0) {
497 // Extension is outdated.
498 return false;
500 } else {
501 CHECK(type == syncer::EXTENSIONS || type == syncer::APPS);
502 if (!extension_service_->pending_extension_manager()->AddFromSync(
504 extension_sync_data.update_url(),
505 extensions::sync_helper::IsSyncable,
506 extension_sync_data.remote_install(),
507 extension_sync_data.installed_by_custodian())) {
508 LOG(WARNING) << "Could not add pending extension for " << id;
509 // This means that the extension is already pending installation, with a
510 // non-INTERNAL location. Add to pending_sync_data, even though it will
511 // never be removed (we'll never install a syncable version of the
512 // extension), so that GetAllSyncData() continues to send it.
514 // Track pending extensions so that we can return them in GetAllSyncData().
515 return false;
518 return true;