OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/testing_profile.h" | 5 #include "chrome/test/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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 }; | 163 }; |
164 | 164 |
165 } // namespace | 165 } // namespace |
166 | 166 |
167 TestingProfile::TestingProfile() | 167 TestingProfile::TestingProfile() |
168 : start_time_(Time::Now()), | 168 : start_time_(Time::Now()), |
169 created_theme_provider_(false), | 169 created_theme_provider_(false), |
170 has_history_service_(false), | 170 has_history_service_(false), |
171 off_the_record_(false), | 171 off_the_record_(false), |
172 last_session_exited_cleanly_(true) { | 172 last_session_exited_cleanly_(true) { |
173 PathService::Get(base::DIR_TEMP, &path_); | 173 if (!temp_dir_.CreateUniqueTempDir()) { |
174 path_ = path_.Append(FILE_PATH_LITERAL("TestingProfilePath")); | 174 LOG(ERROR) << "Failed to create unique temporary directory."; |
175 file_util::Delete(path_, true); | |
176 file_util::CreateDirectory(path_); | |
177 } | |
178 | 175 |
179 TestingProfile::TestingProfile(int count) | 176 // Fallback logic in case we fail to create unique temporary directory. |
180 : start_time_(Time::Now()), | 177 FilePath system_tmp_dir; |
181 created_theme_provider_(false), | 178 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir); |
182 has_history_service_(false), | |
183 off_the_record_(false), | |
184 last_session_exited_cleanly_(true) { | |
185 PathService::Get(base::DIR_TEMP, &path_); | |
186 path_ = path_.Append(FILE_PATH_LITERAL("TestingProfilePath")); | |
187 path_ = path_.AppendASCII(base::IntToString(count)); | |
188 file_util::Delete(path_, true); | |
189 file_util::CreateDirectory(path_); | |
190 } | |
191 | 179 |
192 TestingProfile::TestingProfile(const FilePath& path) | 180 // We're severly screwed if we can't get the system temporary |
193 : start_time_(Time::Now()), | 181 // directory. Die now to avoid writing to the filesystem root |
194 created_theme_provider_(false), | 182 // or other bad places. |
195 has_history_service_(false), | 183 CHECK(success); |
196 off_the_record_(false), | 184 |
197 last_session_exited_cleanly_(true) { | 185 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath")); |
198 path_ = path; | 186 file_util::Delete(fallback_dir, true); |
| 187 file_util::CreateDirectory(fallback_dir); |
| 188 if (!temp_dir_.Set(fallback_dir)) { |
| 189 // That shouldn't happen, but if it does, try to recover. |
| 190 LOG(ERROR) << "Failed to use a fallback temporary directory."; |
| 191 |
| 192 // We're screwed if this fails, see CHECK above. |
| 193 CHECK(temp_dir_.Set(system_tmp_dir)); |
| 194 } |
| 195 } |
199 } | 196 } |
200 | 197 |
201 TestingProfile::~TestingProfile() { | 198 TestingProfile::~TestingProfile() { |
202 NotificationService::current()->Notify( | 199 NotificationService::current()->Notify( |
203 NotificationType::PROFILE_DESTROYED, | 200 NotificationType::PROFILE_DESTROYED, |
204 Source<Profile>(this), | 201 Source<Profile>(this), |
205 NotificationService::NoDetails()); | 202 NotificationService::NoDetails()); |
206 DestroyHistoryService(); | 203 DestroyHistoryService(); |
207 // FaviconService depends on HistoryServce so destroying it later. | 204 // FaviconService depends on HistoryServce so destroying it later. |
208 DestroyFaviconService(); | 205 DestroyFaviconService(); |
209 DestroyWebDataService(); | 206 DestroyWebDataService(); |
210 if (top_sites_.get()) | 207 if (top_sites_.get()) |
211 top_sites_->ClearProfile(); | 208 top_sites_->ClearProfile(); |
212 history::TopSites::DeleteTopSites(top_sites_); | 209 history::TopSites::DeleteTopSites(top_sites_); |
213 | |
214 file_util::Delete(path_, true); | |
215 } | 210 } |
216 | 211 |
217 void TestingProfile::CreateFaviconService() { | 212 void TestingProfile::CreateFaviconService() { |
218 favicon_service_ = NULL; | 213 favicon_service_ = NULL; |
219 favicon_service_ = new FaviconService(this); | 214 favicon_service_ = new FaviconService(this); |
220 } | 215 } |
221 | 216 |
222 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { | 217 void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { |
223 if (history_service_.get()) | 218 if (history_service_.get()) |
224 history_service_->Cleanup(); | 219 history_service_->Cleanup(); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 void TestingProfile::CreateTemplateURLModel() { | 308 void TestingProfile::CreateTemplateURLModel() { |
314 template_url_model_.reset(new TemplateURLModel(this)); | 309 template_url_model_.reset(new TemplateURLModel(this)); |
315 } | 310 } |
316 | 311 |
317 void TestingProfile::UseThemeProvider(BrowserThemeProvider* theme_provider) { | 312 void TestingProfile::UseThemeProvider(BrowserThemeProvider* theme_provider) { |
318 theme_provider->Init(this); | 313 theme_provider->Init(this); |
319 created_theme_provider_ = true; | 314 created_theme_provider_ = true; |
320 theme_provider_.reset(theme_provider); | 315 theme_provider_.reset(theme_provider); |
321 } | 316 } |
322 | 317 |
| 318 FilePath TestingProfile::GetPath() { |
| 319 DCHECK(temp_dir_.IsValid()); // TODO(phajdan.jr): do it better. |
| 320 return temp_dir_.path(); |
| 321 } |
| 322 |
323 TestingPrefService* TestingProfile::GetTestingPrefService() { | 323 TestingPrefService* TestingProfile::GetTestingPrefService() { |
324 return static_cast<TestingPrefService*>(GetPrefs()); | 324 return static_cast<TestingPrefService*>(GetPrefs()); |
325 } | 325 } |
326 | 326 |
327 webkit_database::DatabaseTracker* TestingProfile::GetDatabaseTracker() { | 327 webkit_database::DatabaseTracker* TestingProfile::GetDatabaseTracker() { |
328 if (!db_tracker_) | 328 if (!db_tracker_) |
329 db_tracker_ = new webkit_database::DatabaseTracker(GetPath(), false); | 329 db_tracker_ = new webkit_database::DatabaseTracker(GetPath(), false); |
330 return db_tracker_; | 330 return db_tracker_; |
331 } | 331 } |
332 | 332 |
(...skipping 20 matching lines...) Expand all Loading... |
353 prefs_.reset(new TestingPrefService()); | 353 prefs_.reset(new TestingPrefService()); |
354 Profile::RegisterUserPrefs(prefs_.get()); | 354 Profile::RegisterUserPrefs(prefs_.get()); |
355 browser::RegisterAllPrefs(prefs_.get(), prefs_.get()); | 355 browser::RegisterAllPrefs(prefs_.get(), prefs_.get()); |
356 } | 356 } |
357 return prefs_.get(); | 357 return prefs_.get(); |
358 } | 358 } |
359 | 359 |
360 history::TopSites* TestingProfile::GetTopSites() { | 360 history::TopSites* TestingProfile::GetTopSites() { |
361 if (!top_sites_.get()) { | 361 if (!top_sites_.get()) { |
362 top_sites_ = new history::TopSites(this); | 362 top_sites_ = new history::TopSites(this); |
363 if (!temp_dir_.CreateUniqueTempDir()) | |
364 return NULL; | |
365 FilePath file_name = temp_dir_.path().AppendASCII("TopSites.db"); | 363 FilePath file_name = temp_dir_.path().AppendASCII("TopSites.db"); |
366 top_sites_->Init(file_name); | 364 top_sites_->Init(file_name); |
367 } | 365 } |
368 return top_sites_; | 366 return top_sites_; |
369 } | 367 } |
370 | 368 |
371 URLRequestContextGetter* TestingProfile::GetRequestContext() { | 369 URLRequestContextGetter* TestingProfile::GetRequestContext() { |
372 return request_context_.get(); | 370 return request_context_.get(); |
373 } | 371 } |
374 | 372 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } | 462 } |
465 return profile_sync_service_.get(); | 463 return profile_sync_service_.get(); |
466 } | 464 } |
467 | 465 |
468 void TestingProfile::DestroyWebDataService() { | 466 void TestingProfile::DestroyWebDataService() { |
469 if (!web_data_service_.get()) | 467 if (!web_data_service_.get()) |
470 return; | 468 return; |
471 | 469 |
472 web_data_service_->Shutdown(); | 470 web_data_service_->Shutdown(); |
473 } | 471 } |
OLD | NEW |