Extension syncing: Introduce a NeedsSync pref
that indicates the extension has local changes that still need to be synced.
It's set when something changes before sync is ready, and cleared once the extension state has been synced.

This should handle all conflicts between sync and local state reasonably well, and as a bonus allows us to get rid of the (weird and not-really-working) PendingEnables class.

BUG=509990

Review URL: https://codereview.chromium.org/1240573012

Cr-Commit-Position: refs/heads/master@{#339651}
diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc
index 4a1919f9..94c94fd 100644
--- a/chrome/browser/extensions/extension_service.cc
+++ b/chrome/browser/extensions/extension_service.cc
@@ -278,7 +278,6 @@
       system_(extensions::ExtensionSystem::Get(profile)),
       extension_prefs_(extension_prefs),
       blacklist_(blacklist),
-      extension_sync_service_(NULL),
       registry_(extensions::ExtensionRegistry::Get(profile)),
       pending_extension_manager_(profile),
       install_directory_(install_directory),
@@ -776,9 +775,8 @@
 
   // Don't sync the uninstall if we're going to reinstall the extension
   // momentarily.
-  if (extension_sync_service_ &&
-      reason != extensions::UNINSTALL_REASON_REINSTALL) {
-    extension_sync_service_->SyncUninstallExtension(*extension);
+  if (reason != extensions::UNINSTALL_REASON_REINSTALL) {
+    ExtensionSyncService::Get(profile_)->SyncUninstallExtension(*extension);
   }
 
   delayed_installs_.Remove(extension->id());
@@ -866,8 +864,7 @@
       content::Source<Profile>(profile_),
       content::Details<const Extension>(extension));
 
-  if (extension_sync_service_)
-    extension_sync_service_->SyncEnableExtension(*extension);
+  ExtensionSyncService::Get(profile_)->SyncExtensionChangeIfNeeded(*extension);
 }
 
 void ExtensionService::DisableExtension(const std::string& extension_id,
@@ -917,8 +914,7 @@
     registry_->RemoveTerminated(extension->id());
   }
 
-  if (extension_sync_service_)
-    extension_sync_service_->SyncDisableExtension(*extension);
+  ExtensionSyncService::Get(profile_)->SyncExtensionChangeIfNeeded(*extension);
 }
 
 void ExtensionService::DisableUserExtensions(
@@ -1510,8 +1506,8 @@
   } else if (!reloading &&
              extension_prefs_->IsExtensionDisabled(extension->id())) {
     registry_->AddDisabled(extension);
-    if (extension_sync_service_)
-      extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
+    ExtensionSyncService::Get(profile_)->SyncExtensionChangeIfNeeded(
+        *extension);
     content::NotificationService::current()->Notify(
         extensions::NOTIFICATION_EXTENSION_UPDATE_DISABLED,
         content::Source<Profile>(profile_),
@@ -1548,8 +1544,8 @@
     }
 
     registry_->AddEnabled(extension);
-    if (extension_sync_service_)
-      extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
+    ExtensionSyncService::Get(profile_)->SyncExtensionChangeIfNeeded(
+        *extension);
     NotifyExtensionLoaded(extension);
   }
   system_->runtime_data()->SetBeingUpgraded(extension->id(), false);
@@ -2062,8 +2058,10 @@
 
   registry_->TriggerOnInstalled(extension, true);
 
-  if (!is_from_sync && extension_sync_service_)
-    extension_sync_service_->SyncExtensionChangeIfNeeded(*extension);
+  if (!is_from_sync) {
+    ExtensionSyncService::Get(profile_)->SyncExtensionChangeIfNeeded(
+        *extension);
+  }
 }
 
 const Extension* ExtensionService::GetPendingExtensionUpdate(