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/scoped_observer.h"
12 #include "base/version.h"
13 #include "chrome/browser/extensions/sync_bundle.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "extensions/browser/extension_prefs_observer.h"
16 #include "extensions/browser/extension_registry_observer.h"
17 #include "sync/api/syncable_service.h"
19 class ExtensionService
;
22 namespace extensions
{
25 class ExtensionSyncData
;
26 } // namespace extensions
28 // SyncableService implementation responsible for the APPS and EXTENSIONS data
29 // types, i.e. "proper" apps/extensions (not themes).
30 class ExtensionSyncService
: public syncer::SyncableService
,
32 public extensions::ExtensionRegistryObserver
,
33 public extensions::ExtensionPrefsObserver
{
35 explicit ExtensionSyncService(Profile
* profile
);
36 ~ExtensionSyncService() override
;
38 // Convenience function to get the ExtensionSyncService for a BrowserContext.
39 static ExtensionSyncService
* Get(content::BrowserContext
* context
);
41 // Notifies Sync (if needed) of a newly-installed extension or a change to
42 // an existing extension. Call this when you change an extension setting that
43 // is synced as part of ExtensionSyncData (e.g. incognito_enabled or
45 void SyncExtensionChangeIfNeeded(const extensions::Extension
& extension
);
47 // syncer::SyncableService implementation.
48 syncer::SyncMergeResult
MergeDataAndStartSyncing(
49 syncer::ModelType type
,
50 const syncer::SyncDataList
& initial_sync_data
,
51 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
52 scoped_ptr
<syncer::SyncErrorFactory
> sync_error_factory
) override
;
53 void StopSyncing(syncer::ModelType type
) override
;
54 syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const override
;
55 syncer::SyncError
ProcessSyncChanges(
56 const tracked_objects::Location
& from_here
,
57 const syncer::SyncChangeList
& change_list
) override
;
59 void SetSyncStartFlareForTesting(
60 const syncer::SyncableService::StartSyncFlare
& flare
);
63 FRIEND_TEST_ALL_PREFIXES(TwoClientAppsSyncTest
, UnexpectedLaunchType
);
64 FRIEND_TEST_ALL_PREFIXES(ExtensionDisabledGlobalErrorTest
,
65 HigherPermissionsFromSync
);
67 ExtensionService
* extension_service() const;
69 // extensions::ExtensionRegistryObserver:
70 void OnExtensionInstalled(content::BrowserContext
* browser_context
,
71 const extensions::Extension
* extension
,
72 bool is_update
) override
;
73 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
74 const extensions::Extension
* extension
,
75 extensions::UninstallReason reason
) override
;
77 // extensions::ExtensionPrefsObserver:
78 void OnExtensionStateChanged(const std::string
& extension_id
,
80 void OnExtensionDisableReasonsChanged(const std::string
& extension_id
,
81 int disabled_reasons
) override
;
83 // Gets the SyncBundle for the given |type|.
84 extensions::SyncBundle
* GetSyncBundle(syncer::ModelType type
);
85 const extensions::SyncBundle
* GetSyncBundle(syncer::ModelType type
) const;
87 // Creates the ExtensionSyncData for the given app/extension.
88 extensions::ExtensionSyncData
CreateSyncData(
89 const extensions::Extension
& extension
) const;
91 // Applies the given change coming in from the server to the local state.
92 void ApplySyncData(const extensions::ExtensionSyncData
& extension_sync_data
);
94 // Applies the bookmark app specific parts of |extension_sync_data|.
95 void ApplyBookmarkAppSyncData(
96 const extensions::ExtensionSyncData
& extension_sync_data
);
98 // Collects the ExtensionSyncData for all installed apps or extensions.
99 // If |include_everything| is true, includes all installed extensions,
100 // otherwise only those that have the NeedsSync pref set, i.e. which have
101 // local changes that need to be pushed.
102 std::vector
<extensions::ExtensionSyncData
> GetLocalSyncDataList(
103 syncer::ModelType type
, bool include_everything
) const;
105 // Helper for GetLocalSyncDataList.
106 void FillSyncDataList(
107 const extensions::ExtensionSet
& extensions
,
108 syncer::ModelType type
,
109 bool include_everything
,
110 std::vector
<extensions::ExtensionSyncData
>* sync_data_list
) const;
112 // The normal profile associated with this ExtensionSyncService.
115 ScopedObserver
<extensions::ExtensionRegistry
,
116 extensions::ExtensionRegistryObserver
> registry_observer_
;
117 ScopedObserver
<extensions::ExtensionPrefs
,
118 extensions::ExtensionPrefsObserver
> prefs_observer_
;
120 extensions::SyncBundle app_sync_bundle_
;
121 extensions::SyncBundle extension_sync_bundle_
;
123 // Map from extension id to new version. Used to send the new version back to
124 // the sync server while we're waiting for an extension to update.
125 std::map
<std::string
, base::Version
> pending_update_versions_
;
127 // Run()ning tells sync to try and start soon, because syncable changes
128 // have started happening. It will cause sync to call us back
129 // asynchronously via MergeDataAndStartSyncing as soon as possible.
130 syncer::SyncableService::StartSyncFlare flare_
;
132 DISALLOW_COPY_AND_ASSIGN(ExtensionSyncService
);
135 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SYNC_SERVICE_H_