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