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 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 #endif | 166 #endif |
167 | 167 |
168 TestingProfile::TestingProfile() | 168 TestingProfile::TestingProfile() |
169 : start_time_(Time::Now()), | 169 : start_time_(Time::Now()), |
170 testing_prefs_(NULL), | 170 testing_prefs_(NULL), |
171 incognito_(false), | 171 incognito_(false), |
172 original_profile_(NULL), | 172 original_profile_(NULL), |
173 last_session_exited_cleanly_(true), | 173 last_session_exited_cleanly_(true), |
174 browser_context_dependency_manager_( | 174 browser_context_dependency_manager_( |
175 BrowserContextDependencyManager::GetInstance()), | 175 BrowserContextDependencyManager::GetInstance()), |
| 176 resource_context_(NULL), |
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 browser_context_dependency_manager_( | 192 browser_context_dependency_manager_( |
192 BrowserContextDependencyManager::GetInstance()), | 193 BrowserContextDependencyManager::GetInstance()), |
| 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), |
203 original_profile_(NULL), | 205 original_profile_(NULL), |
204 last_session_exited_cleanly_(true), | 206 last_session_exited_cleanly_(true), |
205 profile_path_(path), | 207 profile_path_(path), |
206 browser_context_dependency_manager_( | 208 browser_context_dependency_manager_( |
207 BrowserContextDependencyManager::GetInstance()), | 209 BrowserContextDependencyManager::GetInstance()), |
| 210 resource_context_(NULL), |
208 delegate_(delegate) { | 211 delegate_(delegate) { |
209 Init(); | 212 Init(); |
210 if (delegate_) { | 213 if (delegate_) { |
211 base::MessageLoop::current()->PostTask( | 214 base::MessageLoop::current()->PostTask( |
212 FROM_HERE, | 215 FROM_HERE, |
213 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); | 216 base::Bind(&TestingProfile::FinishInit, base::Unretained(this))); |
214 } else { | 217 } else { |
215 FinishInit(); | 218 FinishInit(); |
216 } | 219 } |
217 } | 220 } |
218 | 221 |
219 TestingProfile::TestingProfile( | 222 TestingProfile::TestingProfile( |
220 const base::FilePath& path, | 223 const base::FilePath& path, |
221 Delegate* delegate, | 224 Delegate* delegate, |
222 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, | 225 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, |
223 scoped_ptr<PrefServiceSyncable> prefs) | 226 scoped_ptr<PrefServiceSyncable> prefs) |
224 : start_time_(Time::Now()), | 227 : start_time_(Time::Now()), |
225 prefs_(prefs.release()), | 228 prefs_(prefs.release()), |
226 testing_prefs_(NULL), | 229 testing_prefs_(NULL), |
227 incognito_(false), | 230 incognito_(false), |
228 original_profile_(NULL), | 231 original_profile_(NULL), |
229 last_session_exited_cleanly_(true), | 232 last_session_exited_cleanly_(true), |
230 extension_special_storage_policy_(extension_policy), | 233 extension_special_storage_policy_(extension_policy), |
231 profile_path_(path), | 234 profile_path_(path), |
232 browser_context_dependency_manager_( | 235 browser_context_dependency_manager_( |
233 BrowserContextDependencyManager::GetInstance()), | 236 BrowserContextDependencyManager::GetInstance()), |
| 237 resource_context_(NULL), |
234 delegate_(delegate) { | 238 delegate_(delegate) { |
235 | 239 |
236 // If no profile path was supplied, create one. | 240 // If no profile path was supplied, create one. |
237 if (profile_path_.empty()) { | 241 if (profile_path_.empty()) { |
238 CreateTempProfileDir(); | 242 CreateTempProfileDir(); |
239 profile_path_ = temp_dir_.path(); | 243 profile_path_ = temp_dir_.path(); |
240 } | 244 } |
241 | 245 |
242 Init(); | 246 Init(); |
243 // If caller supplied a delegate, delay the FinishInit invocation until other | 247 // If caller supplied a delegate, delay the FinishInit invocation until other |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 content::NotificationService::current()->Notify( | 320 content::NotificationService::current()->Notify( |
317 chrome::NOTIFICATION_PROFILE_CREATED, | 321 chrome::NOTIFICATION_PROFILE_CREATED, |
318 content::Source<Profile>(static_cast<Profile*>(this)), | 322 content::Source<Profile>(static_cast<Profile*>(this)), |
319 content::NotificationService::NoDetails()); | 323 content::NotificationService::NoDetails()); |
320 | 324 |
321 if (delegate_) | 325 if (delegate_) |
322 delegate_->OnProfileCreated(this, true, false); | 326 delegate_->OnProfileCreated(this, true, false); |
323 } | 327 } |
324 | 328 |
325 TestingProfile::~TestingProfile() { | 329 TestingProfile::~TestingProfile() { |
| 330 // Any objects holding live URLFetchers should be deleted before teardown. |
| 331 TemplateURLFetcherFactory::ShutdownForProfile(this); |
| 332 |
326 MaybeSendDestroyedNotification(); | 333 MaybeSendDestroyedNotification(); |
327 | 334 |
328 browser_context_dependency_manager_->DestroyBrowserContextServices(this); | 335 browser_context_dependency_manager_->DestroyBrowserContextServices(this); |
329 | 336 |
330 if (host_content_settings_map_.get()) | 337 if (host_content_settings_map_.get()) |
331 host_content_settings_map_->ShutdownOnUIThread(); | 338 host_content_settings_map_->ShutdownOnUIThread(); |
332 | 339 |
333 DestroyTopSites(); | 340 DestroyTopSites(); |
334 | 341 |
335 if (pref_proxy_config_tracker_.get()) | 342 if (pref_proxy_config_tracker_.get()) |
336 pref_proxy_config_tracker_->DetachFromPrefService(); | 343 pref_proxy_config_tracker_->DetachFromPrefService(); |
| 344 // Failing a post == leaks == heapcheck failure. Make that an immediate test |
| 345 // failure. |
| 346 if (resource_context_) { |
| 347 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, |
| 348 resource_context_)); |
| 349 resource_context_ = NULL; |
| 350 content::RunAllPendingInMessageLoop(BrowserThread::IO); |
| 351 } |
337 } | 352 } |
338 | 353 |
339 static BrowserContextKeyedService* BuildFaviconService( | 354 static BrowserContextKeyedService* BuildFaviconService( |
340 content::BrowserContext* profile) { | 355 content::BrowserContext* profile) { |
341 return new FaviconService( | 356 return new FaviconService( |
342 HistoryServiceFactory::GetForProfileWithoutCreating( | 357 HistoryServiceFactory::GetForProfileWithoutCreating( |
343 static_cast<Profile*>(profile))); | 358 static_cast<Profile*>(profile))); |
344 } | 359 } |
345 | 360 |
346 void TestingProfile::CreateFaviconService() { | 361 void TestingProfile::CreateFaviconService() { |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 | 607 |
593 history::TopSites* TestingProfile::GetTopSitesWithoutCreating() { | 608 history::TopSites* TestingProfile::GetTopSitesWithoutCreating() { |
594 return top_sites_.get(); | 609 return top_sites_.get(); |
595 } | 610 } |
596 | 611 |
597 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { | 612 DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { |
598 return NULL; | 613 return NULL; |
599 } | 614 } |
600 | 615 |
601 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { | 616 net::URLRequestContextGetter* TestingProfile::GetRequestContext() { |
602 return request_context_.get(); | 617 return GetDefaultStoragePartition(this)->GetURLRequestContext(); |
603 } | 618 } |
604 | 619 |
605 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( | 620 net::URLRequestContextGetter* TestingProfile::CreateRequestContext( |
606 content::ProtocolHandlerMap* protocol_handlers) { | 621 content::ProtocolHandlerMap* protocol_handlers) { |
607 return request_context_.get(); | 622 return new net::TestURLRequestContextGetter( |
| 623 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
608 } | 624 } |
609 | 625 |
610 net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess( | 626 net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess( |
611 int renderer_child_id) { | 627 int renderer_child_id) { |
612 content::RenderProcessHost* rph = content::RenderProcessHost::FromID( | 628 content::RenderProcessHost* rph = content::RenderProcessHost::FromID( |
613 renderer_child_id); | 629 renderer_child_id); |
614 return rph->GetStoragePartition()->GetURLRequestContext(); | 630 return rph->GetStoragePartition()->GetURLRequestContext(); |
615 } | 631 } |
616 | 632 |
617 void TestingProfile::CreateRequestContext() { | |
618 if (!request_context_.get()) | |
619 request_context_ = new net::TestURLRequestContextGetter( | |
620 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); | |
621 } | |
622 | |
623 void TestingProfile::ResetRequestContext() { | |
624 // Any objects holding live URLFetchers should be deleted before the request | |
625 // context is shut down. | |
626 TemplateURLFetcherFactory::ShutdownForProfile(this); | |
627 | |
628 request_context_ = NULL; | |
629 } | |
630 | |
631 net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() { | 633 net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() { |
632 return NULL; | 634 return NULL; |
633 } | 635 } |
634 | 636 |
635 net::URLRequestContextGetter* | 637 net::URLRequestContextGetter* |
636 TestingProfile::GetMediaRequestContextForRenderProcess( | 638 TestingProfile::GetMediaRequestContextForRenderProcess( |
637 int renderer_child_id) { | 639 int renderer_child_id) { |
638 return NULL; | 640 return NULL; |
639 } | 641 } |
640 | 642 |
(...skipping 20 matching lines...) Expand all Loading... |
661 TestingProfile::CreateRequestContextForStoragePartition( | 663 TestingProfile::CreateRequestContextForStoragePartition( |
662 const base::FilePath& partition_path, | 664 const base::FilePath& partition_path, |
663 bool in_memory, | 665 bool in_memory, |
664 content::ProtocolHandlerMap* protocol_handlers) { | 666 content::ProtocolHandlerMap* protocol_handlers) { |
665 // We don't test storage partitions here yet, so returning the same dummy | 667 // We don't test storage partitions here yet, so returning the same dummy |
666 // context is sufficient for now. | 668 // context is sufficient for now. |
667 return GetRequestContext(); | 669 return GetRequestContext(); |
668 } | 670 } |
669 | 671 |
670 content::ResourceContext* TestingProfile::GetResourceContext() { | 672 content::ResourceContext* TestingProfile::GetResourceContext() { |
671 if (!resource_context_.get()) | 673 if (!resource_context_) |
672 resource_context_.reset(new content::MockResourceContext()); | 674 resource_context_ = new content::MockResourceContext(); |
673 return resource_context_.get(); | 675 return resource_context_; |
674 } | 676 } |
675 | 677 |
676 HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() { | 678 HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() { |
677 if (!host_content_settings_map_.get()) { | 679 if (!host_content_settings_map_.get()) { |
678 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false); | 680 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false); |
679 #if defined(ENABLE_EXTENSIONS) | 681 #if defined(ENABLE_EXTENSIONS) |
680 ExtensionService* extension_service = GetExtensionService(); | 682 ExtensionService* extension_service = GetExtensionService(); |
681 if (extension_service) | 683 if (extension_service) |
682 host_content_settings_map_->RegisterExtensionService(extension_service); | 684 host_content_settings_map_->RegisterExtensionService(extension_service); |
683 #endif | 685 #endif |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 | 799 |
798 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { | 800 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { |
799 DCHECK(!build_called_); | 801 DCHECK(!build_called_); |
800 build_called_ = true; | 802 build_called_ = true; |
801 return scoped_ptr<TestingProfile>(new TestingProfile( | 803 return scoped_ptr<TestingProfile>(new TestingProfile( |
802 path_, | 804 path_, |
803 delegate_, | 805 delegate_, |
804 extension_policy_, | 806 extension_policy_, |
805 pref_service_.Pass())); | 807 pref_service_.Pass())); |
806 } | 808 } |
OLD | NEW |