Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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_APP_SYNC_BUNDLE_H_
      6 #define CHROME_BROWSER_EXTENSIONS_APP_SYNC_BUNDLE_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "chrome/browser/extensions/app_sync_data.h"
     16 #include "sync/api/syncable_service.h"
     17 
     18 class ExtensionService;
     19 class ExtensionSet;
     20 
     21 namespace syncer {
     22 class SyncChangeProcessor;
     23 class SyncErrorFactory;
     24 }
     25 
     26 namespace extensions {
     27 
     28 class Extension;
     29 
     30 // Bundle of app specific sync stuff.
     31 class AppSyncBundle {
     32  public:
     33   explicit AppSyncBundle(ExtensionService* extension_service);
     34   virtual ~AppSyncBundle();
     35 
     36   // Setup this bundle to be sync application data.
     37   void SetupSync(syncer::SyncChangeProcessor* sync_proccessor,
     38                  syncer::SyncErrorFactory* sync_error_factory,
     39                  const syncer::SyncDataList& initial_sync_data);
     40 
     41   // Resets this class back to it default values, which will disable all syncing
     42   // until a new sync processor is set.
     43   void Reset();
     44 
     45   // Returns a syncer::SyncChange that will delete the given application.
     46   syncer::SyncChange CreateSyncChangeToDelete(const Extension* extension) const;
     47 
     48   // Process the sync deletion of the given application.
     49   void ProcessDeletion(
     50       std::string extension_id, const syncer::SyncChange& sync_change);
     51 
     52   // Create a sync change based on |sync_data|.
     53   syncer::SyncChange CreateSyncChange(const syncer::SyncData& sync_data);
     54 
     55   // Get all the sync data contained in this bundle.
     56   syncer::SyncDataList GetAllSyncData() const;
     57 
     58   // Sync a newly-installed application or change an existing one.
     59   void SyncChangeIfNeeded(const Extension& extension);
     60 
     61   // Process the given sync change and apply it.
     62   void ProcessSyncChange(AppSyncData app_sync_data);
     63 
     64   // Process the list of sync changes.
     65   void ProcessSyncChangeList(syncer::SyncChangeList sync_change_list);
     66 
     67   // Check to see if the given |id| is either synced or pending to be synced.
     68   bool HasExtensionId(const std::string& id) const;
     69   bool HasPendingExtensionId(const std::string& id) const;
     70 
     71   // Add a pending app to be synced.
     72   void AddPendingApp(const std::string& id,
     73                      const AppSyncData& app_sync_data);
     74 
     75   // Returns a vector of all the pending sync data.
     76   std::vector<AppSyncData> GetPendingData() const;
     77 
     78   // Appends sync data objects for every app in |extensions|.
     79   void GetAppSyncDataListHelper(
     80       const ExtensionSet& extensions,
     81       std::vector<extensions::AppSyncData>* sync_data_list) const;
     82 
     83   // Returns true if SetupSync has been called, false otherwise.
     84   bool IsSyncing() const;
     85 
     86  private:
     87   // Add a synced app.
     88   void AddApp(const std::string& id);
     89 
     90   // Remove a synced app.
     91   void RemoveApp(const std::string& id); // make private
     92 
     93   // Change an app from being pending to synced.
     94   void MarkPendingAppSynced(const std::string& id);
     95 
     96   ExtensionService* extension_service_; // Own us.
     97   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
     98   scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
     99 
    100   std::set<std::string> synced_apps_;
    101   std::map<std::string, AppSyncData> pending_sync_data_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(AppSyncBundle);
    104 };
    105 
    106 }  // namespace extensions
    107 
    108 #endif  // CHROME_BROWSER_EXTENSIONS_APP_SYNC_BUNDLE_H_
    109