blob: 72f1e80b7b13ed31f89f5080308e4d0a89ac0caf [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"
[email protected]dbbad7a2010-08-13 18:18:369#include "base/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"
[email protected]540380fc2014-03-14 10:10:3456#include "components/keyed_service/content/browser_context_dependency_manager.h"
[email protected]76b4b152013-12-08 21:10:0457#include "components/policy/core/common/policy_service.h"
[email protected]75fee372013-03-06 00:42:4458#include "components/user_prefs/user_prefs.h"
[email protected]c38831a12011-10-28 12:44:4959#include "content/public/browser/browser_thread.h"
[email protected]7c4b66b2014-01-04 12:28:1360#include "content/public/browser/cookie_store_factory.h"
[email protected]ad50def52011-10-19 23:17:0761#include "content/public/browser/notification_service.h"
[email protected]fb29e6cf2012-07-12 21:27:2062#include "content/public/browser/render_process_host.h"
[email protected]885c0e92012-11-13 20:27:4263#include "content/public/browser/storage_partition.h"
[email protected]08a932d52012-06-03 21:42:1264#include "content/public/test/mock_resource_context.h"
[email protected]a7fe9112012-07-20 02:34:4565#include "content/public/test/test_utils.h"
[email protected]885c0e92012-11-13 20:27:4266#include "extensions/common/constants.h"
[email protected]aa84a7e2012-03-15 21:29:0667#include "net/cookies/cookie_monster.h"
[email protected]812b3a32010-01-08 05:36:0468#include "net/url_request/url_request_context.h"
[email protected]abe2c032011-03-31 18:49:3469#include "net/url_request/url_request_context_getter.h"
[email protected]d2db0292011-01-26 20:23:4470#include "net/url_request/url_request_test_util.h"
[email protected]4772b072010-03-30 17:45:4671#include "testing/gmock/include/gmock/gmock.h"
[email protected]248ce192011-02-10 15:26:3472
[email protected]21d3a882012-05-31 14:41:5573#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:2474#include "chrome/browser/policy/schema_registry_service.h"
75#include "chrome/browser/policy/schema_registry_service_factory.h"
[email protected]f20a3a22013-12-03 16:12:3776#include "components/policy/core/common/configuration_policy_provider.h"
[email protected]76b4b152013-12-08 21:10:0477#include "components/policy/core/common/policy_service_impl.h"
[email protected]eb83c392013-11-07 12:53:2478#include "components/policy/core/common/schema.h"
[email protected]21d3a882012-05-31 14:41:5579#else
[email protected]76b4b152013-12-08 21:10:0480#include "components/policy/core/common/policy_service_stub.h"
[email protected]21d3a882012-05-31 14:41:5581#endif // defined(ENABLE_CONFIGURATION_POLICY)
82
[email protected]1df3d972014-06-11 04:55:1883#if defined(ENABLE_EXTENSIONS)
[email protected]50356f82014-07-31 13:25:1884#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]d5a74be2014-08-08 01:01:2785#include "chrome/browser/extensions/extension_system_factory.h"
86#include "chrome/browser/extensions/test_extension_system.h"
[email protected]1df3d972014-06-11 04:55:1887#include "chrome/browser/guest_view/guest_view_manager.h"
[email protected]994fdb252014-06-26 13:26:3788#include "extensions/browser/extension_system.h"
[email protected]1df3d972014-06-11 04:55:1889#endif
90
[email protected]bd3093c2014-06-13 07:38:1691#if defined(OS_ANDROID)
92#include "chrome/browser/signin/android_profile_oauth2_token_service.h"
93#endif
94
[email protected]e1480482013-09-11 11:49:5895#if defined(ENABLE_MANAGED_USERS)
[email protected]cce15bb2014-06-17 13:43:5196#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
97#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]e1480482013-09-11 11:49:5898#endif
99
[email protected]e1acf6f2008-10-27 20:43:33100using base::Time;
[email protected]631bb742011-11-02 11:29:39101using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:57102using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:23103using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:46104using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:33105
[email protected]d364c652008-08-29 19:46:56106namespace {
107
[email protected]0bfc29a2009-04-27 16:15:44108// Task used to make sure history has finished processing a request. Intended
109// for use with BlockUntilHistoryProcessesPendingRequests.
110
[email protected]d891f522013-02-08 03:24:41111class QuittingHistoryDBTask : public history::HistoryDBTask {
[email protected]0bfc29a2009-04-27 16:15:44112 public:
113 QuittingHistoryDBTask() {}
114
115 virtual bool RunOnDBThread(history::HistoryBackend* backend,
[email protected]be9826e62013-02-07 02:00:58116 history::HistoryDatabase* db) OVERRIDE {
[email protected]0bfc29a2009-04-27 16:15:44117 return true;
118 }
119
[email protected]be9826e62013-02-07 02:00:58120 virtual void DoneRunOnMainThread() OVERRIDE {
[email protected]bb024fe2013-05-10 21:33:26121 base::MessageLoop::current()->Quit();
[email protected]0bfc29a2009-04-27 16:15:44122 }
123
124 private:
[email protected]be9826e62013-02-07 02:00:58125 virtual ~QuittingHistoryDBTask() {}
[email protected]7991a232009-11-06 01:55:48126
[email protected]0bfc29a2009-04-27 16:15:44127 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
128};
129
[email protected]33ad6ce92013-08-27 14:39:08130class TestExtensionURLRequestContext : public net::URLRequestContext {
131 public:
132 TestExtensionURLRequestContext() {
[email protected]7c4b66b2014-01-04 12:28:13133 net::CookieMonster* cookie_monster =
[email protected]9a6c2aa2014-01-11 22:39:39134 content::CreateCookieStore(content::CookieStoreConfig())->
135 GetCookieMonster();
[email protected]5edff3c52014-06-23 20:27:48136 const char* const schemes[] = {extensions::kExtensionScheme};
137 cookie_monster->SetCookieableSchemes(schemes, arraysize(schemes));
[email protected]33ad6ce92013-08-27 14:39:08138 set_cookie_store(cookie_monster);
139 }
140
[email protected]424559492014-07-22 00:27:40141 virtual ~TestExtensionURLRequestContext() {
142 AssertNoURLRequests();
143 }
[email protected]33ad6ce92013-08-27 14:39:08144};
145
146class TestExtensionURLRequestContextGetter
147 : public net::URLRequestContextGetter {
148 public:
149 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
150 if (!context_.get())
151 context_.reset(new TestExtensionURLRequestContext());
152 return context_.get();
153 }
154 virtual scoped_refptr<base::SingleThreadTaskRunner>
155 GetNetworkTaskRunner() const OVERRIDE {
156 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
157 }
158
159 protected:
160 virtual ~TestExtensionURLRequestContextGetter() {}
161
162 private:
163 scoped_ptr<net::URLRequestContext> context_;
164};
165
[email protected]1d51882f2013-11-12 01:59:02166#if defined(ENABLE_NOTIFICATIONS)
[email protected]540380fc2014-03-14 10:10:34167KeyedService* CreateTestDesktopNotificationService(
[email protected]c7fa4362013-04-26 18:09:02168 content::BrowserContext* profile) {
[email protected]c7fa4362013-04-26 18:09:02169 return new DesktopNotificationService(static_cast<Profile*>(profile), NULL);
[email protected]dcb72d52011-04-13 12:36:53170}
[email protected]1d51882f2013-11-12 01:59:02171#endif
[email protected]dcb72d52011-04-13 12:36:53172
[email protected]d364c652008-08-29 19:46:56173} // namespace
174
[email protected]89dbb1772012-07-17 13:47:25175// static
176#if defined(OS_CHROMEOS)
177// Must be kept in sync with
178// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
[email protected]0f13fe8d2013-11-19 08:25:00179const char TestingProfile::kTestUserProfileDir[] = "test-user";
[email protected]89dbb1772012-07-17 13:47:25180#else
181const char TestingProfile::kTestUserProfileDir[] = "Default";
182#endif
183
[email protected]f25387b2008-08-21 15:20:33184TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55185 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28186 testing_prefs_(NULL),
[email protected]5bb2f522011-03-25 19:04:44187 incognito_(false),
[email protected]9819fd02013-08-22 10:49:39188 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09189 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34190 guest_session_(false),
[email protected]5cdcd152011-03-31 22:48:52191 last_session_exited_cleanly_(true),
[email protected]f0901a22013-05-27 21:37:53192 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44193 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35194 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17195 delegate_(NULL) {
[email protected]5c4c89f2012-08-07 21:09:59196 CreateTempProfileDir();
[email protected]49a25632011-08-31 17:03:48197 profile_path_ = temp_dir_.path();
198
199 Init();
[email protected]537c1082011-12-02 02:37:17200 FinishInit();
[email protected]49a25632011-08-31 17:03:48201}
202
[email protected]152ea302013-02-11 04:08:40203TestingProfile::TestingProfile(const base::FilePath& path)
[email protected]49a25632011-08-31 17:03:48204 : start_time_(Time::Now()),
205 testing_prefs_(NULL),
206 incognito_(false),
[email protected]9819fd02013-08-22 10:49:39207 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09208 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34209 guest_session_(false),
[email protected]49a25632011-08-31 17:03:48210 last_session_exited_cleanly_(true),
211 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53212 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44213 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35214 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17215 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48216 Init();
[email protected]537c1082011-12-02 02:37:17217 FinishInit();
218}
219
[email protected]152ea302013-02-11 04:08:40220TestingProfile::TestingProfile(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:17221 Delegate* delegate)
222 : start_time_(Time::Now()),
223 testing_prefs_(NULL),
224 incognito_(false),
[email protected]9819fd02013-08-22 10:49:39225 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09226 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34227 guest_session_(false),
[email protected]537c1082011-12-02 02:37:17228 last_session_exited_cleanly_(true),
229 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53230 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44231 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35232 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17233 delegate_(delegate) {
234 Init();
235 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26236 base::MessageLoop::current()->PostTask(
237 FROM_HERE,
238 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]537c1082011-12-02 02:37:17239 } else {
240 FinishInit();
241 }
[email protected]49a25632011-08-31 17:03:48242}
243
[email protected]5c4c89f2012-08-07 21:09:59244TestingProfile::TestingProfile(
[email protected]152ea302013-02-11 04:08:40245 const base::FilePath& path,
[email protected]5c4c89f2012-08-07 21:09:59246 Delegate* delegate,
[email protected]50356f82014-07-31 13:25:18247#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59248 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
[email protected]50356f82014-07-31 13:25:18249#endif
[email protected]9819fd02013-08-22 10:49:39250 scoped_ptr<PrefServiceSyncable> prefs,
251 bool incognito,
[email protected]e9476cd2013-12-04 05:55:34252 bool guest_session,
[email protected]d20d0432014-06-12 17:14:05253 const std::string& supervised_user_id,
[email protected]a43b9a82013-11-28 16:56:41254 scoped_ptr<policy::PolicyService> policy_service,
[email protected]9819fd02013-08-22 10:49:39255 const TestingFactories& factories)
[email protected]5c4c89f2012-08-07 21:09:59256 : start_time_(Time::Now()),
257 prefs_(prefs.release()),
258 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39259 incognito_(incognito),
260 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09261 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34262 guest_session_(guest_session),
[email protected]d20d0432014-06-12 17:14:05263 supervised_user_id_(supervised_user_id),
[email protected]5c4c89f2012-08-07 21:09:59264 last_session_exited_cleanly_(true),
[email protected]50356f82014-07-31 13:25:18265#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59266 extension_special_storage_policy_(extension_policy),
[email protected]50356f82014-07-31 13:25:18267#endif
[email protected]5c4c89f2012-08-07 21:09:59268 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53269 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44270 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35271 resource_context_(NULL),
[email protected]a43b9a82013-11-28 16:56:41272 delegate_(delegate),
273 policy_service_(policy_service.release()) {
[email protected]5c4c89f2012-08-07 21:09:59274 // If no profile path was supplied, create one.
275 if (profile_path_.empty()) {
276 CreateTempProfileDir();
277 profile_path_ = temp_dir_.path();
278 }
279
[email protected]9819fd02013-08-22 10:49:39280 // Set any testing factories prior to initializing the services.
281 for (TestingFactories::const_iterator it = factories.begin();
282 it != factories.end(); ++it) {
283 it->first->SetTestingFactory(this, it->second);
284 }
285
[email protected]5c4c89f2012-08-07 21:09:59286 Init();
287 // If caller supplied a delegate, delay the FinishInit invocation until other
288 // tasks have run.
289 // TODO(atwilson): See if this is still required once we convert the current
290 // users of the constructor that takes a Delegate* param.
291 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26292 base::MessageLoop::current()->PostTask(
293 FROM_HERE,
294 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]5c4c89f2012-08-07 21:09:59295 } else {
296 FinishInit();
297 }
298}
299
300void TestingProfile::CreateTempProfileDir() {
301 if (!temp_dir_.CreateUniqueTempDir()) {
302 LOG(ERROR) << "Failed to create unique temporary directory.";
303
304 // Fallback logic in case we fail to create unique temporary directory.
[email protected]152ea302013-02-11 04:08:40305 base::FilePath system_tmp_dir;
[email protected]5c4c89f2012-08-07 21:09:59306 bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir);
307
308 // We're severly screwed if we can't get the system temporary
309 // directory. Die now to avoid writing to the filesystem root
310 // or other bad places.
311 CHECK(success);
312
[email protected]152ea302013-02-11 04:08:40313 base::FilePath fallback_dir(
314 system_tmp_dir.AppendASCII("TestingProfilePath"));
[email protected]dd3aa792013-07-16 19:10:23315 base::DeleteFile(fallback_dir, true);
[email protected]426d1c92013-12-03 20:08:54316 base::CreateDirectory(fallback_dir);
[email protected]5c4c89f2012-08-07 21:09:59317 if (!temp_dir_.Set(fallback_dir)) {
318 // That shouldn't happen, but if it does, try to recover.
319 LOG(ERROR) << "Failed to use a fallback temporary directory.";
320
321 // We're screwed if this fails, see CHECK above.
322 CHECK(temp_dir_.Set(system_tmp_dir));
323 }
324 }
325}
326
[email protected]49a25632011-08-31 17:03:48327void TestingProfile::Init() {
[email protected]bf390cb2013-08-06 02:48:08328 // If threads have been initialized, we should be on the UI thread.
[email protected]33d06242013-08-12 05:20:30329 DCHECK(!content::BrowserThread::IsThreadInitialized(
330 content::BrowserThread::UI) ||
331 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
[email protected]bf390cb2013-08-06 02:48:08332
[email protected]bd3093c2014-06-13 07:38:16333#if defined(OS_ANDROID)
334 // Make sure token service knows its running in tests.
335 AndroidProfileOAuth2TokenService::set_is_testing_profile();
336#endif
337
[email protected]7e75e4a2013-05-17 17:20:03338 // Normally this would happen during browser startup, but for tests
339 // we need to trigger creation of Profile-related services.
340 ChromeBrowserMainExtraPartsProfiles::
[email protected]f1484c52013-05-22 23:25:44341 EnsureBrowserContextKeyedServiceFactoriesBuilt();
[email protected]7e75e4a2013-05-17 17:20:03342
[email protected]75fee372013-03-06 00:42:44343 if (prefs_.get())
[email protected]c7141feb2013-06-11 13:00:16344 user_prefs::UserPrefs::Set(this, prefs_.get());
[email protected]c7fa4362013-04-26 18:09:02345 else
346 CreateTestingPrefService();
[email protected]75fee372013-03-06 00:42:44347
[email protected]7567484142013-07-11 17:36:07348 if (!base::PathExists(profile_path_))
[email protected]426d1c92013-12-03 20:08:54349 base::CreateDirectory(profile_path_);
[email protected]d41e6572012-05-04 20:49:42350
[email protected]d81bb3e2013-05-02 18:17:03351 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl
352 // anymore, after converting the PrefService to a PKS. Until then it must
353 // be associated with a TestingProfile too.
[email protected]eb83c392013-11-07 12:53:24354 if (!IsOffTheRecord())
355 CreateProfilePolicyConnector();
[email protected]d81bb3e2013-05-02 18:17:03356
[email protected]d5a74be2014-08-08 01:01:27357#if defined(ENABLE_EXTENSIONS)
[email protected]bd306722012-07-11 20:43:59358 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
359 this, extensions::TestExtensionSystem::Build);
[email protected]d5a74be2014-08-08 01:01:27360#endif
[email protected]31d8f5f22012-04-02 15:22:08361
[email protected]d45c5d52013-12-04 23:41:16362 // If no original profile was specified for this profile: register preferences
363 // even if this is an incognito profile - this allows tests to create a
[email protected]9819fd02013-08-22 10:49:39364 // standalone incognito profile while still having prefs registered.
[email protected]d45c5d52013-12-04 23:41:16365 if (!IsOffTheRecord() || !original_profile_) {
366 user_prefs::PrefRegistrySyncable* pref_registry =
367 static_cast<user_prefs::PrefRegistrySyncable*>(
368 prefs_->DeprecatedGetPrefRegistry());
369 browser_context_dependency_manager_->
370 RegisterProfilePrefsForServices(this, pref_registry);
371 }
372
[email protected]9819fd02013-08-22 10:49:39373 browser_context_dependency_manager_->CreateBrowserContextServicesForTest(
[email protected]d45c5d52013-12-04 23:41:16374 this);
[email protected]49a25632011-08-31 17:03:48375
[email protected]5fd2e842012-03-01 00:29:11376#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53377 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22378 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
379 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11380#endif
[email protected]e1480482013-09-11 11:49:58381
382#if defined(ENABLE_MANAGED_USERS)
[email protected]cce15bb2014-06-17 13:43:51383 SupervisedUserSettingsService* settings_service =
384 SupervisedUserSettingsServiceFactory::GetForProfile(this);
[email protected]e1480482013-09-11 11:49:58385 TestingPrefStore* store = new TestingPrefStore();
386 settings_service->Init(store);
387 store->SetInitializationCompleted();
388#endif
[email protected]d6001872013-09-24 18:31:26389
390 profile_name_ = "testing_profile";
[email protected]537c1082011-12-02 02:37:17391}
[email protected]12463cd2011-07-19 09:40:20392
[email protected]537c1082011-12-02 02:37:17393void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40394 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07395 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20396 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53397 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07398 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17399
[email protected]371662e372013-10-17 22:05:22400 ProfileManager* profile_manager = g_browser_process->profile_manager();
401 if (profile_manager)
402 profile_manager->InitProfileUserPrefs(this);
403
[email protected]537c1082011-12-02 02:37:17404 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03405 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46406}
407
[email protected]f25387b2008-08-21 15:20:33408TestingProfile::~TestingProfile() {
[email protected]9819fd02013-08-22 10:49:39409 // Revert to non-incognito mode before shutdown.
410 force_incognito_ = false;
411
[email protected]37ac95b2013-07-23 23:39:35412 // Any objects holding live URLFetchers should be deleted before teardown.
413 TemplateURLFetcherFactory::ShutdownForProfile(this);
414
[email protected]a7c4c032012-07-27 00:36:32415 MaybeSendDestroyedNotification();
[email protected]5cdcd152011-03-31 22:48:52416
[email protected]f0901a22013-05-27 21:37:53417 browser_context_dependency_manager_->DestroyBrowserContextServices(this);
[email protected]5cdcd152011-03-31 22:48:52418
[email protected]5c6ac842013-06-02 23:37:03419 if (host_content_settings_map_.get())
[email protected]35552dc52011-07-12 09:04:38420 host_content_settings_map_->ShutdownOnUIThread();
421
[email protected]809cc4d2010-10-27 15:22:54422 DestroyTopSites();
[email protected]cdba46992011-06-07 11:51:39423
[email protected]cc5bfd42010-11-24 14:44:02424 if (pref_proxy_config_tracker_.get())
425 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]37ac95b2013-07-23 23:39:35426 // Failing a post == leaks == heapcheck failure. Make that an immediate test
427 // failure.
428 if (resource_context_) {
429 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
430 resource_context_));
431 resource_context_ = NULL;
432 content::RunAllPendingInMessageLoop(BrowserThread::IO);
433 }
[email protected]f25387b2008-08-21 15:20:33434}
initial.commit09911bf2008-07-26 23:55:29435
[email protected]540380fc2014-03-14 10:10:34436static KeyedService* BuildFaviconService(content::BrowserContext* profile) {
[email protected]0e199ed72014-07-04 13:05:22437 FaviconClient* favicon_client =
438 ChromeFaviconClientFactory::GetForProfile(static_cast<Profile*>(profile));
439 return new FaviconService(static_cast<Profile*>(profile), favicon_client);
[email protected]f3d2b312012-08-23 22:27:59440}
441
[email protected]d2879af2010-02-08 16:02:56442void TestingProfile::CreateFaviconService() {
[email protected]f3d2b312012-08-23 22:27:59443 // It is up to the caller to create the history service if one is needed.
444 FaviconServiceFactory::GetInstance()->SetTestingFactory(
445 this, BuildFaviconService);
[email protected]d2879af2010-02-08 16:02:56446}
447
[email protected]ad34610c2014-06-04 14:43:52448static KeyedService* BuildHistoryService(content::BrowserContext* context) {
449 Profile* profile = static_cast<Profile*>(context);
450 return new HistoryService(ChromeHistoryClientFactory::GetForProfile(profile),
451 profile);
[email protected]f61f4782012-06-08 21:54:21452}
453
[email protected]608e7e02013-07-24 12:23:31454bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54455 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33456 if (delete_file) {
[email protected]152ea302013-02-11 04:08:40457 base::FilePath path = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32458 path = path.Append(chrome::kHistoryFilename);
[email protected]608e7e02013-07-24 12:23:31459 if (!base::DeleteFile(path, false) || base::PathExists(path))
460 return false;
[email protected]f25387b2008-08-21 15:20:33461 }
[email protected]7019bb6d2012-08-23 09:09:48462 // This will create and init the history service.
[email protected]f61f4782012-06-08 21:54:21463 HistoryService* history_service = static_cast<HistoryService*>(
464 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
[email protected]a5894fe2012-11-01 14:40:56465 this, BuildHistoryService));
[email protected]ad34610c2014-06-04 14:43:52466 if (!history_service->Init(this->GetPath(), no_db)) {
[email protected]f61f4782012-06-08 21:54:21467 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
[email protected]7019bb6d2012-08-23 09:09:48468 }
[email protected]a50e16a2013-04-25 14:07:17469 // Disable WebHistoryService by default, since it makes network requests.
470 WebHistoryServiceFactory::GetInstance()->SetTestingFactory(this, NULL);
[email protected]608e7e02013-07-24 12:23:31471 return true;
initial.commit09911bf2008-07-26 23:55:29472}
473
474void TestingProfile::DestroyHistoryService() {
[email protected]a5894fe2012-11-01 14:40:56475 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02476 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]a5894fe2012-11-01 14:40:56477 if (!history_service)
initial.commit09911bf2008-07-26 23:55:29478 return;
479
[email protected]378970412014-06-12 07:31:17480 history_service->ClearCachedDataForContextID(0);
[email protected]bb024fe2013-05-10 21:33:26481 history_service->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure());
[email protected]f61f4782012-06-08 21:54:21482 history_service->Cleanup();
483 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29484
485 // Wait for the backend class to terminate before deleting the files and
486 // moving to the next test. Note: if this never terminates, somebody is
487 // probably leaking a reference to the history backend, so it never calls
488 // our destroy task.
[email protected]bb024fe2013-05-10 21:33:26489 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29490
initial.commit09911bf2008-07-26 23:55:29491 // Make sure we don't have any event pending that could disrupt the next
492 // test.
[email protected]bb024fe2013-05-10 21:33:26493 base::MessageLoop::current()->PostTask(FROM_HERE,
494 base::MessageLoop::QuitClosure());
495 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29496}
[email protected]4d0cd7ce2008-08-11 16:40:57497
[email protected]809cc4d2010-10-27 15:22:54498void TestingProfile::CreateTopSites() {
499 DestroyTopSites();
[email protected]9a93d3432013-04-29 20:52:12500 top_sites_ = history::TopSites::Create(
501 this, GetPath().Append(chrome::kTopSitesFilename));
[email protected]809cc4d2010-10-27 15:22:54502}
503
504void TestingProfile::DestroyTopSites() {
505 if (top_sites_.get()) {
506 top_sites_->Shutdown();
507 top_sites_ = NULL;
[email protected]9a93d3432013-04-29 20:52:12508 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that
509 // need to be run to properly shutdown. Run all pending tasks now. This is
[email protected]809cc4d2010-10-27 15:22:54510 // normally handled by browser_process shutdown.
[email protected]bb024fe2013-05-10 21:33:26511 if (base::MessageLoop::current())
512 base::MessageLoop::current()->RunUntilIdle();
[email protected]809cc4d2010-10-27 15:22:54513 }
514}
515
[email protected]540380fc2014-03-14 10:10:34516static KeyedService* BuildBookmarkModel(content::BrowserContext* context) {
[email protected]c7fa4362013-04-26 18:09:02517 Profile* profile = static_cast<Profile*>(context);
[email protected]6a848b52014-04-26 22:06:54518 ChromeBookmarkClient* bookmark_client =
[email protected]0b0e8a12014-06-14 00:11:31519 ChromeBookmarkClientFactory::GetForProfile(profile);
520 BookmarkModel* bookmark_model = new BookmarkModel(bookmark_client, false);
521 bookmark_client->Init(bookmark_model);
522 bookmark_model->Load(profile->GetPrefs(),
523 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
524 profile->GetPath(),
525 profile->GetIOTaskRunner(),
526 content::BrowserThread::GetMessageLoopProxyForThread(
527 content::BrowserThread::UI));
528 return bookmark_model;
529}
530
531static KeyedService* BuildChromeBookmarkClient(
532 content::BrowserContext* context) {
533 return new ChromeBookmarkClient(static_cast<Profile*>(context));
[email protected]f61f4782012-06-08 21:54:21534}
535
[email protected]ad34610c2014-06-04 14:43:52536static KeyedService* BuildChromeHistoryClient(
537 content::BrowserContext* context) {
538 Profile* profile = static_cast<Profile*>(context);
539 return new ChromeHistoryClient(BookmarkModelFactory::GetForProfile(profile));
540}
541
[email protected]d8e41ed2008-09-11 15:22:32542void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46543 if (delete_file) {
[email protected]d4a67d52014-04-12 05:17:03544 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName);
[email protected]dd3aa792013-07-16 19:10:23545 base::DeleteFile(path, false);
[email protected]90ef13132008-08-27 03:27:46546 }
[email protected]ad34610c2014-06-04 14:43:52547 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory(
548 this, BuildChromeHistoryClient);
[email protected]0b0e8a12014-06-14 00:11:31549 ChromeBookmarkClientFactory::GetInstance()->SetTestingFactory(
550 this, BuildChromeBookmarkClient);
551 // This creates the BookmarkModel.
552 ignore_result(BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
553 this, BuildBookmarkModel));
[email protected]4d0cd7ce2008-08-11 16:40:57554}
[email protected]d2c017a2008-08-13 21:51:45555
[email protected]540380fc2014-03-14 10:10:34556static KeyedService* BuildWebDataService(content::BrowserContext* profile) {
[email protected]c7fa4362013-04-26 18:09:02557 return new WebDataServiceWrapper(static_cast<Profile*>(profile));
[email protected]d07edd42012-05-14 23:49:46558}
[email protected]2609bc12010-01-24 08:32:55559
[email protected]d07edd42012-05-14 23:49:46560void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01561 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46562 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55563}
564
[email protected]18a15ca82012-08-30 00:28:16565void TestingProfile::BlockUntilHistoryIndexIsRefreshed() {
566 // Only get the history service if it actually exists since the caller of the
567 // test should explicitly call CreateHistoryService to build it.
568 HistoryService* history_service =
569 HistoryServiceFactory::GetForProfileWithoutCreating(this);
570 DCHECK(history_service);
571 history::InMemoryURLIndex* index = history_service->InMemoryIndex();
572 if (!index || index->restored())
573 return;
574 base::RunLoop run_loop;
575 HistoryIndexRestoreObserver observer(
576 content::GetQuitTaskForRunLoop(&run_loop));
577 index->set_restore_cache_observer(&observer);
578 run_loop.Run();
579 index->set_restore_cache_observer(NULL);
580 DCHECK(index->restored());
581}
582
[email protected]120abf132011-09-27 21:38:06583// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54584void TestingProfile::BlockUntilTopSitesLoaded() {
[email protected]a7fe9112012-07-20 02:34:45585 content::WindowedNotificationObserver top_sites_loaded_observer(
[email protected]120abf132011-09-27 21:38:06586 chrome::NOTIFICATION_TOP_SITES_LOADED,
[email protected]ad50def52011-10-19 23:17:07587 content::NotificationService::AllSources());
[email protected]120abf132011-09-27 21:38:06588 top_sites_loaded_observer.Wait();
[email protected]809cc4d2010-10-27 15:22:54589}
590
[email protected]90efc3e2014-04-22 18:39:03591void TestingProfile::SetGuestSession(bool guest) {
592 guest_session_ = guest;
593}
594
[email protected]4251165a2013-07-17 04:33:40595base::FilePath TestingProfile::GetPath() const {
[email protected]49a25632011-08-31 17:03:48596 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19597}
598
[email protected]0de615a2012-11-08 04:40:59599scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
[email protected]bb024fe2013-05-10 21:33:26600 return base::MessageLoop::current()->message_loop_proxy();
[email protected]0de615a2012-11-08 04:40:59601}
602
[email protected]5b199522012-12-22 17:24:44603TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
[email protected]2fb7dc982010-09-29 12:24:28604 if (!prefs_.get())
605 CreateTestingPrefService();
606 DCHECK(testing_prefs_);
607 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36608}
609
[email protected]8e5c89a2011-06-07 18:13:33610TestingProfile* TestingProfile::AsTestingProfile() {
611 return this;
612}
613
[email protected]29d70252011-04-28 02:16:58614std::string TestingProfile::GetProfileName() {
[email protected]d6001872013-09-24 18:31:26615 return profile_name_;
[email protected]29d70252011-04-28 02:16:58616}
617
[email protected]7b7e0b32014-03-17 16:09:23618Profile::ProfileType TestingProfile::GetProfileType() const {
619 if (guest_session_)
620 return GUEST_PROFILE;
621 if (force_incognito_ || incognito_)
622 return INCOGNITO_PROFILE;
623 return REGULAR_PROFILE;
624}
625
[email protected]27d6e852012-03-02 21:31:32626bool TestingProfile::IsOffTheRecord() const {
[email protected]9819fd02013-08-22 10:49:39627 return force_incognito_ || incognito_;
[email protected]fadc607b62011-02-07 17:55:50628}
629
[email protected]9819fd02013-08-22 10:49:39630void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
631 DCHECK(!IsOffTheRecord());
632 incognito_profile_ = profile.Pass();
[email protected]93dba942011-02-23 22:45:01633}
634
[email protected]074311a2013-02-28 23:14:09635void TestingProfile::SetOriginalProfile(Profile* profile) {
[email protected]9819fd02013-08-22 10:49:39636 DCHECK(IsOffTheRecord());
[email protected]074311a2013-02-28 23:14:09637 original_profile_ = profile;
638}
639
[email protected]fadc607b62011-02-07 17:55:50640Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]9819fd02013-08-22 10:49:39641 if (IsOffTheRecord())
642 return this;
[email protected]cbed02ae2014-03-07 17:16:26643 if (!incognito_profile_) {
644 TestingProfile::Builder builder;
645 builder.SetIncognito();
646 scoped_ptr<TestingProfile> incognito_test_profile(builder.Build());
647 incognito_test_profile->SetOriginalProfile(this);
648 SetOffTheRecordProfile(incognito_test_profile.PassAs<Profile>());
649 }
[email protected]5bb2f522011-03-25 19:04:44650 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50651}
652
653bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44654 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50655}
656
657Profile* TestingProfile::GetOriginalProfile() {
[email protected]074311a2013-02-28 23:14:09658 if (original_profile_)
659 return original_profile_;
[email protected]fadc607b62011-02-07 17:55:50660 return this;
661}
662
[email protected]d20d0432014-06-12 17:14:05663bool TestingProfile::IsSupervised() {
664 return !supervised_user_id_.empty();
[email protected]e000daf2013-07-31 16:50:58665}
666
[email protected]50356f82014-07-31 13:25:18667#if defined(ENABLE_EXTENSIONS)
[email protected]2261e7b72011-07-12 13:43:55668void TestingProfile::SetExtensionSpecialStoragePolicy(
669 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
670 extension_special_storage_policy_ = extension_special_storage_policy;
671}
[email protected]50356f82014-07-31 13:25:18672#endif
[email protected]2261e7b72011-07-12 13:43:55673
[email protected]19eb80152011-02-26 00:28:43674ExtensionSpecialStoragePolicy*
675TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18676#if defined(ENABLE_EXTENSIONS)
[email protected]cdba46992011-06-07 11:51:39677 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05678 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39679 return extension_special_storage_policy_.get();
[email protected]50356f82014-07-31 13:25:18680#else
681 return NULL;
682#endif
[email protected]19eb80152011-02-26 00:28:43683}
684
[email protected]33ad6ce92013-08-27 14:39:08685net::CookieMonster* TestingProfile::GetCookieMonster() {
686 if (!GetRequestContext())
687 return NULL;
688 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
689 GetCookieMonster();
690}
691
[email protected]2fb7dc982010-09-29 12:24:28692void TestingProfile::CreateTestingPrefService() {
693 DCHECK(!prefs_.get());
[email protected]5b199522012-12-22 17:24:44694 testing_prefs_ = new TestingPrefServiceSyncable();
[email protected]2fb7dc982010-09-29 12:24:28695 prefs_.reset(testing_prefs_);
[email protected]c7141feb2013-06-11 13:00:16696 user_prefs::UserPrefs::Set(this, prefs_.get());
[email protected]37ca3fe02013-07-05 15:32:44697 chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
[email protected]2fb7dc982010-09-29 12:24:28698}
699
[email protected]d81bb3e2013-05-02 18:17:03700void TestingProfile::CreateProfilePolicyConnector() {
[email protected]d81bb3e2013-05-02 18:17:03701#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24702 schema_registry_service_ =
703 policy::SchemaRegistryServiceFactory::CreateForContext(
704 this, policy::Schema(), NULL);
705 CHECK_EQ(schema_registry_service_.get(),
706 policy::SchemaRegistryServiceFactory::GetForContext(this));
[email protected]a43b9a82013-11-28 16:56:41707#endif // defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24708
[email protected]a43b9a82013-11-28 16:56:41709if (!policy_service_) {
710#if defined(ENABLE_CONFIGURATION_POLICY)
711 std::vector<policy::ConfigurationPolicyProvider*> providers;
[email protected]33528402013-12-16 18:38:32712 policy_service_.reset(new policy::PolicyServiceImpl(providers));
[email protected]d81bb3e2013-05-02 18:17:03713#else
[email protected]a43b9a82013-11-28 16:56:41714 policy_service_.reset(new policy::PolicyServiceStub());
[email protected]d81bb3e2013-05-02 18:17:03715#endif
[email protected]a43b9a82013-11-28 16:56:41716 }
[email protected]24a9f1c92013-11-13 12:33:37717 profile_policy_connector_.reset(new policy::ProfilePolicyConnector());
[email protected]a43b9a82013-11-28 16:56:41718 profile_policy_connector_->InitForTesting(policy_service_.Pass());
[email protected]d81bb3e2013-05-02 18:17:03719 policy::ProfilePolicyConnectorFactory::GetInstance()->SetServiceForTesting(
720 this, profile_policy_connector_.get());
721 CHECK_EQ(profile_policy_connector_.get(),
722 policy::ProfilePolicyConnectorFactory::GetForProfile(this));
723}
724
[email protected]c753f142013-02-10 13:14:04725PrefService* TestingProfile::GetPrefs() {
[email protected]dbbad7a2010-08-13 18:18:36726 if (!prefs_.get()) {
[email protected]2fb7dc982010-09-29 12:24:28727 CreateTestingPrefService();
[email protected]dbbad7a2010-08-13 18:18:36728 }
729 return prefs_.get();
730}
731
732history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54733 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36734}
735
[email protected]fadc607b62011-02-07 17:55:50736history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
737 return top_sites_.get();
738}
739
[email protected]b441a8492012-06-06 14:55:57740DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50741 return NULL;
742}
743
[email protected]abe2c032011-03-31 18:49:34744net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]37ac95b2013-07-23 23:39:35745 return GetDefaultStoragePartition(this)->GetURLRequestContext();
[email protected]812b3a32010-01-08 05:36:04746}
747
[email protected]6bd30072013-02-08 18:17:11748net::URLRequestContextGetter* TestingProfile::CreateRequestContext(
[email protected]7571263c2014-03-10 22:57:09749 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15750 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]37ac95b2013-07-23 23:39:35751 return new net::TestURLRequestContextGetter(
752 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]6bd30072013-02-08 18:17:11753}
754
[email protected]da5683db2011-04-23 17:12:21755net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
756 int renderer_child_id) {
[email protected]fb29e6cf2012-07-12 21:27:20757 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
758 renderer_child_id);
[email protected]1bc28312012-11-08 08:31:53759 return rph->GetStoragePartition()->GetURLRequestContext();
[email protected]d9696672011-03-15 22:45:09760}
761
[email protected]10705a7b2012-08-21 19:07:08762net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
763 return NULL;
764}
765
766net::URLRequestContextGetter*
767TestingProfile::GetMediaRequestContextForRenderProcess(
768 int renderer_child_id) {
[email protected]fadc607b62011-02-07 17:55:50769 return NULL;
770}
771
[email protected]10eb28162012-09-18 03:04:09772net::URLRequestContextGetter*
773TestingProfile::GetMediaRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40774 const base::FilePath& partition_path,
[email protected]27ddfed22012-10-30 23:22:43775 bool in_memory) {
[email protected]10eb28162012-09-18 03:04:09776 return NULL;
777}
778
[email protected]33ad6ce92013-08-27 14:39:08779net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
780 if (!extensions_request_context_.get())
781 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
782 return extensions_request_context_.get();
783}
784
[email protected]fadc607b62011-02-07 17:55:50785net::SSLConfigService* TestingProfile::GetSSLConfigService() {
[email protected]dc6655b2012-12-14 05:58:24786 if (!GetRequestContext())
787 return NULL;
788 return GetRequestContext()->GetURLRequestContext()->ssl_config_service();
[email protected]fadc607b62011-02-07 17:55:50789}
790
[email protected]10eb28162012-09-18 03:04:09791net::URLRequestContextGetter*
[email protected]6bd30072013-02-08 18:17:11792TestingProfile::CreateRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40793 const base::FilePath& partition_path,
[email protected]6bd30072013-02-08 18:17:11794 bool in_memory,
[email protected]7571263c2014-03-10 22:57:09795 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15796 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]10eb28162012-09-18 03:04:09797 // We don't test storage partitions here yet, so returning the same dummy
[email protected]d9696672011-03-15 22:45:09798 // context is sufficient for now.
799 return GetRequestContext();
800}
801
[email protected]df02aca2012-02-09 21:03:20802content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]37ac95b2013-07-23 23:39:35803 if (!resource_context_)
804 resource_context_ = new content::MockResourceContext();
805 return resource_context_;
[email protected]e89b77d2011-04-15 18:58:10806}
807
[email protected]dbbad7a2010-08-13 18:18:36808HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38809 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05810 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
[email protected]701550ad2012-09-06 23:43:29811#if defined(ENABLE_EXTENSIONS)
[email protected]994fdb252014-06-26 13:26:37812 ExtensionService* extension_service =
813 extensions::ExtensionSystem::Get(this)->extension_service();
[email protected]646c29cb62012-06-18 14:31:05814 if (extension_service)
815 host_content_settings_map_->RegisterExtensionService(extension_service);
[email protected]701550ad2012-09-06 23:43:29816#endif
[email protected]35552dc52011-07-12 09:04:38817 }
[email protected]dbbad7a2010-08-13 18:18:36818 return host_content_settings_map_.get();
819}
820
[email protected]139355f2014-05-11 14:21:28821content::BrowserPluginGuestManager* TestingProfile::GetGuestManager() {
[email protected]1df3d972014-06-11 04:55:18822#if defined(ENABLE_EXTENSIONS)
[email protected]24569262014-05-06 03:31:30823 return GuestViewManager::FromBrowserContext(this);
[email protected]1df3d972014-06-11 04:55:18824#else
825 return NULL;
826#endif
[email protected]24569262014-05-06 03:31:30827}
828
[email protected]c5c89d042014-06-13 14:43:37829content::PushMessagingService* TestingProfile::GetPushMessagingService() {
830 return NULL;
831}
832
[email protected]fadc607b62011-02-07 17:55:50833bool TestingProfile::IsSameProfile(Profile *p) {
834 return this == p;
835}
836
837base::Time TestingProfile::GetStartTime() const {
838 return start_time_;
839}
840
[email protected]152ea302013-02-11 04:08:40841base::FilePath TestingProfile::last_selected_directory() {
[email protected]fadc607b62011-02-07 17:55:50842 return last_selected_directory_;
843}
844
[email protected]152ea302013-02-11 04:08:40845void TestingProfile::set_last_selected_directory(const base::FilePath& path) {
[email protected]fadc607b62011-02-07 17:55:50846 last_selected_directory_ = path;
847}
848
[email protected]cc5bfd42010-11-24 14:44:02849PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07850 if (!pref_proxy_config_tracker_.get()) {
[email protected]e2930d0902013-07-17 05:25:42851 // TestingProfile is used in unit tests, where local state is not available.
[email protected]6f96cbcb2011-11-04 02:26:07852 pref_proxy_config_tracker_.reset(
[email protected]e2930d0902013-07-17 05:25:42853 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(GetPrefs(),
854 NULL));
[email protected]6f96cbcb2011-11-04 02:26:07855 }
856 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02857}
858
[email protected]0bfc29a2009-04-27 16:15:44859void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]a5894fe2012-11-01 14:40:56860 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02861 HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
[email protected]a5894fe2012-11-01 14:40:56862 DCHECK(history_service);
[email protected]bb024fe2013-05-10 21:33:26863 DCHECK(base::MessageLoop::current());
[email protected]0bfc29a2009-04-27 16:15:44864
[email protected]58bef622014-07-03 11:41:14865 base::CancelableTaskTracker tracker;
[email protected]6a0b6672014-07-29 12:00:23866 history_service->ScheduleDBTask(
867 scoped_ptr<history::HistoryDBTask>(
868 new QuittingHistoryDBTask()),
869 &tracker);
[email protected]bb024fe2013-05-10 21:33:26870 base::MessageLoop::current()->Run();
[email protected]0bfc29a2009-04-27 16:15:44871}
[email protected]345a8b72009-09-29 09:11:44872
[email protected]8382d8c2011-09-15 03:43:17873chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
874 return NULL;
875}
876
[email protected]a4205202014-06-02 16:03:08877DevToolsNetworkController* TestingProfile::GetDevToolsNetworkController() {
878 return NULL;
879}
880
[email protected]e0e1fc22012-11-05 20:25:36881void TestingProfile::ClearNetworkingHistorySince(
882 base::Time time,
883 const base::Closure& completion) {
884 if (!completion.is_null()) {
885 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
886 }
[email protected]02896a82011-09-21 18:54:32887}
888
[email protected]671f9c62011-10-28 19:22:07889GURL TestingProfile::GetHomePage() {
890 return GURL(chrome::kChromeUINewTabURL);
891}
892
[email protected]c753f142013-02-10 13:14:04893PrefService* TestingProfile::GetOffTheRecordPrefs() {
[email protected]fadc607b62011-02-07 17:55:50894 return NULL;
895}
896
[email protected]36fb2c7c2011-04-04 15:49:08897quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18898#if defined(ENABLE_EXTENSIONS)
[email protected]36fb2c7c2011-04-04 15:49:08899 return GetExtensionSpecialStoragePolicy();
[email protected]50356f82014-07-31 13:25:18900#else
901 return NULL;
902#endif
[email protected]36fb2c7c2011-04-04 15:49:08903}
904
[email protected]c5bbe0e2014-08-01 23:23:30905content::SSLHostStateDelegate* TestingProfile::GetSSLHostStateDelegate() {
906 return NULL;
907}
908
[email protected]1bee6ed2012-03-14 06:46:36909bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
910 return true;
911}
[email protected]ef92e172012-04-25 19:40:41912
[email protected]4251165a2013-07-17 04:33:40913bool TestingProfile::IsGuestSession() const {
[email protected]e9476cd2013-12-04 05:55:34914 return guest_session_;
[email protected]4251165a2013-07-17 04:33:40915}
[email protected]1101dbc52013-10-05 00:19:12916
[email protected]6c0ca7fc2012-10-05 16:27:22917Profile::ExitType TestingProfile::GetLastSessionExitType() {
918 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED;
[email protected]2ca1ea662012-10-04 02:26:36919}
920
[email protected]5c4c89f2012-08-07 21:09:59921TestingProfile::Builder::Builder()
922 : build_called_(false),
[email protected]9819fd02013-08-22 10:49:39923 delegate_(NULL),
[email protected]e9476cd2013-12-04 05:55:34924 incognito_(false),
925 guest_session_(false) {
[email protected]5c4c89f2012-08-07 21:09:59926}
927
928TestingProfile::Builder::~Builder() {
929}
930
[email protected]152ea302013-02-11 04:08:40931void TestingProfile::Builder::SetPath(const base::FilePath& path) {
[email protected]5c4c89f2012-08-07 21:09:59932 path_ = path;
933}
934
935void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
936 delegate_ = delegate;
937}
938
[email protected]50356f82014-07-31 13:25:18939#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59940void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
941 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
942 extension_policy_ = policy;
943}
[email protected]50356f82014-07-31 13:25:18944#endif
[email protected]5c4c89f2012-08-07 21:09:59945
[email protected]5b199522012-12-22 17:24:44946void TestingProfile::Builder::SetPrefService(
947 scoped_ptr<PrefServiceSyncable> prefs) {
[email protected]5c4c89f2012-08-07 21:09:59948 pref_service_ = prefs.Pass();
949}
950
[email protected]9819fd02013-08-22 10:49:39951void TestingProfile::Builder::SetIncognito() {
952 incognito_ = true;
953}
954
[email protected]e9476cd2013-12-04 05:55:34955void TestingProfile::Builder::SetGuestSession() {
956 guest_session_ = true;
957}
958
[email protected]d20d0432014-06-12 17:14:05959void TestingProfile::Builder::SetSupervisedUserId(
960 const std::string& supervised_user_id) {
961 supervised_user_id_ = supervised_user_id;
[email protected]371662e372013-10-17 22:05:22962}
963
[email protected]a43b9a82013-11-28 16:56:41964void TestingProfile::Builder::SetPolicyService(
965 scoped_ptr<policy::PolicyService> policy_service) {
966 policy_service_ = policy_service.Pass();
967}
968
[email protected]9819fd02013-08-22 10:49:39969void TestingProfile::Builder::AddTestingFactory(
970 BrowserContextKeyedServiceFactory* service_factory,
[email protected]d45c5d52013-12-04 23:41:16971 BrowserContextKeyedServiceFactory::TestingFactoryFunction callback) {
[email protected]9819fd02013-08-22 10:49:39972 testing_factories_.push_back(std::make_pair(service_factory, callback));
973}
974
[email protected]5c4c89f2012-08-07 21:09:59975scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
976 DCHECK(!build_called_);
977 build_called_ = true;
[email protected]371662e372013-10-17 22:05:22978
[email protected]5c4c89f2012-08-07 21:09:59979 return scoped_ptr<TestingProfile>(new TestingProfile(
980 path_,
981 delegate_,
[email protected]50356f82014-07-31 13:25:18982#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59983 extension_policy_,
[email protected]50356f82014-07-31 13:25:18984#endif
[email protected]9819fd02013-08-22 10:49:39985 pref_service_.Pass(),
986 incognito_,
[email protected]e9476cd2013-12-04 05:55:34987 guest_session_,
[email protected]d20d0432014-06-12 17:14:05988 supervised_user_id_,
[email protected]a43b9a82013-11-28 16:56:41989 policy_service_.Pass(),
[email protected]9819fd02013-08-22 10:49:39990 testing_factories_));
[email protected]5c4c89f2012-08-07 21:09:59991}