Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: chrome/browser/sync/glue/new_non_frontend_data_type_controller.cc

Issue 8065016: [Sync] Refactor non-frontend DTC to handle new API properly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/sync/profile_sync_factory.h"
9 #include "chrome/browser/sync/profile_sync_service.h"
10 #include "chrome/browser/sync/api/sync_error.h"
11 #include "chrome/browser/sync/api/syncable_service.h"
12 #include "chrome/browser/sync/glue/shared_change_processor_ref.h"
13 #include "chrome/browser/sync/syncable/model_type.h"
14 #include "content/browser/browser_thread.h"
15
16 namespace browser_sync {
17
18 NewNonFrontendDataTypeController::NewNonFrontendDataTypeController()
19 : shared_change_processor_(NULL) {}
20
21 NewNonFrontendDataTypeController::NewNonFrontendDataTypeController(
22 ProfileSyncFactory* profile_sync_factory,
23 Profile* profile)
24 : NonFrontendDataTypeController(profile_sync_factory, profile),
25 shared_change_processor_(NULL) {
26 }
27
28 NewNonFrontendDataTypeController::~NewNonFrontendDataTypeController() {}
29
30 void NewNonFrontendDataTypeController::Start(StartCallback* start_callback) {
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32 DCHECK(start_callback);
33 if (state() != NOT_RUNNING) {
34 start_callback->Run(BUSY, SyncError());
35 delete start_callback;
36 return;
37 }
38
39 set_start_callback(start_callback);
40
41 set_state(MODEL_STARTING);
42 if (!StartModels()) {
43 // If we are waiting for some external service to load before associating
44 // or we failed to start the models, we exit early.
45 DCHECK(state() == MODEL_STARTING || state() == NOT_RUNNING);
46 return;
47 }
48
49 shared_change_processor_ =
50 profile_sync_factory()->CreateSharedChangeProcessor();
51
52 // Kick off association on the thread the datatype resides on.
53 set_state(ASSOCIATING);
54 if (!StartAssociationAsync()) {
55 // Releasing the shared_change_processor_ here is safe since it was never
56 // connected.
57 shared_change_processor_ = NULL;
58 SyncError error(FROM_HERE, "Failed to post StartAssociation", type());
59 StartDoneImpl(ASSOCIATION_FAILED, NOT_RUNNING, error);
60 }
61 }
62
63 // This method can execute after we've already stopped (and possibly even
64 // destroyed) both the Syncer and the SyncableService. As a result, all actions
65 // must either have no side effects outside of the DTC or must be protected
66 // by |shared_change_processor_|, which is guaranteed to have been Disconnected
67 // if the syncer shut down.
68 void NewNonFrontendDataTypeController::StartAssociation() {
69 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
70 DCHECK_EQ(state(), ASSOCIATING);
71
72 // We're dependent on the SyncableService being destroyed on the same thread
73 // we access it. Therefore, as long as GetWeakPtrToSyncableService returns
74 // a valid WeakPtr, we can rely on it remaining initialized for the
75 // life of this method.
76 local_service_ = GetWeakPtrToSyncableService();
77 if (!local_service_) {
78 // The SyncableService was destroyed before this task had a chance to
79 // execute.
80 SyncError error(FROM_HERE, "Local service destroyed before association.",
81 type());
82 StartFailed(UNRECOVERABLE_ERROR, error);
83 return;
84 }
85
86 // Connect |shared_change_processor_| to the syncer and |local_service_|.
87 // Note that it's possible the shared_change_processor_ has already been
88 // disconnected at this point, so all our accesses to the syncer from this
89 // point on are through it.
90 if (!shared_change_processor_->Connect(profile_sync_factory(),
91 profile_sync_service(),
92 this,
93 local_service_)) {
94 SyncError error(FROM_HERE, "Failed to connect to syncer.", type());
95 StartFailed(UNRECOVERABLE_ERROR, error);
96 }
97
98 if (!shared_change_processor_->CryptoReadyIfNecessary(type())) {
99 StartFailed(NEEDS_CRYPTO, SyncError());
100 return;
101 }
102
103 bool sync_has_nodes = false;
104 if (!shared_change_processor_->SyncModelHasUserCreatedNodes(
105 type(), &sync_has_nodes)) {
106 SyncError error(FROM_HERE, "Failed to load sync nodes", type());
107 StartFailed(UNRECOVERABLE_ERROR, error);
108 return;
109 }
110
111 base::TimeTicks start_time = base::TimeTicks::Now();
112 SyncError error;
113 SyncDataList initial_sync_data;
114 error = shared_change_processor_->GetSyncDataForType(type(),
115 &initial_sync_data);
116 if (error.IsSet()) {
117 StartFailed(ASSOCIATION_FAILED, error);
118 return;
119 }
120 // Passes a reference to the shared_change_processor_;
121 error = local_service_->MergeDataAndStartSyncing(
122 type(),
123 initial_sync_data,
124 new SharedChangeProcessorRef(shared_change_processor_));
125 RecordAssociationTime(base::TimeTicks::Now() - start_time);
126 if (error.IsSet()) {
127 local_service_->StopSyncing(type());
128 StartFailed(ASSOCIATION_FAILED, error);
129 return;
130 }
131
132 // If we've been disconnected, profile_sync_service() may return an invalid
133 // pointer, but the shared_change_processor_ protects us from attempting to
134 // access it.
135 // Note: This must be done on the datatype's thread to ensure local_service_
136 // doesn't start trying to push changes from it's thread before we activate
137 // the datatype.
138 shared_change_processor_->ActivateDataType(profile_sync_service(),
139 type(), model_safe_group());
140 StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK, RUNNING, SyncError());
141 }
142
143 void NewNonFrontendDataTypeController::StartDone(
144 DataTypeController::StartResult result,
145 DataTypeController::State new_state,
146 const SyncError& error) {
147 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
148 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
149 base::Bind(
150 &NewNonFrontendDataTypeController::StartDoneImpl,
151 this,
152 result,
153 new_state,
154 error));
155 }
156
157 void NewNonFrontendDataTypeController::Stop() {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
159
160 // Disconnect the change processor. At this point, the SyncableService
161 // can no longer interact with the Syncer, even if it hasn't finished
162 // MergeDataAndStartSyncing.
163 if (shared_change_processor_.get()) {
akalin 2011/10/13 00:08:16 perhaps if (scp_ && scp_->Disconnect()) { StopL
Nicolas Zea 2011/10/13 01:16:14 Done.
164 // If we had not successfully connected, we don't need to stop
165 // |local_service_|
166 if (shared_change_processor_->Disconnect())
167 StopLocalServiceAsync();
168 }
169
170 // If we haven't finished starting, we need to abort the start.
171 if (state() == MODEL_STARTING) {
akalin 2011/10/13 00:08:16 better done as a switch?
Nicolas Zea 2011/10/13 01:16:14 Done.
172 set_state(STOPPING);
173 StartDoneImpl(ABORTED, NOT_RUNNING, SyncError());
174 return; // The datatype was never activated, we're done.
175 } else if (state() == ASSOCIATING) {
176 set_state(STOPPING);
177 StartDoneImpl(ABORTED, NOT_RUNNING, SyncError());
178 // We continue on to deactivate the datatype.
179 } else if (state() == DISABLED) {
180 // If we're disabled we never succeded assocaiting and never activated the
181 // datatype.
182 set_state(NOT_RUNNING);
183 StopModels();
184 return;
185 } else {
186 // Datatype was fully started.
187 set_state(STOPPING);
188 StopModels();
189 }
190
191 // Deactivate the DataType on the UI thread. We dont want to listen
192 // for any more changes or process them from the server.
193 profile_sync_service()->DeactivateDataType(type());
194
195 set_state(NOT_RUNNING);
196 }
197
198 void NewNonFrontendDataTypeController::StopLocalService() {
199 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
200 if (local_service_)
akalin 2011/10/13 00:08:16 prefer if (local_service_.get())
Nicolas Zea 2011/10/13 01:16:14 Done.
201 local_service_->StopSyncing(type());
202 local_service_.reset();
203 shared_change_processor_ = NULL;
204 }
205
206 bool NewNonFrontendDataTypeController::StopAssociationAsync() {
207 NOTIMPLEMENTED();
208 return false;
209 }
210
211 void NewNonFrontendDataTypeController::CreateSyncComponents() {
212 NOTIMPLEMENTED();
213 }
214
215 } // namepsace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698