[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 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_SYNC_BUNDLE_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_SYNC_BUNDLE_H_ |
| 7 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 12 | #include "base/macros.h" |
skym | 7160384 | 2016-10-10 18:17:31 | [diff] [blame] | 13 | #include "components/sync/model/sync_change.h" |
| 14 | #include "components/sync/model/sync_change_processor.h" |
| 15 | #include "components/sync/model/sync_data.h" |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 16 | |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 17 | namespace extensions { |
| 18 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 19 | class ExtensionSyncData; |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 20 | |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 21 | class SyncBundle { |
| 22 | public: |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 23 | SyncBundle(); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 24 | ~SyncBundle(); |
| 25 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 26 | void StartSyncing( |
| 27 | std::unique_ptr<syncer::SyncChangeProcessor> sync_processor); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 28 | |
| 29 | // Resets this class back to its default values, which will disable all |
| 30 | // syncing until StartSyncing is called again. |
| 31 | void Reset(); |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 32 | |
| 33 | // Has this bundle started syncing yet? |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 34 | // Returns true if StartSyncing has been called, false otherwise. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 35 | bool IsSyncing() const; |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 36 | |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 37 | // Handles the given list of local SyncDatas. This updates the set of synced |
| 38 | // extensions as appropriate, and then pushes the corresponding SyncChanges |
| 39 | // to the server. |
| 40 | void PushSyncDataList(const syncer::SyncDataList& sync_data_list); |
| 41 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 42 | // Updates the set of synced extensions as appropriate, and then pushes a |
| 43 | // SyncChange to the server. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 44 | void PushSyncDeletion(const std::string& extension_id, |
| 45 | const syncer::SyncData& sync_data); |
| 46 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 47 | // Pushes any sync changes to an extension to the server and, if necessary, |
| 48 | // updates the set of synced extension. This also clears any pending data for |
| 49 | // the extension. |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 50 | void PushSyncAddOrUpdate(const std::string& extension_id, |
| 51 | const syncer::SyncData& sync_data); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 52 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 53 | // Applies the given sync change coming in from the server. This just updates |
| 54 | // the list of synced extensions. |
| 55 | void ApplySyncData(const ExtensionSyncData& extension_sync_data); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 56 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 57 | // Checks if there is pending sync data for the extension with the given |id|, |
| 58 | // i.e. data to be sent to the sync server until the extension is installed |
| 59 | // locally. |
| 60 | bool HasPendingExtensionData(const std::string& id) const; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 61 | |
treib | 227b258 | 2015-12-09 09:28:26 | [diff] [blame] | 62 | // Adds pending data for the given extension. |
| 63 | void AddPendingExtensionData(const ExtensionSyncData& extension_sync_data); |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 64 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 65 | // Returns a vector of all the pending extension data. |
| 66 | std::vector<ExtensionSyncData> GetPendingExtensionData() const; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | // Creates a SyncChange to add or update an extension. |
| 70 | syncer::SyncChange CreateSyncChange(const std::string& extension_id, |
| 71 | const syncer::SyncData& sync_data) const; |
| 72 | |
| 73 | // Pushes the given list of SyncChanges to the server. |
| 74 | void PushSyncChanges(const syncer::SyncChangeList& sync_change_list); |
| 75 | |
| 76 | void AddSyncedExtension(const std::string& id); |
| 77 | void RemoveSyncedExtension(const std::string& id); |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 78 | bool HasSyncedExtension(const std::string& id) const; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 79 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 80 | std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_; |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 81 | |
treib | c349453 | 2015-07-21 14:51:45 | [diff] [blame] | 82 | // Stores the set of extensions we know about. Used to decide if a sync change |
| 83 | // should be ACTION_ADD or ACTION_UPDATE. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 84 | std::set<std::string> synced_extensions_; |
| 85 | |
treib | ecc63c8d | 2015-09-07 16:34:47 | [diff] [blame] | 86 | // This stores pending installs we got from sync. We'll send this back to the |
| 87 | // server until we've installed the extension locally, to prevent the sync |
| 88 | // state from flipping back and forth until all clients are up to date. |
treib | 0c714f7c | 2015-07-08 10:04:58 | [diff] [blame] | 89 | std::map<std::string, ExtensionSyncData> pending_sync_data_; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(SyncBundle); |
[email protected] | 50736a1 | 2013-09-26 08:58:34 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace extensions |
| 95 | |
| 96 | #endif // CHROME_BROWSER_EXTENSIONS_SYNC_BUNDLE_H_ |