blob: 0497fbd5fbdcac93f58e04c5433e75b1bd3f2a32 [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]dbbad7a2010-08-13 18:18:367#include "base/base_paths.h"
[email protected]e3e43d92010-02-26 22:02:388#include "base/command_line.h"
thestig18dfb7a52014-08-26 10:44:049#include "base/files/file_util.h"
[email protected]76ae8a62013-05-10 05:34:2210#include "base/message_loop/message_loop_proxy.h"
[email protected]dbbad7a2010-08-13 18:18:3611#include "base/path_service.h"
[email protected]03b9b4e2012-10-22 20:01:5212#include "base/prefs/testing_pref_store.h"
[email protected]8e937c1e2012-06-28 22:57:3013#include "base/run_loop.h"
[email protected]3ea1b182013-02-08 22:38:4114#include "base/strings/string_number_conversions.h"
[email protected]dbbad7a2010-08-13 18:18:3615#include "chrome/browser/autocomplete/autocomplete_classifier.h"
sdefresnea5ad6c72015-03-12 10:06:5716#include "chrome/browser/autocomplete/in_memory_url_index.h"
17#include "chrome/browser/autocomplete/in_memory_url_index_factory.h"
[email protected]f61f4782012-06-08 21:54:2118#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]6a848b52014-04-26 22:06:5419#include "chrome/browser/bookmarks/chrome_bookmark_client.h"
[email protected]0b0e8a12014-06-14 00:11:3120#include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
[email protected]1459fb62011-05-25 19:03:2721#include "chrome/browser/browser_process.h"
[email protected]81d49782013-07-12 01:50:5222#include "chrome/browser/chrome_notification_types.h"
huangsf16444b2015-03-27 21:59:1423#include "chrome/browser/favicon/chrome_fallback_icon_client_factory.h"
[email protected]0e199ed72014-07-04 13:05:2224#include "chrome/browser/favicon/chrome_favicon_client_factory.h"
huangsf16444b2015-03-27 21:59:1425#include "chrome/browser/favicon/fallback_icon_service_factory.h"
[email protected]f3d2b312012-08-23 22:27:5926#include "chrome/browser/favicon/favicon_service_factory.h"
[email protected]ad34610c2014-06-04 14:43:5227#include "chrome/browser/history/chrome_history_client.h"
28#include "chrome/browser/history/chrome_history_client_factory.h"
[email protected]f61f4782012-06-08 21:54:2129#include "chrome/browser/history/history_service_factory.h"
[email protected]a50e16a2013-04-25 14:07:1730#include "chrome/browser/history/web_history_service_factory.h"
[email protected]53c5b0f2013-07-22 10:54:1131#include "chrome/browser/net/pref_proxy_config_tracker.h"
[email protected]6f96cbcb2011-11-04 02:26:0732#include "chrome/browser/net/proxy_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3633#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5334#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]d81bb3e2013-05-02 18:17:0335#include "chrome/browser/policy/profile_policy_connector.h"
36#include "chrome/browser/policy/profile_policy_connector_factory.h"
[email protected]37858e52010-08-26 00:22:0237#include "chrome/browser/prefs/browser_prefs.h"
[email protected]c753f142013-02-10 13:14:0438#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]4850a7f2011-03-08 23:36:5939#include "chrome/browser/prerender/prerender_manager.h"
[email protected]7e75e4a2013-05-17 17:20:0340#include "chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.h"
[email protected]371662e372013-10-17 22:05:2241#include "chrome/browser/profiles/profile_manager.h"
[email protected]27ddfed22012-10-30 23:22:4342#include "chrome/browser/profiles/storage_partition_descriptor.h"
[email protected]16bd088b2012-04-03 08:12:1243#include "chrome/browser/search_engines/template_url_fetcher_factory.h"
sdefresnef8cf5be2014-12-16 20:08:0944#include "chrome/browser/sync/glue/sync_start_util.h"
a.sarkar.arundaadc712015-02-26 05:39:0845#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.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]9e33d7e2011-09-30 16:43:5448#include "chrome/common/chrome_switches.h"
[email protected]e000daf2013-07-31 16:50:5849#include "chrome/common/pref_names.h"
[email protected]dbbad7a2010-08-13 18:18:3650#include "chrome/common/url_constants.h"
[email protected]18a15ca82012-08-30 00:28:1651#include "chrome/test/base/history_index_restore_observer.h"
[email protected]7688968a2013-02-12 21:45:1352#include "chrome/test/base/testing_pref_service_syncable.h"
sdefresnecb955cd2014-12-15 23:21:5653#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
[email protected]a90c8ca2014-05-20 17:16:0454#include "components/bookmarks/browser/bookmark_model.h"
55#include "components/bookmarks/common/bookmark_constants.h"
mukai8eaec822014-10-25 17:53:1656#include "components/content_settings/core/browser/host_content_settings_map.h"
huangsf16444b2015-03-27 21:59:1457#include "components/favicon/core/fallback_icon_service.h"
sdefresnecbacfd72015-03-20 12:11:3258#include "components/favicon/core/favicon_service.h"
sdefresne506dd522015-03-12 18:49:1259#include "components/history/content/browser/content_visit_delegate.h"
sdefresne46129152015-01-20 19:24:4360#include "components/history/content/browser/history_database_helper.h"
sdefresne506dd522015-03-12 18:49:1261#include "components/history/core/browser/history_backend.h"
naiem.shaikb9f5bffa2014-11-30 09:02:3162#include "components/history/core/browser/history_constants.h"
sdefresne46129152015-01-20 19:24:4363#include "components/history/core/browser/history_database_params.h"
sdefresneabac1922014-12-29 15:54:0664#include "components/history/core/browser/history_db_task.h"
sdefresne506dd522015-03-12 18:49:1265#include "components/history/core/browser/history_service.h"
[email protected]540380fc2014-03-14 10:10:3466#include "components/keyed_service/content/browser_context_dependency_manager.h"
jitendra.ks30f03392015-01-28 09:47:1867#include "components/keyed_service/core/refcounted_keyed_service.h"
[email protected]76b4b152013-12-08 21:10:0468#include "components/policy/core/common/policy_service.h"
a.sarkar.arundaadc712015-02-26 05:39:0869#include "components/ui/zoom/zoom_event_manager.h"
[email protected]75fee372013-03-06 00:42:4470#include "components/user_prefs/user_prefs.h"
sdefresnecb955cd2014-12-15 23:21:5671#include "components/webdata_services/web_data_service_wrapper.h"
[email protected]c38831a12011-10-28 12:44:4972#include "content/public/browser/browser_thread.h"
[email protected]7c4b66b2014-01-04 12:28:1373#include "content/public/browser/cookie_store_factory.h"
[email protected]ad50def52011-10-19 23:17:0774#include "content/public/browser/notification_service.h"
[email protected]fb29e6cf2012-07-12 21:27:2075#include "content/public/browser/render_process_host.h"
[email protected]885c0e92012-11-13 20:27:4276#include "content/public/browser/storage_partition.h"
a.sarkar.arundaadc712015-02-26 05:39:0877#include "content/public/browser/zoom_level_delegate.h"
[email protected]08a932d52012-06-03 21:42:1278#include "content/public/test/mock_resource_context.h"
[email protected]a7fe9112012-07-20 02:34:4579#include "content/public/test/test_utils.h"
[email protected]885c0e92012-11-13 20:27:4280#include "extensions/common/constants.h"
[email protected]aa84a7e2012-03-15 21:29:0681#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0482#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3483#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4484#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4685#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3486
[email protected]21d3a882012-05-31 14:41:5587#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:2488#include "chrome/browser/policy/schema_registry_service.h"
89#include "chrome/browser/policy/schema_registry_service_factory.h"
[email protected]f20a3a22013-12-03 16:12:3790#include "components/policy/core/common/configuration_policy_provider.h"
[email protected]76b4b152013-12-08 21:10:0491#include "components/policy/core/common/policy_service_impl.h"
[email protected]eb83c392013-11-07 12:53:2492#include "components/policy/core/common/schema.h"
[email protected]21d3a882012-05-31 14:41:5593#else
[email protected]76b4b152013-12-08 21:10:0494#include "components/policy/core/common/policy_service_stub.h"
[email protected]21d3a882012-05-31 14:41:5595#endif // defined(ENABLE_CONFIGURATION_POLICY)
96
[email protected]1df3d972014-06-11 04:55:1897#if defined(ENABLE_EXTENSIONS)
thestig44b9ce22014-10-17 03:19:2898#include "chrome/browser/extensions/extension_service.h"
[email protected]50356f82014-07-31 13:25:1899#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]d5a74be2014-08-08 01:01:27100#include "chrome/browser/extensions/extension_system_factory.h"
101#include "chrome/browser/extensions/test_extension_system.h"
fsamuel8dfa19a2015-05-05 01:00:39102#include "components/guest_view/browser/guest_view_manager.h"
juncaicf523332015-06-04 00:14:04103#include "extensions/browser/event_router_factory.h"
isherman30fa851a2015-06-09 23:32:10104#include "extensions/browser/extension_prefs.h"
juncaicf523332015-06-04 00:14:04105#include "extensions/browser/extension_prefs_factory.h"
[email protected]994fdb252014-06-26 13:26:37106#include "extensions/browser/extension_system.h"
[email protected]1df3d972014-06-11 04:55:18107#endif
108
[email protected]bd3093c2014-06-13 07:38:16109#if defined(OS_ANDROID)
110#include "chrome/browser/signin/android_profile_oauth2_token_service.h"
111#endif
112
treib87bb89cbb2014-12-01 16:01:47113#if defined(ENABLE_SUPERVISED_USERS)
treib3c8cf132014-12-08 15:09:32114#include "chrome/browser/supervised_user/supervised_user_constants.h"
[email protected]cce15bb2014-06-17 13:43:51115#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
116#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]e1480482013-09-11 11:49:58117#endif
118
[email protected]e1acf6f2008-10-27 20:43:33119using base::Time;
tfarinaa0ec34e2015-01-12 18:46:48120using bookmarks::BookmarkModel;
[email protected]631bb742011-11-02 11:29:39121using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:57122using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:23123using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:46124using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:33125
[email protected]d364c652008-08-29 19:46:56126namespace {
127
[email protected]0bfc29a2009-04-27 16:15:44128// Task used to make sure history has finished processing a request. Intended
129// for use with BlockUntilHistoryProcessesPendingRequests.
130
[email protected]d891f522013-02-08 03:24:41131class QuittingHistoryDBTask : public history::HistoryDBTask {
[email protected]0bfc29a2009-04-27 16:15:44132 public:
133 QuittingHistoryDBTask() {}
134
dchenge59c1bb92014-10-21 11:55:49135 bool RunOnDBThread(history::HistoryBackend* backend,
136 history::HistoryDatabase* db) override {
[email protected]0bfc29a2009-04-27 16:15:44137 return true;
138 }
139
dchenge59c1bb92014-10-21 11:55:49140 void DoneRunOnMainThread() override { base::MessageLoop::current()->Quit(); }
[email protected]0bfc29a2009-04-27 16:15:44141
142 private:
dchenge59c1bb92014-10-21 11:55:49143 ~QuittingHistoryDBTask() override {}
[email protected]7991a232009-11-06 01:55:48144
[email protected]0bfc29a2009-04-27 16:15:44145 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
146};
147
[email protected]33ad6ce92013-08-27 14:39:08148class TestExtensionURLRequestContext : public net::URLRequestContext {
149 public:
150 TestExtensionURLRequestContext() {
[email protected]7c4b66b2014-01-04 12:28:13151 net::CookieMonster* cookie_monster =
[email protected]9a6c2aa2014-01-11 22:39:39152 content::CreateCookieStore(content::CookieStoreConfig())->
153 GetCookieMonster();
[email protected]5edff3c52014-06-23 20:27:48154 const char* const schemes[] = {extensions::kExtensionScheme};
155 cookie_monster->SetCookieableSchemes(schemes, arraysize(schemes));
[email protected]33ad6ce92013-08-27 14:39:08156 set_cookie_store(cookie_monster);
157 }
158
dchenge59c1bb92014-10-21 11:55:49159 ~TestExtensionURLRequestContext() override { AssertNoURLRequests(); }
[email protected]33ad6ce92013-08-27 14:39:08160};
161
162class TestExtensionURLRequestContextGetter
163 : public net::URLRequestContextGetter {
164 public:
dchenge59c1bb92014-10-21 11:55:49165 net::URLRequestContext* GetURLRequestContext() override {
[email protected]33ad6ce92013-08-27 14:39:08166 if (!context_.get())
167 context_.reset(new TestExtensionURLRequestContext());
168 return context_.get();
169 }
dchenge59c1bb92014-10-21 11:55:49170 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
171 const override {
[email protected]33ad6ce92013-08-27 14:39:08172 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
173 }
174
175 protected:
dchenge59c1bb92014-10-21 11:55:49176 ~TestExtensionURLRequestContextGetter() override {}
[email protected]33ad6ce92013-08-27 14:39:08177
178 private:
179 scoped_ptr<net::URLRequestContext> context_;
180};
181
[email protected]1d51882f2013-11-12 01:59:02182#if defined(ENABLE_NOTIFICATIONS)
isherman30fa851a2015-06-09 23:32:10183scoped_ptr<KeyedService> CreateTestDesktopNotificationService(
[email protected]c7fa4362013-04-26 18:09:02184 content::BrowserContext* profile) {
isherman30fa851a2015-06-09 23:32:10185 return make_scoped_ptr(
186 new DesktopNotificationService(static_cast<Profile*>(profile)));
[email protected]dcb72d52011-04-13 12:36:53187}
[email protected]1d51882f2013-11-12 01:59:02188#endif
[email protected]dcb72d52011-04-13 12:36:53189
isherman30fa851a2015-06-09 23:32:10190scoped_ptr<KeyedService> BuildFaviconService(content::BrowserContext* context) {
sdefresne4118d85c2015-03-13 17:37:57191 Profile* profile = Profile::FromBrowserContext(context);
isherman30fa851a2015-06-09 23:32:10192 return make_scoped_ptr(new favicon::FaviconService(
hayatobe13e672015-03-31 04:51:38193 ChromeFaviconClientFactory::GetForProfile(profile),
isherman30fa851a2015-06-09 23:32:10194 HistoryServiceFactory::GetForProfile(
195 profile, ServiceAccessType::EXPLICIT_ACCESS)));
sdefresnecb955cd2014-12-15 23:21:56196}
197
isherman30fa851a2015-06-09 23:32:10198scoped_ptr<KeyedService> BuildHistoryService(content::BrowserContext* context) {
sdefresned4d9fe42015-02-18 16:18:47199 Profile* profile = Profile::FromBrowserContext(context);
isherman30fa851a2015-06-09 23:32:10200 return make_scoped_ptr(new history::HistoryService(
sdefresned4d9fe42015-02-18 16:18:47201 ChromeHistoryClientFactory::GetForProfile(profile),
sdefresnec62efa32015-03-12 22:46:31202 scoped_ptr<history::VisitDelegate>(
isherman30fa851a2015-06-09 23:32:10203 new history::ContentVisitDelegate(profile))));
sdefresnecb955cd2014-12-15 23:21:56204}
205
isherman30fa851a2015-06-09 23:32:10206scoped_ptr<KeyedService> BuildInMemoryURLIndex(
207 content::BrowserContext* context) {
sdefresnea5ad6c72015-03-12 10:06:57208 Profile* profile = Profile::FromBrowserContext(context);
isherman30fa851a2015-06-09 23:32:10209 scoped_ptr<InMemoryURLIndex> in_memory_url_index(new InMemoryURLIndex(
sdefresnec38a45a2015-03-17 15:32:19210 BookmarkModelFactory::GetForProfile(profile),
sdefresnea5ad6c72015-03-12 10:06:57211 HistoryServiceFactory::GetForProfile(profile,
212 ServiceAccessType::IMPLICIT_ACCESS),
213 profile->GetPath(),
isherman30fa851a2015-06-09 23:32:10214 profile->GetPrefs()->GetString(prefs::kAcceptLanguages)));
sdefresnea5ad6c72015-03-12 10:06:57215 in_memory_url_index->Init();
isherman30fa851a2015-06-09 23:32:10216 return in_memory_url_index.Pass();
sdefresnea5ad6c72015-03-12 10:06:57217}
218
isherman30fa851a2015-06-09 23:32:10219scoped_ptr<KeyedService> BuildBookmarkModel(content::BrowserContext* context) {
sdefresnecb955cd2014-12-15 23:21:56220 Profile* profile = static_cast<Profile*>(context);
221 ChromeBookmarkClient* bookmark_client =
222 ChromeBookmarkClientFactory::GetForProfile(profile);
isherman30fa851a2015-06-09 23:32:10223 scoped_ptr<BookmarkModel> bookmark_model(new BookmarkModel(bookmark_client));
224 bookmark_client->Init(bookmark_model.get());
sdefresnecb955cd2014-12-15 23:21:56225 bookmark_model->Load(profile->GetPrefs(),
226 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
227 profile->GetPath(),
228 profile->GetIOTaskRunner(),
229 content::BrowserThread::GetMessageLoopProxyForThread(
230 content::BrowserThread::UI));
isherman30fa851a2015-06-09 23:32:10231 return bookmark_model.Pass();
sdefresnecb955cd2014-12-15 23:21:56232}
233
isherman30fa851a2015-06-09 23:32:10234scoped_ptr<KeyedService> BuildChromeBookmarkClient(
sdefresnecb955cd2014-12-15 23:21:56235 content::BrowserContext* context) {
isherman30fa851a2015-06-09 23:32:10236 return make_scoped_ptr(
237 new ChromeBookmarkClient(static_cast<Profile*>(context)));
sdefresnecb955cd2014-12-15 23:21:56238}
239
isherman30fa851a2015-06-09 23:32:10240scoped_ptr<KeyedService> BuildChromeHistoryClient(
sdefresnecb955cd2014-12-15 23:21:56241 content::BrowserContext* context) {
242 Profile* profile = static_cast<Profile*>(context);
isherman30fa851a2015-06-09 23:32:10243 return make_scoped_ptr(
244 new ChromeHistoryClient(BookmarkModelFactory::GetForProfile(profile)));
sdefresnecb955cd2014-12-15 23:21:56245}
246
247void TestProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
248 sql::InitStatus status) {
249 NOTREACHED();
250}
251
isherman30fa851a2015-06-09 23:32:10252scoped_ptr<KeyedService> BuildWebDataService(content::BrowserContext* context) {
sdefresnef8cf5be2014-12-16 20:08:09253 const base::FilePath& context_path = context->GetPath();
isherman30fa851a2015-06-09 23:32:10254 return make_scoped_ptr(new WebDataServiceWrapper(
sdefresnef8cf5be2014-12-16 20:08:09255 context_path, g_browser_process->GetApplicationLocale(),
sdefresnecb955cd2014-12-15 23:21:56256 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
257 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
sdefresnef8cf5be2014-12-16 20:08:09258 sync_start_util::GetFlareForSyncableService(context_path),
isherman30fa851a2015-06-09 23:32:10259 &TestProfileErrorCallback));
sdefresnecb955cd2014-12-15 23:21:56260}
261
[email protected]d364c652008-08-29 19:46:56262} // namespace
263
[email protected]89dbb1772012-07-17 13:47:25264// static
265#if defined(OS_CHROMEOS)
266// Must be kept in sync with
267// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
[email protected]0f13fe8d2013-11-19 08:25:00268const char TestingProfile::kTestUserProfileDir[] = "test-user";
[email protected]89dbb1772012-07-17 13:47:25269#else
270const char TestingProfile::kTestUserProfileDir[] = "Default";
271#endif
272
[email protected]f25387b2008-08-21 15:20:33273TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55274 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28275 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39276 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09277 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34278 guest_session_(false),
[email protected]5cdcd152011-03-31 22:48:52279 last_session_exited_cleanly_(true),
[email protected]f0901a22013-05-27 21:37:53280 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44281 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35282 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17283 delegate_(NULL) {
[email protected]5c4c89f2012-08-07 21:09:59284 CreateTempProfileDir();
[email protected]49a25632011-08-31 17:03:48285 profile_path_ = temp_dir_.path();
286
287 Init();
[email protected]537c1082011-12-02 02:37:17288 FinishInit();
[email protected]49a25632011-08-31 17:03:48289}
290
[email protected]152ea302013-02-11 04:08:40291TestingProfile::TestingProfile(const base::FilePath& path)
[email protected]49a25632011-08-31 17:03:48292 : start_time_(Time::Now()),
293 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39294 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09295 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34296 guest_session_(false),
[email protected]49a25632011-08-31 17:03:48297 last_session_exited_cleanly_(true),
298 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53299 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44300 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35301 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17302 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48303 Init();
[email protected]537c1082011-12-02 02:37:17304 FinishInit();
305}
306
[email protected]152ea302013-02-11 04:08:40307TestingProfile::TestingProfile(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:17308 Delegate* delegate)
309 : start_time_(Time::Now()),
310 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39311 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09312 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34313 guest_session_(false),
[email protected]537c1082011-12-02 02:37:17314 last_session_exited_cleanly_(true),
315 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53316 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44317 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35318 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17319 delegate_(delegate) {
320 Init();
321 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26322 base::MessageLoop::current()->PostTask(
323 FROM_HERE,
324 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]537c1082011-12-02 02:37:17325 } else {
326 FinishInit();
327 }
[email protected]49a25632011-08-31 17:03:48328}
329
[email protected]5c4c89f2012-08-07 21:09:59330TestingProfile::TestingProfile(
[email protected]152ea302013-02-11 04:08:40331 const base::FilePath& path,
[email protected]5c4c89f2012-08-07 21:09:59332 Delegate* delegate,
[email protected]50356f82014-07-31 13:25:18333#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59334 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
[email protected]50356f82014-07-31 13:25:18335#endif
[email protected]9819fd02013-08-22 10:49:39336 scoped_ptr<PrefServiceSyncable> prefs,
tapted1ab9b7092014-09-15 23:49:40337 TestingProfile* parent,
[email protected]e9476cd2013-12-04 05:55:34338 bool guest_session,
[email protected]d20d0432014-06-12 17:14:05339 const std::string& supervised_user_id,
[email protected]a43b9a82013-11-28 16:56:41340 scoped_ptr<policy::PolicyService> policy_service,
[email protected]9819fd02013-08-22 10:49:39341 const TestingFactories& factories)
[email protected]5c4c89f2012-08-07 21:09:59342 : start_time_(Time::Now()),
343 prefs_(prefs.release()),
344 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39345 force_incognito_(false),
tapted1ab9b7092014-09-15 23:49:40346 original_profile_(parent),
[email protected]e9476cd2013-12-04 05:55:34347 guest_session_(guest_session),
[email protected]5c4c89f2012-08-07 21:09:59348 last_session_exited_cleanly_(true),
[email protected]50356f82014-07-31 13:25:18349#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59350 extension_special_storage_policy_(extension_policy),
[email protected]50356f82014-07-31 13:25:18351#endif
[email protected]5c4c89f2012-08-07 21:09:59352 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53353 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44354 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35355 resource_context_(NULL),
[email protected]a43b9a82013-11-28 16:56:41356 delegate_(delegate),
357 policy_service_(policy_service.release()) {
tapted1ab9b7092014-09-15 23:49:40358 if (parent)
359 parent->SetOffTheRecordProfile(scoped_ptr<Profile>(this));
360
[email protected]5c4c89f2012-08-07 21:09:59361 // If no profile path was supplied, create one.
362 if (profile_path_.empty()) {
363 CreateTempProfileDir();
364 profile_path_ = temp_dir_.path();
365 }
366
[email protected]9819fd02013-08-22 10:49:39367 // Set any testing factories prior to initializing the services.
368 for (TestingFactories::const_iterator it = factories.begin();
369 it != factories.end(); ++it) {
370 it->first->SetTestingFactory(this, it->second);
371 }
372
[email protected]5c4c89f2012-08-07 21:09:59373 Init();
374 // If caller supplied a delegate, delay the FinishInit invocation until other
375 // tasks have run.
376 // TODO(atwilson): See if this is still required once we convert the current
377 // users of the constructor that takes a Delegate* param.
378 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26379 base::MessageLoop::current()->PostTask(
380 FROM_HERE,
381 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]5c4c89f2012-08-07 21:09:59382 } else {
383 FinishInit();
384 }
treib9e30e302015-04-15 08:12:16385
386 SetSupervisedUserId(supervised_user_id);
[email protected]5c4c89f2012-08-07 21:09:59387}
388
389void TestingProfile::CreateTempProfileDir() {
390 if (!temp_dir_.CreateUniqueTempDir()) {
391 LOG(ERROR) << "Failed to create unique temporary directory.";
392
393 // Fallback logic in case we fail to create unique temporary directory.
[email protected]152ea302013-02-11 04:08:40394 base::FilePath system_tmp_dir;
[email protected]5c4c89f2012-08-07 21:09:59395 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
396
397 // We're severly screwed if we can't get the system temporary
398 // directory. Die now to avoid writing to the filesystem root
399 // or other bad places.
400 CHECK(success);
401
[email protected]152ea302013-02-11 04:08:40402 base::FilePath fallback_dir(
403 system_tmp_dir.AppendASCII("TestingProfilePath"));
[email protected]dd3aa792013-07-16 19:10:23404 base::DeleteFile(fallback_dir, true);
[email protected]426d1c92013-12-03 20:08:54405 base::CreateDirectory(fallback_dir);
[email protected]5c4c89f2012-08-07 21:09:59406 if (!temp_dir_.Set(fallback_dir)) {
407 // That shouldn't happen, but if it does, try to recover.
408 LOG(ERROR) << "Failed to use a fallback temporary directory.";
409
410 // We're screwed if this fails, see CHECK above.
411 CHECK(temp_dir_.Set(system_tmp_dir));
412 }
413 }
414}
415
[email protected]49a25632011-08-31 17:03:48416void TestingProfile::Init() {
[email protected]bf390cb2013-08-06 02:48:08417 // If threads have been initialized, we should be on the UI thread.
[email protected]33d06242013-08-12 05:20:30418 DCHECK(!content::BrowserThread::IsThreadInitialized(
419 content::BrowserThread::UI) ||
420 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]bf390cb2013-08-06 02:48:08421
mlerman1f090b422015-01-15 16:26:18422 set_is_guest_profile(guest_session_);
423
[email protected]bd3093c2014-06-13 07:38:16424#if defined(OS_ANDROID)
425 // Make sure token service knows its running in tests.
426 AndroidProfileOAuth2TokenService::set_is_testing_profile();
427#endif
428
[email protected]7e75e4a2013-05-17 17:20:03429 // Normally this would happen during browser startup, but for tests
430 // we need to trigger creation of Profile-related services.
431 ChromeBrowserMainExtraPartsProfiles::
[email protected]f1484c52013-05-22 23:25:44432 EnsureBrowserContextKeyedServiceFactoriesBuilt();
[email protected]7e75e4a2013-05-17 17:20:03433
[email protected]75fee372013-03-06 00:42:44434 if (prefs_.get())
[email protected]c7141feb2013-06-11 13:00:16435 user_prefs::UserPrefs::Set(this, prefs_.get());
tapted1ab9b7092014-09-15 23:49:40436 else if (IsOffTheRecord())
437 CreateIncognitoPrefService();
[email protected]c7fa4362013-04-26 18:09:02438 else
439 CreateTestingPrefService();
[email protected]75fee372013-03-06 00:42:44440
[email protected]7567484142013-07-11 17:36:07441 if (!base::PathExists(profile_path_))
[email protected]426d1c92013-12-03 20:08:54442 base::CreateDirectory(profile_path_);
[email protected]d41e6572012-05-04 20:49:42443
[email protected]d81bb3e2013-05-02 18:17:03444 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl
445 // anymore, after converting the PrefService to a PKS. Until then it must
446 // be associated with a TestingProfile too.
[email protected]eb83c392013-11-07 12:53:24447 if (!IsOffTheRecord())
448 CreateProfilePolicyConnector();
[email protected]d81bb3e2013-05-02 18:17:03449
juncaicf523332015-06-04 00:14:04450 extensions_path_ = profile_path_.AppendASCII("Extensions");
451
[email protected]d5a74be2014-08-08 01:01:27452#if defined(ENABLE_EXTENSIONS)
[email protected]bd306722012-07-11 20:43:59453 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
454 this, extensions::TestExtensionSystem::Build);
juncaicf523332015-06-04 00:14:04455
456 extensions::TestExtensionSystem* test_extension_system =
457 static_cast<extensions::TestExtensionSystem*>(
458 extensions::ExtensionSystem::Get(this));
isherman30fa851a2015-06-09 23:32:10459 scoped_ptr<extensions::ExtensionPrefs> extension_prefs =
juncaicf523332015-06-04 00:14:04460 test_extension_system->CreateExtensionPrefs(
461 base::CommandLine::ForCurrentProcess(), extensions_path_);
462 extensions::ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
isherman30fa851a2015-06-09 23:32:10463 this, extension_prefs.Pass());
juncaicf523332015-06-04 00:14:04464
465 extensions::EventRouterFactory::GetInstance()->SetTestingFactory(this,
466 nullptr);
[email protected]d5a74be2014-08-08 01:01:27467#endif
[email protected]31d8f5f22012-04-02 15:22:08468
tapted1ab9b7092014-09-15 23:49:40469 // Prefs for incognito profiles are set in CreateIncognitoPrefService() by
470 // simulating ProfileImpl::GetOffTheRecordPrefs().
471 if (!IsOffTheRecord()) {
472 DCHECK(!original_profile_);
[email protected]d45c5d52013-12-04 23:41:16473 user_prefs::PrefRegistrySyncable* pref_registry =
474 static_cast<user_prefs::PrefRegistrySyncable*>(
475 prefs_->DeprecatedGetPrefRegistry());
476 browser_context_dependency_manager_->
477 RegisterProfilePrefsForServices(this, pref_registry);
478 }
479
[email protected]9819fd02013-08-22 10:49:39480 browser_context_dependency_manager_->CreateBrowserContextServicesForTest(
[email protected]d45c5d52013-12-04 23:41:16481 this);
[email protected]49a25632011-08-31 17:03:48482
[email protected]5fd2e842012-03-01 00:29:11483#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53484 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22485 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
486 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11487#endif
[email protected]e1480482013-09-11 11:49:58488
treib87bb89cbb2014-12-01 16:01:47489#if defined(ENABLE_SUPERVISED_USERS)
tapted1ab9b7092014-09-15 23:49:40490 if (!IsOffTheRecord()) {
491 SupervisedUserSettingsService* settings_service =
492 SupervisedUserSettingsServiceFactory::GetForProfile(this);
493 TestingPrefStore* store = new TestingPrefStore();
494 settings_service->Init(store);
495 store->SetInitializationCompleted();
496 }
[email protected]e1480482013-09-11 11:49:58497#endif
[email protected]d6001872013-09-24 18:31:26498
499 profile_name_ = "testing_profile";
[email protected]537c1082011-12-02 02:37:17500}
[email protected]12463cd2011-07-19 09:40:20501
[email protected]537c1082011-12-02 02:37:17502void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40503 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07504 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20505 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53506 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07507 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17508
[email protected]371662e372013-10-17 22:05:22509 ProfileManager* profile_manager = g_browser_process->profile_manager();
510 if (profile_manager)
511 profile_manager->InitProfileUserPrefs(this);
512
[email protected]537c1082011-12-02 02:37:17513 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03514 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46515}
516
[email protected]f25387b2008-08-21 15:20:33517TestingProfile::~TestingProfile() {
[email protected]9819fd02013-08-22 10:49:39518 // Revert to non-incognito mode before shutdown.
519 force_incognito_ = false;
520
tapted1ab9b7092014-09-15 23:49:40521 // If this profile owns an incognito profile, tear it down first.
522 incognito_profile_.reset();
523
[email protected]37ac95b2013-07-23 23:39:35524 // Any objects holding live URLFetchers should be deleted before teardown.
525 TemplateURLFetcherFactory::ShutdownForProfile(this);
526
[email protected]a7c4c032012-07-27 00:36:32527 MaybeSendDestroyedNotification();
[email protected]5cdcd152011-03-31 22:48:52528
[email protected]f0901a22013-05-27 21:37:53529 browser_context_dependency_manager_->DestroyBrowserContextServices(this);
[email protected]5cdcd152011-03-31 22:48:52530
[email protected]5c6ac842013-06-02 23:37:03531 if (host_content_settings_map_.get())
[email protected]35552dc52011-07-12 09:04:38532 host_content_settings_map_->ShutdownOnUIThread();
533
[email protected]cc5bfd42010-11-24 14:44:02534 if (pref_proxy_config_tracker_.get())
535 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]37ac95b2013-07-23 23:39:35536 // Failing a post == leaks == heapcheck failure. Make that an immediate test
537 // failure.
538 if (resource_context_) {
539 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
540 resource_context_));
541 resource_context_ = NULL;
542 content::RunAllPendingInMessageLoop(BrowserThread::IO);
543 }
[email protected]f25387b2008-08-21 15:20:33544}
initial.commit09911bf2008-07-26 23:55:29545
[email protected]d2879af2010-02-08 16:02:56546void TestingProfile::CreateFaviconService() {
[email protected]f3d2b312012-08-23 22:27:59547 // It is up to the caller to create the history service if one is needed.
548 FaviconServiceFactory::GetInstance()->SetTestingFactory(
549 this, BuildFaviconService);
[email protected]d2879af2010-02-08 16:02:56550}
551
[email protected]608e7e02013-07-24 12:23:31552bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54553 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33554 if (delete_file) {
[email protected]152ea302013-02-11 04:08:40555 base::FilePath path = GetPath();
naiem.shaikb9f5bffa2014-11-30 09:02:31556 path = path.Append(history::kHistoryFilename);
[email protected]608e7e02013-07-24 12:23:31557 if (!base::DeleteFile(path, false) || base::PathExists(path))
558 return false;
[email protected]f25387b2008-08-21 15:20:33559 }
[email protected]7019bb6d2012-08-23 09:09:48560 // This will create and init the history service.
sdefresnec62efa32015-03-12 22:46:31561 history::HistoryService* history_service =
562 static_cast<history::HistoryService*>(
563 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
564 this, BuildHistoryService));
sdefresne46129152015-01-20 19:24:43565 if (!history_service->Init(
sdefresnec6ca4452015-02-05 09:18:14566 no_db, GetPrefs()->GetString(prefs::kAcceptLanguages),
567 history::HistoryDatabaseParamsForPath(GetPath()))) {
sdefresnef29a4392015-02-03 19:40:27568 HistoryServiceFactory::GetInstance()->SetTestingFactory(this, nullptr);
569 return false;
[email protected]7019bb6d2012-08-23 09:09:48570 }
sdefresnea5ad6c72015-03-12 10:06:57571 // Some tests expect that CreateHistoryService() will also make the
572 // InMemoryURLIndex available.
573 InMemoryURLIndexFactory::GetInstance()->SetTestingFactory(
574 this, BuildInMemoryURLIndex);
[email protected]a50e16a2013-04-25 14:07:17575 // Disable WebHistoryService by default, since it makes network requests.
sdefresnef29a4392015-02-03 19:40:27576 WebHistoryServiceFactory::GetInstance()->SetTestingFactory(this, nullptr);
[email protected]608e7e02013-07-24 12:23:31577 return true;
initial.commit09911bf2008-07-26 23:55:29578}
579
580void TestingProfile::DestroyHistoryService() {
sdefresnec62efa32015-03-12 22:46:31581 history::HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02582 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]a5894fe2012-11-01 14:40:56583 if (!history_service)
initial.commit09911bf2008-07-26 23:55:29584 return;
585
[email protected]378970412014-06-12 07:31:17586 history_service->ClearCachedDataForContextID(0);
[email protected]bb024fe2013-05-10 21:33:26587 history_service->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure());
[email protected]f61f4782012-06-08 21:54:21588 history_service->Cleanup();
589 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29590
591 // Wait for the backend class to terminate before deleting the files and
592 // moving to the next test. Note: if this never terminates, somebody is
593 // probably leaking a reference to the history backend, so it never calls
594 // our destroy task.
[email protected]bb024fe2013-05-10 21:33:26595 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29596
initial.commit09911bf2008-07-26 23:55:29597 // Make sure we don't have any event pending that could disrupt the next
598 // test.
[email protected]bb024fe2013-05-10 21:33:26599 base::MessageLoop::current()->PostTask(FROM_HERE,
600 base::MessageLoop::QuitClosure());
601 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29602}
[email protected]4d0cd7ce2008-08-11 16:40:57603
[email protected]d8e41ed2008-09-11 15:22:32604void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46605 if (delete_file) {
[email protected]d4a67d52014-04-12 05:17:03606 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName);
[email protected]dd3aa792013-07-16 19:10:23607 base::DeleteFile(path, false);
[email protected]90ef13132008-08-27 03:27:46608 }
[email protected]ad34610c2014-06-04 14:43:52609 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory(
610 this, BuildChromeHistoryClient);
[email protected]0b0e8a12014-06-14 00:11:31611 ChromeBookmarkClientFactory::GetInstance()->SetTestingFactory(
612 this, BuildChromeBookmarkClient);
613 // This creates the BookmarkModel.
614 ignore_result(BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
615 this, BuildBookmarkModel));
[email protected]4d0cd7ce2008-08-11 16:40:57616}
[email protected]d2c017a2008-08-13 21:51:45617
[email protected]d07edd42012-05-14 23:49:46618void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01619 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46620 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55621}
622
[email protected]18a15ca82012-08-30 00:28:16623void TestingProfile::BlockUntilHistoryIndexIsRefreshed() {
624 // Only get the history service if it actually exists since the caller of the
625 // test should explicitly call CreateHistoryService to build it.
sdefresnec62efa32015-03-12 22:46:31626 history::HistoryService* history_service =
[email protected]18a15ca82012-08-30 00:28:16627 HistoryServiceFactory::GetForProfileWithoutCreating(this);
628 DCHECK(history_service);
sdefresnea5ad6c72015-03-12 10:06:57629 InMemoryURLIndex* index = InMemoryURLIndexFactory::GetForProfile(this);
[email protected]18a15ca82012-08-30 00:28:16630 if (!index || index->restored())
631 return;
632 base::RunLoop run_loop;
633 HistoryIndexRestoreObserver observer(
634 content::GetQuitTaskForRunLoop(&run_loop));
635 index->set_restore_cache_observer(&observer);
636 run_loop.Run();
637 index->set_restore_cache_observer(NULL);
638 DCHECK(index->restored());
639}
640
[email protected]90efc3e2014-04-22 18:39:03641void TestingProfile::SetGuestSession(bool guest) {
642 guest_session_ = guest;
643}
644
[email protected]4251165a2013-07-17 04:33:40645base::FilePath TestingProfile::GetPath() const {
[email protected]49a25632011-08-31 17:03:48646 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19647}
648
wjmacleancaa7d6d2014-11-12 16:42:11649scoped_ptr<content::ZoomLevelDelegate> TestingProfile::CreateZoomLevelDelegate(
650 const base::FilePath& partition_path) {
a.sarkar.arundaadc712015-02-26 05:39:08651 return make_scoped_ptr(new chrome::ChromeZoomLevelPrefs(
652 GetPrefs(), GetPath(), partition_path,
653 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
wjmacleancaa7d6d2014-11-12 16:42:11654}
655
[email protected]0de615a2012-11-08 04:40:59656scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
[email protected]bb024fe2013-05-10 21:33:26657 return base::MessageLoop::current()->message_loop_proxy();
[email protected]0de615a2012-11-08 04:40:59658}
659
[email protected]5b199522012-12-22 17:24:44660TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
tapted1ab9b7092014-09-15 23:49:40661 DCHECK(prefs_);
[email protected]2fb7dc982010-09-29 12:24:28662 DCHECK(testing_prefs_);
663 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36664}
665
[email protected]8e5c89a2011-06-07 18:13:33666TestingProfile* TestingProfile::AsTestingProfile() {
667 return this;
668}
669
isherman91ffd8b2015-02-19 08:51:32670std::string TestingProfile::GetProfileUserName() const {
[email protected]d6001872013-09-24 18:31:26671 return profile_name_;
[email protected]29d70252011-04-28 02:16:58672}
673
[email protected]7b7e0b32014-03-17 16:09:23674Profile::ProfileType TestingProfile::GetProfileType() const {
675 if (guest_session_)
676 return GUEST_PROFILE;
tapted1ab9b7092014-09-15 23:49:40677 if (force_incognito_ || original_profile_)
[email protected]7b7e0b32014-03-17 16:09:23678 return INCOGNITO_PROFILE;
679 return REGULAR_PROFILE;
680}
681
[email protected]27d6e852012-03-02 21:31:32682bool TestingProfile::IsOffTheRecord() const {
tapted1ab9b7092014-09-15 23:49:40683 return force_incognito_ || original_profile_;
[email protected]fadc607b62011-02-07 17:55:50684}
685
[email protected]9819fd02013-08-22 10:49:39686void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
687 DCHECK(!IsOffTheRecord());
tapted1ab9b7092014-09-15 23:49:40688 DCHECK_EQ(this, profile->GetOriginalProfile());
[email protected]9819fd02013-08-22 10:49:39689 incognito_profile_ = profile.Pass();
[email protected]93dba942011-02-23 22:45:01690}
691
[email protected]fadc607b62011-02-07 17:55:50692Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]9819fd02013-08-22 10:49:39693 if (IsOffTheRecord())
694 return this;
tapted1ab9b7092014-09-15 23:49:40695 if (!incognito_profile_)
696 TestingProfile::Builder().BuildIncognito(this);
[email protected]5bb2f522011-03-25 19:04:44697 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50698}
699
700bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44701 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50702}
703
704Profile* TestingProfile::GetOriginalProfile() {
[email protected]074311a2013-02-28 23:14:09705 if (original_profile_)
706 return original_profile_;
[email protected]fadc607b62011-02-07 17:55:50707 return this;
708}
709
treib9e30e302015-04-15 08:12:16710void TestingProfile::SetSupervisedUserId(const std::string& id) {
711 supervised_user_id_ = id;
712 if (!id.empty())
713 GetPrefs()->SetString(prefs::kSupervisedUserId, id);
714 else
715 GetPrefs()->ClearPref(prefs::kSupervisedUserId);
716}
717
[email protected]d20d0432014-06-12 17:14:05718bool TestingProfile::IsSupervised() {
719 return !supervised_user_id_.empty();
[email protected]e000daf2013-07-31 16:50:58720}
721
treib3c8cf132014-12-08 15:09:32722bool TestingProfile::IsChild() {
723#if defined(ENABLE_SUPERVISED_USERS)
724 return supervised_user_id_ == supervised_users::kChildAccountSUID;
725#else
merkulovae19a3522014-12-04 11:14:06726 return false;
treib3c8cf132014-12-08 15:09:32727#endif
728}
729
730bool TestingProfile::IsLegacySupervised() {
731 return IsSupervised() && !IsChild();
merkulovae19a3522014-12-04 11:14:06732}
733
[email protected]50356f82014-07-31 13:25:18734#if defined(ENABLE_EXTENSIONS)
[email protected]2261e7b72011-07-12 13:43:55735void TestingProfile::SetExtensionSpecialStoragePolicy(
736 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
737 extension_special_storage_policy_ = extension_special_storage_policy;
738}
[email protected]50356f82014-07-31 13:25:18739#endif
[email protected]2261e7b72011-07-12 13:43:55740
[email protected]19eb80152011-02-26 00:28:43741ExtensionSpecialStoragePolicy*
742TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18743#if defined(ENABLE_EXTENSIONS)
[email protected]cdba46992011-06-07 11:51:39744 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05745 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39746 return extension_special_storage_policy_.get();
[email protected]50356f82014-07-31 13:25:18747#else
748 return NULL;
749#endif
[email protected]19eb80152011-02-26 00:28:43750}
751
[email protected]33ad6ce92013-08-27 14:39:08752net::CookieMonster* TestingProfile::GetCookieMonster() {
753 if (!GetRequestContext())
754 return NULL;
755 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
756 GetCookieMonster();
757}
758
[email protected]2fb7dc982010-09-29 12:24:28759void TestingProfile::CreateTestingPrefService() {
760 DCHECK(!prefs_.get());
[email protected]5b199522012-12-22 17:24:44761 testing_prefs_ = new TestingPrefServiceSyncable();
[email protected]2fb7dc982010-09-29 12:24:28762 prefs_.reset(testing_prefs_);
[email protected]c7141feb2013-06-11 13:00:16763 user_prefs::UserPrefs::Set(this, prefs_.get());
[email protected]37ca3fe02013-07-05 15:32:44764 chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
[email protected]2fb7dc982010-09-29 12:24:28765}
766
tapted1ab9b7092014-09-15 23:49:40767void TestingProfile::CreateIncognitoPrefService() {
768 DCHECK(original_profile_);
769 DCHECK(!testing_prefs_);
770 // Simplified version of ProfileImpl::GetOffTheRecordPrefs(). Note this
771 // leaves testing_prefs_ unset.
772 prefs_.reset(original_profile_->prefs_->CreateIncognitoPrefService(NULL));
773 user_prefs::UserPrefs::Set(this, prefs_.get());
774}
775
[email protected]d81bb3e2013-05-02 18:17:03776void TestingProfile::CreateProfilePolicyConnector() {
[email protected]d81bb3e2013-05-02 18:17:03777#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24778 schema_registry_service_ =
779 policy::SchemaRegistryServiceFactory::CreateForContext(
780 this, policy::Schema(), NULL);
781 CHECK_EQ(schema_registry_service_.get(),
782 policy::SchemaRegistryServiceFactory::GetForContext(this));
[email protected]a43b9a82013-11-28 16:56:41783#endif // defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24784
[email protected]a43b9a82013-11-28 16:56:41785if (!policy_service_) {
786#if defined(ENABLE_CONFIGURATION_POLICY)
787 std::vector<policy::ConfigurationPolicyProvider*> providers;
[email protected]33528402013-12-16 18:38:32788 policy_service_.reset(new policy::PolicyServiceImpl(providers));
[email protected]d81bb3e2013-05-02 18:17:03789#else
[email protected]a43b9a82013-11-28 16:56:41790 policy_service_.reset(new policy::PolicyServiceStub());
[email protected]d81bb3e2013-05-02 18:17:03791#endif
[email protected]a43b9a82013-11-28 16:56:41792 }
[email protected]24a9f1c92013-11-13 12:33:37793 profile_policy_connector_.reset(new policy::ProfilePolicyConnector());
[email protected]a43b9a82013-11-28 16:56:41794 profile_policy_connector_->InitForTesting(policy_service_.Pass());
[email protected]d81bb3e2013-05-02 18:17:03795 policy::ProfilePolicyConnectorFactory::GetInstance()->SetServiceForTesting(
796 this, profile_policy_connector_.get());
797 CHECK_EQ(profile_policy_connector_.get(),
pneubeck91534e82015-02-18 17:37:52798 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(this));
[email protected]d81bb3e2013-05-02 18:17:03799}
800
[email protected]c753f142013-02-10 13:14:04801PrefService* TestingProfile::GetPrefs() {
tapted1ab9b7092014-09-15 23:49:40802 DCHECK(prefs_);
[email protected]dbbad7a2010-08-13 18:18:36803 return prefs_.get();
804}
805
isherman91ffd8b2015-02-19 08:51:32806const PrefService* TestingProfile::GetPrefs() const {
807 DCHECK(prefs_);
808 return prefs_.get();
809}
810
a.sarkar.arundaadc712015-02-26 05:39:08811chrome::ChromeZoomLevelPrefs* TestingProfile::GetZoomLevelPrefs() {
812 return static_cast<chrome::ChromeZoomLevelPrefs*>(
813 GetDefaultStoragePartition(this)->GetZoomLevelDelegate());
814}
815
[email protected]b441a8492012-06-06 14:55:57816DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50817 return NULL;
818}
819
[email protected]abe2c032011-03-31 18:49:34820net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]37ac95b2013-07-23 23:39:35821 return GetDefaultStoragePartition(this)->GetURLRequestContext();
[email protected]812b3a32010-01-08 05:36:04822}
823
[email protected]6bd30072013-02-08 18:17:11824net::URLRequestContextGetter* TestingProfile::CreateRequestContext(
[email protected]7571263c2014-03-10 22:57:09825 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15826 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]37ac95b2013-07-23 23:39:35827 return new net::TestURLRequestContextGetter(
828 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]6bd30072013-02-08 18:17:11829}
830
[email protected]da5683db2011-04-23 17:12:21831net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
832 int renderer_child_id) {
[email protected]fb29e6cf2012-07-12 21:27:20833 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
834 renderer_child_id);
[email protected]1bc28312012-11-08 08:31:53835 return rph->GetStoragePartition()->GetURLRequestContext();
[email protected]d9696672011-03-15 22:45:09836}
837
[email protected]10705a7b2012-08-21 19:07:08838net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
839 return NULL;
840}
841
842net::URLRequestContextGetter*
843TestingProfile::GetMediaRequestContextForRenderProcess(
844 int renderer_child_id) {
[email protected]fadc607b62011-02-07 17:55:50845 return NULL;
846}
847
[email protected]10eb28162012-09-18 03:04:09848net::URLRequestContextGetter*
849TestingProfile::GetMediaRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40850 const base::FilePath& partition_path,
[email protected]27ddfed22012-10-30 23:22:43851 bool in_memory) {
[email protected]10eb28162012-09-18 03:04:09852 return NULL;
853}
854
[email protected]33ad6ce92013-08-27 14:39:08855net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
856 if (!extensions_request_context_.get())
857 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
858 return extensions_request_context_.get();
859}
860
[email protected]fadc607b62011-02-07 17:55:50861net::SSLConfigService* TestingProfile::GetSSLConfigService() {
[email protected]dc6655b2012-12-14 05:58:24862 if (!GetRequestContext())
863 return NULL;
864 return GetRequestContext()->GetURLRequestContext()->ssl_config_service();
[email protected]fadc607b62011-02-07 17:55:50865}
866
[email protected]10eb28162012-09-18 03:04:09867net::URLRequestContextGetter*
[email protected]6bd30072013-02-08 18:17:11868TestingProfile::CreateRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40869 const base::FilePath& partition_path,
[email protected]6bd30072013-02-08 18:17:11870 bool in_memory,
[email protected]7571263c2014-03-10 22:57:09871 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15872 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]10eb28162012-09-18 03:04:09873 // We don't test storage partitions here yet, so returning the same dummy
[email protected]d9696672011-03-15 22:45:09874 // context is sufficient for now.
875 return GetRequestContext();
876}
877
[email protected]df02aca2012-02-09 21:03:20878content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]37ac95b2013-07-23 23:39:35879 if (!resource_context_)
880 resource_context_ = new content::MockResourceContext();
881 return resource_context_;
[email protected]e89b77d2011-04-15 18:58:10882}
883
[email protected]dbbad7a2010-08-13 18:18:36884HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
mukaia1aa3512014-10-22 20:14:11885 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
[email protected]35552dc52011-07-12 09:04:38886 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05887 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
[email protected]701550ad2012-09-06 23:43:29888#if defined(ENABLE_EXTENSIONS)
[email protected]994fdb252014-06-26 13:26:37889 ExtensionService* extension_service =
890 extensions::ExtensionSystem::Get(this)->extension_service();
mukai87a8d402014-09-15 20:15:09891 if (extension_service) {
892 extension_service->RegisterContentSettings(
893 host_content_settings_map_.get());
894 }
[email protected]701550ad2012-09-06 23:43:29895#endif
[email protected]35552dc52011-07-12 09:04:38896 }
[email protected]dbbad7a2010-08-13 18:18:36897 return host_content_settings_map_.get();
898}
899
[email protected]139355f2014-05-11 14:21:28900content::BrowserPluginGuestManager* TestingProfile::GetGuestManager() {
[email protected]1df3d972014-06-11 04:55:18901#if defined(ENABLE_EXTENSIONS)
fsamuel8dfa19a2015-05-05 01:00:39902 return guest_view::GuestViewManager::FromBrowserContext(this);
[email protected]1df3d972014-06-11 04:55:18903#else
904 return NULL;
905#endif
[email protected]24569262014-05-06 03:31:30906}
907
[email protected]c5c89d042014-06-13 14:43:37908content::PushMessagingService* TestingProfile::GetPushMessagingService() {
909 return NULL;
910}
911
[email protected]fadc607b62011-02-07 17:55:50912bool TestingProfile::IsSameProfile(Profile *p) {
913 return this == p;
914}
915
916base::Time TestingProfile::GetStartTime() const {
917 return start_time_;
918}
919
[email protected]152ea302013-02-11 04:08:40920base::FilePath TestingProfile::last_selected_directory() {
[email protected]fadc607b62011-02-07 17:55:50921 return last_selected_directory_;
922}
923
[email protected]152ea302013-02-11 04:08:40924void TestingProfile::set_last_selected_directory(const base::FilePath& path) {
[email protected]fadc607b62011-02-07 17:55:50925 last_selected_directory_ = path;
926}
927
[email protected]cc5bfd42010-11-24 14:44:02928PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07929 if (!pref_proxy_config_tracker_.get()) {
[email protected]e2930d0902013-07-17 05:25:42930 // TestingProfile is used in unit tests, where local state is not available.
[email protected]6f96cbcb2011-11-04 02:26:07931 pref_proxy_config_tracker_.reset(
[email protected]e2930d0902013-07-17 05:25:42932 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(GetPrefs(),
933 NULL));
[email protected]6f96cbcb2011-11-04 02:26:07934 }
935 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02936}
937
[email protected]0bfc29a2009-04-27 16:15:44938void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
sdefresnec62efa32015-03-12 22:46:31939 history::HistoryService* history_service =
940 HistoryServiceFactory::GetForProfile(this,
941 ServiceAccessType::EXPLICIT_ACCESS);
[email protected]a5894fe2012-11-01 14:40:56942 DCHECK(history_service);
[email protected]bb024fe2013-05-10 21:33:26943 DCHECK(base::MessageLoop::current());
[email protected]0bfc29a2009-04-27 16:15:44944
[email protected]58bef622014-07-03 11:41:14945 base::CancelableTaskTracker tracker;
[email protected]6a0b6672014-07-29 12:00:23946 history_service->ScheduleDBTask(
947 scoped_ptr<history::HistoryDBTask>(
948 new QuittingHistoryDBTask()),
949 &tracker);
[email protected]bb024fe2013-05-10 21:33:26950 base::MessageLoop::current()->Run();
[email protected]0bfc29a2009-04-27 16:15:44951}
[email protected]345a8b72009-09-29 09:11:44952
[email protected]8382d8c2011-09-15 03:43:17953chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
954 return NULL;
955}
956
[email protected]a4205202014-06-02 16:03:08957DevToolsNetworkController* TestingProfile::GetDevToolsNetworkController() {
958 return NULL;
959}
960
[email protected]e0e1fc22012-11-05 20:25:36961void TestingProfile::ClearNetworkingHistorySince(
962 base::Time time,
963 const base::Closure& completion) {
964 if (!completion.is_null()) {
965 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
966 }
[email protected]02896a82011-09-21 18:54:32967}
968
[email protected]671f9c62011-10-28 19:22:07969GURL TestingProfile::GetHomePage() {
970 return GURL(chrome::kChromeUINewTabURL);
971}
972
[email protected]c753f142013-02-10 13:14:04973PrefService* TestingProfile::GetOffTheRecordPrefs() {
[email protected]fadc607b62011-02-07 17:55:50974 return NULL;
975}
976
[email protected]cd501a72014-08-22 19:58:31977storage::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18978#if defined(ENABLE_EXTENSIONS)
[email protected]36fb2c7c2011-04-04 15:49:08979 return GetExtensionSpecialStoragePolicy();
[email protected]50356f82014-07-31 13:25:18980#else
981 return NULL;
982#endif
[email protected]36fb2c7c2011-04-04 15:49:08983}
984
[email protected]c5bbe0e2014-08-01 23:23:30985content::SSLHostStateDelegate* TestingProfile::GetSSLHostStateDelegate() {
986 return NULL;
987}
988
mlamouri4e372022015-03-29 14:51:06989content::PermissionManager* TestingProfile::GetPermissionManager() {
990 return NULL;
991}
992
[email protected]1bee6ed2012-03-14 06:46:36993bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
994 return true;
995}
[email protected]ef92e172012-04-25 19:40:41996
[email protected]4251165a2013-07-17 04:33:40997bool TestingProfile::IsGuestSession() const {
[email protected]e9476cd2013-12-04 05:55:34998 return guest_session_;
[email protected]4251165a2013-07-17 04:33:40999}
[email protected]1101dbc52013-10-05 00:19:121000
[email protected]6c0ca7fc2012-10-05 16:27:221001Profile::ExitType TestingProfile::GetLastSessionExitType() {
1002 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED;
[email protected]2ca1ea662012-10-04 02:26:361003}
1004
[email protected]5c4c89f2012-08-07 21:09:591005TestingProfile::Builder::Builder()
1006 : build_called_(false),
[email protected]9819fd02013-08-22 10:49:391007 delegate_(NULL),
[email protected]e9476cd2013-12-04 05:55:341008 guest_session_(false) {
[email protected]5c4c89f2012-08-07 21:09:591009}
1010
1011TestingProfile::Builder::~Builder() {
1012}
1013
[email protected]152ea302013-02-11 04:08:401014void TestingProfile::Builder::SetPath(const base::FilePath& path) {
[email protected]5c4c89f2012-08-07 21:09:591015 path_ = path;
1016}
1017
1018void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
1019 delegate_ = delegate;
1020}
1021
[email protected]50356f82014-07-31 13:25:181022#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:591023void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
1024 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
1025 extension_policy_ = policy;
1026}
[email protected]50356f82014-07-31 13:25:181027#endif
[email protected]5c4c89f2012-08-07 21:09:591028
[email protected]5b199522012-12-22 17:24:441029void TestingProfile::Builder::SetPrefService(
1030 scoped_ptr<PrefServiceSyncable> prefs) {
[email protected]5c4c89f2012-08-07 21:09:591031 pref_service_ = prefs.Pass();
1032}
1033
[email protected]e9476cd2013-12-04 05:55:341034void TestingProfile::Builder::SetGuestSession() {
1035 guest_session_ = true;
1036}
1037
[email protected]d20d0432014-06-12 17:14:051038void TestingProfile::Builder::SetSupervisedUserId(
1039 const std::string& supervised_user_id) {
1040 supervised_user_id_ = supervised_user_id;
[email protected]371662e372013-10-17 22:05:221041}
1042
[email protected]a43b9a82013-11-28 16:56:411043void TestingProfile::Builder::SetPolicyService(
1044 scoped_ptr<policy::PolicyService> policy_service) {
1045 policy_service_ = policy_service.Pass();
1046}
1047
[email protected]9819fd02013-08-22 10:49:391048void TestingProfile::Builder::AddTestingFactory(
1049 BrowserContextKeyedServiceFactory* service_factory,
[email protected]d45c5d52013-12-04 23:41:161050 BrowserContextKeyedServiceFactory::TestingFactoryFunction callback) {
[email protected]9819fd02013-08-22 10:49:391051 testing_factories_.push_back(std::make_pair(service_factory, callback));
1052}
1053
[email protected]5c4c89f2012-08-07 21:09:591054scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
1055 DCHECK(!build_called_);
1056 build_called_ = true;
[email protected]371662e372013-10-17 22:05:221057
tapted1ab9b7092014-09-15 23:49:401058 return scoped_ptr<TestingProfile>(new TestingProfile(path_,
1059 delegate_,
[email protected]50356f82014-07-31 13:25:181060#if defined(ENABLE_EXTENSIONS)
tapted1ab9b7092014-09-15 23:49:401061 extension_policy_,
[email protected]50356f82014-07-31 13:25:181062#endif
tapted1ab9b7092014-09-15 23:49:401063 pref_service_.Pass(),
1064 NULL,
1065 guest_session_,
1066 supervised_user_id_,
1067 policy_service_.Pass(),
1068 testing_factories_));
1069}
1070
1071TestingProfile* TestingProfile::Builder::BuildIncognito(
1072 TestingProfile* original_profile) {
1073 DCHECK(!build_called_);
1074 DCHECK(original_profile);
1075 build_called_ = true;
1076
1077 // Note: Owned by |original_profile|.
1078 return new TestingProfile(path_,
1079 delegate_,
1080#if defined(ENABLE_EXTENSIONS)
1081 extension_policy_,
1082#endif
1083 pref_service_.Pass(),
1084 original_profile,
1085 guest_session_,
1086 supervised_user_id_,
1087 policy_service_.Pass(),
1088 testing_factories_);
[email protected]5c4c89f2012-08-07 21:09:591089}