| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 content::NotificationService::current()->Notify( | 335 content::NotificationService::current()->Notify( |
| 324 chrome::NOTIFICATION_PROFILE_CREATED, | 336 chrome::NOTIFICATION_PROFILE_CREATED, |
| 325 content::Source<Profile>(static_cast<Profile*>(this)), | 337 content::Source<Profile>(static_cast<Profile*>(this)), |
| 326 content::NotificationService::NoDetails()); | 338 content::NotificationService::NoDetails()); |
| 327 | 339 |
| 328 if (delegate_) | 340 if (delegate_) |
| 329 delegate_->OnProfileCreated(this, true, false); | 341 delegate_->OnProfileCreated(this, true, false); |
| 330 } | 342 } |
| 331 | 343 |
| 332 TestingProfile::~TestingProfile() { | 344 TestingProfile::~TestingProfile() { |
| 345 // Revert to non-incognito mode before shutdown. |
| 346 force_incognito_ = false; |
| 347 |
| 333 // Any objects holding live URLFetchers should be deleted before teardown. | 348 // Any objects holding live URLFetchers should be deleted before teardown. |
| 334 TemplateURLFetcherFactory::ShutdownForProfile(this); | 349 TemplateURLFetcherFactory::ShutdownForProfile(this); |
| 335 | 350 |
| 336 MaybeSendDestroyedNotification(); | 351 MaybeSendDestroyedNotification(); |
| 337 | 352 |
| 338 browser_context_dependency_manager_->DestroyBrowserContextServices(this); | 353 browser_context_dependency_manager_->DestroyBrowserContextServices(this); |
| 339 | 354 |
| 340 if (host_content_settings_map_.get()) | 355 if (host_content_settings_map_.get()) |
| 341 host_content_settings_map_->ShutdownOnUIThread(); | 356 host_content_settings_map_->ShutdownOnUIThread(); |
| 342 | 357 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 | 534 |
| 520 TestingProfile* TestingProfile::AsTestingProfile() { | 535 TestingProfile* TestingProfile::AsTestingProfile() { |
| 521 return this; | 536 return this; |
| 522 } | 537 } |
| 523 | 538 |
| 524 std::string TestingProfile::GetProfileName() { | 539 std::string TestingProfile::GetProfileName() { |
| 525 return std::string("testing_profile"); | 540 return std::string("testing_profile"); |
| 526 } | 541 } |
| 527 | 542 |
| 528 bool TestingProfile::IsOffTheRecord() const { | 543 bool TestingProfile::IsOffTheRecord() const { |
| 529 return incognito_; | 544 return force_incognito_ || incognito_; |
| 530 } | 545 } |
| 531 | 546 |
| 532 void TestingProfile::SetOffTheRecordProfile(Profile* profile) { | 547 void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) { |
| 533 incognito_profile_.reset(profile); | 548 DCHECK(!IsOffTheRecord()); |
| 549 incognito_profile_ = profile.Pass(); |
| 534 } | 550 } |
| 535 | 551 |
| 536 void TestingProfile::SetOriginalProfile(Profile* profile) { | 552 void TestingProfile::SetOriginalProfile(Profile* profile) { |
| 553 DCHECK(IsOffTheRecord()); |
| 537 original_profile_ = profile; | 554 original_profile_ = profile; |
| 538 } | 555 } |
| 539 | 556 |
| 540 Profile* TestingProfile::GetOffTheRecordProfile() { | 557 Profile* TestingProfile::GetOffTheRecordProfile() { |
| 541 return incognito_profile_.get(); | 558 return incognito_profile_.get(); |
| 542 } | 559 } |
| 543 | 560 |
| 544 bool TestingProfile::HasOffTheRecordProfile() { | 561 bool TestingProfile::HasOffTheRecordProfile() { |
| 545 return incognito_profile_.get() != NULL; | 562 return incognito_profile_.get() != NULL; |
| 546 } | 563 } |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 | 807 |
| 791 bool TestingProfile::IsGuestSession() const { | 808 bool TestingProfile::IsGuestSession() const { |
| 792 return false; | 809 return false; |
| 793 } | 810 } |
| 794 Profile::ExitType TestingProfile::GetLastSessionExitType() { | 811 Profile::ExitType TestingProfile::GetLastSessionExitType() { |
| 795 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED; | 812 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED; |
| 796 } | 813 } |
| 797 | 814 |
| 798 TestingProfile::Builder::Builder() | 815 TestingProfile::Builder::Builder() |
| 799 : build_called_(false), | 816 : build_called_(false), |
| 800 delegate_(NULL) { | 817 delegate_(NULL), |
| 818 incognito_(false) { |
| 801 } | 819 } |
| 802 | 820 |
| 803 TestingProfile::Builder::~Builder() { | 821 TestingProfile::Builder::~Builder() { |
| 804 } | 822 } |
| 805 | 823 |
| 806 void TestingProfile::Builder::SetPath(const base::FilePath& path) { | 824 void TestingProfile::Builder::SetPath(const base::FilePath& path) { |
| 807 path_ = path; | 825 path_ = path; |
| 808 } | 826 } |
| 809 | 827 |
| 810 void TestingProfile::Builder::SetDelegate(Delegate* delegate) { | 828 void TestingProfile::Builder::SetDelegate(Delegate* delegate) { |
| 811 delegate_ = delegate; | 829 delegate_ = delegate; |
| 812 } | 830 } |
| 813 | 831 |
| 814 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy( | 832 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy( |
| 815 scoped_refptr<ExtensionSpecialStoragePolicy> policy) { | 833 scoped_refptr<ExtensionSpecialStoragePolicy> policy) { |
| 816 extension_policy_ = policy; | 834 extension_policy_ = policy; |
| 817 } | 835 } |
| 818 | 836 |
| 819 void TestingProfile::Builder::SetPrefService( | 837 void TestingProfile::Builder::SetPrefService( |
| 820 scoped_ptr<PrefServiceSyncable> prefs) { | 838 scoped_ptr<PrefServiceSyncable> prefs) { |
| 821 pref_service_ = prefs.Pass(); | 839 pref_service_ = prefs.Pass(); |
| 822 } | 840 } |
| 823 | 841 |
| 842 void TestingProfile::Builder::SetIncognito() { |
| 843 incognito_ = true; |
| 844 } |
| 845 |
| 846 void TestingProfile::Builder::AddTestingFactory( |
| 847 BrowserContextKeyedServiceFactory* service_factory, |
| 848 BrowserContextKeyedServiceFactory::FactoryFunction callback) { |
| 849 testing_factories_.push_back(std::make_pair(service_factory, callback)); |
| 850 } |
| 851 |
| 824 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { | 852 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { |
| 825 DCHECK(!build_called_); | 853 DCHECK(!build_called_); |
| 826 build_called_ = true; | 854 build_called_ = true; |
| 827 return scoped_ptr<TestingProfile>(new TestingProfile( | 855 return scoped_ptr<TestingProfile>(new TestingProfile( |
| 828 path_, | 856 path_, |
| 829 delegate_, | 857 delegate_, |
| 830 extension_policy_, | 858 extension_policy_, |
| 831 pref_service_.Pass())); | 859 pref_service_.Pass(), |
| 860 incognito_, |
| 861 testing_factories_)); |
| 832 } | 862 } |
| OLD | NEW |