OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #include "chrome/browser/sync/glue/new_non_frontend_data_type_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/sync/api/sync_error.h" |
| 10 #include "chrome/browser/sync/api/syncable_service.h" |
| 11 #include "chrome/browser/sync/glue/change_processor.h" |
| 12 #include "chrome/browser/sync/glue/data_type_change_processor.h" |
| 13 #include "chrome/browser/sync/glue/generic_change_processor.h" |
| 14 #include "chrome/browser/sync/glue/model_associator.h" |
| 15 #include "chrome/browser/sync/profile_sync_factory.h" |
| 16 #include "chrome/browser/sync/profile_sync_service.h" |
| 17 #include "chrome/browser/sync/syncable/model_type.h" |
| 18 #include "content/browser/browser_thread.h" |
| 19 |
| 20 namespace browser_sync { |
| 21 |
| 22 NewNonFrontendDataTypeController::NewNonFrontendDataTypeController() |
| 23 : user_share_(NULL), |
| 24 local_service_(NULL), |
| 25 generic_change_processor_(NULL) {} |
| 26 |
| 27 NewNonFrontendDataTypeController::NewNonFrontendDataTypeController( |
| 28 ProfileSyncFactory* profile_sync_factory, |
| 29 Profile* profile) |
| 30 : NonFrontendDataTypeController(profile_sync_factory, profile), |
| 31 user_share_(NULL), |
| 32 local_service_(NULL), |
| 33 generic_change_processor_(NULL) { |
| 34 } |
| 35 |
| 36 NewNonFrontendDataTypeController::~NewNonFrontendDataTypeController() {} |
| 37 |
| 38 void NewNonFrontendDataTypeController::Start(StartCallback* start_callback) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 DCHECK(start_callback); |
| 41 if (state() != NOT_RUNNING) { |
| 42 start_callback->Run(BUSY, SyncError()); |
| 43 delete start_callback; |
| 44 return; |
| 45 } |
| 46 |
| 47 set_start_callback(start_callback); |
| 48 |
| 49 set_state(MODEL_STARTING); |
| 50 if (!StartModels()) { |
| 51 // If we are waiting for some external service to load before associating |
| 52 // or we failed to start the models, we exit early. |
| 53 DCHECK(state() == MODEL_STARTING || state() == NOT_RUNNING); |
| 54 return; |
| 55 } |
| 56 |
| 57 generic_change_processor_ = |
| 58 profile_sync_factory()->CreateChangeProcessor(this); |
| 59 user_share_ = profile_sync_service()->GetUserShare(); |
| 60 |
| 61 // Kick off association on the thread the datatype resides on. |
| 62 set_state(ASSOCIATING); |
| 63 if (!StartAssociationAsync()) { |
| 64 generic_change_processor_ = NULL; |
| 65 SyncError error(FROM_HERE, "Failed to post StartAssociation", type()); |
| 66 StartDoneImpl(ASSOCIATION_FAILED, NOT_RUNNING, error); |
| 67 } |
| 68 } |
| 69 |
| 70 void NewNonFrontendDataTypeController::StartAssociation() { |
| 71 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 72 DCHECK_EQ(state(), ASSOCIATING); |
| 73 |
| 74 local_service_ = GetSyncableService(); |
| 75 generic_change_processor_->Connect(local_service_, user_share_); |
| 76 |
| 77 if (!generic_change_processor_->CryptoReadyIfNecessary(type())) { |
| 78 StartFailed(NEEDS_CRYPTO, SyncError()); |
| 79 return; |
| 80 } |
| 81 |
| 82 bool sync_has_nodes = false; |
| 83 if (!generic_change_processor_->SyncModelHasUserCreatedNodes( |
| 84 type(), &sync_has_nodes)) { |
| 85 SyncError error(FROM_HERE, "Failed to load sync nodes", type()); |
| 86 StartFailed(UNRECOVERABLE_ERROR, error); |
| 87 return; |
| 88 } |
| 89 |
| 90 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 91 SyncError error; |
| 92 SyncDataList initial_sync_data; |
| 93 error = generic_change_processor_->GetSyncDataForType(type(), |
| 94 &initial_sync_data); |
| 95 if (error.IsSet()) { |
| 96 StartFailed(ASSOCIATION_FAILED, error); |
| 97 return; |
| 98 } |
| 99 // Passes a reference to the generic_change_processor_; |
| 100 error = local_service_->MergeDataAndStartSyncing( |
| 101 type(), |
| 102 initial_sync_data, |
| 103 new DataTypeChangeProcessor(generic_change_processor_.get())); |
| 104 RecordAssociationTime(base::TimeTicks::Now() - start_time); |
| 105 if (error.IsSet()) { |
| 106 local_service_->StopSyncing(type()); |
| 107 StartFailed(ASSOCIATION_FAILED, error); |
| 108 return; |
| 109 } |
| 110 |
| 111 // This is kind of ugly, but safe since we guarantee that the SyncableService |
| 112 // will be around longer than the PSS, and hence the referene it holds |
| 113 // to the GenericChangeProcessor will stick around. |
| 114 profile_sync_service()->ActivateDataType(type(), model_safe_group(), |
| 115 generic_change_processor_); |
| 116 StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK, RUNNING, SyncError()); |
| 117 } |
| 118 |
| 119 void NewNonFrontendDataTypeController::Stop() { |
| 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 121 |
| 122 // Disconnect the change processor. At this point, the SyncableService |
| 123 // can no longer interact with the Syncer, even if it hasn't finished |
| 124 // MergeDataAndStartSyncing. The generic change processor is owned by |
| 125 // local_service_ so we don't have to delete it. Just post a task to tell it |
| 126 // to StopSyncing. |
| 127 if (generic_change_processor_.get()) { |
| 128 generic_change_processor_->Disconnect(); |
| 129 if (!StopLocalServiceAsync()) { |
| 130 LOG(ERROR) << "Failed to stop " << type() << "'s syncable service. " |
| 131 << "Continuing, but this may cause problems on sync restart."; |
| 132 } |
| 133 } |
| 134 |
| 135 // If we haven't finished starting, we need to abort the start. |
| 136 if (state() == MODEL_STARTING) { |
| 137 set_state(STOPPING); |
| 138 StartDoneImpl(ABORTED, NOT_RUNNING, SyncError()); |
| 139 return; // The datatype was never activated, we're done. |
| 140 } else if (state() == ASSOCIATING) { |
| 141 set_state(STOPPING); |
| 142 StartDoneImpl(ABORTED, NOT_RUNNING, SyncError()); |
| 143 // We continue on to deactivate the datatype. |
| 144 } else { |
| 145 // Datatype was fully started. |
| 146 set_state(STOPPING); |
| 147 StopModels(); |
| 148 } |
| 149 |
| 150 // Deactivate the DataType on the UI thread. We dont want to listen |
| 151 // for any more changes or process them from the server. |
| 152 profile_sync_service()->DeactivateDataType(type()); |
| 153 |
| 154 set_state(NOT_RUNNING); |
| 155 } |
| 156 |
| 157 // Static. |
| 158 void NewNonFrontendDataTypeController::StopLocalService( |
| 159 SyncableService* service, syncable::ModelType type) { |
| 160 service->StopSyncing(type); |
| 161 } |
| 162 |
| 163 SyncableService* NewNonFrontendDataTypeController::local_service() const { |
| 164 return local_service_; |
| 165 } |
| 166 |
| 167 bool NewNonFrontendDataTypeController::StopAssociationAsync() { |
| 168 NOTIMPLEMENTED(); |
| 169 return false; |
| 170 } |
| 171 |
| 172 void NewNonFrontendDataTypeController::CreateSyncComponents() { |
| 173 NOTIMPLEMENTED(); |
| 174 } |
| 175 |
| 176 } // namepsace browser_sync |
OLD | NEW |