OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/sync/profile_sync_service_harness.h" | 5 #include "chrome/browser/sync/profile_sync_service_harness.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <ostream> | 9 #include <ostream> |
10 #include <set> | 10 #include <set> |
11 #include <sstream> | 11 #include <sstream> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/base64.h" | 14 #include "base/base64.h" |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
18 #include "base/json/json_writer.h" | 18 #include "base/json/json_writer.h" |
19 #include "base/location.h" | 19 #include "base/location.h" |
20 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
22 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
23 #include "base/prefs/pref_service.h" | 23 #include "base/prefs/pref_service.h" |
| 24 #include "chrome/browser/invalidation/p2p_invalidation_service.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/signin/signin_manager_base.h" | 26 #include "chrome/browser/signin/signin_manager_base.h" |
26 #include "chrome/browser/signin/token_service.h" | 27 #include "chrome/browser/signin/token_service.h" |
27 #include "chrome/browser/signin/token_service_factory.h" | 28 #include "chrome/browser/signin/token_service_factory.h" |
28 #include "chrome/browser/sync/about_sync_util.h" | 29 #include "chrome/browser/sync/about_sync_util.h" |
29 #include "chrome/browser/sync/glue/data_type_controller.h" | 30 #include "chrome/browser/sync/glue/data_type_controller.h" |
30 #include "chrome/browser/sync/profile_sync_service_factory.h" | 31 #include "chrome/browser/sync/profile_sync_service_factory.h" |
31 #include "chrome/common/chrome_notification_types.h" | 32 #include "chrome/common/chrome_notification_types.h" |
32 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
33 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
34 #include "content/public/browser/notification_service.h" | 35 #include "content/public/browser/notification_service.h" |
35 #include "google_apis/gaia/gaia_constants.h" | 36 #include "google_apis/gaia/gaia_constants.h" |
36 #include "sync/internal_api/public/base/progress_marker_map.h" | 37 #include "sync/internal_api/public/base/progress_marker_map.h" |
37 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" | 38 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
38 #include "sync/internal_api/public/util/sync_string_conversions.h" | 39 #include "sync/internal_api/public/util/sync_string_conversions.h" |
39 | 40 |
40 using syncer::sessions::SyncSessionSnapshot; | 41 using syncer::sessions::SyncSessionSnapshot; |
| 42 using invalidation::P2PInvalidationService; |
41 | 43 |
42 // TODO(rsimha): Remove the following lines once crbug.com/91863 is fixed. | 44 // TODO(rsimha): Remove the following lines once crbug.com/91863 is fixed. |
43 // The amount of time for which we wait for a live sync operation to complete. | 45 // The amount of time for which we wait for a live sync operation to complete. |
44 static const int kLiveSyncOperationTimeoutMs = 45000; | 46 static const int kLiveSyncOperationTimeoutMs = 45000; |
45 | 47 |
46 // The amount of time we wait for test cases that verify exponential backoff. | 48 // The amount of time we wait for test cases that verify exponential backoff. |
47 static const int kExponentialBackoffVerificationTimeoutMs = 60000; | 49 static const int kExponentialBackoffVerificationTimeoutMs = 60000; |
48 | 50 |
49 // Simple class to implement a timeout using PostDelayedTask. If it is not | 51 // Simple class to implement a timeout using PostDelayedTask. If it is not |
50 // aborted before picked up by a message queue, then it asserts with the message | 52 // aborted before picked up by a message queue, then it asserts with the message |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 101 } |
100 } | 102 } |
101 } | 103 } |
102 | 104 |
103 bool StateChangeTimeoutEvent::Abort() { | 105 bool StateChangeTimeoutEvent::Abort() { |
104 aborted_ = true; | 106 aborted_ = true; |
105 caller_ = NULL; | 107 caller_ = NULL; |
106 return !did_timeout_; | 108 return !did_timeout_; |
107 } | 109 } |
108 | 110 |
| 111 // static |
| 112 ProfileSyncServiceHarness* ProfileSyncServiceHarness::Create( |
| 113 Profile* profile, |
| 114 const std::string& username, |
| 115 const std::string& password) { |
| 116 return new ProfileSyncServiceHarness(profile, username, password, NULL); |
| 117 } |
| 118 |
| 119 // static |
| 120 ProfileSyncServiceHarness* ProfileSyncServiceHarness::CreateForIntegrationTest( |
| 121 Profile* profile, |
| 122 const std::string& username, |
| 123 const std::string& password, |
| 124 P2PInvalidationService* p2p_invalidation_service) { |
| 125 return new ProfileSyncServiceHarness(profile, |
| 126 username, |
| 127 password, |
| 128 p2p_invalidation_service); |
| 129 } |
| 130 |
109 ProfileSyncServiceHarness::ProfileSyncServiceHarness( | 131 ProfileSyncServiceHarness::ProfileSyncServiceHarness( |
110 Profile* profile, | 132 Profile* profile, |
111 const std::string& username, | 133 const std::string& username, |
112 const std::string& password) | 134 const std::string& password, |
| 135 P2PInvalidationService* p2p_invalidation_service) |
113 : waiting_for_encryption_type_(syncer::UNSPECIFIED), | 136 : waiting_for_encryption_type_(syncer::UNSPECIFIED), |
114 wait_state_(INITIAL_WAIT_STATE), | 137 wait_state_(INITIAL_WAIT_STATE), |
115 profile_(profile), | 138 profile_(profile), |
116 service_(NULL), | 139 service_(NULL), |
| 140 p2p_invalidation_service_(p2p_invalidation_service), |
117 progress_marker_partner_(NULL), | 141 progress_marker_partner_(NULL), |
118 username_(username), | 142 username_(username), |
119 password_(password), | 143 password_(password), |
120 profile_debug_name_(profile->GetDebugName()), | 144 profile_debug_name_(profile->GetDebugName()), |
121 waiting_for_status_change_(false) { | 145 waiting_for_status_change_(false) { |
122 if (IsSyncAlreadySetup()) { | 146 if (IsSyncAlreadySetup()) { |
123 service_ = ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 147 service_ = ProfileSyncServiceFactory::GetInstance()->GetForProfile( |
124 profile_); | 148 profile_); |
125 service_->AddObserver(this); | 149 service_->AddObserver(this); |
126 ignore_result(TryListeningToMigrationEvents()); | 150 ignore_result(TryListeningToMigrationEvents()); |
127 wait_state_ = FULLY_SYNCED; | 151 wait_state_ = FULLY_SYNCED; |
128 } | 152 } |
129 } | 153 } |
130 | 154 |
131 ProfileSyncServiceHarness::~ProfileSyncServiceHarness() { | 155 ProfileSyncServiceHarness::~ProfileSyncServiceHarness() { |
132 if (service_->HasObserver(this)) | 156 if (service_->HasObserver(this)) |
133 service_->RemoveObserver(this); | 157 service_->RemoveObserver(this); |
134 } | 158 } |
135 | 159 |
136 // static | |
137 ProfileSyncServiceHarness* ProfileSyncServiceHarness::CreateAndAttach( | |
138 Profile* profile) { | |
139 ProfileSyncServiceFactory* f = ProfileSyncServiceFactory::GetInstance(); | |
140 if (!f->HasProfileSyncService(profile)) { | |
141 NOTREACHED() << "Profile has never signed into sync."; | |
142 return NULL; | |
143 } | |
144 return new ProfileSyncServiceHarness(profile, std::string(), std::string()); | |
145 } | |
146 | |
147 void ProfileSyncServiceHarness::SetCredentials(const std::string& username, | 160 void ProfileSyncServiceHarness::SetCredentials(const std::string& username, |
148 const std::string& password) { | 161 const std::string& password) { |
149 username_ = username; | 162 username_ = username; |
150 password_ = password; | 163 password_ = password; |
151 } | 164 } |
152 | 165 |
153 bool ProfileSyncServiceHarness::IsSyncAlreadySetup() { | 166 bool ProfileSyncServiceHarness::IsSyncAlreadySetup() { |
154 return ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( | 167 return ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( |
155 profile_); | 168 profile_); |
156 } | 169 } |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 // classes using the the UI message loop. Defer to their handling. | 488 // classes using the the UI message loop. Defer to their handling. |
476 break; | 489 break; |
477 } | 490 } |
478 return original_wait_state != wait_state_; | 491 return original_wait_state != wait_state_; |
479 } | 492 } |
480 | 493 |
481 void ProfileSyncServiceHarness::OnStateChanged() { | 494 void ProfileSyncServiceHarness::OnStateChanged() { |
482 RunStateChangeMachine(); | 495 RunStateChangeMachine(); |
483 } | 496 } |
484 | 497 |
| 498 void ProfileSyncServiceHarness::OnSyncCycleCompleted() { |
| 499 // Integration tests still use p2p notifications. |
| 500 const SyncSessionSnapshot& snap = GetLastSessionSnapshot(); |
| 501 bool is_notifiable_commit = |
| 502 (snap.model_neutral_state().num_successful_commits > 0); |
| 503 if (is_notifiable_commit && p2p_invalidation_service_) { |
| 504 const syncer::ObjectIdInvalidationMap& invalidation_map = |
| 505 ModelTypeInvalidationMapToObjectIdInvalidationMap( |
| 506 snap.source().types); |
| 507 p2p_invalidation_service_->SendInvalidation(invalidation_map); |
| 508 } |
| 509 |
| 510 OnStateChanged(); |
| 511 } |
| 512 |
485 void ProfileSyncServiceHarness::OnMigrationStateChange() { | 513 void ProfileSyncServiceHarness::OnMigrationStateChange() { |
486 // Update migration state. | 514 // Update migration state. |
487 if (HasPendingBackendMigration()) { | 515 if (HasPendingBackendMigration()) { |
488 // Merge current pending migration types into | 516 // Merge current pending migration types into |
489 // |pending_migration_types_|. | 517 // |pending_migration_types_|. |
490 pending_migration_types_.PutAll( | 518 pending_migration_types_.PutAll( |
491 service()->GetBackendMigratorForTest()-> | 519 service()->GetBackendMigratorForTest()-> |
492 GetPendingMigrationTypesForTest()); | 520 GetPendingMigrationTypesForTest()); |
493 DVLOG(1) << profile_debug_name_ << ": new pending migration types " | 521 DVLOG(1) << profile_debug_name_ << ": new pending migration types " |
494 << syncer::ModelTypeSetToString(pending_migration_types_); | 522 << syncer::ModelTypeSetToString(pending_migration_types_); |
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1129 | 1157 |
1130 std::string ProfileSyncServiceHarness::GetServiceStatus() { | 1158 std::string ProfileSyncServiceHarness::GetServiceStatus() { |
1131 scoped_ptr<DictionaryValue> value( | 1159 scoped_ptr<DictionaryValue> value( |
1132 sync_ui_util::ConstructAboutInformation(service_)); | 1160 sync_ui_util::ConstructAboutInformation(service_)); |
1133 std::string service_status; | 1161 std::string service_status; |
1134 base::JSONWriter::WriteWithOptions(value.get(), | 1162 base::JSONWriter::WriteWithOptions(value.get(), |
1135 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 1163 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
1136 &service_status); | 1164 &service_status); |
1137 return service_status; | 1165 return service_status; |
1138 } | 1166 } |
OLD | NEW |