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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SYNC_SERVICE_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SYNC_SERVICE_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/compiler_specific.h" 12 #include "chrome/browser/extensions/app_sync_bundle.h" 13 #include "chrome/browser/extensions/extension_prefs.h" 14 #include "chrome/browser/extensions/extension_sync_bundle.h" 15 #include "chrome/browser/extensions/pending_enables.h" 16 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 17 #include "extensions/common/extension.h" 18 #include "sync/api/string_ordinal.h" 19 #include "sync/api/sync_change.h" 20 #include "sync/api/syncable_service.h" 21 22 class ExtensionErrorUI; 23 class ExtensionSyncData; 24 class Profile; 25 26 namespace base { 27 class SequencedTaskRunner; 28 } 29 30 namespace extensions { 31 class AppSyncData; 32 class ExtensionPrefs; 33 class ExtensionSyncData; 34 } // namespace extensions 35 36 namespace syncer { 37 class SyncErrorFactory; 38 } 39 40 class ExtensionSyncService : public syncer::SyncableService, 41 public BrowserContextKeyedService { 42 public: 43 ExtensionSyncService(Profile* profile, 44 extensions::ExtensionPrefs* extension_prefs, 45 ExtensionService* extension_service); 46 47 virtual ~ExtensionSyncService(); 48 49 // Convenience function to get the ExtensionSyncService for a Profile. 50 static ExtensionSyncService* Get(Profile* profile); 51 52 const extensions::ExtensionPrefs& extension_prefs() const { 53 return *extension_prefs_; 54 } 55 56 // Notifies Sync (if needed) of a newly-installed extension or a change to 57 // an existing extension. 58 virtual void SyncExtensionChangeIfNeeded( 59 const extensions::Extension& extension); 60 61 // syncer::SyncableService implementation. 62 virtual syncer::SyncMergeResult MergeDataAndStartSyncing( 63 syncer::ModelType type, 64 const syncer::SyncDataList& initial_sync_data, 65 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, 66 scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE; 67 virtual void StopSyncing(syncer::ModelType type) OVERRIDE; 68 virtual syncer::SyncDataList GetAllSyncData( 69 syncer::ModelType type) const OVERRIDE; 70 virtual syncer::SyncError ProcessSyncChanges( 71 const tracked_objects::Location& from_here, 72 const syncer::SyncChangeList& change_list) OVERRIDE; 73 74 // Gets the sync data for the given extension, assuming that the extension is 75 // syncable. 76 extensions::ExtensionSyncData GetExtensionSyncData( 77 const extensions::Extension& extension) const; 78 79 // Gets the sync data for the given app, assuming that the app is 80 // syncable. 81 extensions::AppSyncData GetAppSyncData( 82 const extensions::Extension& extension) const; 83 84 // Gets the ExtensionSyncData for all extensions. 85 std::vector<extensions::ExtensionSyncData> GetExtensionSyncDataList() const; 86 87 // Gets the AppSyncData for all extensions. 88 std::vector<extensions::AppSyncData> GetAppSyncDataList() const; 89 90 // Applies the change specified passed in by either ExtensionSyncData or 91 // AppSyncData to the current system. 92 // Returns false if the changes were not completely applied and were added 93 // to the pending list to be tried again. 94 bool ProcessExtensionSyncData( 95 const extensions::ExtensionSyncData& extension_sync_data); 96 bool ProcessAppSyncData(const extensions::AppSyncData& app_sync_data); 97 98 syncer::SyncChange PrepareToSyncUninstallExtension( 99 const extensions::Extension* extension, 100 bool extensions_ready); 101 void ProcessSyncUninstallExtension(const std::string& extension_id, 102 const syncer::SyncChange& sync_change); 103 104 void SyncEnableExtension(const extensions::Extension& extension); 105 void SyncDisableExtension(const extensions::Extension& extension); 106 107 void SyncOrderingChange(const std::string& extension_id); 108 109 // |flare| provides a StartSyncFlare to the SyncableService. See 110 // sync_start_util for more. 111 void SetSyncStartFlare(const syncer::SyncableService::StartSyncFlare& flare); 112 113 private: 114 // Return true if the sync type of |extension| matches |type|. 115 bool IsCorrectSyncType(const extensions::Extension& extension, 116 syncer::ModelType type) 117 const; 118 119 // Whether the given extension has been enabled before sync has started. 120 bool IsPendingEnable(const std::string& extension_id) const; 121 122 // Handles setting the extension specific values in |extension_sync_data| to 123 // the current system. 124 // Returns false if the changes were not completely applied and need to be 125 // tried again later. 126 bool ProcessExtensionSyncDataHelper( 127 const extensions::ExtensionSyncData& extension_sync_data, 128 syncer::ModelType type); 129 130 // The normal profile associated with this ExtensionService. 131 Profile* profile_; 132 133 // Preferences for the owning profile. 134 extensions::ExtensionPrefs* extension_prefs_; 135 136 ExtensionService* extension_service_; 137 138 extensions::AppSyncBundle app_sync_bundle_; 139 extensions::ExtensionSyncBundle extension_sync_bundle_; 140 141 // Set of extensions/apps that have been enabled before sync has started. 142 extensions::PendingEnables pending_app_enables_; 143 extensions::PendingEnables pending_extension_enables_; 144 145 scoped_ptr<ExtensionErrorUI> extension_error_ui_; 146 // Sequenced task runner for extension related file operations. 147 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 148 149 // Run()ning tells sync to try and start soon, because syncable changes 150 // have started happening. It will cause sync to call us back 151 // asynchronously via MergeDataAndStartSyncing as soon as possible. 152 syncer::SyncableService::StartSyncFlare flare_; 153 154 DISALLOW_COPY_AND_ASSIGN(ExtensionSyncService); 155 }; 156 157 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SYNC_SERVICE_H_ 158