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 16 matching lines...) Expand all Loading... |
27 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" | 27 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" |
28 #include "chrome/browser/history/history.h" | 28 #include "chrome/browser/history/history.h" |
29 #include "chrome/browser/history/history_backend.h" | 29 #include "chrome/browser/history/history_backend.h" |
30 #include "chrome/browser/history/history_service_factory.h" | 30 #include "chrome/browser/history/history_service_factory.h" |
31 #include "chrome/browser/history/shortcuts_backend.h" | 31 #include "chrome/browser/history/shortcuts_backend.h" |
32 #include "chrome/browser/history/shortcuts_backend_factory.h" | 32 #include "chrome/browser/history/shortcuts_backend_factory.h" |
33 #include "chrome/browser/history/top_sites.h" | 33 #include "chrome/browser/history/top_sites.h" |
34 #include "chrome/browser/net/proxy_service_factory.h" | 34 #include "chrome/browser/net/proxy_service_factory.h" |
35 #include "chrome/browser/notifications/desktop_notification_service.h" | 35 #include "chrome/browser/notifications/desktop_notification_service.h" |
36 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 36 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
37 #include "chrome/browser/policy/user_cloud_policy_manager.h" | |
38 #include "chrome/browser/prefs/browser_prefs.h" | 37 #include "chrome/browser/prefs/browser_prefs.h" |
39 #include "chrome/browser/prefs/testing_pref_store.h" | 38 #include "chrome/browser/prefs/testing_pref_store.h" |
40 #include "chrome/browser/prerender/prerender_manager.h" | 39 #include "chrome/browser/prerender/prerender_manager.h" |
41 #include "chrome/browser/profiles/profile_dependency_manager.h" | 40 #include "chrome/browser/profiles/profile_dependency_manager.h" |
42 #include "chrome/browser/protector/protector_service_factory.h" | 41 #include "chrome/browser/protector/protector_service_factory.h" |
43 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" | 42 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
44 #include "chrome/browser/speech/chrome_speech_recognition_preferences.h" | 43 #include "chrome/browser/speech/chrome_speech_recognition_preferences.h" |
45 #include "chrome/browser/webdata/web_data_service.h" | 44 #include "chrome/browser/webdata/web_data_service.h" |
46 #include "chrome/browser/webdata/web_data_service_factory.h" | 45 #include "chrome/browser/webdata/web_data_service_factory.h" |
47 #include "chrome/common/chrome_constants.h" | 46 #include "chrome/common/chrome_constants.h" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 const char TestingProfile::kTestUserProfileDir[] = "Default"; | 152 const char TestingProfile::kTestUserProfileDir[] = "Default"; |
154 #endif | 153 #endif |
155 | 154 |
156 TestingProfile::TestingProfile() | 155 TestingProfile::TestingProfile() |
157 : start_time_(Time::Now()), | 156 : start_time_(Time::Now()), |
158 testing_prefs_(NULL), | 157 testing_prefs_(NULL), |
159 incognito_(false), | 158 incognito_(false), |
160 last_session_exited_cleanly_(true), | 159 last_session_exited_cleanly_(true), |
161 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), | 160 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), |
162 delegate_(NULL) { | 161 delegate_(NULL) { |
163 CreateTempProfileDir(); | 162 if (!temp_dir_.CreateUniqueTempDir()) { |
| 163 LOG(ERROR) << "Failed to create unique temporary directory."; |
| 164 |
| 165 // Fallback logic in case we fail to create unique temporary directory. |
| 166 FilePath system_tmp_dir; |
| 167 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir); |
| 168 |
| 169 // We're severly screwed if we can't get the system temporary |
| 170 // directory. Die now to avoid writing to the filesystem root |
| 171 // or other bad places. |
| 172 CHECK(success); |
| 173 |
| 174 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath")); |
| 175 file_util::Delete(fallback_dir, true); |
| 176 file_util::CreateDirectory(fallback_dir); |
| 177 if (!temp_dir_.Set(fallback_dir)) { |
| 178 // That shouldn't happen, but if it does, try to recover. |
| 179 LOG(ERROR) << "Failed to use a fallback temporary directory."; |
| 180 |
| 181 // We're screwed if this fails, see CHECK above. |
| 182 CHECK(temp_dir_.Set(system_tmp_dir)); |
| 183 } |
| 184 } |
| 185 |
164 profile_path_ = temp_dir_.path(); | 186 profile_path_ = temp_dir_.path(); |
165 | 187 |
166 Init(); | 188 Init(); |
167 FinishInit(); | 189 FinishInit(); |
168 } | 190 } |
169 | 191 |
170 TestingProfile::TestingProfile(const FilePath& path) | 192 TestingProfile::TestingProfile(const FilePath& path) |
171 : start_time_(Time::Now()), | 193 : start_time_(Time::Now()), |
172 testing_prefs_(NULL), | 194 testing_prefs_(NULL), |
173 incognito_(false), | 195 incognito_(false), |
(...skipping 17 matching lines...) Expand all Loading... |
191 Init(); | 213 Init(); |
192 if (delegate_) { | 214 if (delegate_) { |
193 MessageLoop::current()->PostTask(FROM_HERE, | 215 MessageLoop::current()->PostTask(FROM_HERE, |
194 base::Bind(&TestingProfile::FinishInit, | 216 base::Bind(&TestingProfile::FinishInit, |
195 base::Unretained(this))); | 217 base::Unretained(this))); |
196 } else { | 218 } else { |
197 FinishInit(); | 219 FinishInit(); |
198 } | 220 } |
199 } | 221 } |
200 | 222 |
201 TestingProfile::TestingProfile( | |
202 const FilePath& path, | |
203 Delegate* delegate, | |
204 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, | |
205 scoped_ptr<PrefService> prefs, | |
206 scoped_ptr<policy::UserCloudPolicyManager> user_cloud_policy_manager) | |
207 : start_time_(Time::Now()), | |
208 prefs_(prefs.release()), | |
209 testing_prefs_(NULL), | |
210 incognito_(false), | |
211 last_session_exited_cleanly_(true), | |
212 extension_special_storage_policy_(extension_policy), | |
213 user_cloud_policy_manager_(user_cloud_policy_manager.release()), | |
214 profile_path_(path), | |
215 profile_dependency_manager_(ProfileDependencyManager::GetInstance()), | |
216 delegate_(delegate) { | |
217 | |
218 // If no profile path was supplied, create one. | |
219 if (profile_path_.empty()) { | |
220 CreateTempProfileDir(); | |
221 profile_path_ = temp_dir_.path(); | |
222 } | |
223 | |
224 Init(); | |
225 // If caller supplied a delegate, delay the FinishInit invocation until other | |
226 // tasks have run. | |
227 // TODO(atwilson): See if this is still required once we convert the current | |
228 // users of the constructor that takes a Delegate* param. | |
229 if (delegate_) { | |
230 MessageLoop::current()->PostTask(FROM_HERE, | |
231 base::Bind(&TestingProfile::FinishInit, | |
232 base::Unretained(this))); | |
233 } else { | |
234 FinishInit(); | |
235 } | |
236 } | |
237 | |
238 void TestingProfile::CreateTempProfileDir() { | |
239 if (!temp_dir_.CreateUniqueTempDir()) { | |
240 LOG(ERROR) << "Failed to create unique temporary directory."; | |
241 | |
242 // Fallback logic in case we fail to create unique temporary directory. | |
243 FilePath system_tmp_dir; | |
244 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir); | |
245 | |
246 // We're severly screwed if we can't get the system temporary | |
247 // directory. Die now to avoid writing to the filesystem root | |
248 // or other bad places. | |
249 CHECK(success); | |
250 | |
251 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath")); | |
252 file_util::Delete(fallback_dir, true); | |
253 file_util::CreateDirectory(fallback_dir); | |
254 if (!temp_dir_.Set(fallback_dir)) { | |
255 // That shouldn't happen, but if it does, try to recover. | |
256 LOG(ERROR) << "Failed to use a fallback temporary directory."; | |
257 | |
258 // We're screwed if this fails, see CHECK above. | |
259 CHECK(temp_dir_.Set(system_tmp_dir)); | |
260 } | |
261 } | |
262 } | |
263 | |
264 void TestingProfile::Init() { | 223 void TestingProfile::Init() { |
265 if (!file_util::PathExists(profile_path_)) | 224 if (!file_util::PathExists(profile_path_)) |
266 file_util::CreateDirectory(profile_path_); | 225 file_util::CreateDirectory(profile_path_); |
267 | 226 |
268 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( | 227 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( |
269 this, extensions::TestExtensionSystem::Build); | 228 this, extensions::TestExtensionSystem::Build); |
270 | 229 |
271 profile_dependency_manager_->CreateProfileServices(this, true); | 230 profile_dependency_manager_->CreateProfileServices(this, true); |
272 | 231 |
273 #if defined(ENABLE_NOTIFICATIONS) | 232 #if defined(ENABLE_NOTIFICATIONS) |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 return HistoryServiceFactory::GetForProfileWithoutCreating(this); | 487 return HistoryServiceFactory::GetForProfileWithoutCreating(this); |
529 } | 488 } |
530 | 489 |
531 net::CookieMonster* TestingProfile::GetCookieMonster() { | 490 net::CookieMonster* TestingProfile::GetCookieMonster() { |
532 if (!GetRequestContext()) | 491 if (!GetRequestContext()) |
533 return NULL; | 492 return NULL; |
534 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> | 493 return GetRequestContext()->GetURLRequestContext()->cookie_store()-> |
535 GetCookieMonster(); | 494 GetCookieMonster(); |
536 } | 495 } |
537 | 496 |
538 policy::UserCloudPolicyManager* TestingProfile::GetUserCloudPolicyManager() { | |
539 return user_cloud_policy_manager_.get(); | |
540 } | |
541 | |
542 policy::PolicyService* TestingProfile::GetPolicyService() { | 497 policy::PolicyService* TestingProfile::GetPolicyService() { |
543 if (!policy_service_.get()) { | 498 if (!policy_service_.get()) { |
544 #if defined(ENABLE_CONFIGURATION_POLICY) | 499 #if defined(ENABLE_CONFIGURATION_POLICY) |
545 policy::PolicyServiceImpl::Providers providers; | 500 policy::PolicyServiceImpl::Providers providers; |
546 policy_service_.reset(new policy::PolicyServiceImpl(providers)); | 501 policy_service_.reset(new policy::PolicyServiceImpl(providers)); |
547 #else | 502 #else |
548 policy_service_.reset(new policy::PolicyServiceStub()); | 503 policy_service_.reset(new policy::PolicyServiceStub()); |
549 #endif | 504 #endif |
550 } | 505 } |
551 return policy_service_.get(); | 506 return policy_service_.get(); |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 } | 725 } |
771 | 726 |
772 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { | 727 bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { |
773 return true; | 728 return true; |
774 } | 729 } |
775 | 730 |
776 base::Callback<ChromeURLDataManagerBackend*(void)> | 731 base::Callback<ChromeURLDataManagerBackend*(void)> |
777 TestingProfile::GetChromeURLDataManagerBackendGetter() const { | 732 TestingProfile::GetChromeURLDataManagerBackendGetter() const { |
778 return base::Callback<ChromeURLDataManagerBackend*(void)>(); | 733 return base::Callback<ChromeURLDataManagerBackend*(void)>(); |
779 } | 734 } |
780 | |
781 TestingProfile::Builder::Builder() | |
782 : build_called_(false), | |
783 delegate_(NULL) { | |
784 } | |
785 | |
786 TestingProfile::Builder::~Builder() { | |
787 } | |
788 | |
789 void TestingProfile::Builder::SetPath(const FilePath& path) { | |
790 path_ = path; | |
791 } | |
792 | |
793 void TestingProfile::Builder::SetDelegate(Delegate* delegate) { | |
794 delegate_ = delegate; | |
795 } | |
796 | |
797 void TestingProfile::Builder::SetExtensionSpecialStoragePolicy( | |
798 scoped_refptr<ExtensionSpecialStoragePolicy> policy) { | |
799 extension_policy_ = policy; | |
800 } | |
801 | |
802 void TestingProfile::Builder::SetPrefService(scoped_ptr<PrefService> prefs) { | |
803 pref_service_ = prefs.Pass(); | |
804 } | |
805 | |
806 void TestingProfile::Builder::SetUserCloudPolicyManager( | |
807 scoped_ptr<policy::UserCloudPolicyManager> manager) { | |
808 user_cloud_policy_manager_ = manager.Pass(); | |
809 } | |
810 | |
811 scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { | |
812 DCHECK(!build_called_); | |
813 build_called_ = true; | |
814 return scoped_ptr<TestingProfile>(new TestingProfile( | |
815 path_, | |
816 delegate_, | |
817 extension_policy_, | |
818 pref_service_.Pass(), | |
819 user_cloud_policy_manager_.Pass())); | |
820 } | |
821 | |
822 | |
OLD | NEW |