blob: be0e04fb75556eaa024b35c21bfea2cb0b5a753a [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"
[email protected]f61f4782012-06-08 21:54:2116#include "chrome/browser/bookmarks/bookmark_model_factory.h"
[email protected]6a848b52014-04-26 22:06:5417#include "chrome/browser/bookmarks/chrome_bookmark_client.h"
[email protected]0b0e8a12014-06-14 00:11:3118#include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
[email protected]1459fb62011-05-25 19:03:2719#include "chrome/browser/browser_process.h"
[email protected]81d49782013-07-12 01:50:5220#include "chrome/browser/chrome_notification_types.h"
[email protected]9d01a6a2010-11-30 12:03:3321#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]0e199ed72014-07-04 13:05:2222#include "chrome/browser/favicon/chrome_favicon_client_factory.h"
[email protected]ee2ed42c2011-04-28 22:19:1423#include "chrome/browser/favicon/favicon_service.h"
[email protected]f3d2b312012-08-23 22:27:5924#include "chrome/browser/favicon/favicon_service_factory.h"
[email protected]ad34610c2014-06-04 14:43:5225#include "chrome/browser/history/chrome_history_client.h"
26#include "chrome/browser/history/chrome_history_client_factory.h"
[email protected]075ae732009-02-11 23:58:3127#include "chrome/browser/history/history_backend.h"
[email protected]d891f522013-02-08 03:24:4128#include "chrome/browser/history/history_db_task.h"
[email protected]6a2c09f2013-01-25 04:50:0729#include "chrome/browser/history/history_service.h"
[email protected]f61f4782012-06-08 21:54:2130#include "chrome/browser/history/history_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3631#include "chrome/browser/history/top_sites.h"
[email protected]a50e16a2013-04-25 14:07:1732#include "chrome/browser/history/web_history_service_factory.h"
[email protected]53c5b0f2013-07-22 10:54:1133#include "chrome/browser/net/pref_proxy_config_tracker.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]d81bb3e2013-05-02 18:17:0337#include "chrome/browser/policy/profile_policy_connector.h"
38#include "chrome/browser/policy/profile_policy_connector_factory.h"
[email protected]37858e52010-08-26 00:22:0239#include "chrome/browser/prefs/browser_prefs.h"
[email protected]c753f142013-02-10 13:14:0440#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]4850a7f2011-03-08 23:36:5941#include "chrome/browser/prerender/prerender_manager.h"
[email protected]7e75e4a2013-05-17 17:20:0342#include "chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.h"
[email protected]371662e372013-10-17 22:05:2243#include "chrome/browser/profiles/profile_manager.h"
[email protected]27ddfed22012-10-30 23:22:4344#include "chrome/browser/profiles/storage_partition_descriptor.h"
[email protected]16bd088b2012-04-03 08:12:1245#include "chrome/browser/search_engines/template_url_fetcher_factory.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"
[email protected]af44e7fb2011-07-29 18:32:3253#include "chrome/test/base/ui_test_utils.h"
[email protected]a90c8ca2014-05-20 17:16:0454#include "components/bookmarks/browser/bookmark_model.h"
55#include "components/bookmarks/common/bookmark_constants.h"
naiem.shaike091cc32014-09-18 02:20:5256#include "components/history/core/browser/top_sites_observer.h"
[email protected]540380fc2014-03-14 10:10:3457#include "components/keyed_service/content/browser_context_dependency_manager.h"
[email protected]76b4b152013-12-08 21:10:0458#include "components/policy/core/common/policy_service.h"
[email protected]75fee372013-03-06 00:42:4459#include "components/user_prefs/user_prefs.h"
[email protected]c38831a12011-10-28 12:44:4960#include "content/public/browser/browser_thread.h"
[email protected]7c4b66b2014-01-04 12:28:1361#include "content/public/browser/cookie_store_factory.h"
[email protected]ad50def52011-10-19 23:17:0762#include "content/public/browser/notification_service.h"
[email protected]fb29e6cf2012-07-12 21:27:2063#include "content/public/browser/render_process_host.h"
[email protected]885c0e92012-11-13 20:27:4264#include "content/public/browser/storage_partition.h"
[email protected]08a932d52012-06-03 21:42:1265#include "content/public/test/mock_resource_context.h"
[email protected]a7fe9112012-07-20 02:34:4566#include "content/public/test/test_utils.h"
[email protected]885c0e92012-11-13 20:27:4267#include "extensions/common/constants.h"
[email protected]aa84a7e2012-03-15 21:29:0668#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0469#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3470#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4471#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4672#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3473
[email protected]21d3a882012-05-31 14:41:5574#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:2475#include "chrome/browser/policy/schema_registry_service.h"
76#include "chrome/browser/policy/schema_registry_service_factory.h"
[email protected]f20a3a22013-12-03 16:12:3777#include "components/policy/core/common/configuration_policy_provider.h"
[email protected]76b4b152013-12-08 21:10:0478#include "components/policy/core/common/policy_service_impl.h"
[email protected]eb83c392013-11-07 12:53:2479#include "components/policy/core/common/schema.h"
[email protected]21d3a882012-05-31 14:41:5580#else
[email protected]76b4b152013-12-08 21:10:0481#include "components/policy/core/common/policy_service_stub.h"
[email protected]21d3a882012-05-31 14:41:5582#endif // defined(ENABLE_CONFIGURATION_POLICY)
83
[email protected]1df3d972014-06-11 04:55:1884#if defined(ENABLE_EXTENSIONS)
thestig44b9ce22014-10-17 03:19:2885#include "chrome/browser/extensions/extension_service.h"
[email protected]50356f82014-07-31 13:25:1886#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]d5a74be2014-08-08 01:01:2787#include "chrome/browser/extensions/extension_system_factory.h"
88#include "chrome/browser/extensions/test_extension_system.h"
[email protected]994fdb252014-06-26 13:26:3789#include "extensions/browser/extension_system.h"
[email protected]140d6cd92014-08-12 18:26:4690#include "extensions/browser/guest_view/guest_view_manager.h"
[email protected]1df3d972014-06-11 04:55:1891#endif
92
[email protected]bd3093c2014-06-13 07:38:1693#if defined(OS_ANDROID)
94#include "chrome/browser/signin/android_profile_oauth2_token_service.h"
95#endif
96
[email protected]e1480482013-09-11 11:49:5897#if defined(ENABLE_MANAGED_USERS)
[email protected]cce15bb2014-06-17 13:43:5198#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
99#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]e1480482013-09-11 11:49:58100#endif
101
[email protected]e1acf6f2008-10-27 20:43:33102using base::Time;
[email protected]631bb742011-11-02 11:29:39103using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:57104using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:23105using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:46106using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:33107
[email protected]d364c652008-08-29 19:46:56108namespace {
109
naiem.shaike091cc32014-09-18 02:20:52110// Used to make sure TopSites has finished loading
111class WaitTopSitesLoadedObserver : public history::TopSitesObserver {
112 public:
113 explicit WaitTopSitesLoadedObserver(content::MessageLoopRunner* runner)
114 : runner_(runner) {}
dchenge59c1bb92014-10-21 11:55:49115 void TopSitesLoaded(history::TopSites* top_sites) override {
naiem.shaike091cc32014-09-18 02:20:52116 runner_->Quit();
117 }
dchenge59c1bb92014-10-21 11:55:49118 void TopSitesChanged(history::TopSites* top_sites) override {}
naiem.shaike091cc32014-09-18 02:20:52119
120 private:
121 // weak
122 content::MessageLoopRunner* runner_;
123};
124
[email protected]0bfc29a2009-04-27 16:15:44125// Task used to make sure history has finished processing a request. Intended
126// for use with BlockUntilHistoryProcessesPendingRequests.
127
[email protected]d891f522013-02-08 03:24:41128class QuittingHistoryDBTask : public history::HistoryDBTask {
[email protected]0bfc29a2009-04-27 16:15:44129 public:
130 QuittingHistoryDBTask() {}
131
dchenge59c1bb92014-10-21 11:55:49132 bool RunOnDBThread(history::HistoryBackend* backend,
133 history::HistoryDatabase* db) override {
[email protected]0bfc29a2009-04-27 16:15:44134 return true;
135 }
136
dchenge59c1bb92014-10-21 11:55:49137 void DoneRunOnMainThread() override { base::MessageLoop::current()->Quit(); }
[email protected]0bfc29a2009-04-27 16:15:44138
139 private:
dchenge59c1bb92014-10-21 11:55:49140 ~QuittingHistoryDBTask() override {}
[email protected]7991a232009-11-06 01:55:48141
[email protected]0bfc29a2009-04-27 16:15:44142 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
143};
144
[email protected]33ad6ce92013-08-27 14:39:08145class TestExtensionURLRequestContext : public net::URLRequestContext {
146 public:
147 TestExtensionURLRequestContext() {
[email protected]7c4b66b2014-01-04 12:28:13148 net::CookieMonster* cookie_monster =
[email protected]9a6c2aa2014-01-11 22:39:39149 content::CreateCookieStore(content::CookieStoreConfig())->
150 GetCookieMonster();
[email protected]5edff3c52014-06-23 20:27:48151 const char* const schemes[] = {extensions::kExtensionScheme};
152 cookie_monster->SetCookieableSchemes(schemes, arraysize(schemes));
[email protected]33ad6ce92013-08-27 14:39:08153 set_cookie_store(cookie_monster);
154 }
155
dchenge59c1bb92014-10-21 11:55:49156 ~TestExtensionURLRequestContext() override { AssertNoURLRequests(); }
[email protected]33ad6ce92013-08-27 14:39:08157};
158
159class TestExtensionURLRequestContextGetter
160 : public net::URLRequestContextGetter {
161 public:
dchenge59c1bb92014-10-21 11:55:49162 net::URLRequestContext* GetURLRequestContext() override {
[email protected]33ad6ce92013-08-27 14:39:08163 if (!context_.get())
164 context_.reset(new TestExtensionURLRequestContext());
165 return context_.get();
166 }
dchenge59c1bb92014-10-21 11:55:49167 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
168 const override {
[email protected]33ad6ce92013-08-27 14:39:08169 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
170 }
171
172 protected:
dchenge59c1bb92014-10-21 11:55:49173 ~TestExtensionURLRequestContextGetter() override {}
[email protected]33ad6ce92013-08-27 14:39:08174
175 private:
176 scoped_ptr<net::URLRequestContext> context_;
177};
178
[email protected]1d51882f2013-11-12 01:59:02179#if defined(ENABLE_NOTIFICATIONS)
[email protected]540380fc2014-03-14 10:10:34180KeyedService* CreateTestDesktopNotificationService(
[email protected]c7fa4362013-04-26 18:09:02181 content::BrowserContext* profile) {
peterb1f28c8f2014-09-11 17:30:11182 return new DesktopNotificationService(static_cast<Profile*>(profile));
[email protected]dcb72d52011-04-13 12:36:53183}
[email protected]1d51882f2013-11-12 01:59:02184#endif
[email protected]dcb72d52011-04-13 12:36:53185
[email protected]d364c652008-08-29 19:46:56186} // namespace
187
[email protected]89dbb1772012-07-17 13:47:25188// static
189#if defined(OS_CHROMEOS)
190// Must be kept in sync with
191// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
[email protected]0f13fe8d2013-11-19 08:25:00192const char TestingProfile::kTestUserProfileDir[] = "test-user";
[email protected]89dbb1772012-07-17 13:47:25193#else
194const char TestingProfile::kTestUserProfileDir[] = "Default";
195#endif
196
[email protected]f25387b2008-08-21 15:20:33197TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55198 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28199 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39200 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09201 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34202 guest_session_(false),
[email protected]5cdcd152011-03-31 22:48:52203 last_session_exited_cleanly_(true),
[email protected]f0901a22013-05-27 21:37:53204 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44205 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35206 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17207 delegate_(NULL) {
[email protected]5c4c89f2012-08-07 21:09:59208 CreateTempProfileDir();
[email protected]49a25632011-08-31 17:03:48209 profile_path_ = temp_dir_.path();
210
211 Init();
[email protected]537c1082011-12-02 02:37:17212 FinishInit();
[email protected]49a25632011-08-31 17:03:48213}
214
[email protected]152ea302013-02-11 04:08:40215TestingProfile::TestingProfile(const base::FilePath& path)
[email protected]49a25632011-08-31 17:03:48216 : start_time_(Time::Now()),
217 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39218 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09219 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34220 guest_session_(false),
[email protected]49a25632011-08-31 17:03:48221 last_session_exited_cleanly_(true),
222 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53223 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44224 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35225 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17226 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48227 Init();
[email protected]537c1082011-12-02 02:37:17228 FinishInit();
229}
230
[email protected]152ea302013-02-11 04:08:40231TestingProfile::TestingProfile(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:17232 Delegate* delegate)
233 : start_time_(Time::Now()),
234 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39235 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09236 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34237 guest_session_(false),
[email protected]537c1082011-12-02 02:37:17238 last_session_exited_cleanly_(true),
239 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53240 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44241 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35242 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17243 delegate_(delegate) {
244 Init();
245 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26246 base::MessageLoop::current()->PostTask(
247 FROM_HERE,
248 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]537c1082011-12-02 02:37:17249 } else {
250 FinishInit();
251 }
[email protected]49a25632011-08-31 17:03:48252}
253
[email protected]5c4c89f2012-08-07 21:09:59254TestingProfile::TestingProfile(
[email protected]152ea302013-02-11 04:08:40255 const base::FilePath& path,
[email protected]5c4c89f2012-08-07 21:09:59256 Delegate* delegate,
[email protected]50356f82014-07-31 13:25:18257#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59258 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
[email protected]50356f82014-07-31 13:25:18259#endif
[email protected]9819fd02013-08-22 10:49:39260 scoped_ptr<PrefServiceSyncable> prefs,
tapted1ab9b7092014-09-15 23:49:40261 TestingProfile* parent,
[email protected]e9476cd2013-12-04 05:55:34262 bool guest_session,
[email protected]d20d0432014-06-12 17:14:05263 const std::string& supervised_user_id,
[email protected]a43b9a82013-11-28 16:56:41264 scoped_ptr<policy::PolicyService> policy_service,
[email protected]9819fd02013-08-22 10:49:39265 const TestingFactories& factories)
[email protected]5c4c89f2012-08-07 21:09:59266 : start_time_(Time::Now()),
267 prefs_(prefs.release()),
268 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39269 force_incognito_(false),
tapted1ab9b7092014-09-15 23:49:40270 original_profile_(parent),
[email protected]e9476cd2013-12-04 05:55:34271 guest_session_(guest_session),
[email protected]d20d0432014-06-12 17:14:05272 supervised_user_id_(supervised_user_id),
[email protected]5c4c89f2012-08-07 21:09:59273 last_session_exited_cleanly_(true),
[email protected]50356f82014-07-31 13:25:18274#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59275 extension_special_storage_policy_(extension_policy),
[email protected]50356f82014-07-31 13:25:18276#endif
[email protected]5c4c89f2012-08-07 21:09:59277 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53278 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44279 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35280 resource_context_(NULL),
[email protected]a43b9a82013-11-28 16:56:41281 delegate_(delegate),
282 policy_service_(policy_service.release()) {
tapted1ab9b7092014-09-15 23:49:40283 if (parent)
284 parent->SetOffTheRecordProfile(scoped_ptr<Profile>(this));
285
[email protected]5c4c89f2012-08-07 21:09:59286 // If no profile path was supplied, create one.
287 if (profile_path_.empty()) {
288 CreateTempProfileDir();
289 profile_path_ = temp_dir_.path();
290 }
291
[email protected]9819fd02013-08-22 10:49:39292 // Set any testing factories prior to initializing the services.
293 for (TestingFactories::const_iterator it = factories.begin();
294 it != factories.end(); ++it) {
295 it->first->SetTestingFactory(this, it->second);
296 }
297
[email protected]5c4c89f2012-08-07 21:09:59298 Init();
299 // If caller supplied a delegate, delay the FinishInit invocation until other
300 // tasks have run.
301 // TODO(atwilson): See if this is still required once we convert the current
302 // users of the constructor that takes a Delegate* param.
303 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26304 base::MessageLoop::current()->PostTask(
305 FROM_HERE,
306 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]5c4c89f2012-08-07 21:09:59307 } else {
308 FinishInit();
309 }
310}
311
312void TestingProfile::CreateTempProfileDir() {
313 if (!temp_dir_.CreateUniqueTempDir()) {
314 LOG(ERROR) << "Failed to create unique temporary directory.";
315
316 // Fallback logic in case we fail to create unique temporary directory.
[email protected]152ea302013-02-11 04:08:40317 base::FilePath system_tmp_dir;
[email protected]5c4c89f2012-08-07 21:09:59318 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
319
320 // We're severly screwed if we can't get the system temporary
321 // directory. Die now to avoid writing to the filesystem root
322 // or other bad places.
323 CHECK(success);
324
[email protected]152ea302013-02-11 04:08:40325 base::FilePath fallback_dir(
326 system_tmp_dir.AppendASCII("TestingProfilePath"));
[email protected]dd3aa792013-07-16 19:10:23327 base::DeleteFile(fallback_dir, true);
[email protected]426d1c92013-12-03 20:08:54328 base::CreateDirectory(fallback_dir);
[email protected]5c4c89f2012-08-07 21:09:59329 if (!temp_dir_.Set(fallback_dir)) {
330 // That shouldn't happen, but if it does, try to recover.
331 LOG(ERROR) << "Failed to use a fallback temporary directory.";
332
333 // We're screwed if this fails, see CHECK above.
334 CHECK(temp_dir_.Set(system_tmp_dir));
335 }
336 }
337}
338
[email protected]49a25632011-08-31 17:03:48339void TestingProfile::Init() {
[email protected]bf390cb2013-08-06 02:48:08340 // If threads have been initialized, we should be on the UI thread.
[email protected]33d06242013-08-12 05:20:30341 DCHECK(!content::BrowserThread::IsThreadInitialized(
342 content::BrowserThread::UI) ||
343 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]bf390cb2013-08-06 02:48:08344
[email protected]bd3093c2014-06-13 07:38:16345#if defined(OS_ANDROID)
346 // Make sure token service knows its running in tests.
347 AndroidProfileOAuth2TokenService::set_is_testing_profile();
348#endif
349
[email protected]7e75e4a2013-05-17 17:20:03350 // Normally this would happen during browser startup, but for tests
351 // we need to trigger creation of Profile-related services.
352 ChromeBrowserMainExtraPartsProfiles::
[email protected]f1484c52013-05-22 23:25:44353 EnsureBrowserContextKeyedServiceFactoriesBuilt();
[email protected]7e75e4a2013-05-17 17:20:03354
[email protected]75fee372013-03-06 00:42:44355 if (prefs_.get())
[email protected]c7141feb2013-06-11 13:00:16356 user_prefs::UserPrefs::Set(this, prefs_.get());
tapted1ab9b7092014-09-15 23:49:40357 else if (IsOffTheRecord())
358 CreateIncognitoPrefService();
[email protected]c7fa4362013-04-26 18:09:02359 else
360 CreateTestingPrefService();
[email protected]75fee372013-03-06 00:42:44361
[email protected]7567484142013-07-11 17:36:07362 if (!base::PathExists(profile_path_))
[email protected]426d1c92013-12-03 20:08:54363 base::CreateDirectory(profile_path_);
[email protected]d41e6572012-05-04 20:49:42364
[email protected]d81bb3e2013-05-02 18:17:03365 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl
366 // anymore, after converting the PrefService to a PKS. Until then it must
367 // be associated with a TestingProfile too.
[email protected]eb83c392013-11-07 12:53:24368 if (!IsOffTheRecord())
369 CreateProfilePolicyConnector();
[email protected]d81bb3e2013-05-02 18:17:03370
[email protected]d5a74be2014-08-08 01:01:27371#if defined(ENABLE_EXTENSIONS)
[email protected]bd306722012-07-11 20:43:59372 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
373 this, extensions::TestExtensionSystem::Build);
[email protected]d5a74be2014-08-08 01:01:27374#endif
[email protected]31d8f5f22012-04-02 15:22:08375
tapted1ab9b7092014-09-15 23:49:40376 // Prefs for incognito profiles are set in CreateIncognitoPrefService() by
377 // simulating ProfileImpl::GetOffTheRecordPrefs().
378 if (!IsOffTheRecord()) {
379 DCHECK(!original_profile_);
[email protected]d45c5d52013-12-04 23:41:16380 user_prefs::PrefRegistrySyncable* pref_registry =
381 static_cast<user_prefs::PrefRegistrySyncable*>(
382 prefs_->DeprecatedGetPrefRegistry());
383 browser_context_dependency_manager_->
384 RegisterProfilePrefsForServices(this, pref_registry);
385 }
386
[email protected]9819fd02013-08-22 10:49:39387 browser_context_dependency_manager_->CreateBrowserContextServicesForTest(
[email protected]d45c5d52013-12-04 23:41:16388 this);
[email protected]49a25632011-08-31 17:03:48389
[email protected]5fd2e842012-03-01 00:29:11390#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53391 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22392 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
393 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11394#endif
[email protected]e1480482013-09-11 11:49:58395
396#if defined(ENABLE_MANAGED_USERS)
tapted1ab9b7092014-09-15 23:49:40397 if (!IsOffTheRecord()) {
398 SupervisedUserSettingsService* settings_service =
399 SupervisedUserSettingsServiceFactory::GetForProfile(this);
400 TestingPrefStore* store = new TestingPrefStore();
401 settings_service->Init(store);
402 store->SetInitializationCompleted();
403 }
[email protected]e1480482013-09-11 11:49:58404#endif
[email protected]d6001872013-09-24 18:31:26405
406 profile_name_ = "testing_profile";
[email protected]537c1082011-12-02 02:37:17407}
[email protected]12463cd2011-07-19 09:40:20408
[email protected]537c1082011-12-02 02:37:17409void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40410 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07411 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20412 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53413 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07414 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17415
[email protected]371662e372013-10-17 22:05:22416 ProfileManager* profile_manager = g_browser_process->profile_manager();
417 if (profile_manager)
418 profile_manager->InitProfileUserPrefs(this);
419
[email protected]537c1082011-12-02 02:37:17420 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03421 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46422}
423
[email protected]f25387b2008-08-21 15:20:33424TestingProfile::~TestingProfile() {
[email protected]9819fd02013-08-22 10:49:39425 // Revert to non-incognito mode before shutdown.
426 force_incognito_ = false;
427
tapted1ab9b7092014-09-15 23:49:40428 // If this profile owns an incognito profile, tear it down first.
429 incognito_profile_.reset();
430
[email protected]37ac95b2013-07-23 23:39:35431 // Any objects holding live URLFetchers should be deleted before teardown.
432 TemplateURLFetcherFactory::ShutdownForProfile(this);
433
[email protected]a7c4c032012-07-27 00:36:32434 MaybeSendDestroyedNotification();
[email protected]5cdcd152011-03-31 22:48:52435
[email protected]f0901a22013-05-27 21:37:53436 browser_context_dependency_manager_->DestroyBrowserContextServices(this);
[email protected]5cdcd152011-03-31 22:48:52437
[email protected]5c6ac842013-06-02 23:37:03438 if (host_content_settings_map_.get())
[email protected]35552dc52011-07-12 09:04:38439 host_content_settings_map_->ShutdownOnUIThread();
440
[email protected]809cc4d2010-10-27 15:22:54441 DestroyTopSites();
[email protected]cdba46992011-06-07 11:51:39442
[email protected]cc5bfd42010-11-24 14:44:02443 if (pref_proxy_config_tracker_.get())
444 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]37ac95b2013-07-23 23:39:35445 // Failing a post == leaks == heapcheck failure. Make that an immediate test
446 // failure.
447 if (resource_context_) {
448 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
449 resource_context_));
450 resource_context_ = NULL;
451 content::RunAllPendingInMessageLoop(BrowserThread::IO);
452 }
[email protected]f25387b2008-08-21 15:20:33453}
initial.commit09911bf2008-07-26 23:55:29454
[email protected]540380fc2014-03-14 10:10:34455static KeyedService* BuildFaviconService(content::BrowserContext* profile) {
[email protected]0e199ed72014-07-04 13:05:22456 FaviconClient* favicon_client =
457 ChromeFaviconClientFactory::GetForProfile(static_cast<Profile*>(profile));
458 return new FaviconService(static_cast<Profile*>(profile), favicon_client);
[email protected]f3d2b312012-08-23 22:27:59459}
460
[email protected]d2879af2010-02-08 16:02:56461void TestingProfile::CreateFaviconService() {
[email protected]f3d2b312012-08-23 22:27:59462 // It is up to the caller to create the history service if one is needed.
463 FaviconServiceFactory::GetInstance()->SetTestingFactory(
464 this, BuildFaviconService);
[email protected]d2879af2010-02-08 16:02:56465}
466
[email protected]ad34610c2014-06-04 14:43:52467static KeyedService* BuildHistoryService(content::BrowserContext* context) {
468 Profile* profile = static_cast<Profile*>(context);
sdefresnec8eb77e2014-10-15 18:15:44469 ChromeHistoryClient* history_client =
470 ChromeHistoryClientFactory::GetForProfile(profile);
471 HistoryService* history_service = new HistoryService(history_client, profile);
472 if (history_client)
473 history_client->SetHistoryService(history_service);
474 return history_service;
[email protected]f61f4782012-06-08 21:54:21475}
476
[email protected]608e7e02013-07-24 12:23:31477bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54478 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33479 if (delete_file) {
[email protected]152ea302013-02-11 04:08:40480 base::FilePath path = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32481 path = path.Append(chrome::kHistoryFilename);
[email protected]608e7e02013-07-24 12:23:31482 if (!base::DeleteFile(path, false) || base::PathExists(path))
483 return false;
[email protected]f25387b2008-08-21 15:20:33484 }
[email protected]7019bb6d2012-08-23 09:09:48485 // This will create and init the history service.
[email protected]f61f4782012-06-08 21:54:21486 HistoryService* history_service = static_cast<HistoryService*>(
487 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
[email protected]a5894fe2012-11-01 14:40:56488 this, BuildHistoryService));
[email protected]ad34610c2014-06-04 14:43:52489 if (!history_service->Init(this->GetPath(), no_db)) {
[email protected]f61f4782012-06-08 21:54:21490 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
[email protected]7019bb6d2012-08-23 09:09:48491 }
[email protected]a50e16a2013-04-25 14:07:17492 // Disable WebHistoryService by default, since it makes network requests.
493 WebHistoryServiceFactory::GetInstance()->SetTestingFactory(this, NULL);
[email protected]608e7e02013-07-24 12:23:31494 return true;
initial.commit09911bf2008-07-26 23:55:29495}
496
497void TestingProfile::DestroyHistoryService() {
sdefresnec8eb77e2014-10-15 18:15:44498 // TODO(sdefresne): remove this once ChromeHistoryClient is no longer an
499 // HistoryServiceObserver, http://crbug.com/373326
500 ChromeHistoryClient* history_client =
501 ChromeHistoryClientFactory::GetForProfileWithoutCreating(this);
502 if (history_client)
503 history_client->Shutdown();
504
[email protected]a5894fe2012-11-01 14:40:56505 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02506 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]a5894fe2012-11-01 14:40:56507 if (!history_service)
initial.commit09911bf2008-07-26 23:55:29508 return;
509
[email protected]378970412014-06-12 07:31:17510 history_service->ClearCachedDataForContextID(0);
[email protected]bb024fe2013-05-10 21:33:26511 history_service->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure());
[email protected]f61f4782012-06-08 21:54:21512 history_service->Cleanup();
513 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29514
515 // Wait for the backend class to terminate before deleting the files and
516 // moving to the next test. Note: if this never terminates, somebody is
517 // probably leaking a reference to the history backend, so it never calls
518 // our destroy task.
[email protected]bb024fe2013-05-10 21:33:26519 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29520
initial.commit09911bf2008-07-26 23:55:29521 // Make sure we don't have any event pending that could disrupt the next
522 // test.
[email protected]bb024fe2013-05-10 21:33:26523 base::MessageLoop::current()->PostTask(FROM_HERE,
524 base::MessageLoop::QuitClosure());
525 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29526}
[email protected]4d0cd7ce2008-08-11 16:40:57527
[email protected]809cc4d2010-10-27 15:22:54528void TestingProfile::CreateTopSites() {
529 DestroyTopSites();
[email protected]9a93d3432013-04-29 20:52:12530 top_sites_ = history::TopSites::Create(
531 this, GetPath().Append(chrome::kTopSitesFilename));
[email protected]809cc4d2010-10-27 15:22:54532}
533
534void TestingProfile::DestroyTopSites() {
535 if (top_sites_.get()) {
536 top_sites_->Shutdown();
537 top_sites_ = NULL;
[email protected]9a93d3432013-04-29 20:52:12538 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that
539 // need to be run to properly shutdown. Run all pending tasks now. This is
[email protected]809cc4d2010-10-27 15:22:54540 // normally handled by browser_process shutdown.
[email protected]bb024fe2013-05-10 21:33:26541 if (base::MessageLoop::current())
542 base::MessageLoop::current()->RunUntilIdle();
[email protected]809cc4d2010-10-27 15:22:54543 }
544}
545
[email protected]540380fc2014-03-14 10:10:34546static KeyedService* BuildBookmarkModel(content::BrowserContext* context) {
[email protected]c7fa4362013-04-26 18:09:02547 Profile* profile = static_cast<Profile*>(context);
[email protected]6a848b52014-04-26 22:06:54548 ChromeBookmarkClient* bookmark_client =
[email protected]0b0e8a12014-06-14 00:11:31549 ChromeBookmarkClientFactory::GetForProfile(profile);
mpearson56bd38e2014-08-27 22:12:57550 BookmarkModel* bookmark_model = new BookmarkModel(bookmark_client);
[email protected]0b0e8a12014-06-14 00:11:31551 bookmark_client->Init(bookmark_model);
552 bookmark_model->Load(profile->GetPrefs(),
553 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
554 profile->GetPath(),
555 profile->GetIOTaskRunner(),
556 content::BrowserThread::GetMessageLoopProxyForThread(
557 content::BrowserThread::UI));
558 return bookmark_model;
559}
560
561static KeyedService* BuildChromeBookmarkClient(
562 content::BrowserContext* context) {
563 return new ChromeBookmarkClient(static_cast<Profile*>(context));
[email protected]f61f4782012-06-08 21:54:21564}
565
[email protected]ad34610c2014-06-04 14:43:52566static KeyedService* BuildChromeHistoryClient(
567 content::BrowserContext* context) {
568 Profile* profile = static_cast<Profile*>(context);
naiem.shaike091cc32014-09-18 02:20:52569 return new ChromeHistoryClient(BookmarkModelFactory::GetForProfile(profile),
570 profile,
571 profile->GetTopSites());
[email protected]ad34610c2014-06-04 14:43:52572}
573
[email protected]d8e41ed2008-09-11 15:22:32574void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46575 if (delete_file) {
[email protected]d4a67d52014-04-12 05:17:03576 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName);
[email protected]dd3aa792013-07-16 19:10:23577 base::DeleteFile(path, false);
[email protected]90ef13132008-08-27 03:27:46578 }
[email protected]ad34610c2014-06-04 14:43:52579 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory(
580 this, BuildChromeHistoryClient);
[email protected]0b0e8a12014-06-14 00:11:31581 ChromeBookmarkClientFactory::GetInstance()->SetTestingFactory(
582 this, BuildChromeBookmarkClient);
583 // This creates the BookmarkModel.
584 ignore_result(BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
585 this, BuildBookmarkModel));
[email protected]4d0cd7ce2008-08-11 16:40:57586}
[email protected]d2c017a2008-08-13 21:51:45587
[email protected]540380fc2014-03-14 10:10:34588static KeyedService* BuildWebDataService(content::BrowserContext* profile) {
[email protected]c7fa4362013-04-26 18:09:02589 return new WebDataServiceWrapper(static_cast<Profile*>(profile));
[email protected]d07edd42012-05-14 23:49:46590}
[email protected]2609bc12010-01-24 08:32:55591
[email protected]d07edd42012-05-14 23:49:46592void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01593 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46594 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55595}
596
[email protected]18a15ca82012-08-30 00:28:16597void TestingProfile::BlockUntilHistoryIndexIsRefreshed() {
598 // Only get the history service if it actually exists since the caller of the
599 // test should explicitly call CreateHistoryService to build it.
600 HistoryService* history_service =
601 HistoryServiceFactory::GetForProfileWithoutCreating(this);
602 DCHECK(history_service);
603 history::InMemoryURLIndex* index = history_service->InMemoryIndex();
604 if (!index || index->restored())
605 return;
606 base::RunLoop run_loop;
607 HistoryIndexRestoreObserver observer(
608 content::GetQuitTaskForRunLoop(&run_loop));
609 index->set_restore_cache_observer(&observer);
610 run_loop.Run();
611 index->set_restore_cache_observer(NULL);
612 DCHECK(index->restored());
613}
614
[email protected]120abf132011-09-27 21:38:06615// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54616void TestingProfile::BlockUntilTopSitesLoaded() {
naiem.shaike091cc32014-09-18 02:20:52617 scoped_refptr<content::MessageLoopRunner> runner =
618 new content::MessageLoopRunner;
619 WaitTopSitesLoadedObserver observer(runner.get());
620 top_sites_->AddObserver(&observer);
621 runner->Run();
622 top_sites_->RemoveObserver(&observer);
[email protected]809cc4d2010-10-27 15:22:54623}
624
[email protected]90efc3e2014-04-22 18:39:03625void TestingProfile::SetGuestSession(bool guest) {
626 guest_session_ = guest;
627}
628
[email protected]4251165a2013-07-17 04:33:40629base::FilePath TestingProfile::GetPath() const {
[email protected]49a25632011-08-31 17:03:48630 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19631}
632
[email protected]0de615a2012-11-08 04:40:59633scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
[email protected]bb024fe2013-05-10 21:33:26634 return base::MessageLoop::current()->message_loop_proxy();
[email protected]0de615a2012-11-08 04:40:59635}
636
[email protected]5b199522012-12-22 17:24:44637TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
tapted1ab9b7092014-09-15 23:49:40638 DCHECK(prefs_);
[email protected]2fb7dc982010-09-29 12:24:28639 DCHECK(testing_prefs_);
640 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36641}
642
[email protected]8e5c89a2011-06-07 18:13:33643TestingProfile* TestingProfile::AsTestingProfile() {
644 return this;
645}
646
[email protected]29d70252011-04-28 02:16:58647std::string TestingProfile::GetProfileName() {
[email protected]d6001872013-09-24 18:31:26648 return profile_name_;
[email protected]29d70252011-04-28 02:16:58649}
650
[email protected]7b7e0b32014-03-17 16:09:23651Profile::ProfileType TestingProfile::GetProfileType() const {
652 if (guest_session_)
653 return GUEST_PROFILE;
tapted1ab9b7092014-09-15 23:49:40654 if (force_incognito_ || original_profile_)
[email protected]7b7e0b32014-03-17 16:09:23655 return INCOGNITO_PROFILE;
656 return REGULAR_PROFILE;
657}
658
[email protected]27d6e852012-03-02 21:31:32659bool TestingProfile::IsOffTheRecord() const {
tapted1ab9b7092014-09-15 23:49:40660 return force_incognito_ || original_profile_;
[email protected]fadc607b62011-02-07 17:55:50661}
662
[email protected]9819fd02013-08-22 10:49:39663void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
664 DCHECK(!IsOffTheRecord());
tapted1ab9b7092014-09-15 23:49:40665 DCHECK_EQ(this, profile->GetOriginalProfile());
[email protected]9819fd02013-08-22 10:49:39666 incognito_profile_ = profile.Pass();
[email protected]93dba942011-02-23 22:45:01667}
668
[email protected]fadc607b62011-02-07 17:55:50669Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]9819fd02013-08-22 10:49:39670 if (IsOffTheRecord())
671 return this;
tapted1ab9b7092014-09-15 23:49:40672 if (!incognito_profile_)
673 TestingProfile::Builder().BuildIncognito(this);
[email protected]5bb2f522011-03-25 19:04:44674 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50675}
676
677bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44678 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50679}
680
681Profile* TestingProfile::GetOriginalProfile() {
[email protected]074311a2013-02-28 23:14:09682 if (original_profile_)
683 return original_profile_;
[email protected]fadc607b62011-02-07 17:55:50684 return this;
685}
686
[email protected]d20d0432014-06-12 17:14:05687bool TestingProfile::IsSupervised() {
688 return !supervised_user_id_.empty();
[email protected]e000daf2013-07-31 16:50:58689}
690
[email protected]50356f82014-07-31 13:25:18691#if defined(ENABLE_EXTENSIONS)
[email protected]2261e7b72011-07-12 13:43:55692void TestingProfile::SetExtensionSpecialStoragePolicy(
693 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
694 extension_special_storage_policy_ = extension_special_storage_policy;
695}
[email protected]50356f82014-07-31 13:25:18696#endif
[email protected]2261e7b72011-07-12 13:43:55697
[email protected]19eb80152011-02-26 00:28:43698ExtensionSpecialStoragePolicy*
699TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18700#if defined(ENABLE_EXTENSIONS)
[email protected]cdba46992011-06-07 11:51:39701 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05702 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39703 return extension_special_storage_policy_.get();
[email protected]50356f82014-07-31 13:25:18704#else
705 return NULL;
706#endif
[email protected]19eb80152011-02-26 00:28:43707}
708
[email protected]33ad6ce92013-08-27 14:39:08709net::CookieMonster* TestingProfile::GetCookieMonster() {
710 if (!GetRequestContext())
711 return NULL;
712 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
713 GetCookieMonster();
714}
715
[email protected]2fb7dc982010-09-29 12:24:28716void TestingProfile::CreateTestingPrefService() {
717 DCHECK(!prefs_.get());
[email protected]5b199522012-12-22 17:24:44718 testing_prefs_ = new TestingPrefServiceSyncable();
[email protected]2fb7dc982010-09-29 12:24:28719 prefs_.reset(testing_prefs_);
[email protected]c7141feb2013-06-11 13:00:16720 user_prefs::UserPrefs::Set(this, prefs_.get());
[email protected]37ca3fe02013-07-05 15:32:44721 chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
[email protected]2fb7dc982010-09-29 12:24:28722}
723
tapted1ab9b7092014-09-15 23:49:40724void TestingProfile::CreateIncognitoPrefService() {
725 DCHECK(original_profile_);
726 DCHECK(!testing_prefs_);
727 // Simplified version of ProfileImpl::GetOffTheRecordPrefs(). Note this
728 // leaves testing_prefs_ unset.
729 prefs_.reset(original_profile_->prefs_->CreateIncognitoPrefService(NULL));
730 user_prefs::UserPrefs::Set(this, prefs_.get());
731}
732
[email protected]d81bb3e2013-05-02 18:17:03733void TestingProfile::CreateProfilePolicyConnector() {
[email protected]d81bb3e2013-05-02 18:17:03734#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24735 schema_registry_service_ =
736 policy::SchemaRegistryServiceFactory::CreateForContext(
737 this, policy::Schema(), NULL);
738 CHECK_EQ(schema_registry_service_.get(),
739 policy::SchemaRegistryServiceFactory::GetForContext(this));
[email protected]a43b9a82013-11-28 16:56:41740#endif // defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24741
[email protected]a43b9a82013-11-28 16:56:41742if (!policy_service_) {
743#if defined(ENABLE_CONFIGURATION_POLICY)
744 std::vector<policy::ConfigurationPolicyProvider*> providers;
[email protected]33528402013-12-16 18:38:32745 policy_service_.reset(new policy::PolicyServiceImpl(providers));
[email protected]d81bb3e2013-05-02 18:17:03746#else
[email protected]a43b9a82013-11-28 16:56:41747 policy_service_.reset(new policy::PolicyServiceStub());
[email protected]d81bb3e2013-05-02 18:17:03748#endif
[email protected]a43b9a82013-11-28 16:56:41749 }
[email protected]24a9f1c92013-11-13 12:33:37750 profile_policy_connector_.reset(new policy::ProfilePolicyConnector());
[email protected]a43b9a82013-11-28 16:56:41751 profile_policy_connector_->InitForTesting(policy_service_.Pass());
[email protected]d81bb3e2013-05-02 18:17:03752 policy::ProfilePolicyConnectorFactory::GetInstance()->SetServiceForTesting(
753 this, profile_policy_connector_.get());
754 CHECK_EQ(profile_policy_connector_.get(),
755 policy::ProfilePolicyConnectorFactory::GetForProfile(this));
756}
757
[email protected]c753f142013-02-10 13:14:04758PrefService* TestingProfile::GetPrefs() {
tapted1ab9b7092014-09-15 23:49:40759 DCHECK(prefs_);
[email protected]dbbad7a2010-08-13 18:18:36760 return prefs_.get();
761}
762
763history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54764 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36765}
766
[email protected]fadc607b62011-02-07 17:55:50767history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
768 return top_sites_.get();
769}
770
[email protected]b441a8492012-06-06 14:55:57771DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50772 return NULL;
773}
774
[email protected]abe2c032011-03-31 18:49:34775net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]37ac95b2013-07-23 23:39:35776 return GetDefaultStoragePartition(this)->GetURLRequestContext();
[email protected]812b3a32010-01-08 05:36:04777}
778
[email protected]6bd30072013-02-08 18:17:11779net::URLRequestContextGetter* TestingProfile::CreateRequestContext(
[email protected]7571263c2014-03-10 22:57:09780 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15781 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]37ac95b2013-07-23 23:39:35782 return new net::TestURLRequestContextGetter(
783 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]6bd30072013-02-08 18:17:11784}
785
[email protected]da5683db2011-04-23 17:12:21786net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
787 int renderer_child_id) {
[email protected]fb29e6cf2012-07-12 21:27:20788 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
789 renderer_child_id);
[email protected]1bc28312012-11-08 08:31:53790 return rph->GetStoragePartition()->GetURLRequestContext();
[email protected]d9696672011-03-15 22:45:09791}
792
[email protected]10705a7b2012-08-21 19:07:08793net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
794 return NULL;
795}
796
797net::URLRequestContextGetter*
798TestingProfile::GetMediaRequestContextForRenderProcess(
799 int renderer_child_id) {
[email protected]fadc607b62011-02-07 17:55:50800 return NULL;
801}
802
[email protected]10eb28162012-09-18 03:04:09803net::URLRequestContextGetter*
804TestingProfile::GetMediaRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40805 const base::FilePath& partition_path,
[email protected]27ddfed22012-10-30 23:22:43806 bool in_memory) {
[email protected]10eb28162012-09-18 03:04:09807 return NULL;
808}
809
[email protected]33ad6ce92013-08-27 14:39:08810net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
811 if (!extensions_request_context_.get())
812 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
813 return extensions_request_context_.get();
814}
815
[email protected]fadc607b62011-02-07 17:55:50816net::SSLConfigService* TestingProfile::GetSSLConfigService() {
[email protected]dc6655b2012-12-14 05:58:24817 if (!GetRequestContext())
818 return NULL;
819 return GetRequestContext()->GetURLRequestContext()->ssl_config_service();
[email protected]fadc607b62011-02-07 17:55:50820}
821
[email protected]10eb28162012-09-18 03:04:09822net::URLRequestContextGetter*
[email protected]6bd30072013-02-08 18:17:11823TestingProfile::CreateRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40824 const base::FilePath& partition_path,
[email protected]6bd30072013-02-08 18:17:11825 bool in_memory,
[email protected]7571263c2014-03-10 22:57:09826 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15827 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]10eb28162012-09-18 03:04:09828 // We don't test storage partitions here yet, so returning the same dummy
[email protected]d9696672011-03-15 22:45:09829 // context is sufficient for now.
830 return GetRequestContext();
831}
832
[email protected]df02aca2012-02-09 21:03:20833content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]37ac95b2013-07-23 23:39:35834 if (!resource_context_)
835 resource_context_ = new content::MockResourceContext();
836 return resource_context_;
[email protected]e89b77d2011-04-15 18:58:10837}
838
[email protected]dbbad7a2010-08-13 18:18:36839HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38840 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05841 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
[email protected]701550ad2012-09-06 23:43:29842#if defined(ENABLE_EXTENSIONS)
[email protected]994fdb252014-06-26 13:26:37843 ExtensionService* extension_service =
844 extensions::ExtensionSystem::Get(this)->extension_service();
mukai87a8d402014-09-15 20:15:09845 if (extension_service) {
846 extension_service->RegisterContentSettings(
847 host_content_settings_map_.get());
848 }
[email protected]701550ad2012-09-06 23:43:29849#endif
[email protected]35552dc52011-07-12 09:04:38850 }
[email protected]dbbad7a2010-08-13 18:18:36851 return host_content_settings_map_.get();
852}
853
[email protected]139355f2014-05-11 14:21:28854content::BrowserPluginGuestManager* TestingProfile::GetGuestManager() {
[email protected]1df3d972014-06-11 04:55:18855#if defined(ENABLE_EXTENSIONS)
[email protected]140d6cd92014-08-12 18:26:46856 return extensions::GuestViewManager::FromBrowserContext(this);
[email protected]1df3d972014-06-11 04:55:18857#else
858 return NULL;
859#endif
[email protected]24569262014-05-06 03:31:30860}
861
[email protected]c5c89d042014-06-13 14:43:37862content::PushMessagingService* TestingProfile::GetPushMessagingService() {
863 return NULL;
864}
865
[email protected]fadc607b62011-02-07 17:55:50866bool TestingProfile::IsSameProfile(Profile *p) {
867 return this == p;
868}
869
870base::Time TestingProfile::GetStartTime() const {
871 return start_time_;
872}
873
[email protected]152ea302013-02-11 04:08:40874base::FilePath TestingProfile::last_selected_directory() {
[email protected]fadc607b62011-02-07 17:55:50875 return last_selected_directory_;
876}
877
[email protected]152ea302013-02-11 04:08:40878void TestingProfile::set_last_selected_directory(const base::FilePath& path) {
[email protected]fadc607b62011-02-07 17:55:50879 last_selected_directory_ = path;
880}
881
[email protected]cc5bfd42010-11-24 14:44:02882PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07883 if (!pref_proxy_config_tracker_.get()) {
[email protected]e2930d0902013-07-17 05:25:42884 // TestingProfile is used in unit tests, where local state is not available.
[email protected]6f96cbcb2011-11-04 02:26:07885 pref_proxy_config_tracker_.reset(
[email protected]e2930d0902013-07-17 05:25:42886 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(GetPrefs(),
887 NULL));
[email protected]6f96cbcb2011-11-04 02:26:07888 }
889 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02890}
891
[email protected]0bfc29a2009-04-27 16:15:44892void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]a5894fe2012-11-01 14:40:56893 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02894 HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
[email protected]a5894fe2012-11-01 14:40:56895 DCHECK(history_service);
[email protected]bb024fe2013-05-10 21:33:26896 DCHECK(base::MessageLoop::current());
[email protected]0bfc29a2009-04-27 16:15:44897
[email protected]58bef622014-07-03 11:41:14898 base::CancelableTaskTracker tracker;
[email protected]6a0b6672014-07-29 12:00:23899 history_service->ScheduleDBTask(
900 scoped_ptr<history::HistoryDBTask>(
901 new QuittingHistoryDBTask()),
902 &tracker);
[email protected]bb024fe2013-05-10 21:33:26903 base::MessageLoop::current()->Run();
[email protected]0bfc29a2009-04-27 16:15:44904}
[email protected]345a8b72009-09-29 09:11:44905
[email protected]8382d8c2011-09-15 03:43:17906chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
907 return NULL;
908}
909
[email protected]a4205202014-06-02 16:03:08910DevToolsNetworkController* TestingProfile::GetDevToolsNetworkController() {
911 return NULL;
912}
913
[email protected]e0e1fc22012-11-05 20:25:36914void TestingProfile::ClearNetworkingHistorySince(
915 base::Time time,
916 const base::Closure& completion) {
917 if (!completion.is_null()) {
918 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
919 }
[email protected]02896a82011-09-21 18:54:32920}
921
[email protected]671f9c62011-10-28 19:22:07922GURL TestingProfile::GetHomePage() {
923 return GURL(chrome::kChromeUINewTabURL);
924}
925
[email protected]c753f142013-02-10 13:14:04926PrefService* TestingProfile::GetOffTheRecordPrefs() {
[email protected]fadc607b62011-02-07 17:55:50927 return NULL;
928}
929
[email protected]cd501a72014-08-22 19:58:31930storage::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18931#if defined(ENABLE_EXTENSIONS)
[email protected]36fb2c7c2011-04-04 15:49:08932 return GetExtensionSpecialStoragePolicy();
[email protected]50356f82014-07-31 13:25:18933#else
934 return NULL;
935#endif
[email protected]36fb2c7c2011-04-04 15:49:08936}
937
[email protected]c5bbe0e2014-08-01 23:23:30938content::SSLHostStateDelegate* TestingProfile::GetSSLHostStateDelegate() {
939 return NULL;
940}
941
[email protected]1bee6ed2012-03-14 06:46:36942bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
943 return true;
944}
[email protected]ef92e172012-04-25 19:40:41945
[email protected]4251165a2013-07-17 04:33:40946bool TestingProfile::IsGuestSession() const {
[email protected]e9476cd2013-12-04 05:55:34947 return guest_session_;
[email protected]4251165a2013-07-17 04:33:40948}
[email protected]1101dbc52013-10-05 00:19:12949
[email protected]6c0ca7fc2012-10-05 16:27:22950Profile::ExitType TestingProfile::GetLastSessionExitType() {
951 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED;
[email protected]2ca1ea662012-10-04 02:26:36952}
953
[email protected]5c4c89f2012-08-07 21:09:59954TestingProfile::Builder::Builder()
955 : build_called_(false),
[email protected]9819fd02013-08-22 10:49:39956 delegate_(NULL),
[email protected]e9476cd2013-12-04 05:55:34957 guest_session_(false) {
[email protected]5c4c89f2012-08-07 21:09:59958}
959
960TestingProfile::Builder::~Builder() {
961}
962
[email protected]152ea302013-02-11 04:08:40963void TestingProfile::Builder::SetPath(const base::FilePath& path) {
[email protected]5c4c89f2012-08-07 21:09:59964 path_ = path;
965}
966
967void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
968 delegate_ = delegate;
969}
970
[email protected]50356f82014-07-31 13:25:18971#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59972void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
973 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
974 extension_policy_ = policy;
975}
[email protected]50356f82014-07-31 13:25:18976#endif
[email protected]5c4c89f2012-08-07 21:09:59977
[email protected]5b199522012-12-22 17:24:44978void TestingProfile::Builder::SetPrefService(
979 scoped_ptr<PrefServiceSyncable> prefs) {
[email protected]5c4c89f2012-08-07 21:09:59980 pref_service_ = prefs.Pass();
981}
982
[email protected]e9476cd2013-12-04 05:55:34983void TestingProfile::Builder::SetGuestSession() {
984 guest_session_ = true;
985}
986
[email protected]d20d0432014-06-12 17:14:05987void TestingProfile::Builder::SetSupervisedUserId(
988 const std::string& supervised_user_id) {
989 supervised_user_id_ = supervised_user_id;
[email protected]371662e372013-10-17 22:05:22990}
991
[email protected]a43b9a82013-11-28 16:56:41992void TestingProfile::Builder::SetPolicyService(
993 scoped_ptr<policy::PolicyService> policy_service) {
994 policy_service_ = policy_service.Pass();
995}
996
[email protected]9819fd02013-08-22 10:49:39997void TestingProfile::Builder::AddTestingFactory(
998 BrowserContextKeyedServiceFactory* service_factory,
[email protected]d45c5d52013-12-04 23:41:16999 BrowserContextKeyedServiceFactory::TestingFactoryFunction callback) {
[email protected]9819fd02013-08-22 10:49:391000 testing_factories_.push_back(std::make_pair(service_factory, callback));
1001}
1002
[email protected]5c4c89f2012-08-07 21:09:591003scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
1004 DCHECK(!build_called_);
1005 build_called_ = true;
[email protected]371662e372013-10-17 22:05:221006
tapted1ab9b7092014-09-15 23:49:401007 return scoped_ptr<TestingProfile>(new TestingProfile(path_,
1008 delegate_,
[email protected]50356f82014-07-31 13:25:181009#if defined(ENABLE_EXTENSIONS)
tapted1ab9b7092014-09-15 23:49:401010 extension_policy_,
[email protected]50356f82014-07-31 13:25:181011#endif
tapted1ab9b7092014-09-15 23:49:401012 pref_service_.Pass(),
1013 NULL,
1014 guest_session_,
1015 supervised_user_id_,
1016 policy_service_.Pass(),
1017 testing_factories_));
1018}
1019
1020TestingProfile* TestingProfile::Builder::BuildIncognito(
1021 TestingProfile* original_profile) {
1022 DCHECK(!build_called_);
1023 DCHECK(original_profile);
1024 build_called_ = true;
1025
1026 // Note: Owned by |original_profile|.
1027 return new TestingProfile(path_,
1028 delegate_,
1029#if defined(ENABLE_EXTENSIONS)
1030 extension_policy_,
1031#endif
1032 pref_service_.Pass(),
1033 original_profile,
1034 guest_session_,
1035 supervised_user_id_,
1036 policy_service_.Pass(),
1037 testing_factories_);
[email protected]5c4c89f2012-08-07 21:09:591038}