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