blob: e7f5c44913110afd2c884b1942ea4c25bfd95d72 [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]5c4c89f2012-08-07 21:09:5937#include "chrome/browser/policy/user_cloud_policy_manager.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]fb29e6cf2012-07-12 21:27:2056#include "content/public/browser/render_process_host.h"
[email protected]08a932d52012-06-03 21:42:1257#include "content/public/test/mock_resource_context.h"
[email protected]a7fe9112012-07-20 02:34:4558#include "content/public/test/test_utils.h"
[email protected]aa84a7e2012-03-15 21:29:0659#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0460#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3461#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4462#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4663#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3464
[email protected]21d3a882012-05-31 14:41:5565#if defined(ENABLE_CONFIGURATION_POLICY)
66#include "chrome/browser/policy/policy_service_impl.h"
67#else
68#include "chrome/browser/policy/policy_service_stub.h"
69#endif // defined(ENABLE_CONFIGURATION_POLICY)
70
[email protected]6f96cbcb2011-11-04 02:26:0771#if defined(OS_CHROMEOS)
72#include "chrome/browser/chromeos/proxy_config_service_impl.h"
73#endif // defined(OS_CHROMEOS)
74
[email protected]e1acf6f2008-10-27 20:43:3375using base::Time;
[email protected]631bb742011-11-02 11:29:3976using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:5777using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:2378using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:4679using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:3380
[email protected]d364c652008-08-29 19:46:5681namespace {
82
[email protected]0bfc29a2009-04-27 16:15:4483// Task used to make sure history has finished processing a request. Intended
84// for use with BlockUntilHistoryProcessesPendingRequests.
85
86class QuittingHistoryDBTask : public HistoryDBTask {
87 public:
88 QuittingHistoryDBTask() {}
89
90 virtual bool RunOnDBThread(history::HistoryBackend* backend,
91 history::HistoryDatabase* db) {
92 return true;
93 }
94
95 virtual void DoneRunOnMainThread() {
96 MessageLoop::current()->Quit();
97 }
98
99 private:
[email protected]7991a232009-11-06 01:55:48100 ~QuittingHistoryDBTask() {}
101
[email protected]0bfc29a2009-04-27 16:15:44102 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
103};
104
[email protected]aeb53f02011-01-15 00:21:34105class TestExtensionURLRequestContext : public net::URLRequestContext {
[email protected]c10da4b02010-03-25 14:38:32106 public:
107 TestExtensionURLRequestContext() {
108 net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL);
109 const char* schemes[] = {chrome::kExtensionScheme};
110 cookie_monster->SetCookieableSchemes(schemes, 1);
[email protected]f6c21cb2011-02-16 19:45:41111 set_cookie_store(cookie_monster);
[email protected]c10da4b02010-03-25 14:38:32112 }
[email protected]7b2720b2012-04-25 16:59:11113
[email protected]7b2720b2012-04-25 16:59:11114 virtual ~TestExtensionURLRequestContext() {}
[email protected]c10da4b02010-03-25 14:38:32115};
116
[email protected]abe2c032011-03-31 18:49:34117class TestExtensionURLRequestContextGetter
118 : public net::URLRequestContextGetter {
[email protected]c10da4b02010-03-25 14:38:32119 public:
[email protected]aeb53f02011-01-15 00:21:34120 virtual net::URLRequestContext* GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09121 if (!context_.get())
122 context_.reset(new TestExtensionURLRequestContext());
[email protected]c10da4b02010-03-25 14:38:32123 return context_.get();
124 }
[email protected]4969b0122012-06-16 01:58:28125 virtual scoped_refptr<base::SingleThreadTaskRunner>
126 GetNetworkTaskRunner() const OVERRIDE {
[email protected]0c7d74f2010-10-11 11:55:26127 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
[email protected]83a7d2eb2010-05-03 21:46:19128 }
[email protected]c10da4b02010-03-25 14:38:32129
[email protected]13ed17f82012-04-06 02:27:18130 protected:
131 virtual ~TestExtensionURLRequestContextGetter() {}
132
[email protected]c10da4b02010-03-25 14:38:32133 private:
[email protected]ef2bf422012-05-11 03:27:09134 scoped_ptr<net::URLRequestContext> context_;
[email protected]c10da4b02010-03-25 14:38:32135};
136
[email protected]abc77242012-03-16 04:32:31137ProfileKeyedService* CreateTestDesktopNotificationService(Profile* profile) {
[email protected]29cebbd2012-06-06 21:43:42138#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53139 return new DesktopNotificationService(profile, NULL);
[email protected]29cebbd2012-06-06 21:43:42140#else
141 return NULL;
142#endif
[email protected]dcb72d52011-04-13 12:36:53143}
144
[email protected]d364c652008-08-29 19:46:56145} // namespace
146
[email protected]89dbb1772012-07-17 13:47:25147// static
148#if defined(OS_CHROMEOS)
149// Must be kept in sync with
150// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
151const char TestingProfile::kTestUserProfileDir[] = "test-user";
152#else
153const char TestingProfile::kTestUserProfileDir[] = "Default";
154#endif
155
[email protected]f25387b2008-08-21 15:20:33156TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55157 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28158 testing_prefs_(NULL),
[email protected]5bb2f522011-03-25 19:04:44159 incognito_(false),
[email protected]5cdcd152011-03-31 22:48:52160 last_session_exited_cleanly_(true),
[email protected]537c1082011-12-02 02:37:17161 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
162 delegate_(NULL) {
[email protected]5c4c89f2012-08-07 21:09:59163 CreateTempProfileDir();
[email protected]49a25632011-08-31 17:03:48164 profile_path_ = temp_dir_.path();
165
166 Init();
[email protected]537c1082011-12-02 02:37:17167 FinishInit();
[email protected]49a25632011-08-31 17:03:48168}
169
170TestingProfile::TestingProfile(const FilePath& path)
171 : start_time_(Time::Now()),
172 testing_prefs_(NULL),
173 incognito_(false),
174 last_session_exited_cleanly_(true),
175 profile_path_(path),
[email protected]537c1082011-12-02 02:37:17176 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
177 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48178 Init();
[email protected]537c1082011-12-02 02:37:17179 FinishInit();
180}
181
182TestingProfile::TestingProfile(const FilePath& path,
183 Delegate* delegate)
184 : start_time_(Time::Now()),
185 testing_prefs_(NULL),
186 incognito_(false),
187 last_session_exited_cleanly_(true),
188 profile_path_(path),
189 profile_dependency_manager_(ProfileDependencyManager::GetInstance()),
190 delegate_(delegate) {
191 Init();
192 if (delegate_) {
193 MessageLoop::current()->PostTask(FROM_HERE,
194 base::Bind(&TestingProfile::FinishInit,
195 base::Unretained(this)));
196 } else {
197 FinishInit();
198 }
[email protected]49a25632011-08-31 17:03:48199}
200
[email protected]5c4c89f2012-08-07 21:09:59201TestingProfile::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
238void 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
[email protected]49a25632011-08-31 17:03:48264void TestingProfile::Init() {
[email protected]d41e6572012-05-04 20:49:42265 if (!file_util::PathExists(profile_path_))
266 file_util::CreateDirectory(profile_path_);
267
[email protected]bd306722012-07-11 20:43:59268 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
269 this, extensions::TestExtensionSystem::Build);
[email protected]31d8f5f22012-04-02 15:22:08270
[email protected]49a25632011-08-31 17:03:48271 profile_dependency_manager_->CreateProfileServices(this, true);
272
[email protected]5fd2e842012-03-01 00:29:11273#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53274 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22275 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
276 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11277#endif
[email protected]537c1082011-12-02 02:37:17278}
[email protected]12463cd2011-07-19 09:40:20279
[email protected]537c1082011-12-02 02:37:17280void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40281 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07282 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20283 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53284 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07285 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17286
287 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03288 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46289}
290
[email protected]f25387b2008-08-21 15:20:33291TestingProfile::~TestingProfile() {
[email protected]a7c4c032012-07-27 00:36:32292 MaybeSendDestroyedNotification();
[email protected]5cdcd152011-03-31 22:48:52293
294 profile_dependency_manager_->DestroyProfileServices(this);
295
[email protected]35552dc52011-07-12 09:04:38296 if (host_content_settings_map_)
297 host_content_settings_map_->ShutdownOnUIThread();
298
[email protected]809cc4d2010-10-27 15:22:54299 DestroyTopSites();
[email protected]1facac22012-08-23 07:20:58300 DestroyFaviconService();
[email protected]cdba46992011-06-07 11:51:39301
[email protected]cc5bfd42010-11-24 14:44:02302 if (pref_proxy_config_tracker_.get())
303 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]f25387b2008-08-21 15:20:33304}
initial.commit09911bf2008-07-26 23:55:29305
[email protected]d2879af2010-02-08 16:02:56306void TestingProfile::CreateFaviconService() {
[email protected]1facac22012-08-23 07:20:58307 favicon_service_.reset(new FaviconService(this));
[email protected]d2879af2010-02-08 16:02:56308}
309
[email protected]f61f4782012-06-08 21:54:21310static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService(
311 Profile* profile) {
312 return new HistoryService(profile);
313}
314
[email protected]d486a0852009-11-02 21:40:00315void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54316 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33317 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32318 FilePath path = GetPath();
319 path = path.Append(chrome::kHistoryFilename);
[email protected]f25387b2008-08-21 15:20:33320 file_util::Delete(path, false);
321 }
[email protected]7019bb6d2012-08-23 09:09:48322 // This will create and init the history service.
[email protected]f61f4782012-06-08 21:54:21323 HistoryService* history_service = static_cast<HistoryService*>(
324 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
325 this, BuildHistoryService).get());
[email protected]7019bb6d2012-08-23 09:09:48326 if (!history_service->Init(this->GetPath(),
327 BookmarkModelFactory::GetForProfile(this),
328 no_db)) {
[email protected]f61f4782012-06-08 21:54:21329 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
[email protected]7019bb6d2012-08-23 09:09:48330 }
initial.commit09911bf2008-07-26 23:55:29331}
332
333void TestingProfile::DestroyHistoryService() {
[email protected]f61f4782012-06-08 21:54:21334 scoped_refptr<HistoryService> history_service =
[email protected]6c537042012-07-13 00:59:02335 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]f61f4782012-06-08 21:54:21336 if (!history_service.get())
initial.commit09911bf2008-07-26 23:55:29337 return;
338
[email protected]f61f4782012-06-08 21:54:21339 history_service->NotifyRenderProcessHostDestruction(0);
340 history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure());
341 history_service->Cleanup();
342 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29343
344 // Wait for the backend class to terminate before deleting the files and
345 // moving to the next test. Note: if this never terminates, somebody is
346 // probably leaking a reference to the history backend, so it never calls
347 // our destroy task.
348 MessageLoop::current()->Run();
349
initial.commit09911bf2008-07-26 23:55:29350 // Make sure we don't have any event pending that could disrupt the next
351 // test.
[email protected]1cb1a242011-12-10 18:36:51352 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
initial.commit09911bf2008-07-26 23:55:29353 MessageLoop::current()->Run();
354}
[email protected]4d0cd7ce2008-08-11 16:40:57355
[email protected]809cc4d2010-10-27 15:22:54356void TestingProfile::CreateTopSites() {
357 DestroyTopSites();
358 top_sites_ = new history::TopSites(this);
[email protected]73c47932010-12-06 18:13:43359 FilePath file_name = GetPath().Append(chrome::kTopSitesFilename);
[email protected]809cc4d2010-10-27 15:22:54360 top_sites_->Init(file_name);
361}
362
363void TestingProfile::DestroyTopSites() {
364 if (top_sites_.get()) {
365 top_sites_->Shutdown();
366 top_sites_ = NULL;
367 // TopSites::Shutdown schedules some tasks (from TopSitesBackend) that need
368 // to be run to properly shutdown. Run all pending tasks now. This is
369 // normally handled by browser_process shutdown.
370 if (MessageLoop::current())
371 MessageLoop::current()->RunAllPending();
372 }
373}
374
[email protected]1facac22012-08-23 07:20:58375void TestingProfile::DestroyFaviconService() {
376 favicon_service_.reset();
377}
378
[email protected]f61f4782012-06-08 21:54:21379static ProfileKeyedService* BuildBookmarkModel(Profile* profile) {
380 BookmarkModel* bookmark_model = new BookmarkModel(profile);
381 bookmark_model->Load();
382 return bookmark_model;
383}
384
385
[email protected]d8e41ed2008-09-11 15:22:32386void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46387
388 if (delete_file) {
[email protected]f7011fcb2009-01-28 21:54:32389 FilePath path = GetPath();
390 path = path.Append(chrome::kBookmarksFileName);
[email protected]90ef13132008-08-27 03:27:46391 file_util::Delete(path, false);
392 }
[email protected]f61f4782012-06-08 21:54:21393 // This will create a bookmark model.
394 BookmarkModel* bookmark_service =
395 static_cast<BookmarkModel*>(
396 BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
397 this, BuildBookmarkModel));
398
399 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02400 HistoryServiceFactory::GetForProfileWithoutCreating(this).get();
[email protected]f61f4782012-06-08 21:54:21401 if (history_service) {
402 history_service->history_backend_->bookmark_service_ =
403 bookmark_service;
404 history_service->history_backend_->expirer_.bookmark_service_ =
405 bookmark_service;
[email protected]90ef13132008-08-27 03:27:46406 }
[email protected]4d0cd7ce2008-08-11 16:40:57407}
[email protected]d2c017a2008-08-13 21:51:45408
[email protected]d07edd42012-05-14 23:49:46409static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService(
410 Profile* profile) {
411 WebDataService* web_data_service = new WebDataService();
412 if (web_data_service)
413 web_data_service->Init(profile->GetPath());
[email protected]a5c65292012-05-25 00:50:01414 return web_data_service;
[email protected]d07edd42012-05-14 23:49:46415}
[email protected]2609bc12010-01-24 08:32:55416
[email protected]d07edd42012-05-14 23:49:46417void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01418 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46419 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55420}
421
[email protected]d364c652008-08-29 19:46:56422void TestingProfile::BlockUntilBookmarkModelLoaded() {
[email protected]d7f7f752012-08-08 02:02:55423 // Only get the bookmark model if it actually exists since the caller of the
424 // test should explicitly call CreateBookmarkModel to build it.
425 BookmarkModel* bookmark_model =
426 BookmarkModelFactory::GetForProfileIfExists(this);
427 DCHECK(bookmark_model);
428 if (bookmark_model->IsLoaded())
[email protected]d364c652008-08-29 19:46:56429 return;
[email protected]8e937c1e2012-06-28 22:57:30430 base::RunLoop run_loop;
[email protected]78089f02012-07-19 06:11:28431 BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
[email protected]d7f7f752012-08-08 02:02:55432 bookmark_model->AddObserver(&observer);
[email protected]8e937c1e2012-06-28 22:57:30433 run_loop.Run();
[email protected]d7f7f752012-08-08 02:02:55434 bookmark_model->RemoveObserver(&observer);
435 DCHECK(bookmark_model->IsLoaded());
[email protected]d364c652008-08-29 19:46:56436}
437
[email protected]120abf132011-09-27 21:38:06438// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54439void TestingProfile::BlockUntilTopSitesLoaded() {
[email protected]a7fe9112012-07-20 02:34:45440 content::WindowedNotificationObserver top_sites_loaded_observer(
[email protected]120abf132011-09-27 21:38:06441 chrome::NOTIFICATION_TOP_SITES_LOADED,
[email protected]ad50def52011-10-19 23:17:07442 content::NotificationService::AllSources());
[email protected]d7f7f752012-08-08 02:02:55443 if (!HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS))
[email protected]809cc4d2010-10-27 15:22:54444 GetTopSites()->HistoryLoaded();
[email protected]120abf132011-09-27 21:38:06445 top_sites_loaded_observer.Wait();
[email protected]809cc4d2010-10-27 15:22:54446}
447
[email protected]f9dec9482010-08-20 20:42:19448FilePath TestingProfile::GetPath() {
[email protected]49a25632011-08-31 17:03:48449 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19450}
451
[email protected]dbbad7a2010-08-13 18:18:36452TestingPrefService* TestingProfile::GetTestingPrefService() {
[email protected]2fb7dc982010-09-29 12:24:28453 if (!prefs_.get())
454 CreateTestingPrefService();
455 DCHECK(testing_prefs_);
456 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36457}
458
[email protected]8e5c89a2011-06-07 18:13:33459TestingProfile* TestingProfile::AsTestingProfile() {
460 return this;
461}
462
[email protected]29d70252011-04-28 02:16:58463std::string TestingProfile::GetProfileName() {
464 return std::string("testing_profile");
465}
466
[email protected]27d6e852012-03-02 21:31:32467bool TestingProfile::IsOffTheRecord() const {
[email protected]5bb2f522011-03-25 19:04:44468 return incognito_;
[email protected]fadc607b62011-02-07 17:55:50469}
470
[email protected]93dba942011-02-23 22:45:01471void TestingProfile::SetOffTheRecordProfile(Profile* profile) {
[email protected]5bb2f522011-03-25 19:04:44472 incognito_profile_.reset(profile);
[email protected]93dba942011-02-23 22:45:01473}
474
[email protected]fadc607b62011-02-07 17:55:50475Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44476 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50477}
478
[email protected]d4f5d1162011-11-30 01:41:52479GAIAInfoUpdateService* TestingProfile::GetGAIAInfoUpdateService() {
480 return NULL;
481}
482
[email protected]fadc607b62011-02-07 17:55:50483bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44484 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50485}
486
487Profile* TestingProfile::GetOriginalProfile() {
488 return this;
489}
490
[email protected]fadc607b62011-02-07 17:55:50491VisitedLinkMaster* TestingProfile::GetVisitedLinkMaster() {
492 return NULL;
493}
494
[email protected]eaa7dd182010-12-14 11:09:00495ExtensionService* TestingProfile::GetExtensionService() {
[email protected]bd306722012-07-11 20:43:59496 return extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]2fb7dc982010-09-29 12:24:28497}
498
[email protected]20f97c92012-07-13 23:12:37499extensions::UserScriptMaster* TestingProfile::GetUserScriptMaster() {
[email protected]bd306722012-07-11 20:43:59500 return extensions::ExtensionSystem::Get(this)->user_script_master();
[email protected]fadc607b62011-02-07 17:55:50501}
502
503ExtensionProcessManager* TestingProfile::GetExtensionProcessManager() {
[email protected]bd306722012-07-11 20:43:59504 return extensions::ExtensionSystem::Get(this)->process_manager();
[email protected]fadc607b62011-02-07 17:55:50505}
506
[email protected]5a38dfd2012-07-23 23:22:10507extensions::EventRouter* TestingProfile::GetExtensionEventRouter() {
[email protected]bd306722012-07-11 20:43:59508 return extensions::ExtensionSystem::Get(this)->event_router();
[email protected]fadc607b62011-02-07 17:55:50509}
510
[email protected]2261e7b72011-07-12 13:43:55511void TestingProfile::SetExtensionSpecialStoragePolicy(
512 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
513 extension_special_storage_policy_ = extension_special_storage_policy;
514}
515
[email protected]19eb80152011-02-26 00:28:43516ExtensionSpecialStoragePolicy*
517TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]cdba46992011-06-07 11:51:39518 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05519 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39520 return extension_special_storage_policy_.get();
[email protected]19eb80152011-02-26 00:28:43521}
522
[email protected]1facac22012-08-23 07:20:58523FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) {
524 return favicon_service_.get();
525}
526
[email protected]dbbad7a2010-08-13 18:18:36527net::CookieMonster* TestingProfile::GetCookieMonster() {
528 if (!GetRequestContext())
529 return NULL;
[email protected]277ec262011-03-30 21:09:40530 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
531 GetCookieMonster();
[email protected]dbbad7a2010-08-13 18:18:36532}
533
[email protected]5c4c89f2012-08-07 21:09:59534policy::UserCloudPolicyManager* TestingProfile::GetUserCloudPolicyManager() {
535 return user_cloud_policy_manager_.get();
536}
537
[email protected]21d3a882012-05-31 14:41:55538policy::PolicyService* TestingProfile::GetPolicyService() {
539 if (!policy_service_.get()) {
540#if defined(ENABLE_CONFIGURATION_POLICY)
541 policy::PolicyServiceImpl::Providers providers;
542 policy_service_.reset(new policy::PolicyServiceImpl(providers));
543#else
544 policy_service_.reset(new policy::PolicyServiceStub());
545#endif
546 }
547 return policy_service_.get();
548}
549
[email protected]2fb7dc982010-09-29 12:24:28550void TestingProfile::SetPrefService(PrefService* prefs) {
[email protected]ddcaa412012-03-30 19:57:29551#if defined(ENABLE_PROTECTOR_SERVICE)
[email protected]ee78d8a2012-03-15 16:53:48552 // ProtectorService binds itself very closely to the PrefService at the moment
553 // of Profile creation and watches pref changes to update their backup.
554 // For tests that replace the PrefService after TestingProfile creation,
555 // ProtectorService is disabled to prevent further invalid memory accesses.
556 protector::ProtectorServiceFactory::GetInstance()->
557 SetTestingFactory(this, NULL);
[email protected]ddcaa412012-03-30 19:57:29558#endif
[email protected]2fb7dc982010-09-29 12:24:28559 prefs_.reset(prefs);
560}
561
562void TestingProfile::CreateTestingPrefService() {
563 DCHECK(!prefs_.get());
564 testing_prefs_ = new TestingPrefService();
565 prefs_.reset(testing_prefs_);
566 Profile::RegisterUserPrefs(prefs_.get());
[email protected]572f40c9b2012-07-06 20:35:33567 chrome::RegisterUserPrefs(prefs_.get());
[email protected]2fb7dc982010-09-29 12:24:28568}
569
[email protected]dbbad7a2010-08-13 18:18:36570PrefService* TestingProfile::GetPrefs() {
571 if (!prefs_.get()) {
[email protected]2fb7dc982010-09-29 12:24:28572 CreateTestingPrefService();
[email protected]dbbad7a2010-08-13 18:18:36573 }
574 return prefs_.get();
575}
576
577history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54578 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36579}
580
[email protected]fadc607b62011-02-07 17:55:50581history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
582 return top_sites_.get();
583}
584
[email protected]b441a8492012-06-06 14:55:57585DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50586 return NULL;
587}
588
[email protected]abe2c032011-03-31 18:49:34589net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]812b3a32010-01-08 05:36:04590 return request_context_.get();
591}
592
[email protected]da5683db2011-04-23 17:12:21593net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
594 int renderer_child_id) {
[email protected]31d8f5f22012-04-02 15:22:08595 ExtensionService* extension_service =
[email protected]bd306722012-07-11 20:43:59596 extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]31d8f5f22012-04-02 15:22:08597 if (extension_service) {
[email protected]1c321ee2012-05-21 03:02:34598 const extensions::Extension* installed_app = extension_service->
[email protected]da5683db2011-04-23 17:12:21599 GetInstalledAppForRenderer(renderer_child_id);
600 if (installed_app != NULL && installed_app->is_storage_isolated())
601 return GetRequestContextForIsolatedApp(installed_app->id());
602 }
[email protected]d9696672011-03-15 22:45:09603
[email protected]fb29e6cf2012-07-12 21:27:20604 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
605 renderer_child_id);
606 if (rph && rph->IsGuest()) {
607 // For guest processes (used by the browser tag), we need to isolate the
608 // storage.
609 // TODO(nasko): Until we have proper storage partitions, create a
610 // non-persistent context using the RPH's id.
611 std::string id("guest-");
612 id.append(base::IntToString(renderer_child_id));
613 return GetRequestContextForIsolatedApp(id);
614 }
615
[email protected]d9696672011-03-15 22:45:09616 return GetRequestContext();
617}
618
[email protected]812b3a32010-01-08 05:36:04619void TestingProfile::CreateRequestContext() {
620 if (!request_context_)
[email protected]44738602012-03-02 22:56:01621 request_context_ =
622 new TestURLRequestContextGetter(
623 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]812b3a32010-01-08 05:36:04624}
625
[email protected]7c893202010-10-07 20:18:02626void TestingProfile::ResetRequestContext() {
[email protected]3931fd82012-03-12 21:18:48627 // Any objects holding live URLFetchers should be deleted before the request
628 // context is shut down.
[email protected]16bd088b2012-04-03 08:12:12629 TemplateURLFetcherFactory::ShutdownForProfile(this);
[email protected]3931fd82012-03-12 21:18:48630
[email protected]7c893202010-10-07 20:18:02631 request_context_ = NULL;
632}
633
[email protected]10705a7b2012-08-21 19:07:08634net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
635 return NULL;
636}
637
638net::URLRequestContextGetter*
639TestingProfile::GetMediaRequestContextForRenderProcess(
640 int renderer_child_id) {
[email protected]fadc607b62011-02-07 17:55:50641 return NULL;
642}
643
[email protected]abe2c032011-03-31 18:49:34644net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
[email protected]c10da4b02010-03-25 14:38:32645 if (!extensions_request_context_)
[email protected]00dea9582012-05-09 15:20:05646 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
[email protected]c10da4b02010-03-25 14:38:32647 return extensions_request_context_.get();
648}
649
[email protected]fadc607b62011-02-07 17:55:50650net::SSLConfigService* TestingProfile::GetSSLConfigService() {
651 return NULL;
652}
653
[email protected]abe2c032011-03-31 18:49:34654net::URLRequestContextGetter* TestingProfile::GetRequestContextForIsolatedApp(
[email protected]d9696672011-03-15 22:45:09655 const std::string& app_id) {
656 // We don't test isolated app storage here yet, so returning the same dummy
657 // context is sufficient for now.
658 return GetRequestContext();
659}
660
[email protected]df02aca2012-02-09 21:03:20661content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]314c3e22012-02-21 03:57:42662 if (!resource_context_.get())
663 resource_context_.reset(new content::MockResourceContext());
664 return resource_context_.get();
[email protected]e89b77d2011-04-15 18:58:10665}
666
[email protected]dbbad7a2010-08-13 18:18:36667HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38668 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05669 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
670 ExtensionService* extension_service = GetExtensionService();
671 if (extension_service)
672 host_content_settings_map_->RegisterExtensionService(extension_service);
[email protected]35552dc52011-07-12 09:04:38673 }
[email protected]dbbad7a2010-08-13 18:18:36674 return host_content_settings_map_.get();
675}
676
[email protected]810ddc52012-01-24 01:00:35677content::GeolocationPermissionContext*
[email protected]dbbad7a2010-08-13 18:18:36678TestingProfile::GetGeolocationPermissionContext() {
679 if (!geolocation_permission_context_.get()) {
680 geolocation_permission_context_ =
[email protected]c476e632011-06-23 11:18:04681 new ChromeGeolocationPermissionContext(this);
[email protected]dbbad7a2010-08-13 18:18:36682 }
683 return geolocation_permission_context_.get();
684}
685
[email protected]c52b2892012-03-07 11:01:02686content::SpeechRecognitionPreferences*
687 TestingProfile::GetSpeechRecognitionPreferences() {
[email protected]3297132a2012-02-21 23:27:59688#if defined(ENABLE_INPUT_SPEECH)
[email protected]750e9572012-06-06 15:21:24689 return ChromeSpeechRecognitionPreferences::GetForProfile(this);
[email protected]3297132a2012-02-21 23:27:59690#else
691 return NULL;
692#endif
[email protected]8238dd62011-09-29 15:13:01693}
694
[email protected]fadc607b62011-02-07 17:55:50695std::wstring TestingProfile::GetName() {
696 return std::wstring();
697}
698
699std::wstring TestingProfile::GetID() {
700 return id_;
701}
702
703void TestingProfile::SetID(const std::wstring& id) {
704 id_ = id;
705}
706
707bool TestingProfile::DidLastSessionExitCleanly() {
708 return last_session_exited_cleanly_;
709}
710
[email protected]fadc607b62011-02-07 17:55:50711bool TestingProfile::IsSameProfile(Profile *p) {
712 return this == p;
713}
714
715base::Time TestingProfile::GetStartTime() const {
716 return start_time_;
717}
718
[email protected]a6d36cc2011-02-23 00:39:48719ProtocolHandlerRegistry* TestingProfile::GetProtocolHandlerRegistry() {
[email protected]ab563ff2012-07-21 00:26:32720 return NULL;
[email protected]a6d36cc2011-02-23 00:39:48721}
722
[email protected]fadc607b62011-02-07 17:55:50723FilePath TestingProfile::last_selected_directory() {
724 return last_selected_directory_;
725}
726
727void TestingProfile::set_last_selected_directory(const FilePath& path) {
728 last_selected_directory_ = path;
729}
730
[email protected]cc5bfd42010-11-24 14:44:02731PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07732 if (!pref_proxy_config_tracker_.get()) {
733 pref_proxy_config_tracker_.reset(
734 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs()));
735 }
736 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02737}
738
[email protected]0bfc29a2009-04-27 16:15:44739void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]f61f4782012-06-08 21:54:21740 scoped_refptr<HistoryService> history_service =
[email protected]6c537042012-07-13 00:59:02741 HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
[email protected]f61f4782012-06-08 21:54:21742 DCHECK(history_service.get());
[email protected]0bfc29a2009-04-27 16:15:44743 DCHECK(MessageLoop::current());
744
745 CancelableRequestConsumer consumer;
[email protected]f61f4782012-06-08 21:54:21746 history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer);
[email protected]0bfc29a2009-04-27 16:15:44747 MessageLoop::current()->Run();
748}
[email protected]345a8b72009-09-29 09:11:44749
[email protected]8382d8c2011-09-15 03:43:17750chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
751 return NULL;
752}
753
[email protected]db96a882011-10-09 02:01:54754void TestingProfile::ClearNetworkingHistorySince(base::Time time) {
[email protected]02896a82011-09-21 18:54:32755 NOTIMPLEMENTED();
756}
757
[email protected]671f9c62011-10-28 19:22:07758GURL TestingProfile::GetHomePage() {
759 return GURL(chrome::kChromeUINewTabURL);
760}
761
[email protected]fadc607b62011-02-07 17:55:50762PrefService* TestingProfile::GetOffTheRecordPrefs() {
763 return NULL;
764}
765
[email protected]36fb2c7c2011-04-04 15:49:08766quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
767 return GetExtensionSpecialStoragePolicy();
768}
769
[email protected]1bee6ed2012-03-14 06:46:36770bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
771 return true;
772}
[email protected]ef92e172012-04-25 19:40:41773
774base::Callback<ChromeURLDataManagerBackend*(void)>
775 TestingProfile::GetChromeURLDataManagerBackendGetter() const {
776 return base::Callback<ChromeURLDataManagerBackend*(void)>();
777}
[email protected]5c4c89f2012-08-07 21:09:59778
779TestingProfile::Builder::Builder()
780 : build_called_(false),
781 delegate_(NULL) {
782}
783
784TestingProfile::Builder::~Builder() {
785}
786
787void TestingProfile::Builder::SetPath(const FilePath& path) {
788 path_ = path;
789}
790
791void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
792 delegate_ = delegate;
793}
794
795void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
796 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
797 extension_policy_ = policy;
798}
799
800void TestingProfile::Builder::SetPrefService(scoped_ptr<PrefService> prefs) {
801 pref_service_ = prefs.Pass();
802}
803
804void TestingProfile::Builder::SetUserCloudPolicyManager(
805 scoped_ptr<policy::UserCloudPolicyManager> manager) {
806 user_cloud_policy_manager_ = manager.Pass();
807}
808
809scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
810 DCHECK(!build_called_);
811 build_called_ = true;
812 return scoped_ptr<TestingProfile>(new TestingProfile(
813 path_,
814 delegate_,
815 extension_policy_,
816 pref_service_.Pass(),
817 user_cloud_policy_manager_.Pass()));
818}
819
820