OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "chrome/browser/browser.h" | 6 #include "chrome/browser/browser.h" |
7 #include "chrome/browser/defaults.h" | 7 #include "chrome/browser/defaults.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/profile.h" | 9 #include "chrome/browser/profile.h" |
10 #include "chrome/browser/net/gaia/token_service.h" | 10 #include "chrome/browser/net/gaia/token_service.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 bool StateChangeTimeoutEvent::Abort() { | 80 bool StateChangeTimeoutEvent::Abort() { |
81 aborted_ = true; | 81 aborted_ = true; |
82 caller_ = NULL; | 82 caller_ = NULL; |
83 return !did_timeout_; | 83 return !did_timeout_; |
84 } | 84 } |
85 | 85 |
86 ProfileSyncServiceHarness::ProfileSyncServiceHarness( | 86 ProfileSyncServiceHarness::ProfileSyncServiceHarness( |
87 Profile* p, | 87 Profile* profile, |
88 const std::string& username, | 88 const std::string& username, |
89 const std::string& password, | 89 const std::string& password, |
90 int id) | 90 int id) |
91 : wait_state_(WAITING_FOR_ON_BACKEND_INITIALIZED), | 91 : wait_state_(INITIAL_WAIT_STATE), |
92 profile_(p), | 92 profile_(profile), |
93 service_(NULL), | 93 service_(NULL), |
94 last_timestamp_(0), | 94 last_timestamp_(0), |
95 min_timestamp_needed_(kMinTimestampNeededNone), | 95 min_timestamp_needed_(kMinTimestampNeededNone), |
96 username_(username), | 96 username_(username), |
97 password_(password), | 97 password_(password), |
98 id_(id) {} | 98 id_(id) { |
| 99 if (IsSyncAlreadySetup()) { |
| 100 service_ = profile_->GetProfileSyncService(); |
| 101 service_->AddObserver(this); |
| 102 wait_state_ = FULLY_SYNCED; |
| 103 } |
| 104 } |
| 105 |
| 106 // static |
| 107 ProfileSyncServiceHarness* ProfileSyncServiceHarness::CreateAndAttach( |
| 108 Profile* profile) { |
| 109 if (!profile->HasProfileSyncService()) { |
| 110 NOTREACHED() << "Profile has never signed into sync."; |
| 111 return NULL; |
| 112 } |
| 113 return new ProfileSyncServiceHarness(profile, "", "", 0); |
| 114 } |
| 115 |
| 116 void ProfileSyncServiceHarness::SetCredentials(const std::string& username, |
| 117 const std::string& password) { |
| 118 username_ = username; |
| 119 password_ = password; |
| 120 } |
| 121 |
| 122 bool ProfileSyncServiceHarness::IsSyncAlreadySetup() { |
| 123 return profile_->HasProfileSyncService(); |
| 124 } |
99 | 125 |
100 bool ProfileSyncServiceHarness::SetupSync() { | 126 bool ProfileSyncServiceHarness::SetupSync() { |
101 syncable::ModelTypeSet synced_datatypes; | 127 syncable::ModelTypeSet synced_datatypes; |
102 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | 128 for (int i = syncable::FIRST_REAL_MODEL_TYPE; |
103 i < syncable::MODEL_TYPE_COUNT; ++i) { | 129 i < syncable::MODEL_TYPE_COUNT; ++i) { |
104 synced_datatypes.insert(syncable::ModelTypeFromInt(i)); | 130 synced_datatypes.insert(syncable::ModelTypeFromInt(i)); |
105 } | 131 } |
106 return SetupSync(synced_datatypes); | 132 return SetupSync(synced_datatypes); |
107 } | 133 } |
108 | 134 |
109 bool ProfileSyncServiceHarness::SetupSync( | 135 bool ProfileSyncServiceHarness::SetupSync( |
110 const syncable::ModelTypeSet& synced_datatypes) { | 136 const syncable::ModelTypeSet& synced_datatypes) { |
111 // Initialize the sync client's profile sync service object. | 137 // Initialize the sync client's profile sync service object. |
112 service_ = profile_->GetProfileSyncService(""); | 138 service_ = profile_->GetProfileSyncService(); |
113 if (service_ == NULL) { | 139 if (service_ == NULL) { |
114 LOG(ERROR) << "SetupSync(): service_ is null."; | 140 LOG(ERROR) << "SetupSync(): service_ is null."; |
115 return false; | 141 return false; |
116 } | 142 } |
117 | 143 |
118 // Subscribe sync client to notifications from the profile sync service. | 144 // Subscribe sync client to notifications from the profile sync service. |
119 if (!service_->HasObserver(this)) | 145 if (!service_->HasObserver(this)) |
120 service_->AddObserver(this); | 146 service_->AddObserver(this); |
121 | 147 |
122 // Authenticate sync client using GAIA credentials. | 148 // Authenticate sync client using GAIA credentials. |
123 service_->signin()->StartSignIn(username_, password_, "", ""); | 149 service_->signin()->StartSignIn(username_, password_, "", ""); |
124 | 150 |
125 // Wait for the OnBackendInitialized() callback. | 151 // Wait for the OnBackendInitialized() callback. |
126 DCHECK_EQ(wait_state_, WAITING_FOR_ON_BACKEND_INITIALIZED); | 152 wait_state_ = WAITING_FOR_ON_BACKEND_INITIALIZED; |
127 if (!AwaitStatusChangeWithTimeout(kLiveSyncOperationTimeoutMs, | 153 if (!AwaitStatusChangeWithTimeout(kLiveSyncOperationTimeoutMs, |
128 "Waiting for OnBackendInitialized().")) { | 154 "Waiting for OnBackendInitialized().")) { |
129 LOG(ERROR) << "OnBackendInitialized() not seen after " | 155 LOG(ERROR) << "OnBackendInitialized() not seen after " |
130 << kLiveSyncOperationTimeoutMs / 1000 | 156 << kLiveSyncOperationTimeoutMs / 1000 |
131 << " seconds."; | 157 << " seconds."; |
132 return false; | 158 return false; |
133 } | 159 } |
134 | 160 |
135 // Choose the datatypes to be synced. If all datatypes are to be synced, | 161 // Choose the datatypes to be synced. If all datatypes are to be synced, |
136 // set sync_everything to true; otherwise, set it to false. | 162 // set sync_everything to true; otherwise, set it to false. |
137 bool sync_everything = (synced_datatypes.size() == | 163 bool sync_everything = (synced_datatypes.size() == |
138 (syncable::MODEL_TYPE_COUNT - syncable::FIRST_REAL_MODEL_TYPE)); | 164 (syncable::MODEL_TYPE_COUNT - syncable::FIRST_REAL_MODEL_TYPE)); |
139 service()->OnUserChoseDatatypes(sync_everything, synced_datatypes); | 165 service()->OnUserChoseDatatypes(sync_everything, synced_datatypes); |
140 | 166 |
141 // Wait for initial sync cycle to complete. | 167 // Wait for initial sync cycle to complete. |
142 DCHECK_EQ(wait_state_, WAITING_FOR_INITIAL_SYNC); | 168 DCHECK_EQ(wait_state_, WAITING_FOR_INITIAL_SYNC); |
143 if (!AwaitStatusChangeWithTimeout(kLiveSyncOperationTimeoutMs, | 169 if (!AwaitStatusChangeWithTimeout(kLiveSyncOperationTimeoutMs, |
144 "Waiting for initial sync cycle to complete.")) { | 170 "Waiting for initial sync cycle to complete.")) { |
145 LOG(ERROR) << "Initial sync cycle did not complete after " | 171 LOG(ERROR) << "Initial sync cycle did not complete after " |
146 << kLiveSyncOperationTimeoutMs / 1000 | 172 << kLiveSyncOperationTimeoutMs / 1000 |
147 << " seconds."; | 173 << " seconds."; |
148 return false; | 174 return false; |
149 } | 175 } |
150 | 176 |
| 177 // Indicate to the browser that sync setup is complete. |
| 178 service()->SetSyncSetupCompleted(); |
| 179 |
151 return true; | 180 return true; |
152 } | 181 } |
153 | 182 |
154 void ProfileSyncServiceHarness::SignalStateCompleteWithNextState( | 183 void ProfileSyncServiceHarness::SignalStateCompleteWithNextState( |
155 WaitState next_state) { | 184 WaitState next_state) { |
156 wait_state_ = next_state; | 185 wait_state_ = next_state; |
157 SignalStateComplete(); | 186 SignalStateComplete(); |
158 } | 187 } |
159 | 188 |
| 189 void ProfileSyncServiceHarness::SignalStateComplete() { |
| 190 MessageLoop::current()->Quit(); |
| 191 } |
| 192 |
160 bool ProfileSyncServiceHarness::RunStateChangeMachine() { | 193 bool ProfileSyncServiceHarness::RunStateChangeMachine() { |
161 WaitState original_wait_state = wait_state_; | 194 WaitState original_wait_state = wait_state_; |
162 switch (wait_state_) { | 195 switch (wait_state_) { |
163 case WAITING_FOR_ON_BACKEND_INITIALIZED: { | 196 case WAITING_FOR_ON_BACKEND_INITIALIZED: { |
164 LogClientInfo("WAITING_FOR_ON_BACKEND_INITIALIZED"); | 197 LogClientInfo("WAITING_FOR_ON_BACKEND_INITIALIZED"); |
165 if (service()->sync_initialized()) { | 198 if (service()->sync_initialized()) { |
166 // The sync backend is initialized. Start waiting for the first sync | 199 // The sync backend is initialized. Start waiting for the first sync |
167 // cycle to complete. | 200 // cycle to complete. |
168 SignalStateCompleteWithNextState(WAITING_FOR_INITIAL_SYNC); | 201 SignalStateCompleteWithNextState(WAITING_FOR_INITIAL_SYNC); |
169 } | 202 } |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 bool ProfileSyncServiceHarness::AwaitStatusChangeWithTimeout( | 379 bool ProfileSyncServiceHarness::AwaitStatusChangeWithTimeout( |
347 int timeout_milliseconds, | 380 int timeout_milliseconds, |
348 const std::string& reason) { | 381 const std::string& reason) { |
349 LogClientInfo("AwaitStatusChangeWithTimeout"); | 382 LogClientInfo("AwaitStatusChangeWithTimeout"); |
350 if (wait_state_ == SYNC_DISABLED) { | 383 if (wait_state_ == SYNC_DISABLED) { |
351 LOG(ERROR) << "Sync disabled for Client " << id_ << "."; | 384 LOG(ERROR) << "Sync disabled for Client " << id_ << "."; |
352 return false; | 385 return false; |
353 } | 386 } |
354 scoped_refptr<StateChangeTimeoutEvent> timeout_signal( | 387 scoped_refptr<StateChangeTimeoutEvent> timeout_signal( |
355 new StateChangeTimeoutEvent(this, reason)); | 388 new StateChangeTimeoutEvent(this, reason)); |
356 MessageLoopForUI* loop = MessageLoopForUI::current(); | 389 MessageLoop* loop = MessageLoop::current(); |
| 390 bool did_allow_nestable_tasks = loop->NestableTasksAllowed(); |
| 391 loop->SetNestableTasksAllowed(true); |
357 loop->PostDelayedTask( | 392 loop->PostDelayedTask( |
358 FROM_HERE, | 393 FROM_HERE, |
359 NewRunnableMethod(timeout_signal.get(), | 394 NewRunnableMethod(timeout_signal.get(), |
360 &StateChangeTimeoutEvent::Callback), | 395 &StateChangeTimeoutEvent::Callback), |
361 timeout_milliseconds); | 396 timeout_milliseconds); |
362 AwaitStatusChange(); | 397 loop->Run(); |
| 398 loop->SetNestableTasksAllowed(did_allow_nestable_tasks); |
363 LogClientInfo("AwaitStatusChangeWithTimeout succeeded"); | 399 LogClientInfo("AwaitStatusChangeWithTimeout succeeded"); |
364 return timeout_signal->Abort(); | 400 return timeout_signal->Abort(); |
365 } | 401 } |
366 | 402 |
367 ProfileSyncService::Status ProfileSyncServiceHarness::GetStatus() { | 403 ProfileSyncService::Status ProfileSyncServiceHarness::GetStatus() { |
368 DCHECK(service() != NULL) << "GetStatus(): service() is NULL."; | 404 DCHECK(service() != NULL) << "GetStatus(): service() is NULL."; |
369 return service()->QueryDetailedSyncStatus(); | 405 return service()->QueryDetailedSyncStatus(); |
370 } | 406 } |
371 | 407 |
372 bool ProfileSyncServiceHarness::IsSynced() { | 408 bool ProfileSyncServiceHarness::IsSynced() { |
| 409 if (service() == NULL) |
| 410 return false; |
373 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 411 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
374 // TODO(rsimha): Remove additional checks of snap->has_more_to_sync and | 412 // TODO(rsimha): Remove additional checks of snap->has_more_to_sync and |
375 // snap->unsynced_count once http://crbug.com/48989 is fixed. | 413 // snap->unsynced_count once http://crbug.com/48989 is fixed. |
376 return (service() && | 414 return (snap && |
377 snap && | |
378 ServiceIsPushingChanges() && | 415 ServiceIsPushingChanges() && |
379 GetStatus().notifications_enabled && | 416 GetStatus().notifications_enabled && |
380 !service()->backend()->HasUnsyncedItems() && | 417 !service()->backend()->HasUnsyncedItems() && |
381 !snap->has_more_to_sync && | 418 !snap->has_more_to_sync && |
382 snap->unsynced_count == 0); | 419 snap->unsynced_count == 0); |
383 } | 420 } |
384 | 421 |
385 const SyncSessionSnapshot* | 422 const SyncSessionSnapshot* |
386 ProfileSyncServiceHarness::GetLastSessionSnapshot() const { | 423 ProfileSyncServiceHarness::GetLastSessionSnapshot() const { |
387 DCHECK(service_ != NULL) << "Sync service has not yet been set up."; | 424 DCHECK(service_ != NULL) << "Sync service has not yet been set up."; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 | 508 |
472 int64 ProfileSyncServiceHarness::GetUpdatedTimestamp() { | 509 int64 ProfileSyncServiceHarness::GetUpdatedTimestamp() { |
473 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 510 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
474 DCHECK(snap != NULL) << "GetUpdatedTimestamp(): Sync snapshot is NULL."; | 511 DCHECK(snap != NULL) << "GetUpdatedTimestamp(): Sync snapshot is NULL."; |
475 DCHECK_LE(last_timestamp_, snap->max_local_timestamp); | 512 DCHECK_LE(last_timestamp_, snap->max_local_timestamp); |
476 last_timestamp_ = snap->max_local_timestamp; | 513 last_timestamp_ = snap->max_local_timestamp; |
477 return last_timestamp_; | 514 return last_timestamp_; |
478 } | 515 } |
479 | 516 |
480 void ProfileSyncServiceHarness::LogClientInfo(std::string message) { | 517 void ProfileSyncServiceHarness::LogClientInfo(std::string message) { |
481 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); | 518 if (service()) { |
482 if (snap) { | 519 const SyncSessionSnapshot* snap = GetLastSessionSnapshot(); |
483 VLOG(1) << "Client " << id_ << ": " << message | 520 if (snap) { |
484 << ": max_local_timestamp: " << snap->max_local_timestamp | 521 VLOG(1) << "Client " << id_ << ": " << message |
485 << ", has_more_to_sync: " << snap->has_more_to_sync | 522 << ": max_local_timestamp: " << snap->max_local_timestamp |
486 << ", unsynced_count: " << snap->unsynced_count | 523 << ", has_more_to_sync: " << snap->has_more_to_sync |
487 << ", has_unsynced_items: " | 524 << ", unsynced_count: " << snap->unsynced_count |
488 << service()->backend()->HasUnsyncedItems() | 525 << ", has_unsynced_items: " |
489 << ", notifications_enabled: " | 526 << service()->backend()->HasUnsyncedItems() |
490 << GetStatus().notifications_enabled | 527 << ", notifications_enabled: " |
491 << ", service_is_pushing_changes: " << ServiceIsPushingChanges(); | 528 << GetStatus().notifications_enabled |
| 529 << ", service_is_pushing_changes: " << ServiceIsPushingChanges(); |
| 530 } else { |
| 531 VLOG(1) << "Client " << id_ << ": " << message |
| 532 << ": Sync session snapshot not available."; |
| 533 } |
492 } else { | 534 } else { |
493 VLOG(1) << "Client " << id_ << ": " << message | 535 VLOG(1) << "Client " << id_ << ": " << message |
494 << ": Sync session snapshot not available."; | 536 << ": Sync service not available."; |
495 } | 537 } |
496 } | 538 } |
OLD | NEW |