blob: aa471a87649479b555ec68b5bbc051452a0e478e [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]9b4d241c92012-06-29 19:48:3121#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
[email protected]2f69b382011-02-19 00:34:2522#include "chrome/browser/extensions/extension_service.h"
[email protected]19eb80152011-02-26 00:28:4323#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]31d8f5f22012-04-02 15:22:0824#include "chrome/browser/extensions/extension_system.h"
25#include "chrome/browser/extensions/extension_system_factory.h"
26#include "chrome/browser/extensions/test_extension_system.h"
[email protected]ee2ed42c2011-04-28 22:19:1427#include "chrome/browser/favicon/favicon_service.h"
[email protected]c476e632011-06-23 11:18:0428#include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
[email protected]dbbad7a2010-08-13 18:18:3629#include "chrome/browser/history/history.h"
[email protected]075ae732009-02-11 23:58:3130#include "chrome/browser/history/history_backend.h"
[email protected]f61f4782012-06-08 21:54:2131#include "chrome/browser/history/history_service_factory.h"
[email protected]572f40c9b2012-07-06 20:35:3332#include "chrome/browser/history/shortcuts_backend.h"
33#include "chrome/browser/history/shortcuts_backend_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3634#include "chrome/browser/history/top_sites.h"
[email protected]6f96cbcb2011-11-04 02:26:0735#include "chrome/browser/net/proxy_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3636#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5337#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]37858e52010-08-26 00:22:0238#include "chrome/browser/prefs/browser_prefs.h"
[email protected]9a8c4022011-01-25 14:25:3339#include "chrome/browser/prefs/testing_pref_store.h"
[email protected]4850a7f2011-03-08 23:36:5940#include "chrome/browser/prerender/prerender_manager.h"
[email protected]5cdcd152011-03-31 22:48:5241#include "chrome/browser/profiles/profile_dependency_manager.h"
[email protected]ee78d8a2012-03-15 16:53:4842#include "chrome/browser/protector/protector_service_factory.h"
[email protected]16bd088b2012-04-03 08:12:1243#include "chrome/browser/search_engines/template_url_fetcher_factory.h"
[email protected]c52b2892012-03-07 11:01:0244#include "chrome/browser/speech/chrome_speech_recognition_preferences.h"
[email protected]810ffba2012-06-12 01:07:4845#include "chrome/browser/webdata/web_data_service.h"
[email protected]d07edd42012-05-14 23:49:4646#include "chrome/browser/webdata/web_data_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4247#include "chrome/common/chrome_constants.h"
[email protected]432115822011-07-10 15:52:2748#include "chrome/common/chrome_notification_types.h"
[email protected]9e33d7e2011-09-30 16:43:5449#include "chrome/common/chrome_switches.h"
[email protected]dbbad7a2010-08-13 18:18:3650#include "chrome/common/url_constants.h"
[email protected]7bc60682011-07-29 20:55:5951#include "chrome/test/base/bookmark_load_observer.h"
[email protected]8ad3636e2011-08-01 22:31:4052#include "chrome/test/base/testing_pref_service.h"
[email protected]af44e7fb2011-07-29 18:32:3253#include "chrome/test/base/ui_test_utils.h"
[email protected]c38831a12011-10-28 12:44:4954#include "content/public/browser/browser_thread.h"
[email protected]ad50def52011-10-19 23:17:0755#include "content/public/browser/notification_service.h"
[email protected]08a932d52012-06-03 21:42:1256#include "content/public/test/mock_resource_context.h"
[email protected]aa84a7e2012-03-15 21:29:0657#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0458#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3459#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4460#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4661#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3462
[email protected]21d3a882012-05-31 14:41:5563#if defined(ENABLE_CONFIGURATION_POLICY)
64#include "chrome/browser/policy/policy_service_impl.h"
65#else
66#include "chrome/browser/policy/policy_service_stub.h"
67#endif // defined(ENABLE_CONFIGURATION_POLICY)
68
[email protected]6f96cbcb2011-11-04 02:26:0769#if defined(OS_CHROMEOS)
70#include "chrome/browser/chromeos/proxy_config_service_impl.h"
71#endif // defined(OS_CHROMEOS)
72
[email protected]e1acf6f2008-10-27 20:43:3373using base::Time;
[email protected]631bb742011-11-02 11:29:3974using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:5775using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:2376using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:4677using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:3378
[email protected]d364c652008-08-29 19:46:5679namespace {
80
[email protected]0bfc29a2009-04-27 16:15:4481// Task used to make sure history has finished processing a request. Intended
82// for use with BlockUntilHistoryProcessesPendingRequests.
83
84class QuittingHistoryDBTask : public HistoryDBTask {
85 public:
86 QuittingHistoryDBTask() {}
87
88 virtual bool RunOnDBThread(history::HistoryBackend* backend,
89 history::HistoryDatabase* db) {
90 return true;
91 }
92
93 virtual void DoneRunOnMainThread() {
94 MessageLoop::current()->Quit();
95 }
96
97 private:
[email protected]7991a232009-11-06 01:55:4898 ~QuittingHistoryDBTask() {}
99
[email protected]0bfc29a2009-04-27 16:15:44100 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
101};
102
[email protected]aeb53f02011-01-15 00:21:34103class TestExtensionURLRequestContext : public net::URLRequestContext {
[email protected]c10da4b02010-03-25 14:38:32104 public:
105 TestExtensionURLRequestContext() {
106 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
107 const char* schemes[] = {chrome::kExtensionScheme};
108 cookie_monster->SetCookieableSchemes(schemes, 1);
[email protected]f6c21cb2011-02-16 19:45:41109 set_cookie_store(cookie_monster);
[email protected]c10da4b02010-03-25 14:38:32110 }
[email protected]7b2720b2012-04-25 16:59:11111
[email protected]7b2720b2012-04-25 16:59:11112 virtual ~TestExtensionURLRequestContext() {}
[email protected]c10da4b02010-03-25 14:38:32113};
114
[email protected]abe2c032011-03-31 18:49:34115class TestExtensionURLRequestContextGetter
116 : public net::URLRequestContextGetter {
[email protected]c10da4b02010-03-25 14:38:32117 public:
[email protected]aeb53f02011-01-15 00:21:34118 virtual net::URLRequestContext* GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09119 if (!context_.get())
120 context_.reset(new TestExtensionURLRequestContext());
[email protected]c10da4b02010-03-25 14:38:32121 return context_.get();
122 }
[email protected]4969b0122012-06-16 01:58:28123 virtual scoped_refptr<base::SingleThreadTaskRunner>
124 GetNetworkTaskRunner() const OVERRIDE {
[email protected]0c7d74f2010-10-11 11:55:26125 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]83a7d2eb2010-05-03 21:46:19126 }
[email protected]c10da4b02010-03-25 14:38:32127
[email protected]13ed17f82012-04-06 02:27:18128 protected:
129 virtual ~TestExtensionURLRequestContextGetter() {}
130
[email protected]c10da4b02010-03-25 14:38:32131 private:
[email protected]ef2bf422012-05-11 03:27:09132 scoped_ptr<net::URLRequestContext> context_;
[email protected]c10da4b02010-03-25 14:38:32133};
134
[email protected]abc77242012-03-16 04:32:31135ProfileKeyedService* CreateTestDesktopNotificationService(Profile* profile) {
[email protected]29cebbd2012-06-06 21:43:42136#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53137 return new DesktopNotificationService(profile, NULL);
[email protected]29cebbd2012-06-06 21:43:42138#else
139 return NULL;
140#endif
[email protected]dcb72d52011-04-13 12:36:53141}
142
[email protected]d364c652008-08-29 19:46:56143} // namespace
144
[email protected]f25387b2008-08-21 15:20:33145TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55146 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28147 testing_prefs_(NULL),
[email protected]5bb2f522011-03-25 19:04:44148 incognito_(false),
[email protected]5cdcd152011-03-31 22:48:52149 last_session_exited_cleanly_(true),
[email protected]537c1082011-12-02 02:37:17150 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
151 delegate_(NULL) {
[email protected]f9dec9482010-08-20 20:42:19152 if (!temp_dir_.CreateUniqueTempDir()) {
153 LOG(ERROR) << "Failed to create unique temporary directory.";
initial.commit09911bf2008-07-26 23:55:29154
[email protected]f9dec9482010-08-20 20:42:19155 // Fallback logic in case we fail to create unique temporary directory.
156 FilePath system_tmp_dir;
157 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
[email protected]848cd05e2008-09-19 18:33:48158
[email protected]f9dec9482010-08-20 20:42:19159 // We're severly screwed if we can't get the system temporary
160 // directory. Die now to avoid writing to the filesystem root
161 // or other bad places.
162 CHECK(success);
163
164 FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath"));
165 file_util::Delete(fallback_dir, true);
166 file_util::CreateDirectory(fallback_dir);
167 if (!temp_dir_.Set(fallback_dir)) {
168 // That shouldn't happen, but if it does, try to recover.
169 LOG(ERROR) << "Failed to use a fallback temporary directory.";
170
171 // We're screwed if this fails, see CHECK above.
172 CHECK(temp_dir_.Set(system_tmp_dir));
173 }
174 }
[email protected]dcb72d52011-04-13 12:36:53175
[email protected]49a25632011-08-31 17:03:48176 profile_path_ = temp_dir_.path();
177
178 Init();
[email protected]537c1082011-12-02 02:37:17179 FinishInit();
[email protected]49a25632011-08-31 17:03:48180}
181
182TestingProfile::TestingProfile(const FilePath& path)
183 : start_time_(Time::Now()),
184 testing_prefs_(NULL),
185 incognito_(false),
186 last_session_exited_cleanly_(true),
187 profile_path_(path),
[email protected]537c1082011-12-02 02:37:17188 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
189 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48190 Init();
[email protected]537c1082011-12-02 02:37:17191 FinishInit();
192}
193
194TestingProfile::TestingProfile(const FilePath& path,
195 Delegate* delegate)
196 : start_time_(Time::Now()),
197 testing_prefs_(NULL),
198 incognito_(false),
199 last_session_exited_cleanly_(true),
200 profile_path_(path),
201 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
202 delegate_(delegate) {
203 Init();
204 if (delegate_) {
205 MessageLoop::current()->PostTask(FROM_HERE,
206 base::Bind(&TestingProfile::FinishInit,
207 base::Unretained(this)));
208 } else {
209 FinishInit();
210 }
[email protected]49a25632011-08-31 17:03:48211}
212
213void TestingProfile::Init() {
[email protected]d41e6572012-05-04 20:49:42214 if (!file_util::PathExists(profile_path_))
215 file_util::CreateDirectory(profile_path_);
216
[email protected]bd306722012-07-11 20:43:59217 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
218 this, extensions::TestExtensionSystem::Build);
[email protected]31d8f5f22012-04-02 15:22:08219
[email protected]49a25632011-08-31 17:03:48220 profile_dependency_manager_->CreateProfileServices(this, true);
221
[email protected]5fd2e842012-03-01 00:29:11222#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53223 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22224 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
225 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11226#endif
[email protected]537c1082011-12-02 02:37:17227}
[email protected]12463cd2011-07-19 09:40:20228
[email protected]537c1082011-12-02 02:37:17229void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40230 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07231 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20232 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53233 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07234 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17235
236 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03237 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46238}
239
[email protected]f25387b2008-08-21 15:20:33240TestingProfile::~TestingProfile() {
[email protected]080adba92011-12-07 19:00:40241 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07242 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27243 chrome::NOTIFICATION_PROFILE_DESTROYED,
[email protected]6c2381d2011-10-19 02:52:53244 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07245 content::NotificationService::NoDetails());
[email protected]5cdcd152011-03-31 22:48:52246
247 profile_dependency_manager_->DestroyProfileServices(this);
248
[email protected]35552dc52011-07-12 09:04:38249 if (host_content_settings_map_)
250 host_content_settings_map_->ShutdownOnUIThread();
251
[email protected]809cc4d2010-10-27 15:22:54252 DestroyTopSites();
[email protected]d2879af2010-02-08 16:02:56253 DestroyFaviconService();
[email protected]cdba46992011-06-07 11:51:39254
[email protected]cc5bfd42010-11-24 14:44:02255 if (pref_proxy_config_tracker_.get())
256 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]f25387b2008-08-21 15:20:33257}
initial.commit09911bf2008-07-26 23:55:29258
[email protected]d2879af2010-02-08 16:02:56259void TestingProfile::CreateFaviconService() {
[email protected]04732c0e2011-09-16 13:21:53260 favicon_service_.reset(new FaviconService(this));
[email protected]d2879af2010-02-08 16:02:56261}
262
[email protected]f61f4782012-06-08 21:54:21263static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService(
264 Profile* profile) {
265 return new HistoryService(profile);
266}
267
[email protected]d486a0852009-11-02 21:40:00268void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54269 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33270 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32271 FilePath path = GetPath();
272 path = path.Append(chrome::kHistoryFilename);
[email protected]f25387b2008-08-21 15:20:33273 file_util::Delete(path, false);
274 }
[email protected]f61f4782012-06-08 21:54:21275 // This will create and init the history service.
276 HistoryService* history_service = static_cast<HistoryService*>(
277 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
278 this, BuildHistoryService).get());
279 if (!history_service->Init(this->GetPath(),
[email protected]34f2149b2012-06-13 08:23:23280 BookmarkModelFactory::GetForProfile(this),
[email protected]f61f4782012-06-08 21:54:21281 no_db)) {
282 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
283 }
initial.commit09911bf2008-07-26 23:55:29284}
285
286void TestingProfile::DestroyHistoryService() {
[email protected]f61f4782012-06-08 21:54:21287 scoped_refptr<HistoryService> history_service =
[email protected]92a0fb772012-06-25 20:56:46288 HistoryServiceFactory::GetForProfileIfExists(this);
[email protected]f61f4782012-06-08 21:54:21289 if (!history_service.get())
initial.commit09911bf2008-07-26 23:55:29290 return;
291
[email protected]f61f4782012-06-08 21:54:21292 history_service->NotifyRenderProcessHostDestruction(0);
293 history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure());
294 history_service->Cleanup();
295 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29296
297 // Wait for the backend class to terminate before deleting the files and
298 // moving to the next test. Note: if this never terminates, somebody is
299 // probably leaking a reference to the history backend, so it never calls
300 // our destroy task.
301 MessageLoop::current()->Run();
302
initial.commit09911bf2008-07-26 23:55:29303 // Make sure we don't have any event pending that could disrupt the next
304 // test.
[email protected]1cb1a242011-12-10 18:36:51305 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
initial.commit09911bf2008-07-26 23:55:29306 MessageLoop::current()->Run();
307}
[email protected]4d0cd7ce2008-08-11 16:40:57308
[email protected]809cc4d2010-10-27 15:22:54309void TestingProfile::CreateTopSites() {
310 DestroyTopSites();
311 top_sites_ = new history::TopSites(this);
[email protected]73c47932010-12-06 18:13:43312 FilePath file_name = GetPath().Append(chrome::kTopSitesFilename);
[email protected]809cc4d2010-10-27 15:22:54313 top_sites_->Init(file_name);
314}
315
316void TestingProfile::DestroyTopSites() {
317 if (top_sites_.get()) {
318 top_sites_->Shutdown();
319 top_sites_ = NULL;
320 // TopSites::Shutdown schedules some tasks (from TopSitesBackend) that need
321 // to be run to properly shutdown. Run all pending tasks now. This is
322 // normally handled by browser_process shutdown.
323 if (MessageLoop::current())
324 MessageLoop::current()->RunAllPending();
325 }
326}
327
328void TestingProfile::DestroyFaviconService() {
[email protected]04732c0e2011-09-16 13:21:53329 favicon_service_.reset();
[email protected]809cc4d2010-10-27 15:22:54330}
331
[email protected]f61f4782012-06-08 21:54:21332static ProfileKeyedService* BuildBookmarkModel(Profile* profile) {
333 BookmarkModel* bookmark_model = new BookmarkModel(profile);
334 bookmark_model->Load();
335 return bookmark_model;
336}
337
338
[email protected]d8e41ed2008-09-11 15:22:32339void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46340
341 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32342 FilePath path = GetPath();
343 path = path.Append(chrome::kBookmarksFileName);
[email protected]90ef13132008-08-27 03:27:46344 file_util::Delete(path, false);
345 }
[email protected]f61f4782012-06-08 21:54:21346 // This will create a bookmark model.
347 BookmarkModel* bookmark_service =
348 static_cast<BookmarkModel*>(
349 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
350 this, BuildBookmarkModel));
351
352 HistoryService* history_service =
[email protected]92a0fb772012-06-25 20:56:46353 HistoryServiceFactory::GetForProfileIfExists(this).get();
[email protected]f61f4782012-06-08 21:54:21354 if (history_service) {
355 history_service->history_backend_->bookmark_service_ =
356 bookmark_service;
357 history_service->history_backend_->expirer_.bookmark_service_ =
358 bookmark_service;
[email protected]90ef13132008-08-27 03:27:46359 }
[email protected]4d0cd7ce2008-08-11 16:40:57360}
[email protected]d2c017a2008-08-13 21:51:45361
[email protected]a6d36cc2011-02-23 00:39:48362void TestingProfile::CreateProtocolHandlerRegistry() {
[email protected]9b4d241c92012-06-29 19:48:31363 protocol_handler_registry_ = new ProtocolHandlerRegistry(this,
[email protected]fd8ca522011-05-02 18:54:49364 new ProtocolHandlerRegistry::Delegate());
[email protected]a6d36cc2011-02-23 00:39:48365}
366
[email protected]d07edd42012-05-14 23:49:46367static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService(
368 Profile* profile) {
369 WebDataService* web_data_service = new WebDataService();
370 if (web_data_service)
371 web_data_service->Init(profile->GetPath());
[email protected]a5c65292012-05-25 00:50:01372 return web_data_service;
[email protected]d07edd42012-05-14 23:49:46373}
[email protected]2609bc12010-01-24 08:32:55374
[email protected]d07edd42012-05-14 23:49:46375void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01376 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46377 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55378}
379
[email protected]d364c652008-08-29 19:46:56380void TestingProfile::BlockUntilBookmarkModelLoaded() {
[email protected]f61f4782012-06-08 21:54:21381 DCHECK(GetBookmarkModel());
382 if (GetBookmarkModel()->IsLoaded())
[email protected]d364c652008-08-29 19:46:56383 return;
[email protected]8e937c1e2012-06-28 22:57:30384 base::RunLoop run_loop;
385 BookmarkLoadObserver observer(
386 ui_test_utils::GetQuitTaskForRunLoop(&run_loop));
[email protected]f61f4782012-06-08 21:54:21387 GetBookmarkModel()->AddObserver(&observer);
[email protected]8e937c1e2012-06-28 22:57:30388 run_loop.Run();
[email protected]f61f4782012-06-08 21:54:21389 GetBookmarkModel()->RemoveObserver(&observer);
390 DCHECK(GetBookmarkModel()->IsLoaded());
[email protected]d364c652008-08-29 19:46:56391}
392
[email protected]120abf132011-09-27 21:38:06393// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54394void TestingProfile::BlockUntilTopSitesLoaded() {
[email protected]120abf132011-09-27 21:38:06395 ui_test_utils::WindowedNotificationObserver top_sites_loaded_observer(
396 chrome::NOTIFICATION_TOP_SITES_LOADED,
[email protected]ad50def52011-10-19 23:17:07397 content::NotificationService::AllSources());
[email protected]809cc4d2010-10-27 15:22:54398 if (!GetHistoryService(Profile::EXPLICIT_ACCESS))
399 GetTopSites()->HistoryLoaded();
[email protected]120abf132011-09-27 21:38:06400 top_sites_loaded_observer.Wait();
[email protected]809cc4d2010-10-27 15:22:54401}
402
[email protected]f9dec9482010-08-20 20:42:19403FilePath TestingProfile::GetPath() {
[email protected]49a25632011-08-31 17:03:48404 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19405}
406
[email protected]dbbad7a2010-08-13 18:18:36407TestingPrefService* TestingProfile::GetTestingPrefService() {
[email protected]2fb7dc982010-09-29 12:24:28408 if (!prefs_.get())
409 CreateTestingPrefService();
410 DCHECK(testing_prefs_);
411 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36412}
413
[email protected]8e5c89a2011-06-07 18:13:33414TestingProfile* TestingProfile::AsTestingProfile() {
415 return this;
416}
417
[email protected]29d70252011-04-28 02:16:58418std::string TestingProfile::GetProfileName() {
419 return std::string("testing_profile");
420}
421
[email protected]27d6e852012-03-02 21:31:32422bool TestingProfile::IsOffTheRecord() const {
[email protected]5bb2f522011-03-25 19:04:44423 return incognito_;
[email protected]fadc607b62011-02-07 17:55:50424}
425
[email protected]93dba942011-02-23 22:45:01426void TestingProfile::SetOffTheRecordProfile(Profile* profile) {
[email protected]5bb2f522011-03-25 19:04:44427 incognito_profile_.reset(profile);
[email protected]93dba942011-02-23 22:45:01428}
429
[email protected]fadc607b62011-02-07 17:55:50430Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44431 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50432}
433
[email protected]d4f5d1162011-11-30 01:41:52434GAIAInfoUpdateService* TestingProfile::GetGAIAInfoUpdateService() {
435 return NULL;
436}
437
[email protected]fadc607b62011-02-07 17:55:50438bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44439 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50440}
441
442Profile* TestingProfile::GetOriginalProfile() {
443 return this;
444}
445
[email protected]fadc607b62011-02-07 17:55:50446VisitedLinkMaster* TestingProfile::GetVisitedLinkMaster() {
447 return NULL;
448}
449
[email protected]eaa7dd182010-12-14 11:09:00450ExtensionService* TestingProfile::GetExtensionService() {
[email protected]bd306722012-07-11 20:43:59451 return extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]2fb7dc982010-09-29 12:24:28452}
453
[email protected]fadc607b62011-02-07 17:55:50454UserScriptMaster* TestingProfile::GetUserScriptMaster() {
[email protected]bd306722012-07-11 20:43:59455 return extensions::ExtensionSystem::Get(this)->user_script_master();
[email protected]fadc607b62011-02-07 17:55:50456}
457
458ExtensionProcessManager* TestingProfile::GetExtensionProcessManager() {
[email protected]bd306722012-07-11 20:43:59459 return extensions::ExtensionSystem::Get(this)->process_manager();
[email protected]fadc607b62011-02-07 17:55:50460}
461
462ExtensionEventRouter* TestingProfile::GetExtensionEventRouter() {
[email protected]bd306722012-07-11 20:43:59463 return extensions::ExtensionSystem::Get(this)->event_router();
[email protected]fadc607b62011-02-07 17:55:50464}
465
[email protected]2261e7b72011-07-12 13:43:55466void TestingProfile::SetExtensionSpecialStoragePolicy(
467 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
468 extension_special_storage_policy_ = extension_special_storage_policy;
469}
470
[email protected]19eb80152011-02-26 00:28:43471ExtensionSpecialStoragePolicy*
472TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]cdba46992011-06-07 11:51:39473 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05474 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39475 return extension_special_storage_policy_.get();
[email protected]19eb80152011-02-26 00:28:43476}
477
[email protected]fadc607b62011-02-07 17:55:50478FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) {
479 return favicon_service_.get();
480}
481
482HistoryService* TestingProfile::GetHistoryService(ServiceAccessType access) {
[email protected]92a0fb772012-06-25 20:56:46483 return HistoryServiceFactory::GetForProfileIfExists(this);
[email protected]fadc607b62011-02-07 17:55:50484}
485
486HistoryService* TestingProfile::GetHistoryServiceWithoutCreating() {
[email protected]92a0fb772012-06-25 20:56:46487 return HistoryServiceFactory::GetForProfileIfExists(this);
[email protected]fadc607b62011-02-07 17:55:50488}
489
[email protected]dbbad7a2010-08-13 18:18:36490net::CookieMonster* TestingProfile::GetCookieMonster() {
491 if (!GetRequestContext())
492 return NULL;
[email protected]277ec262011-03-30 21:09:40493 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
494 GetCookieMonster();
[email protected]dbbad7a2010-08-13 18:18:36495}
496
[email protected]21d3a882012-05-31 14:41:55497policy::PolicyService* TestingProfile::GetPolicyService() {
498 if (!policy_service_.get()) {
499#if defined(ENABLE_CONFIGURATION_POLICY)
500 policy::PolicyServiceImpl::Providers providers;
501 policy_service_.reset(new policy::PolicyServiceImpl(providers));
502#else
503 policy_service_.reset(new policy::PolicyServiceStub());
504#endif
505 }
506 return policy_service_.get();
507}
508
[email protected]2fb7dc982010-09-29 12:24:28509void TestingProfile::SetPrefService(PrefService* prefs) {
[email protected]ddcaa412012-03-30 19:57:29510#if defined(ENABLE_PROTECTOR_SERVICE)
[email protected]ee78d8a2012-03-15 16:53:48511 // ProtectorService binds itself very closely to the PrefService at the moment
512 // of Profile creation and watches pref changes to update their backup.
513 // For tests that replace the PrefService after TestingProfile creation,
514 // ProtectorService is disabled to prevent further invalid memory accesses.
515 protector::ProtectorServiceFactory::GetInstance()->
516 SetTestingFactory(this, NULL);
[email protected]ddcaa412012-03-30 19:57:29517#endif
[email protected]2fb7dc982010-09-29 12:24:28518 prefs_.reset(prefs);
519}
520
521void TestingProfile::CreateTestingPrefService() {
522 DCHECK(!prefs_.get());
523 testing_prefs_ = new TestingPrefService();
524 prefs_.reset(testing_prefs_);
525 Profile::RegisterUserPrefs(prefs_.get());
[email protected]572f40c9b2012-07-06 20:35:33526 chrome::RegisterUserPrefs(prefs_.get());
[email protected]2fb7dc982010-09-29 12:24:28527}
528
[email protected]dbbad7a2010-08-13 18:18:36529PrefService* TestingProfile::GetPrefs() {
530 if (!prefs_.get()) {
[email protected]2fb7dc982010-09-29 12:24:28531 CreateTestingPrefService();
[email protected]dbbad7a2010-08-13 18:18:36532 }
533 return prefs_.get();
534}
535
536history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54537 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36538}
539
[email protected]fadc607b62011-02-07 17:55:50540history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
541 return top_sites_.get();
542}
543
[email protected]b441a8492012-06-06 14:55:57544DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50545 return NULL;
546}
547
[email protected]abe2c032011-03-31 18:49:34548net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]812b3a32010-01-08 05:36:04549 return request_context_.get();
550}
551
[email protected]da5683db2011-04-23 17:12:21552net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
553 int renderer_child_id) {
[email protected]31d8f5f22012-04-02 15:22:08554 ExtensionService* extension_service =
[email protected]bd306722012-07-11 20:43:59555 extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]31d8f5f22012-04-02 15:22:08556 if (extension_service) {
[email protected]1c321ee2012-05-21 03:02:34557 const extensions::Extension* installed_app = extension_service->
[email protected]da5683db2011-04-23 17:12:21558 GetInstalledAppForRenderer(renderer_child_id);
559 if (installed_app != NULL && installed_app->is_storage_isolated())
560 return GetRequestContextForIsolatedApp(installed_app->id());
561 }
[email protected]d9696672011-03-15 22:45:09562
563 return GetRequestContext();
564}
565
[email protected]812b3a32010-01-08 05:36:04566void TestingProfile::CreateRequestContext() {
567 if (!request_context_)
[email protected]44738602012-03-02 22:56:01568 request_context_ =
569 new TestURLRequestContextGetter(
570 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]812b3a32010-01-08 05:36:04571}
572
[email protected]7c893202010-10-07 20:18:02573void TestingProfile::ResetRequestContext() {
[email protected]3931fd82012-03-12 21:18:48574 // Any objects holding live URLFetchers should be deleted before the request
575 // context is shut down.
[email protected]16bd088b2012-04-03 08:12:12576 TemplateURLFetcherFactory::ShutdownForProfile(this);
[email protected]3931fd82012-03-12 21:18:48577
[email protected]7c893202010-10-07 20:18:02578 request_context_ = NULL;
579}
580
[email protected]abe2c032011-03-31 18:49:34581net::URLRequestContextGetter* TestingProfile::GetRequestContextForMedia() {
[email protected]fadc607b62011-02-07 17:55:50582 return NULL;
583}
584
[email protected]abe2c032011-03-31 18:49:34585net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
[email protected]c10da4b02010-03-25 14:38:32586 if (!extensions_request_context_)
[email protected]00dea9582012-05-09 15:20:05587 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
[email protected]c10da4b02010-03-25 14:38:32588 return extensions_request_context_.get();
589}
590
[email protected]fadc607b62011-02-07 17:55:50591net::SSLConfigService* TestingProfile::GetSSLConfigService() {
592 return NULL;
593}
594
[email protected]abe2c032011-03-31 18:49:34595net::URLRequestContextGetter* TestingProfile::GetRequestContextForIsolatedApp(
[email protected]d9696672011-03-15 22:45:09596 const std::string& app_id) {
597 // We don't test isolated app storage here yet, so returning the same dummy
598 // context is sufficient for now.
599 return GetRequestContext();
600}
601
[email protected]df02aca2012-02-09 21:03:20602content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]314c3e22012-02-21 03:57:42603 if (!resource_context_.get())
604 resource_context_.reset(new content::MockResourceContext());
605 return resource_context_.get();
[email protected]e89b77d2011-04-15 18:58:10606}
607
[email protected]dbbad7a2010-08-13 18:18:36608HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38609 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05610 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
611 ExtensionService* extension_service = GetExtensionService();
612 if (extension_service)
613 host_content_settings_map_->RegisterExtensionService(extension_service);
[email protected]35552dc52011-07-12 09:04:38614 }
[email protected]dbbad7a2010-08-13 18:18:36615 return host_content_settings_map_.get();
616}
617
[email protected]810ddc52012-01-24 01:00:35618content::GeolocationPermissionContext*
[email protected]dbbad7a2010-08-13 18:18:36619TestingProfile::GetGeolocationPermissionContext() {
620 if (!geolocation_permission_context_.get()) {
621 geolocation_permission_context_ =
[email protected]c476e632011-06-23 11:18:04622 new ChromeGeolocationPermissionContext(this);
[email protected]dbbad7a2010-08-13 18:18:36623 }
624 return geolocation_permission_context_.get();
625}
626
[email protected]c52b2892012-03-07 11:01:02627content::SpeechRecognitionPreferences*
628 TestingProfile::GetSpeechRecognitionPreferences() {
[email protected]3297132a2012-02-21 23:27:59629#if defined(ENABLE_INPUT_SPEECH)
[email protected]750e9572012-06-06 15:21:24630 return ChromeSpeechRecognitionPreferences::GetForProfile(this);
[email protected]3297132a2012-02-21 23:27:59631#else
632 return NULL;
633#endif
[email protected]8238dd62011-09-29 15:13:01634}
635
[email protected]fadc607b62011-02-07 17:55:50636std::wstring TestingProfile::GetName() {
637 return std::wstring();
638}
639
640std::wstring TestingProfile::GetID() {
641 return id_;
642}
643
644void TestingProfile::SetID(const std::wstring& id) {
645 id_ = id;
646}
647
648bool TestingProfile::DidLastSessionExitCleanly() {
649 return last_session_exited_cleanly_;
650}
651
652BookmarkModel* TestingProfile::GetBookmarkModel() {
[email protected]f61f4782012-06-08 21:54:21653 return BookmarkModelFactory::GetForProfileIfExists(this);
[email protected]fadc607b62011-02-07 17:55:50654}
655
656bool TestingProfile::IsSameProfile(Profile *p) {
657 return this == p;
658}
659
660base::Time TestingProfile::GetStartTime() const {
661 return start_time_;
662}
663
[email protected]a6d36cc2011-02-23 00:39:48664ProtocolHandlerRegistry* TestingProfile::GetProtocolHandlerRegistry() {
665 return protocol_handler_registry_.get();
666}
667
[email protected]fadc607b62011-02-07 17:55:50668FilePath TestingProfile::last_selected_directory() {
669 return last_selected_directory_;
670}
671
672void TestingProfile::set_last_selected_directory(const FilePath& path) {
673 last_selected_directory_ = path;
674}
675
[email protected]cc5bfd42010-11-24 14:44:02676PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07677 if (!pref_proxy_config_tracker_.get()) {
678 pref_proxy_config_tracker_.reset(
679 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs()));
680 }
681 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02682}
683
[email protected]0bfc29a2009-04-27 16:15:44684void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]f61f4782012-06-08 21:54:21685 scoped_refptr<HistoryService> history_service =
[email protected]92a0fb772012-06-25 20:56:46686 HistoryServiceFactory::GetForProfileIfExists(this);
[email protected]f61f4782012-06-08 21:54:21687 DCHECK(history_service.get());
[email protected]0bfc29a2009-04-27 16:15:44688 DCHECK(MessageLoop::current());
689
690 CancelableRequestConsumer consumer;
[email protected]f61f4782012-06-08 21:54:21691 history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer);
[email protected]0bfc29a2009-04-27 16:15:44692 MessageLoop::current()->Run();
693}
[email protected]345a8b72009-09-29 09:11:44694
[email protected]8382d8c2011-09-15 03:43:17695chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
696 return NULL;
697}
698
[email protected]db96a882011-10-09 02:01:54699void TestingProfile::ClearNetworkingHistorySince(base::Time time) {
[email protected]02896a82011-09-21 18:54:32700 NOTIMPLEMENTED();
701}
702
[email protected]671f9c62011-10-28 19:22:07703GURL TestingProfile::GetHomePage() {
704 return GURL(chrome::kChromeUINewTabURL);
705}
706
[email protected]fadc607b62011-02-07 17:55:50707PrefService* TestingProfile::GetOffTheRecordPrefs() {
708 return NULL;
709}
710
[email protected]36fb2c7c2011-04-04 15:49:08711quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
712 return GetExtensionSpecialStoragePolicy();
713}
714
[email protected]1bee6ed2012-03-14 06:46:36715bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
716 return true;
717}
[email protected]ef92e172012-04-25 19:40:41718
719base::Callback<ChromeURLDataManagerBackend*(void)>
720 TestingProfile::GetChromeURLDataManagerBackendGetter() const {
721 return base::Callback<ChromeURLDataManagerBackend*(void)>();
722}