| 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/test/base/testing_profile.h" | 5 #include "chrome/test/base/testing_profile.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. | 160 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. |
| 161 const char TestingProfile::kTestUserProfileDir[] = "test-user"; | 161 const char TestingProfile::kTestUserProfileDir[] = "test-user"; |
| 162 #else | 162 #else |
| 163 const char TestingProfile::kTestUserProfileDir[] = "Default"; | 163 const char TestingProfile::kTestUserProfileDir[] = "Default"; |
| 164 #endif | 164 #endif |
| 165 | 165 |
| 166 TestingProfile::TestingProfile() | 166 TestingProfile::TestingProfile() |
| 167 : start_time_(Time::Now()), | 167 : start_time_(Time::Now()), |
| 168 testing_prefs_(NULL), | 168 testing_prefs_(NULL), |
| 169 incognito_(false), | 169 incognito_(false), |
| 170 force_incognito_(false), |
| 170 original_profile_(NULL), | 171 original_profile_(NULL), |
| 171 last_session_exited_cleanly_(true), | 172 last_session_exited_cleanly_(true), |
| 172 browser_context_dependency_manager_( | 173 browser_context_dependency_manager_( |
| 173 BrowserContextDependencyManager::GetInstance()), | 174 BrowserContextDependencyManager::GetInstance()), |
| 174 resource_context_(NULL), | 175 resource_context_(NULL), |
| 175 delegate_(NULL) { | 176 delegate_(NULL) { |
| 176 CreateTempProfileDir(); | 177 CreateTempProfileDir(); |
| 177 profile_path_ = temp_dir_.path(); | 178 profile_path_ = temp_dir_.path(); |
| 178 | 179 |
| 179 Init(); | 180 Init(); |
| 180 FinishInit(); | 181 FinishInit(); |
| 181 } | 182 } |
| 182 | 183 |
| 183 TestingProfile::TestingProfile(const base::FilePath& path) | 184 TestingProfile::TestingProfile(const base::FilePath& path) |
| 184 : start_time_(Time::Now()), | 185 : start_time_(Time::Now()), |
| 185 testing_prefs_(NULL), | 186 testing_prefs_(NULL), |
| 186 incognito_(false), | 187 incognito_(false), |
| 188 force_incognito_(false), |
| 187 original_profile_(NULL), | 189 original_profile_(NULL), |
| 188 last_session_exited_cleanly_(true), | 190 last_session_exited_cleanly_(true), |
| 189 profile_path_(path), | 191 profile_path_(path), |
| 190 browser_context_dependency_manager_( | 192 browser_context_dependency_manager_( |
| 191 BrowserContextDependencyManager::GetInstance()), | 193 BrowserContextDependencyManager::GetInstance()), |
| 192 resource_context_(NULL), | 194 resource_context_(NULL), |
| 193 delegate_(NULL) { | 195 delegate_(NULL) { |
| 194 Init(); | 196 Init(); |
| 195 FinishInit(); | 197 FinishInit(); |
| 196 } | 198 } |
| 197 | 199 |
| 198 TestingProfile::TestingProfile(const base::FilePath& path, | 200 TestingProfile::TestingProfile(const base::FilePath& path, |
| 199 Delegate* delegate) | 201 Delegate* delegate) |
| 200 : start_time_(Time::Now()), | 202 : start_time_(Time::Now()), |
| 201 testing_prefs_(NULL), | 203 testing_prefs_(NULL), |
| 202 incognito_(false), | 204 incognito_(false), |
| 205 force_incognito_(false), |
| 203 original_profile_(NULL), | 206 original_profile_(NULL), |
| 204 last_session_exited_cleanly_(true), | 207 last_session_exited_cleanly_(true), |
| 205 profile_path_(path), | 208 profile_path_(path), |
| 206 browser_context_dependency_manager_( | 209 browser_context_dependency_manager_( |
| 207 BrowserContextDependencyManager::GetInstance()), | 210 BrowserContextDependencyManager::GetInstance()), |
| 208 resource_context_(NULL), | 211 resource_context_(NULL), |
| 209 delegate_(delegate) { | 212 delegate_(delegate) { |
| 210 Init(); | 213 Init(); |
| 211 if (delegate_) { | 214 if (delegate_) { |
| 212 base::MessageLoop::current()->PostTask( | 215 base::MessageLoop::current()->PostTask( |
| 213 FROM_HERE, | 216 FROM_HERE, |
| 214 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); | 217 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); |
| 215 } else { | 218 } else { |
| 216 FinishInit(); | 219 FinishInit(); |
| 217 } | 220 } |
| 218 } | 221 } |
| 219 | 222 |
| 220 TestingProfile::TestingProfile( | 223 TestingProfile::TestingProfile( |
| 221 const base::FilePath& path, | 224 const base::FilePath& path, |
| 222 Delegate* delegate, | 225 Delegate* delegate, |
| 223 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, | 226 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, |
| 224 scoped_ptr<PrefServiceSyncable> prefs) | 227 scoped_ptr<PrefServiceSyncable> prefs, |
| 228 bool incognito, |
| 229 const TestingFactories& factories) |
| 225 : start_time_(Time::Now()), | 230 : start_time_(Time::Now()), |
| 226 prefs_(prefs.release()), | 231 prefs_(prefs.release()), |
| 227 testing_prefs_(NULL), | 232 testing_prefs_(NULL), |
| 228 incognito_(false), | 233 incognito_(incognito), |
| 234 force_incognito_(false), |
| 229 original_profile_(NULL), | 235 original_profile_(NULL), |
| 230 last_session_exited_cleanly_(true), | 236 last_session_exited_cleanly_(true), |
| 231 extension_special_storage_policy_(extension_policy), | 237 extension_special_storage_policy_(extension_policy), |
| 232 profile_path_(path), | 238 profile_path_(path), |
| 233 browser_context_dependency_manager_( | 239 browser_context_dependency_manager_( |
| 234 BrowserContextDependencyManager::GetInstance()), | 240 BrowserContextDependencyManager::GetInstance()), |
| 235 resource_context_(NULL), | 241 resource_context_(NULL), |
| 236 delegate_(delegate) { | 242 delegate_(delegate) { |
| 237 | 243 |
| 238 // If no profile path was supplied, create one. | 244 // If no profile path was supplied, create one. |
| 239 if (profile_path_.empty()) { | 245 if (profile_path_.empty()) { |
| 240 CreateTempProfileDir(); | 246 CreateTempProfileDir(); |
| 241 profile_path_ = temp_dir_.path(); | 247 profile_path_ = temp_dir_.path(); |
| 242 } | 248 } |
| 243 | 249 |
| 250 // Set any testing factories prior to initializing the services. |
| 251 for (TestingFactories::const_iterator it = factories.begin(); |
| 252 it != factories.end(); ++it) { |
| 253 it->first->SetTestingFactory(this, it->second); |
| 254 } |
| 255 |
| 244 Init(); | 256 Init(); |
| 245 // If caller supplied a delegate, delay the FinishInit invocation until other | 257 // If caller supplied a delegate, delay the FinishInit invocation until other |
| 246 // tasks have run. | 258 // tasks have run. |
| 247 // TODO(atwilson): See if this is still required once we convert the current | 259 // TODO(atwilson): See if this is still required once we convert the current |
| 248 // users of the constructor that takes a Delegate* param. | 260 // users of the constructor that takes a Delegate* param. |
| 249 if (delegate_) { | 261 if (delegate_) { |
| 250 base::MessageLoop::current()->PostTask( | 262 base::MessageLoop::current()->PostTask( |
| 251 FROM_HERE, | 263 FROM_HERE, |
| 252 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); | 264 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); |
| 253 } else { | 265 } else { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 file_util::CreateDirectory(profile_path_); | 314 file_util::CreateDirectory(profile_path_); |
| 303 | 315 |
| 304 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl | 316 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl |
| 305 // anymore, after converting the PrefService to a PKS. Until then it must | 317 // anymore, after converting the PrefService to a PKS. Until then it must |
| 306 // be associated with a TestingProfile too. | 318 // be associated with a TestingProfile too. |
| 307 CreateProfilePolicyConnector(); | 319 CreateProfilePolicyConnector(); |
| 308 | 320 |
| 309 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( | 321 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( |
| 310 this, extensions::TestExtensionSystem::Build); | 322 this, extensions::TestExtensionSystem::Build); |
| 311 | 323 |
| 312 browser_context_dependency_manager_->CreateBrowserContextServices(this, true); | 324 // If there is no separate original profile specified for this profile, then |
| 325 // force preferences to be registered - this allows tests to create a |
| 326 // standalone incognito profile while still having prefs registered. |
| 327 browser_context_dependency_manager_->CreateBrowserContextServicesForTest( |
| 328 this, !original_profile_); |
| 313 | 329 |
| 314 #if defined(ENABLE_NOTIFICATIONS) | 330 #if defined(ENABLE_NOTIFICATIONS) |
| 315 // Install profile keyed service factory hooks for dummy/test services | 331 // Install profile keyed service factory hooks for dummy/test services |
| 316 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory( | 332 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory( |
| 317 this, CreateTestDesktopNotificationService); | 333 this, CreateTestDesktopNotificationService); |
| 318 #endif | 334 #endif |
| 319 } | 335 } |
| 320 | 336 |
| 321 void TestingProfile::FinishInit() { | 337 void TestingProfile::FinishInit() { |
| 322 DCHECK(content::NotificationService::current()); | 338 DCHECK(content::NotificationService::current()); |
| 323 content::NotificationService::current()->Notify( | 339 content::NotificationService::current()->Notify( |
| 324 chrome::NOTIFICATION_PROFILE_CREATED, | 340 chrome::NOTIFICATION_PROFILE_CREATED, |
| 325 content::Source<Profile>(static_cast<Profile*>(this)), | 341 content::Source<Profile>(static_cast<Profile*>(this)), |
| 326 content::NotificationService::NoDetails()); | 342 content::NotificationService::NoDetails()); |
| 327 | 343 |
| 328 if (delegate_) | 344 if (delegate_) |
| 329 delegate_->OnProfileCreated(this, true, false); | 345 delegate_->OnProfileCreated(this, true, false); |
| 330 } | 346 } |
| 331 | 347 |
| 332 TestingProfile::~TestingProfile() { | 348 TestingProfile::~TestingProfile() { |
| 349 // Revert to non-incognito mode before shutdown. |
| 350 force_incognito_ = false; |
| 351 |
| 333 // Any objects holding live URLFetchers should be deleted before teardown. | 352 // Any objects holding live URLFetchers should be deleted before teardown. |
| 334 TemplateURLFetcherFactory::ShutdownForProfile(this); | 353 TemplateURLFetcherFactory::ShutdownForProfile(this); |
| 335 | 354 |
| 336 MaybeSendDestroyedNotification(); | 355 MaybeSendDestroyedNotification(); |
| 337 | 356 |
| 338 browser_context_dependency_manager_->DestroyBrowserContextServices(this); | 357 browser_context_dependency_manager_->DestroyBrowserContextServices(this); |
| 339 | 358 |
| 340 if (host_content_settings_map_.get()) | 359 if (host_content_settings_map_.get()) |
| 341 host_content_settings_map_->ShutdownOnUIThread(); | 360 host_content_settings_map_->ShutdownOnUIThread(); |
| 342 | 361 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 | 538 |
| 520 TestingProfile* TestingProfile::AsTestingProfile() { | 539 TestingProfile* TestingProfile::AsTestingProfile() { |
| 521 return this; | 540 return this; |
| 522 } | 541 } |
| 523 | 542 |
| 524 std::string TestingProfile::GetProfileName() { | 543 std::string TestingProfile::GetProfileName() { |
| 525 return std::string("testing_profile"); | 544 return std::string("testing_profile"); |
| 526 } | 545 } |
| 527 | 546 |
| 528 bool TestingProfile::IsOffTheRecord() const { | 547 bool TestingProfile::IsOffTheRecord() const { |
| 529 return incognito_; | 548 return force_incognito_ || incognito_; |
| 530 } | 549 } |
| 531 | 550 |
| 532 void TestingProfile::SetOffTheRecordProfile(Profile* profile) { | 551 void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) { |
| 533 incognito_profile_.reset(profile); | 552 DCHECK(!IsOffTheRecord()); |
| 553 incognito_profile_ = profile.Pass(); |
| 534 } | 554 } |
| 535 | 555 |
| 536 void TestingProfile::SetOriginalProfile(Profile* profile) { | 556 void TestingProfile::SetOriginalProfile(Profile* profile) { |
| 557 DCHECK(IsOffTheRecord()); |
| 537 original_profile_ = profile; | 558 original_profile_ = profile; |
| 538 } | 559 } |
| 539 | 560 |
| 540 Profile* TestingProfile::GetOffTheRecordProfile() { | 561 Profile* TestingProfile::GetOffTheRecordProfile() { |
| 562 if (IsOffTheRecord()) |
| 563 return this; |
| 541 return incognito_profile_.get(); | 564 return incognito_profile_.get(); |
| 542 } | 565 } |
| 543 | 566 |
| 544 bool TestingProfile::HasOffTheRecordProfile() { | 567 bool TestingProfile::HasOffTheRecordProfile() { |
| 545 return incognito_profile_.get() != NULL; | 568 return incognito_profile_.get() != NULL; |
| 546 } | 569 } |
| 547 | 570 |
| 548 Profile* TestingProfile::GetOriginalProfile() { | 571 Profile* TestingProfile::GetOriginalProfile() { |
| 549 if (original_profile_) | 572 if (original_profile_) |
| 550 return original_profile_; | 573 return original_profile_; |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 | 813 |
| 791 bool TestingProfile::IsGuestSession() const { | 814 bool TestingProfile::IsGuestSession() const { |
| 792 return false; | 815 return false; |
| 793 } | 816 } |
| 794 Profile::ExitType TestingProfile::GetLastSessionExitType() { | 817 Profile::ExitType TestingProfile::GetLastSessionExitType() { |
| 795 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED; | 818 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED; |
| 796 } | 819 } |
| 797 | 820 |
| 798 TestingProfile::Builder::Builder() | 821 TestingProfile::Builder::Builder() |
| 799 : build_called_(false), | 822 : build_called_(false), |
| 800 delegate_(NULL) { | 823 delegate_(NULL), |
| 824 incognito_(false) { |
| 801 } | 825 } |
| 802 | 826 |
| 803 TestingProfile::Builder::~Builder() { | 827 TestingProfile::Builder::~Builder() { |
| 804 } | 828 } |
| 805 | 829 |
| 806 void TestingProfile::Builder::SetPath(const base::FilePath& path) { | 830 void TestingProfile::Builder::SetPath(const base::FilePath& path) { |
| 807 path_ = path; | 831 path_ = path; |
| 808 } | 832 } |
| 809 | 833 |
| 810 void TestingProfile::Builder::SetDelegate(Delegate* delegate) { | 834 void TestingProfile::Builder::SetDelegate(Delegate* delegate) { |
| 811 delegate_ = delegate; | 835 delegate_ = delegate; |
| 812 } | 836 } |
| 813 | 837 |
| 814 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy( | 838 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy( |
| 815 scoped_refptr<ExtensionSpecialStoragePolicy> policy) { | 839 scoped_refptr<ExtensionSpecialStoragePolicy> policy) { |
| 816 extension_policy_ = policy; | 840 extension_policy_ = policy; |
| 817 } | 841 } |
| 818 | 842 |
| 819 void TestingProfile::Builder::SetPrefService( | 843 void TestingProfile::Builder::SetPrefService( |
| 820 scoped_ptr<PrefServiceSyncable> prefs) { | 844 scoped_ptr<PrefServiceSyncable> prefs) { |
| 821 pref_service_ = prefs.Pass(); | 845 pref_service_ = prefs.Pass(); |
| 822 } | 846 } |
| 823 | 847 |
| 848 void TestingProfile::Builder::SetIncognito() { |
| 849 incognito_ = true; |
| 850 } |
| 851 |
| 852 void TestingProfile::Builder::AddTestingFactory( |
| 853 BrowserContextKeyedServiceFactory* service_factory, |
| 854 BrowserContextKeyedServiceFactory::FactoryFunction callback) { |
| 855 testing_factories_.push_back(std::make_pair(service_factory, callback)); |
| 856 } |
| 857 |
| 824 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { | 858 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { |
| 825 DCHECK(!build_called_); | 859 DCHECK(!build_called_); |
| 826 build_called_ = true; | 860 build_called_ = true; |
| 827 return scoped_ptr<TestingProfile>(new TestingProfile( | 861 return scoped_ptr<TestingProfile>(new TestingProfile( |
| 828 path_, | 862 path_, |
| 829 delegate_, | 863 delegate_, |
| 830 extension_policy_, | 864 extension_policy_, |
| 831 pref_service_.Pass())); | 865 pref_service_.Pass(), |
| 866 incognito_, |
| 867 testing_factories_)); |
| 832 } | 868 } |
| OLD | NEW |