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/extension_sync_data.h"
15 #include "chrome/browser/extensions/pending_enables.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/common/extension.h"
19 #include "sync/api/string_ordinal.h"
20 #include "sync/api/sync_change.h"
21 #include "sync/api/syncable_service.h"
26 class SequencedTaskRunner
;
29 namespace extensions
{
31 } // namespace extensions
34 class SyncErrorFactory
;
37 class ExtensionSyncService
: public syncer::SyncableService
,
40 ExtensionSyncService(Profile
* profile
,
41 extensions::ExtensionPrefs
* extension_prefs
,
42 ExtensionService
* extension_service
);
44 ~ExtensionSyncService() override
;
46 // Convenience function to get the ExtensionSyncService for a BrowserContext.
47 static ExtensionSyncService
* Get(content::BrowserContext
* context
);
49 const extensions::ExtensionPrefs
& extension_prefs() const {
50 return *extension_prefs_
;
53 // Notifies Sync (if needed) of a newly-installed extension or a change to
54 // an existing extension.
55 virtual void SyncExtensionChangeIfNeeded(
56 const extensions::Extension
& extension
);
58 // syncer::SyncableService implementation.
59 syncer::SyncMergeResult
MergeDataAndStartSyncing(
60 syncer::ModelType type
,
61 const syncer::SyncDataList
& initial_sync_data
,
62 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
63 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory
) override
;
64 void StopSyncing(syncer::ModelType type
) override
;
65 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
66 syncer::SyncError
ProcessSyncChanges(
67 const tracked_objects::Location
& from_here
,
68 const syncer::SyncChangeList
& change_list
) override
;
70 // Gets the sync data for the given extension, assuming that the extension is
72 extensions::ExtensionSyncData
GetExtensionSyncData(
73 const extensions::Extension
& extension
) const;
75 // Gets the sync data for the given app, assuming that the app is
77 extensions::AppSyncData
GetAppSyncData(
78 const extensions::Extension
& extension
) const;
80 // Gets the ExtensionSyncData for all extensions.
81 std::vector
<extensions::ExtensionSyncData
> GetExtensionSyncDataList() const;
83 // Gets the AppSyncData for all extensions.
84 std::vector
<extensions::AppSyncData
> GetAppSyncDataList() const;
86 // Applies the change specified passed in by either ExtensionSyncData or
87 // AppSyncData to the current system.
88 // Returns false if the changes were not completely applied and were added
89 // to the pending list to be tried again.
90 bool ProcessExtensionSyncData(
91 const extensions::ExtensionSyncData
& extension_sync_data
);
92 bool ProcessAppSyncData(const extensions::AppSyncData
& app_sync_data
);
94 // Processes the bookmark app specific parts of an AppSyncData.
95 void ProcessBookmarkAppSyncData(const extensions::AppSyncData
& app_sync_data
);
97 syncer::SyncChange
PrepareToSyncUninstallExtension(
98 const extensions::Extension
* extension
,
99 bool extensions_ready
);
100 void ProcessSyncUninstallExtension(const std::string
& extension_id
,
101 const syncer::SyncChange
& sync_change
);
103 void SyncEnableExtension(const extensions::Extension
& extension
);
104 void SyncDisableExtension(const extensions::Extension
& extension
);
106 void SyncOrderingChange(const std::string
& extension_id
);
108 // |flare| provides a StartSyncFlare to the SyncableService. See
109 // sync_start_util for more.
110 void SetSyncStartFlare(const syncer::SyncableService::StartSyncFlare
& flare
);
112 Profile
* profile() { return profile_
; }
115 // Return true if the sync type of |extension| matches |type|.
116 bool IsCorrectSyncType(const extensions::Extension
& extension
,
117 syncer::ModelType type
)
120 // Whether the given extension has been enabled before sync has started.
121 bool IsPendingEnable(const std::string
& extension_id
) const;
123 // Handles setting the extension specific values in |extension_sync_data| to
124 // the current system.
125 // Returns false if the changes were not completely applied and need to be
126 // tried again later.
127 bool ProcessExtensionSyncDataHelper(
128 const extensions::ExtensionSyncData
& extension_sync_data
,
129 syncer::ModelType type
);
131 // The normal profile associated with this ExtensionService.
134 // Preferences for the owning profile.
135 extensions::ExtensionPrefs
* extension_prefs_
;
137 ExtensionService
* extension_service_
;
139 extensions::AppSyncBundle app_sync_bundle_
;
140 extensions::ExtensionSyncBundle extension_sync_bundle_
;
142 // Set of extensions/apps that have been enabled before sync has started.
143 extensions::PendingEnables pending_app_enables_
;
144 extensions::PendingEnables pending_extension_enables_
;
146 // Sequenced task runner for extension related file operations.
147 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
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_
;
154 DISALLOW_COPY_AND_ASSIGN(ExtensionSyncService
);
157 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SYNC_SERVICE_H_