blob: 27a2dccfe27bf7eab14bf70541adbd543fd3d666 [file] [log] [blame]
[email protected]e7e46732012-01-05 11:45:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]a4ff9eae2011-08-01 19:58:165#include "chrome/test/base/testing_profile.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a42b5592009-09-03 16:52:237#include "build/build_config.h"
[email protected]dbbad7a2010-08-13 18:18:368
9#include "base/base_paths.h"
[email protected]e3e43d92010-02-26 22:02:3810#include "base/command_line.h"
[email protected]dbbad7a2010-08-13 18:18:3611#include "base/file_util.h"
[email protected]83a7d2eb2010-05-03 21:46:1912#include "base/message_loop_proxy.h"
[email protected]dbbad7a2010-08-13 18:18:3613#include "base/path_service.h"
[email protected]8e937c1e2012-06-28 22:57:3014#include "base/run_loop.h"
[email protected]e83326f2010-07-31 17:29:2515#include "base/string_number_conversions.h"
[email protected]dbbad7a2010-08-13 18:18:3616#include "chrome/browser/autocomplete/autocomplete_classifier.h"
[email protected]a9afddb2009-02-12 17:49:4217#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]f61f4782012-06-08 21:54:2118#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]1459fb62011-05-25 19:03:2719#include "chrome/browser/browser_process.h"
[email protected]9d01a6a2010-11-30 12:03:3320#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]2f69b382011-02-19 00:34:2521#include "chrome/browser/extensions/extension_service.h"
[email protected]19eb80152011-02-26 00:28:4322#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]31d8f5f22012-04-02 15:22:0823#include "chrome/browser/extensions/extension_system.h"
24#include "chrome/browser/extensions/extension_system_factory.h"
25#include "chrome/browser/extensions/test_extension_system.h"
[email protected]ee2ed42c2011-04-28 22:19:1426#include "chrome/browser/favicon/favicon_service.h"
[email protected]c476e632011-06-23 11:18:0427#include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
[email protected]dbbad7a2010-08-13 18:18:3628#include "chrome/browser/history/history.h"
[email protected]075ae732009-02-11 23:58:3129#include "chrome/browser/history/history_backend.h"
[email protected]f61f4782012-06-08 21:54:2130#include "chrome/browser/history/history_service_factory.h"
[email protected]572f40c9b2012-07-06 20:35:3331#include "chrome/browser/history/shortcuts_backend.h"
32#include "chrome/browser/history/shortcuts_backend_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3633#include "chrome/browser/history/top_sites.h"
[email protected]6f96cbcb2011-11-04 02:26:0734#include "chrome/browser/net/proxy_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3635#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5336#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]37858e52010-08-26 00:22:0237#include "chrome/browser/prefs/browser_prefs.h"
[email protected]9a8c4022011-01-25 14:25:3338#include "chrome/browser/prefs/testing_pref_store.h"
[email protected]4850a7f2011-03-08 23:36:5939#include "chrome/browser/prerender/prerender_manager.h"
[email protected]5cdcd152011-03-31 22:48:5240#include "chrome/browser/profiles/profile_dependency_manager.h"
[email protected]ee78d8a2012-03-15 16:53:4841#include "chrome/browser/protector/protector_service_factory.h"
[email protected]16bd088b2012-04-03 08:12:1242#include "chrome/browser/search_engines/template_url_fetcher_factory.h"
[email protected]c52b2892012-03-07 11:01:0243#include "chrome/browser/speech/chrome_speech_recognition_preferences.h"
[email protected]810ffba2012-06-12 01:07:4844#include "chrome/browser/webdata/web_data_service.h"
[email protected]d07edd42012-05-14 23:49:4645#include "chrome/browser/webdata/web_data_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4246#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:2747#include "chrome/common/chrome_notification_types.h"
[email protected]9e33d7e2011-09-30 16:43:5448#include "chrome/common/chrome_switches.h"
[email protected]dbbad7a2010-08-13 18:18:3649#include "chrome/common/url_constants.h"
[email protected]7bc60682011-07-29 20:55:5950#include "chrome/test/base/bookmark_load_observer.h"
[email protected]8ad3636e2011-08-01 22:31:4051#include "chrome/test/base/testing_pref_service.h"
[email protected]af44e7fb2011-07-29 18:32:3252#include "chrome/test/base/ui_test_utils.h"
[email protected]c38831a12011-10-28 12:44:4953#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0754#include "content/public/browser/notification_service.h"
[email protected]fb29e6cf2012-07-12 21:27:2055#include "content/public/browser/render_process_host.h"
[email protected]08a932d52012-06-03 21:42:1256#include "content/public/test/mock_resource_context.h"
[email protected]a7fe9112012-07-20 02:34:4557#include "content/public/test/test_utils.h"
[email protected]aa84a7e2012-03-15 21:29:0658#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0459#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3460#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4461#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4662#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3463
[email protected]21d3a882012-05-31 14:41:5564#if defined(ENABLE_CONFIGURATION_POLICY)
65#include "chrome/browser/policy/policy_service_impl.h"
66#else
67#include "chrome/browser/policy/policy_service_stub.h"
68#endif // defined(ENABLE_CONFIGURATION_POLICY)
69
[email protected]6f96cbcb2011-11-04 02:26:0770#if defined(OS_CHROMEOS)
71#include "chrome/browser/chromeos/proxy_config_service_impl.h"
72#endif // defined(OS_CHROMEOS)
73
[email protected]e1acf6f2008-10-27 20:43:3374using base::Time;
[email protected]631bb742011-11-02 11:29:3975using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:5776using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:2377using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:4678using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:3379
[email protected]d364c652008-08-29 19:46:5680namespace {
81
[email protected]0bfc29a2009-04-27 16:15:4482// Task used to make sure history has finished processing a request. Intended
83// for use with BlockUntilHistoryProcessesPendingRequests.
84
85class QuittingHistoryDBTask : public HistoryDBTask {
86 public:
87 QuittingHistoryDBTask() {}
88
89 virtual bool RunOnDBThread(history::HistoryBackend* backend,
90 history::HistoryDatabase* db) {
91 return true;
92 }
93
94 virtual void DoneRunOnMainThread() {
95 MessageLoop::current()->Quit();
96 }
97
98 private:
[email protected]7991a232009-11-06 01:55:4899 ~QuittingHistoryDBTask() {}
100
[email protected]0bfc29a2009-04-27 16:15:44101 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
102};
103
[email protected]aeb53f02011-01-15 00:21:34104class TestExtensionURLRequestContext : public net::URLRequestContext {
[email protected]c10da4b02010-03-25 14:38:32105 public:
106 TestExtensionURLRequestContext() {
107 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
108 const char* schemes[] = {chrome::kExtensionScheme};
109 cookie_monster->SetCookieableSchemes(schemes, 1);
[email protected]f6c21cb2011-02-16 19:45:41110 set_cookie_store(cookie_monster);
[email protected]c10da4b02010-03-25 14:38:32111 }
[email protected]7b2720b2012-04-25 16:59:11112
[email protected]7b2720b2012-04-25 16:59:11113 virtual ~TestExtensionURLRequestContext() {}
[email protected]c10da4b02010-03-25 14:38:32114};
115
[email protected]abe2c032011-03-31 18:49:34116class TestExtensionURLRequestContextGetter
117 : public net::URLRequestContextGetter {
[email protected]c10da4b02010-03-25 14:38:32118 public:
[email protected]aeb53f02011-01-15 00:21:34119 virtual net::URLRequestContext* GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09120 if (!context_.get())
121 context_.reset(new TestExtensionURLRequestContext());
[email protected]c10da4b02010-03-25 14:38:32122 return context_.get();
123 }
[email protected]4969b0122012-06-16 01:58:28124 virtual scoped_refptr<base::SingleThreadTaskRunner>
125 GetNetworkTaskRunner() const OVERRIDE {
[email protected]0c7d74f2010-10-11 11:55:26126 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]83a7d2eb2010-05-03 21:46:19127 }
[email protected]c10da4b02010-03-25 14:38:32128
[email protected]13ed17f82012-04-06 02:27:18129 protected:
130 virtual ~TestExtensionURLRequestContextGetter() {}
131
[email protected]c10da4b02010-03-25 14:38:32132 private:
[email protected]ef2bf422012-05-11 03:27:09133 scoped_ptr<net::URLRequestContext> context_;
[email protected]c10da4b02010-03-25 14:38:32134};
135
[email protected]abc77242012-03-16 04:32:31136ProfileKeyedService* CreateTestDesktopNotificationService(Profile* profile) {
[email protected]29cebbd2012-06-06 21:43:42137#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53138 return new DesktopNotificationService(profile, NULL);
[email protected]29cebbd2012-06-06 21:43:42139#else
140 return NULL;
141#endif
[email protected]dcb72d52011-04-13 12:36:53142}
143
[email protected]d364c652008-08-29 19:46:56144} // namespace
145
[email protected]89dbb1772012-07-17 13:47:25146// static
147#if defined(OS_CHROMEOS)
148// Must be kept in sync with
149// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
150const char TestingProfile::kTestUserProfileDir[] = "test-user";
151#else
152const char TestingProfile::kTestUserProfileDir[] = "Default";
153#endif
154
[email protected]f25387b2008-08-21 15:20:33155TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55156 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28157 testing_prefs_(NULL),
[email protected]5bb2f522011-03-25 19:04:44158 incognito_(false),
[email protected]5cdcd152011-03-31 22:48:52159 last_session_exited_cleanly_(true),
[email protected]537c1082011-12-02 02:37:17160 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
161 delegate_(NULL) {
[email protected]f9dec9482010-08-20 20:42:19162 if (!temp_dir_.CreateUniqueTempDir()) {
163 LOG(ERROR) << "Failed to create unique temporary directory.";
initial.commit09911bf2008-07-26 23:55:29164
[email protected]f9dec9482010-08-20 20:42:19165 // 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);
[email protected]848cd05e2008-09-19 18:33:48168
[email protected]f9dec9482010-08-20 20:42:19169 // 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 }
[email protected]dcb72d52011-04-13 12:36:53185
[email protected]49a25632011-08-31 17:03:48186 profile_path_ = temp_dir_.path();
187
188 Init();
[email protected]537c1082011-12-02 02:37:17189 FinishInit();
[email protected]49a25632011-08-31 17:03:48190}
191
192TestingProfile::TestingProfile(const FilePath& path)
193 : start_time_(Time::Now()),
194 testing_prefs_(NULL),
195 incognito_(false),
196 last_session_exited_cleanly_(true),
197 profile_path_(path),
[email protected]537c1082011-12-02 02:37:17198 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
199 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48200 Init();
[email protected]537c1082011-12-02 02:37:17201 FinishInit();
202}
203
204TestingProfile::TestingProfile(const FilePath& path,
205 Delegate* delegate)
206 : start_time_(Time::Now()),
207 testing_prefs_(NULL),
208 incognito_(false),
209 last_session_exited_cleanly_(true),
210 profile_path_(path),
211 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
212 delegate_(delegate) {
213 Init();
214 if (delegate_) {
215 MessageLoop::current()->PostTask(FROM_HERE,
216 base::Bind(&TestingProfile::FinishInit,
217 base::Unretained(this)));
218 } else {
219 FinishInit();
220 }
[email protected]49a25632011-08-31 17:03:48221}
222
223void TestingProfile::Init() {
[email protected]d41e6572012-05-04 20:49:42224 if (!file_util::PathExists(profile_path_))
225 file_util::CreateDirectory(profile_path_);
226
[email protected]bd306722012-07-11 20:43:59227 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
228 this, extensions::TestExtensionSystem::Build);
[email protected]31d8f5f22012-04-02 15:22:08229
[email protected]49a25632011-08-31 17:03:48230 profile_dependency_manager_->CreateProfileServices(this, true);
231
[email protected]5fd2e842012-03-01 00:29:11232#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53233 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22234 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
235 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11236#endif
[email protected]537c1082011-12-02 02:37:17237}
[email protected]12463cd2011-07-19 09:40:20238
[email protected]537c1082011-12-02 02:37:17239void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40240 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07241 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20242 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53243 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07244 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17245
246 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03247 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46248}
249
[email protected]f25387b2008-08-21 15:20:33250TestingProfile::~TestingProfile() {
[email protected]080adba92011-12-07 19:00:40251 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07252 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27253 chrome::NOTIFICATION_PROFILE_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53254 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07255 content::NotificationService::NoDetails());
[email protected]5cdcd152011-03-31 22:48:52256
257 profile_dependency_manager_->DestroyProfileServices(this);
258
[email protected]35552dc52011-07-12 09:04:38259 if (host_content_settings_map_)
260 host_content_settings_map_->ShutdownOnUIThread();
261
[email protected]809cc4d2010-10-27 15:22:54262 DestroyTopSites();
[email protected]d2879af2010-02-08 16:02:56263 DestroyFaviconService();
[email protected]cdba46992011-06-07 11:51:39264
[email protected]cc5bfd42010-11-24 14:44:02265 if (pref_proxy_config_tracker_.get())
266 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]f25387b2008-08-21 15:20:33267}
initial.commit09911bf2008-07-26 23:55:29268
[email protected]d2879af2010-02-08 16:02:56269void TestingProfile::CreateFaviconService() {
[email protected]04732c0e2011-09-16 13:21:53270 favicon_service_.reset(new FaviconService(this));
[email protected]d2879af2010-02-08 16:02:56271}
272
[email protected]f61f4782012-06-08 21:54:21273static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService(
274 Profile* profile) {
275 return new HistoryService(profile);
276}
277
[email protected]d486a0852009-11-02 21:40:00278void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54279 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33280 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32281 FilePath path = GetPath();
282 path = path.Append(chrome::kHistoryFilename);
[email protected]f25387b2008-08-21 15:20:33283 file_util::Delete(path, false);
284 }
[email protected]f61f4782012-06-08 21:54:21285 // This will create and init the history service.
286 HistoryService* history_service = static_cast<HistoryService*>(
287 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
288 this, BuildHistoryService).get());
289 if (!history_service->Init(this->GetPath(),
[email protected]34f2149b2012-06-13 08:23:23290 BookmarkModelFactory::GetForProfile(this),
[email protected]f61f4782012-06-08 21:54:21291 no_db)) {
292 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
293 }
initial.commit09911bf2008-07-26 23:55:29294}
295
296void TestingProfile::DestroyHistoryService() {
[email protected]f61f4782012-06-08 21:54:21297 scoped_refptr<HistoryService> history_service =
[email protected]6c537042012-07-13 00:59:02298 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]f61f4782012-06-08 21:54:21299 if (!history_service.get())
initial.commit09911bf2008-07-26 23:55:29300 return;
301
[email protected]f61f4782012-06-08 21:54:21302 history_service->NotifyRenderProcessHostDestruction(0);
303 history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure());
304 history_service->Cleanup();
305 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29306
307 // Wait for the backend class to terminate before deleting the files and
308 // moving to the next test. Note: if this never terminates, somebody is
309 // probably leaking a reference to the history backend, so it never calls
310 // our destroy task.
311 MessageLoop::current()->Run();
312
initial.commit09911bf2008-07-26 23:55:29313 // Make sure we don't have any event pending that could disrupt the next
314 // test.
[email protected]1cb1a242011-12-10 18:36:51315 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
initial.commit09911bf2008-07-26 23:55:29316 MessageLoop::current()->Run();
317}
[email protected]4d0cd7ce2008-08-11 16:40:57318
[email protected]809cc4d2010-10-27 15:22:54319void TestingProfile::CreateTopSites() {
320 DestroyTopSites();
321 top_sites_ = new history::TopSites(this);
[email protected]73c47932010-12-06 18:13:43322 FilePath file_name = GetPath().Append(chrome::kTopSitesFilename);
[email protected]809cc4d2010-10-27 15:22:54323 top_sites_->Init(file_name);
324}
325
326void TestingProfile::DestroyTopSites() {
327 if (top_sites_.get()) {
328 top_sites_->Shutdown();
329 top_sites_ = NULL;
330 // TopSites::Shutdown schedules some tasks (from TopSitesBackend) that need
331 // to be run to properly shutdown. Run all pending tasks now. This is
332 // normally handled by browser_process shutdown.
333 if (MessageLoop::current())
334 MessageLoop::current()->RunAllPending();
335 }
336}
337
338void TestingProfile::DestroyFaviconService() {
[email protected]04732c0e2011-09-16 13:21:53339 favicon_service_.reset();
[email protected]809cc4d2010-10-27 15:22:54340}
341
[email protected]f61f4782012-06-08 21:54:21342static ProfileKeyedService* BuildBookmarkModel(Profile* profile) {
343 BookmarkModel* bookmark_model = new BookmarkModel(profile);
344 bookmark_model->Load();
345 return bookmark_model;
346}
347
348
[email protected]d8e41ed2008-09-11 15:22:32349void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46350
351 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32352 FilePath path = GetPath();
353 path = path.Append(chrome::kBookmarksFileName);
[email protected]90ef13132008-08-27 03:27:46354 file_util::Delete(path, false);
355 }
[email protected]f61f4782012-06-08 21:54:21356 // This will create a bookmark model.
357 BookmarkModel* bookmark_service =
358 static_cast<BookmarkModel*>(
359 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
360 this, BuildBookmarkModel));
361
362 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02363 HistoryServiceFactory::GetForProfileWithoutCreating(this).get();
[email protected]f61f4782012-06-08 21:54:21364 if (history_service) {
365 history_service->history_backend_->bookmark_service_ =
366 bookmark_service;
367 history_service->history_backend_->expirer_.bookmark_service_ =
368 bookmark_service;
[email protected]90ef13132008-08-27 03:27:46369 }
[email protected]4d0cd7ce2008-08-11 16:40:57370}
[email protected]d2c017a2008-08-13 21:51:45371
[email protected]d07edd42012-05-14 23:49:46372static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService(
373 Profile* profile) {
374 WebDataService* web_data_service = new WebDataService();
375 if (web_data_service)
376 web_data_service->Init(profile->GetPath());
[email protected]a5c65292012-05-25 00:50:01377 return web_data_service;
[email protected]d07edd42012-05-14 23:49:46378}
[email protected]2609bc12010-01-24 08:32:55379
[email protected]d07edd42012-05-14 23:49:46380void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01381 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46382 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55383}
384
[email protected]d364c652008-08-29 19:46:56385void TestingProfile::BlockUntilBookmarkModelLoaded() {
[email protected]f61f4782012-06-08 21:54:21386 DCHECK(GetBookmarkModel());
387 if (GetBookmarkModel()->IsLoaded())
[email protected]d364c652008-08-29 19:46:56388 return;
[email protected]8e937c1e2012-06-28 22:57:30389 base::RunLoop run_loop;
[email protected]78089f02012-07-19 06:11:28390 BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
[email protected]f61f4782012-06-08 21:54:21391 GetBookmarkModel()->AddObserver(&observer);
[email protected]8e937c1e2012-06-28 22:57:30392 run_loop.Run();
[email protected]f61f4782012-06-08 21:54:21393 GetBookmarkModel()->RemoveObserver(&observer);
394 DCHECK(GetBookmarkModel()->IsLoaded());
[email protected]d364c652008-08-29 19:46:56395}
396
[email protected]120abf132011-09-27 21:38:06397// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54398void TestingProfile::BlockUntilTopSitesLoaded() {
[email protected]a7fe9112012-07-20 02:34:45399 content::WindowedNotificationObserver top_sites_loaded_observer(
[email protected]120abf132011-09-27 21:38:06400 chrome::NOTIFICATION_TOP_SITES_LOADED,
[email protected]ad50def52011-10-19 23:17:07401 content::NotificationService::AllSources());
[email protected]809cc4d2010-10-27 15:22:54402 if (!GetHistoryService(Profile::EXPLICIT_ACCESS))
403 GetTopSites()->HistoryLoaded();
[email protected]120abf132011-09-27 21:38:06404 top_sites_loaded_observer.Wait();
[email protected]809cc4d2010-10-27 15:22:54405}
406
[email protected]f9dec9482010-08-20 20:42:19407FilePath TestingProfile::GetPath() {
[email protected]49a25632011-08-31 17:03:48408 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19409}
410
[email protected]dbbad7a2010-08-13 18:18:36411TestingPrefService* TestingProfile::GetTestingPrefService() {
[email protected]2fb7dc982010-09-29 12:24:28412 if (!prefs_.get())
413 CreateTestingPrefService();
414 DCHECK(testing_prefs_);
415 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36416}
417
[email protected]8e5c89a2011-06-07 18:13:33418TestingProfile* TestingProfile::AsTestingProfile() {
419 return this;
420}
421
[email protected]29d70252011-04-28 02:16:58422std::string TestingProfile::GetProfileName() {
423 return std::string("testing_profile");
424}
425
[email protected]27d6e852012-03-02 21:31:32426bool TestingProfile::IsOffTheRecord() const {
[email protected]5bb2f522011-03-25 19:04:44427 return incognito_;
[email protected]fadc607b62011-02-07 17:55:50428}
429
[email protected]93dba942011-02-23 22:45:01430void TestingProfile::SetOffTheRecordProfile(Profile* profile) {
[email protected]5bb2f522011-03-25 19:04:44431 incognito_profile_.reset(profile);
[email protected]93dba942011-02-23 22:45:01432}
433
[email protected]fadc607b62011-02-07 17:55:50434Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44435 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50436}
437
[email protected]d4f5d1162011-11-30 01:41:52438GAIAInfoUpdateService* TestingProfile::GetGAIAInfoUpdateService() {
439 return NULL;
440}
441
[email protected]fadc607b62011-02-07 17:55:50442bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44443 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50444}
445
446Profile* TestingProfile::GetOriginalProfile() {
447 return this;
448}
449
[email protected]fadc607b62011-02-07 17:55:50450VisitedLinkMaster* TestingProfile::GetVisitedLinkMaster() {
451 return NULL;
452}
453
[email protected]eaa7dd182010-12-14 11:09:00454ExtensionService* TestingProfile::GetExtensionService() {
[email protected]bd306722012-07-11 20:43:59455 return extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]2fb7dc982010-09-29 12:24:28456}
457
[email protected]20f97c92012-07-13 23:12:37458extensions::UserScriptMaster* TestingProfile::GetUserScriptMaster() {
[email protected]bd306722012-07-11 20:43:59459 return extensions::ExtensionSystem::Get(this)->user_script_master();
[email protected]fadc607b62011-02-07 17:55:50460}
461
462ExtensionProcessManager* TestingProfile::GetExtensionProcessManager() {
[email protected]bd306722012-07-11 20:43:59463 return extensions::ExtensionSystem::Get(this)->process_manager();
[email protected]fadc607b62011-02-07 17:55:50464}
465
466ExtensionEventRouter* TestingProfile::GetExtensionEventRouter() {
[email protected]bd306722012-07-11 20:43:59467 return extensions::ExtensionSystem::Get(this)->event_router();
[email protected]fadc607b62011-02-07 17:55:50468}
469
[email protected]2261e7b72011-07-12 13:43:55470void TestingProfile::SetExtensionSpecialStoragePolicy(
471 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
472 extension_special_storage_policy_ = extension_special_storage_policy;
473}
474
[email protected]19eb80152011-02-26 00:28:43475ExtensionSpecialStoragePolicy*
476TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]cdba46992011-06-07 11:51:39477 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05478 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39479 return extension_special_storage_policy_.get();
[email protected]19eb80152011-02-26 00:28:43480}
481
[email protected]fadc607b62011-02-07 17:55:50482FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) {
483 return favicon_service_.get();
484}
485
486HistoryService* TestingProfile::GetHistoryService(ServiceAccessType access) {
[email protected]6c537042012-07-13 00:59:02487 return HistoryServiceFactory::GetForProfileIfExists(this, access);
[email protected]fadc607b62011-02-07 17:55:50488}
489
490HistoryService* TestingProfile::GetHistoryServiceWithoutCreating() {
[email protected]6c537042012-07-13 00:59:02491 return HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]fadc607b62011-02-07 17:55:50492}
493
[email protected]dbbad7a2010-08-13 18:18:36494net::CookieMonster* TestingProfile::GetCookieMonster() {
495 if (!GetRequestContext())
496 return NULL;
[email protected]277ec262011-03-30 21:09:40497 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
498 GetCookieMonster();
[email protected]dbbad7a2010-08-13 18:18:36499}
500
[email protected]21d3a882012-05-31 14:41:55501policy::PolicyService* TestingProfile::GetPolicyService() {
502 if (!policy_service_.get()) {
503#if defined(ENABLE_CONFIGURATION_POLICY)
504 policy::PolicyServiceImpl::Providers providers;
505 policy_service_.reset(new policy::PolicyServiceImpl(providers));
506#else
507 policy_service_.reset(new policy::PolicyServiceStub());
508#endif
509 }
510 return policy_service_.get();
511}
512
[email protected]2fb7dc982010-09-29 12:24:28513void TestingProfile::SetPrefService(PrefService* prefs) {
[email protected]ddcaa412012-03-30 19:57:29514#if defined(ENABLE_PROTECTOR_SERVICE)
[email protected]ee78d8a2012-03-15 16:53:48515 // ProtectorService binds itself very closely to the PrefService at the moment
516 // of Profile creation and watches pref changes to update their backup.
517 // For tests that replace the PrefService after TestingProfile creation,
518 // ProtectorService is disabled to prevent further invalid memory accesses.
519 protector::ProtectorServiceFactory::GetInstance()->
520 SetTestingFactory(this, NULL);
[email protected]ddcaa412012-03-30 19:57:29521#endif
[email protected]2fb7dc982010-09-29 12:24:28522 prefs_.reset(prefs);
523}
524
525void TestingProfile::CreateTestingPrefService() {
526 DCHECK(!prefs_.get());
527 testing_prefs_ = new TestingPrefService();
528 prefs_.reset(testing_prefs_);
529 Profile::RegisterUserPrefs(prefs_.get());
[email protected]572f40c9b2012-07-06 20:35:33530 chrome::RegisterUserPrefs(prefs_.get());
[email protected]2fb7dc982010-09-29 12:24:28531}
532
[email protected]dbbad7a2010-08-13 18:18:36533PrefService* TestingProfile::GetPrefs() {
534 if (!prefs_.get()) {
[email protected]2fb7dc982010-09-29 12:24:28535 CreateTestingPrefService();
[email protected]dbbad7a2010-08-13 18:18:36536 }
537 return prefs_.get();
538}
539
540history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54541 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36542}
543
[email protected]fadc607b62011-02-07 17:55:50544history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
545 return top_sites_.get();
546}
547
[email protected]b441a8492012-06-06 14:55:57548DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50549 return NULL;
550}
551
[email protected]abe2c032011-03-31 18:49:34552net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]812b3a32010-01-08 05:36:04553 return request_context_.get();
554}
555
[email protected]da5683db2011-04-23 17:12:21556net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
557 int renderer_child_id) {
[email protected]31d8f5f22012-04-02 15:22:08558 ExtensionService* extension_service =
[email protected]bd306722012-07-11 20:43:59559 extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]31d8f5f22012-04-02 15:22:08560 if (extension_service) {
[email protected]1c321ee2012-05-21 03:02:34561 const extensions::Extension* installed_app = extension_service->
[email protected]da5683db2011-04-23 17:12:21562 GetInstalledAppForRenderer(renderer_child_id);
563 if (installed_app != NULL && installed_app->is_storage_isolated())
564 return GetRequestContextForIsolatedApp(installed_app->id());
565 }
[email protected]d9696672011-03-15 22:45:09566
[email protected]fb29e6cf2012-07-12 21:27:20567 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
568 renderer_child_id);
569 if (rph && rph->IsGuest()) {
570 // For guest processes (used by the browser tag), we need to isolate the
571 // storage.
572 // TODO(nasko): Until we have proper storage partitions, create a
573 // non-persistent context using the RPH's id.
574 std::string id("guest-");
575 id.append(base::IntToString(renderer_child_id));
576 return GetRequestContextForIsolatedApp(id);
577 }
578
[email protected]d9696672011-03-15 22:45:09579 return GetRequestContext();
580}
581
[email protected]812b3a32010-01-08 05:36:04582void TestingProfile::CreateRequestContext() {
583 if (!request_context_)
[email protected]44738602012-03-02 22:56:01584 request_context_ =
585 new TestURLRequestContextGetter(
586 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]812b3a32010-01-08 05:36:04587}
588
[email protected]7c893202010-10-07 20:18:02589void TestingProfile::ResetRequestContext() {
[email protected]3931fd82012-03-12 21:18:48590 // Any objects holding live URLFetchers should be deleted before the request
591 // context is shut down.
[email protected]16bd088b2012-04-03 08:12:12592 TemplateURLFetcherFactory::ShutdownForProfile(this);
[email protected]3931fd82012-03-12 21:18:48593
[email protected]7c893202010-10-07 20:18:02594 request_context_ = NULL;
595}
596
[email protected]abe2c032011-03-31 18:49:34597net::URLRequestContextGetter* TestingProfile::GetRequestContextForMedia() {
[email protected]fadc607b62011-02-07 17:55:50598 return NULL;
599}
600
[email protected]abe2c032011-03-31 18:49:34601net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
[email protected]c10da4b02010-03-25 14:38:32602 if (!extensions_request_context_)
[email protected]00dea9582012-05-09 15:20:05603 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
[email protected]c10da4b02010-03-25 14:38:32604 return extensions_request_context_.get();
605}
606
[email protected]fadc607b62011-02-07 17:55:50607net::SSLConfigService* TestingProfile::GetSSLConfigService() {
608 return NULL;
609}
610
[email protected]abe2c032011-03-31 18:49:34611net::URLRequestContextGetter* TestingProfile::GetRequestContextForIsolatedApp(
[email protected]d9696672011-03-15 22:45:09612 const std::string& app_id) {
613 // We don't test isolated app storage here yet, so returning the same dummy
614 // context is sufficient for now.
615 return GetRequestContext();
616}
617
[email protected]df02aca2012-02-09 21:03:20618content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]314c3e22012-02-21 03:57:42619 if (!resource_context_.get())
620 resource_context_.reset(new content::MockResourceContext());
621 return resource_context_.get();
[email protected]e89b77d2011-04-15 18:58:10622}
623
[email protected]dbbad7a2010-08-13 18:18:36624HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38625 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05626 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
627 ExtensionService* extension_service = GetExtensionService();
628 if (extension_service)
629 host_content_settings_map_->RegisterExtensionService(extension_service);
[email protected]35552dc52011-07-12 09:04:38630 }
[email protected]dbbad7a2010-08-13 18:18:36631 return host_content_settings_map_.get();
632}
633
[email protected]810ddc52012-01-24 01:00:35634content::GeolocationPermissionContext*
[email protected]dbbad7a2010-08-13 18:18:36635TestingProfile::GetGeolocationPermissionContext() {
636 if (!geolocation_permission_context_.get()) {
637 geolocation_permission_context_ =
[email protected]c476e632011-06-23 11:18:04638 new ChromeGeolocationPermissionContext(this);
[email protected]dbbad7a2010-08-13 18:18:36639 }
640 return geolocation_permission_context_.get();
641}
642
[email protected]c52b2892012-03-07 11:01:02643content::SpeechRecognitionPreferences*
644 TestingProfile::GetSpeechRecognitionPreferences() {
[email protected]3297132a2012-02-21 23:27:59645#if defined(ENABLE_INPUT_SPEECH)
[email protected]750e9572012-06-06 15:21:24646 return ChromeSpeechRecognitionPreferences::GetForProfile(this);
[email protected]3297132a2012-02-21 23:27:59647#else
648 return NULL;
649#endif
[email protected]8238dd62011-09-29 15:13:01650}
651
[email protected]fadc607b62011-02-07 17:55:50652std::wstring TestingProfile::GetName() {
653 return std::wstring();
654}
655
656std::wstring TestingProfile::GetID() {
657 return id_;
658}
659
660void TestingProfile::SetID(const std::wstring& id) {
661 id_ = id;
662}
663
664bool TestingProfile::DidLastSessionExitCleanly() {
665 return last_session_exited_cleanly_;
666}
667
668BookmarkModel* TestingProfile::GetBookmarkModel() {
[email protected]f61f4782012-06-08 21:54:21669 return BookmarkModelFactory::GetForProfileIfExists(this);
[email protected]fadc607b62011-02-07 17:55:50670}
671
672bool TestingProfile::IsSameProfile(Profile *p) {
673 return this == p;
674}
675
676base::Time TestingProfile::GetStartTime() const {
677 return start_time_;
678}
679
[email protected]a6d36cc2011-02-23 00:39:48680ProtocolHandlerRegistry* TestingProfile::GetProtocolHandlerRegistry() {
[email protected]ab563ff2012-07-21 00:26:32681 return NULL;
[email protected]a6d36cc2011-02-23 00:39:48682}
683
[email protected]fadc607b62011-02-07 17:55:50684FilePath TestingProfile::last_selected_directory() {
685 return last_selected_directory_;
686}
687
688void TestingProfile::set_last_selected_directory(const FilePath& path) {
689 last_selected_directory_ = path;
690}
691
[email protected]cc5bfd42010-11-24 14:44:02692PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07693 if (!pref_proxy_config_tracker_.get()) {
694 pref_proxy_config_tracker_.reset(
695 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs()));
696 }
697 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02698}
699
[email protected]0bfc29a2009-04-27 16:15:44700void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]f61f4782012-06-08 21:54:21701 scoped_refptr<HistoryService> history_service =
[email protected]6c537042012-07-13 00:59:02702 HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
[email protected]f61f4782012-06-08 21:54:21703 DCHECK(history_service.get());
[email protected]0bfc29a2009-04-27 16:15:44704 DCHECK(MessageLoop::current());
705
706 CancelableRequestConsumer consumer;
[email protected]f61f4782012-06-08 21:54:21707 history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer);
[email protected]0bfc29a2009-04-27 16:15:44708 MessageLoop::current()->Run();
709}
[email protected]345a8b72009-09-29 09:11:44710
[email protected]8382d8c2011-09-15 03:43:17711chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
712 return NULL;
713}
714
[email protected]db96a882011-10-09 02:01:54715void TestingProfile::ClearNetworkingHistorySince(base::Time time) {
[email protected]02896a82011-09-21 18:54:32716 NOTIMPLEMENTED();
717}
718
[email protected]671f9c62011-10-28 19:22:07719GURL TestingProfile::GetHomePage() {
720 return GURL(chrome::kChromeUINewTabURL);
721}
722
[email protected]fadc607b62011-02-07 17:55:50723PrefService* TestingProfile::GetOffTheRecordPrefs() {
724 return NULL;
725}
726
[email protected]36fb2c7c2011-04-04 15:49:08727quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
728 return GetExtensionSpecialStoragePolicy();
729}
730
[email protected]1bee6ed2012-03-14 06:46:36731bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
732 return true;
733}
[email protected]ef92e172012-04-25 19:40:41734
735base::Callback<ChromeURLDataManagerBackend*(void)>
736 TestingProfile::GetChromeURLDataManagerBackendGetter() const {
737 return base::Callback<ChromeURLDataManagerBackend*(void)>();
738}