| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); | 139 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
| 140 } | 140 } |
| 141 | 141 |
| 142 protected: | 142 protected: |
| 143 virtual ~TestExtensionURLRequestContextGetter() {} | 143 virtual ~TestExtensionURLRequestContextGetter() {} |
| 144 | 144 |
| 145 private: | 145 private: |
| 146 scoped_ptr<net::URLRequestContext> context_; | 146 scoped_ptr<net::URLRequestContext> context_; |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 ProfileKeyedService* CreateTestDesktopNotificationService( | 149 BrowserContextKeyedService* CreateTestDesktopNotificationService( |
| 150 content::BrowserContext* profile) { | 150 content::BrowserContext* profile) { |
| 151 #if defined(ENABLE_NOTIFICATIONS) | 151 #if defined(ENABLE_NOTIFICATIONS) |
| 152 return new DesktopNotificationService(static_cast<Profile*>(profile), NULL); | 152 return new DesktopNotificationService(static_cast<Profile*>(profile), NULL); |
| 153 #else | 153 #else |
| 154 return NULL; | 154 return NULL; |
| 155 #endif | 155 #endif |
| 156 } | 156 } |
| 157 | 157 |
| 158 } // namespace | 158 } // namespace |
| 159 | 159 |
| 160 // static | 160 // static |
| 161 #if defined(OS_CHROMEOS) | 161 #if defined(OS_CHROMEOS) |
| 162 // Must be kept in sync with | 162 // Must be kept in sync with |
| 163 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. | 163 // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. |
| 164 const char TestingProfile::kTestUserProfileDir[] = "test-user"; | 164 const char TestingProfile::kTestUserProfileDir[] = "test-user"; |
| 165 #else | 165 #else |
| 166 const char TestingProfile::kTestUserProfileDir[] = "Default"; | 166 const char TestingProfile::kTestUserProfileDir[] = "Default"; |
| 167 #endif | 167 #endif |
| 168 | 168 |
| 169 TestingProfile::TestingProfile() | 169 TestingProfile::TestingProfile() |
| 170 : start_time_(Time::Now()), | 170 : start_time_(Time::Now()), |
| 171 testing_prefs_(NULL), | 171 testing_prefs_(NULL), |
| 172 incognito_(false), | 172 incognito_(false), |
| 173 original_profile_(NULL), | 173 original_profile_(NULL), |
| 174 last_session_exited_cleanly_(true), | 174 last_session_exited_cleanly_(true), |
| 175 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), | 175 profile_dependency_manager_( |
| 176 BrowserContextDependencyManager::GetInstance()), |
| 176 delegate_(NULL) { | 177 delegate_(NULL) { |
| 177 CreateTempProfileDir(); | 178 CreateTempProfileDir(); |
| 178 profile_path_ = temp_dir_.path(); | 179 profile_path_ = temp_dir_.path(); |
| 179 | 180 |
| 180 Init(); | 181 Init(); |
| 181 FinishInit(); | 182 FinishInit(); |
| 182 } | 183 } |
| 183 | 184 |
| 184 TestingProfile::TestingProfile(const base::FilePath& path) | 185 TestingProfile::TestingProfile(const base::FilePath& path) |
| 185 : start_time_(Time::Now()), | 186 : start_time_(Time::Now()), |
| 186 testing_prefs_(NULL), | 187 testing_prefs_(NULL), |
| 187 incognito_(false), | 188 incognito_(false), |
| 188 original_profile_(NULL), | 189 original_profile_(NULL), |
| 189 last_session_exited_cleanly_(true), | 190 last_session_exited_cleanly_(true), |
| 190 profile_path_(path), | 191 profile_path_(path), |
| 191 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), | 192 profile_dependency_manager_( |
| 193 BrowserContextDependencyManager::GetInstance()), |
| 192 delegate_(NULL) { | 194 delegate_(NULL) { |
| 193 Init(); | 195 Init(); |
| 194 FinishInit(); | 196 FinishInit(); |
| 195 } | 197 } |
| 196 | 198 |
| 197 TestingProfile::TestingProfile(const base::FilePath& path, | 199 TestingProfile::TestingProfile(const base::FilePath& path, |
| 198 Delegate* delegate) | 200 Delegate* delegate) |
| 199 : start_time_(Time::Now()), | 201 : start_time_(Time::Now()), |
| 200 testing_prefs_(NULL), | 202 testing_prefs_(NULL), |
| 201 incognito_(false), | 203 incognito_(false), |
| 202 original_profile_(NULL), | 204 original_profile_(NULL), |
| 203 last_session_exited_cleanly_(true), | 205 last_session_exited_cleanly_(true), |
| 204 profile_path_(path), | 206 profile_path_(path), |
| 205 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), | 207 profile_dependency_manager_( |
| 208 BrowserContextDependencyManager::GetInstance()), |
| 206 delegate_(delegate) { | 209 delegate_(delegate) { |
| 207 Init(); | 210 Init(); |
| 208 if (delegate_) { | 211 if (delegate_) { |
| 209 base::MessageLoop::current()->PostTask( | 212 base::MessageLoop::current()->PostTask( |
| 210 FROM_HERE, | 213 FROM_HERE, |
| 211 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); | 214 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); |
| 212 } else { | 215 } else { |
| 213 FinishInit(); | 216 FinishInit(); |
| 214 } | 217 } |
| 215 } | 218 } |
| 216 | 219 |
| 217 TestingProfile::TestingProfile( | 220 TestingProfile::TestingProfile( |
| 218 const base::FilePath& path, | 221 const base::FilePath& path, |
| 219 Delegate* delegate, | 222 Delegate* delegate, |
| 220 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, | 223 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, |
| 221 scoped_ptr<PrefServiceSyncable> prefs) | 224 scoped_ptr<PrefServiceSyncable> prefs) |
| 222 : start_time_(Time::Now()), | 225 : start_time_(Time::Now()), |
| 223 prefs_(prefs.release()), | 226 prefs_(prefs.release()), |
| 224 testing_prefs_(NULL), | 227 testing_prefs_(NULL), |
| 225 incognito_(false), | 228 incognito_(false), |
| 226 original_profile_(NULL), | 229 original_profile_(NULL), |
| 227 last_session_exited_cleanly_(true), | 230 last_session_exited_cleanly_(true), |
| 228 extension_special_storage_policy_(extension_policy), | 231 extension_special_storage_policy_(extension_policy), |
| 229 profile_path_(path), | 232 profile_path_(path), |
| 230 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), | 233 profile_dependency_manager_( |
| 234 BrowserContextDependencyManager::GetInstance()), |
| 231 delegate_(delegate) { | 235 delegate_(delegate) { |
| 232 | 236 |
| 233 // If no profile path was supplied, create one. | 237 // If no profile path was supplied, create one. |
| 234 if (profile_path_.empty()) { | 238 if (profile_path_.empty()) { |
| 235 CreateTempProfileDir(); | 239 CreateTempProfileDir(); |
| 236 profile_path_ = temp_dir_.path(); | 240 profile_path_ = temp_dir_.path(); |
| 237 } | 241 } |
| 238 | 242 |
| 239 Init(); | 243 Init(); |
| 240 // If caller supplied a delegate, delay the FinishInit invocation until other | 244 // If caller supplied a delegate, delay the FinishInit invocation until other |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // We're screwed if this fails, see CHECK above. | 278 // We're screwed if this fails, see CHECK above. |
| 275 CHECK(temp_dir_.Set(system_tmp_dir)); | 279 CHECK(temp_dir_.Set(system_tmp_dir)); |
| 276 } | 280 } |
| 277 } | 281 } |
| 278 } | 282 } |
| 279 | 283 |
| 280 void TestingProfile::Init() { | 284 void TestingProfile::Init() { |
| 281 // Normally this would happen during browser startup, but for tests | 285 // Normally this would happen during browser startup, but for tests |
| 282 // we need to trigger creation of Profile-related services. | 286 // we need to trigger creation of Profile-related services. |
| 283 ChromeBrowserMainExtraPartsProfiles:: | 287 ChromeBrowserMainExtraPartsProfiles:: |
| 284 EnsureProfileKeyedServiceFactoriesBuilt(); | 288 EnsureBrowserContextKeyedServiceFactoriesBuilt(); |
| 285 | 289 |
| 286 if (prefs_.get()) | 290 if (prefs_.get()) |
| 287 components::UserPrefs::Set(this, prefs_.get()); | 291 components::UserPrefs::Set(this, prefs_.get()); |
| 288 else | 292 else |
| 289 CreateTestingPrefService(); | 293 CreateTestingPrefService(); |
| 290 | 294 |
| 291 if (!file_util::PathExists(profile_path_)) | 295 if (!file_util::PathExists(profile_path_)) |
| 292 file_util::CreateDirectory(profile_path_); | 296 file_util::CreateDirectory(profile_path_); |
| 293 | 297 |
| 294 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl | 298 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl |
| 295 // anymore, after converting the PrefService to a PKS. Until then it must | 299 // anymore, after converting the PrefService to a PKS. Until then it must |
| 296 // be associated with a TestingProfile too. | 300 // be associated with a TestingProfile too. |
| 297 CreateProfilePolicyConnector(); | 301 CreateProfilePolicyConnector(); |
| 298 | 302 |
| 299 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( | 303 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( |
| 300 this, extensions::TestExtensionSystem::Build); | 304 this, extensions::TestExtensionSystem::Build); |
| 301 | 305 |
| 302 profile_dependency_manager_->CreateProfileServices(this, true); | 306 profile_dependency_manager_->CreateBrowserContextServices(this, true); |
| 303 | 307 |
| 304 #if defined(ENABLE_NOTIFICATIONS) | 308 #if defined(ENABLE_NOTIFICATIONS) |
| 305 // Install profile keyed service factory hooks for dummy/test services | 309 // Install profile keyed service factory hooks for dummy/test services |
| 306 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory( | 310 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory( |
| 307 this, CreateTestDesktopNotificationService); | 311 this, CreateTestDesktopNotificationService); |
| 308 #endif | 312 #endif |
| 309 } | 313 } |
| 310 | 314 |
| 311 void TestingProfile::FinishInit() { | 315 void TestingProfile::FinishInit() { |
| 312 DCHECK(content::NotificationService::current()); | 316 DCHECK(content::NotificationService::current()); |
| 313 content::NotificationService::current()->Notify( | 317 content::NotificationService::current()->Notify( |
| 314 chrome::NOTIFICATION_PROFILE_CREATED, | 318 chrome::NOTIFICATION_PROFILE_CREATED, |
| 315 content::Source<Profile>(static_cast<Profile*>(this)), | 319 content::Source<Profile>(static_cast<Profile*>(this)), |
| 316 content::NotificationService::NoDetails()); | 320 content::NotificationService::NoDetails()); |
| 317 | 321 |
| 318 if (delegate_) | 322 if (delegate_) |
| 319 delegate_->OnProfileCreated(this, true, false); | 323 delegate_->OnProfileCreated(this, true, false); |
| 320 } | 324 } |
| 321 | 325 |
| 322 TestingProfile::~TestingProfile() { | 326 TestingProfile::~TestingProfile() { |
| 323 MaybeSendDestroyedNotification(); | 327 MaybeSendDestroyedNotification(); |
| 324 | 328 |
| 325 profile_dependency_manager_->DestroyProfileServices(this); | 329 profile_dependency_manager_->DestroyBrowserContextServices(this); |
| 326 | 330 |
| 327 if (host_content_settings_map_) | 331 if (host_content_settings_map_) |
| 328 host_content_settings_map_->ShutdownOnUIThread(); | 332 host_content_settings_map_->ShutdownOnUIThread(); |
| 329 | 333 |
| 330 DestroyTopSites(); | 334 DestroyTopSites(); |
| 331 | 335 |
| 332 if (pref_proxy_config_tracker_.get()) | 336 if (pref_proxy_config_tracker_.get()) |
| 333 pref_proxy_config_tracker_->DetachFromPrefService(); | 337 pref_proxy_config_tracker_->DetachFromPrefService(); |
| 334 } | 338 } |
| 335 | 339 |
| 336 static ProfileKeyedService* BuildFaviconService( | 340 static BrowserContextKeyedService* BuildFaviconService( |
| 337 content::BrowserContext* profile) { | 341 content::BrowserContext* profile) { |
| 338 return new FaviconService( | 342 return new FaviconService( |
| 339 HistoryServiceFactory::GetForProfileWithoutCreating( | 343 HistoryServiceFactory::GetForProfileWithoutCreating( |
| 340 static_cast<Profile*>(profile))); | 344 static_cast<Profile*>(profile))); |
| 341 } | 345 } |
| 342 | 346 |
| 343 void TestingProfile::CreateFaviconService() { | 347 void TestingProfile::CreateFaviconService() { |
| 344 // It is up to the caller to create the history service if one is needed. | 348 // It is up to the caller to create the history service if one is needed. |
| 345 FaviconServiceFactory::GetInstance()->SetTestingFactory( | 349 FaviconServiceFactory::GetInstance()->SetTestingFactory( |
| 346 this, BuildFaviconService); | 350 this, BuildFaviconService); |
| 347 } | 351 } |
| 348 | 352 |
| 349 static ProfileKeyedService* BuildHistoryService( | 353 static BrowserContextKeyedService* BuildHistoryService( |
| 350 content::BrowserContext* profile) { | 354 content::BrowserContext* profile) { |
| 351 return new HistoryService(static_cast<Profile*>(profile)); | 355 return new HistoryService(static_cast<Profile*>(profile)); |
| 352 } | 356 } |
| 353 | 357 |
| 354 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { | 358 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { |
| 355 DestroyHistoryService(); | 359 DestroyHistoryService(); |
| 356 if (delete_file) { | 360 if (delete_file) { |
| 357 base::FilePath path = GetPath(); | 361 base::FilePath path = GetPath(); |
| 358 path = path.Append(chrome::kHistoryFilename); | 362 path = path.Append(chrome::kHistoryFilename); |
| 359 file_util::Delete(path, false); | 363 file_util::Delete(path, false); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 top_sites_->Shutdown(); | 410 top_sites_->Shutdown(); |
| 407 top_sites_ = NULL; | 411 top_sites_ = NULL; |
| 408 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that | 412 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that |
| 409 // need to be run to properly shutdown. Run all pending tasks now. This is | 413 // need to be run to properly shutdown. Run all pending tasks now. This is |
| 410 // normally handled by browser_process shutdown. | 414 // normally handled by browser_process shutdown. |
| 411 if (base::MessageLoop::current()) | 415 if (base::MessageLoop::current()) |
| 412 base::MessageLoop::current()->RunUntilIdle(); | 416 base::MessageLoop::current()->RunUntilIdle(); |
| 413 } | 417 } |
| 414 } | 418 } |
| 415 | 419 |
| 416 static ProfileKeyedService* BuildBookmarkModel( | 420 static BrowserContextKeyedService* BuildBookmarkModel( |
| 417 content::BrowserContext* context) { | 421 content::BrowserContext* context) { |
| 418 Profile* profile = static_cast<Profile*>(context); | 422 Profile* profile = static_cast<Profile*>(context); |
| 419 BookmarkModel* bookmark_model = new BookmarkModel(profile); | 423 BookmarkModel* bookmark_model = new BookmarkModel(profile); |
| 420 bookmark_model->Load(profile->GetIOTaskRunner()); | 424 bookmark_model->Load(profile->GetIOTaskRunner()); |
| 421 return bookmark_model; | 425 return bookmark_model; |
| 422 } | 426 } |
| 423 | 427 |
| 424 | 428 |
| 425 void TestingProfile::CreateBookmarkModel(bool delete_file) { | 429 void TestingProfile::CreateBookmarkModel(bool delete_file) { |
| 426 if (delete_file) { | 430 if (delete_file) { |
| 427 base::FilePath path = GetPath().Append(chrome::kBookmarksFileName); | 431 base::FilePath path = GetPath().Append(chrome::kBookmarksFileName); |
| 428 file_util::Delete(path, false); | 432 file_util::Delete(path, false); |
| 429 } | 433 } |
| 430 // This will create a bookmark model. | 434 // This will create a bookmark model. |
| 431 BookmarkModel* bookmark_service = static_cast<BookmarkModel*>( | 435 BookmarkModel* bookmark_service = static_cast<BookmarkModel*>( |
| 432 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( | 436 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 433 this, BuildBookmarkModel)); | 437 this, BuildBookmarkModel)); |
| 434 | 438 |
| 435 HistoryService* history_service = | 439 HistoryService* history_service = |
| 436 HistoryServiceFactory::GetForProfileWithoutCreating(this); | 440 HistoryServiceFactory::GetForProfileWithoutCreating(this); |
| 437 if (history_service) { | 441 if (history_service) { |
| 438 history_service->history_backend_->bookmark_service_ = bookmark_service; | 442 history_service->history_backend_->bookmark_service_ = bookmark_service; |
| 439 history_service->history_backend_->expirer_.bookmark_service_ = | 443 history_service->history_backend_->expirer_.bookmark_service_ = |
| 440 bookmark_service; | 444 bookmark_service; |
| 441 } | 445 } |
| 442 } | 446 } |
| 443 | 447 |
| 444 static ProfileKeyedService* BuildWebDataService( | 448 static BrowserContextKeyedService* BuildWebDataService( |
| 445 content::BrowserContext* profile) { | 449 content::BrowserContext* profile) { |
| 446 return new WebDataServiceWrapper(static_cast<Profile*>(profile)); | 450 return new WebDataServiceWrapper(static_cast<Profile*>(profile)); |
| 447 } | 451 } |
| 448 | 452 |
| 449 void TestingProfile::CreateWebDataService() { | 453 void TestingProfile::CreateWebDataService() { |
| 450 WebDataServiceFactory::GetInstance()->SetTestingFactory( | 454 WebDataServiceFactory::GetInstance()->SetTestingFactory( |
| 451 this, BuildWebDataService); | 455 this, BuildWebDataService); |
| 452 } | 456 } |
| 453 | 457 |
| 454 void TestingProfile::BlockUntilHistoryIndexIsRefreshed() { | 458 void TestingProfile::BlockUntilHistoryIndexIsRefreshed() { |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 | 808 |
| 805 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { | 809 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { |
| 806 DCHECK(!build_called_); | 810 DCHECK(!build_called_); |
| 807 build_called_ = true; | 811 build_called_ = true; |
| 808 return scoped_ptr<TestingProfile>(new TestingProfile( | 812 return scoped_ptr<TestingProfile>(new TestingProfile( |
| 809 path_, | 813 path_, |
| 810 delegate_, | 814 delegate_, |
| 811 extension_policy_, | 815 extension_policy_, |
| 812 pref_service_.Pass())); | 816 pref_service_.Pass())); |
| 813 } | 817 } |
| OLD | NEW |