10529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 50529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#ifndef COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ 60529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#define COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/weak_ptr.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/non_thread_safe.h" 130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/sync_driver/change_processor.h" 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/sync_driver/data_type_controller.h" 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/sync_driver/data_type_error_handler.h" 161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "sync/api/attachments/attachment_store.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/api/sync_change_processor.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "sync/api/sync_merge_result.h" 195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/attachments/attachment_service.h" 205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/attachments/attachment_service_proxy.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SyncData; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SyncableService; 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class WriteNode; 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class WriteTransaction; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef std::vector<syncer::SyncData> SyncDataList; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace syncer 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace sync_driver { 32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class SyncApiComponentFactory; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Datatype agnostic change processor. One instance of GenericChangeProcessor 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is created for each datatype and lives on the datatype's thread. It then 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// handles all interaction with the sync api, both translating pushes from the 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// local service into transactions and receiving changes from the sync model, 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// which then get converted into SyncChange's and sent to the local service. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// As a rule, the GenericChangeProcessor is not thread safe, and should only 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// be used on the same thread in which it was created. 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class GenericChangeProcessor : public ChangeProcessor, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public syncer::SyncChangeProcessor, 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public syncer::AttachmentService::Delegate, 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public base::NonThreadSafe { 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Create a change processor for |type| and connect it to the syncer. 481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // |attachment_store| can be NULL which means that datatype will not use sync 491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // attachments. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GenericChangeProcessor( 511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci syncer::ModelType type, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DataTypeErrorHandler* error_handler, 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::WeakPtr<syncer::SyncableService>& local_service, 542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::WeakPtr<syncer::SyncMergeResult>& merge_result, 55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch syncer::UserShare* user_share, 561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci SyncApiComponentFactory* sync_factory, 571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const scoped_refptr<syncer::AttachmentStore>& attachment_store); 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~GenericChangeProcessor(); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ChangeProcessor interface. 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Build and store a list of all changes into |syncer_changes_|. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void ApplyChangesFromSyncModel( 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const syncer::BaseTransaction* trans, 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 version, 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const syncer::ImmutableChangeRecordList& changes) OVERRIDE; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Passes |syncer_changes_|, built in ApplyChangesFromSyncModel, onto 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |local_service_| by way of its ProcessSyncChanges method. 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void CommitChangesFromSyncModel() OVERRIDE; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // syncer::SyncChangeProcessor implementation. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual syncer::SyncError ProcessSyncChanges( 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const tracked_objects::Location& from_here, 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const syncer::SyncChangeList& change_list) OVERRIDE; 7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) 7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const OVERRIDE; 76c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch virtual syncer::SyncError UpdateDataTypeContext( 77c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch syncer::ModelType type, 78c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, 79c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch const std::string& context) OVERRIDE; 8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // syncer::AttachmentService::Delegate implementation. 82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) virtual void OnAttachmentUploaded( 83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const syncer::AttachmentId& attachment_id) OVERRIDE; 84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Similar to above, but returns a SyncError for use by direct clients 8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // of GenericChangeProcessor that may need more error visibility. 8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) virtual syncer::SyncError GetAllSyncDataReturnError( 8858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) syncer::SyncDataList* data) const; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // If a datatype context associated with this GenericChangeProcessor's type 911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // exists, fills |context| and returns true. Otheriwse, if there has not been 921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // a context set, returns false. 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual bool GetDataTypeContext(std::string* context) const; 94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Returns the number of items for this type. 961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual int GetSyncCount(); 972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Generic versions of AssociatorInterface methods. Called by 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // syncer::SyncableServiceAdapter or the DataTypeController. 1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes); 1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual bool CryptoReadyIfNecessary(); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ChangeProcessor interface. 1050529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch virtual void StartImpl() OVERRIDE; // Does nothing. 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual syncer::UserShare* share_handle() const OVERRIDE; 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Logically part of ProcessSyncChanges. 110010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // 111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // |new_attachments| is an output parameter containing newly added attachments 112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // that need to be stored. This method will append to it. 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) syncer::SyncError HandleActionAdd(const syncer::SyncChange& change, 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& type_str, 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const syncer::WriteTransaction& trans, 116010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) syncer::WriteNode* sync_node, 1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci syncer::AttachmentIdSet* new_attachments); 118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 119010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Logically part of ProcessSyncChanges. 120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // 121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // |new_attachments| is an output parameter containing newly added attachments 122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // that need to be stored. This method will append to it. 1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci syncer::SyncError HandleActionUpdate( 1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const syncer::SyncChange& change, 1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const std::string& type_str, 1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const syncer::WriteTransaction& trans, 1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci syncer::WriteNode* sync_node, 1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci syncer::AttachmentIdSet* new_attachments); 1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Begin uploading attachments that have not yet been uploaded to the sync 1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // server. 1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci void UploadAllAttachmentsNotOnServer(); 1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const syncer::ModelType type_; 1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The SyncableService this change processor will forward changes on to. 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::WeakPtr<syncer::SyncableService> local_service_; 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // A SyncMergeResult used to track the changes made during association. The 1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // owner will invalidate the weak pointer when association is complete. While 1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // the pointer is valid though, we increment it with any changes received 1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // via ProcessSyncChanges. 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const base::WeakPtr<syncer::SyncMergeResult> merge_result_; 1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The current list of changes received from the syncer. We buffer because 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // we must ensure no syncapi transaction is held when we pass it on to 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // |local_service_|. 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set in ApplyChangesFromSyncModel, consumed in CommitChangesFromSyncModel. 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) syncer::SyncChangeList syncer_changes_; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Our handle to the sync model. Unlike normal ChangeProcessors, we need to 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // be able to access the sync model before the change processor begins 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // listening to changes (the local_service_ will be interacting with us 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // when it starts up). As such we can't wait until Start(_) has been called, 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // and have to keep a local pointer to the user_share. 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) syncer::UserShare* const share_handle_; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // AttachmentService for datatype. Can be NULL if datatype doesn't use 1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // attachments. 160e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch scoped_ptr<syncer::AttachmentService> attachment_service_; 1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 162e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Must be destroyed before attachment_service_ to ensure WeakPtrs are 163e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // invalidated before attachment_service_ is destroyed. 1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // Can be NULL if attachment_service_ is NULL; 1651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci scoped_ptr<base::WeakPtrFactory<syncer::AttachmentService> > 166e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch attachment_service_weak_ptr_factory_; 1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci scoped_ptr<syncer::AttachmentServiceProxy> attachment_service_proxy_; 1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci 1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci base::WeakPtrFactory<GenericChangeProcessor> weak_ptr_factory_; 170e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(GenericChangeProcessor); 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)} // namespace sync_driver 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#endif // COMPONENTS_SYNC_DRIVER_GENERIC_CHANGE_PROCESSOR_H_ 177