blob: e652fde566fc56a38d896c19045db6a3905b9aba [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"
mukai87a8d402014-09-15 20:15:0922#include "chrome/browser/extensions/extension_service.h"
[email protected]0e199ed72014-07-04 13:05:2223#include "chrome/browser/favicon/chrome_favicon_client_factory.h"
[email protected]ee2ed42c2011-04-28 22:19:1424#include "chrome/browser/favicon/favicon_service.h"
[email protected]f3d2b312012-08-23 22:27:5925#include "chrome/browser/favicon/favicon_service_factory.h"
[email protected]ad34610c2014-06-04 14:43:5226#include "chrome/browser/history/chrome_history_client.h"
27#include "chrome/browser/history/chrome_history_client_factory.h"
[email protected]075ae732009-02-11 23:58:3128#include "chrome/browser/history/history_backend.h"
[email protected]d891f522013-02-08 03:24:4129#include "chrome/browser/history/history_db_task.h"
[email protected]6a2c09f2013-01-25 04:50:0730#include "chrome/browser/history/history_service.h"
[email protected]f61f4782012-06-08 21:54:2131#include "chrome/browser/history/history_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3632#include "chrome/browser/history/top_sites.h"
[email protected]a50e16a2013-04-25 14:07:1733#include "chrome/browser/history/web_history_service_factory.h"
[email protected]53c5b0f2013-07-22 10:54:1134#include "chrome/browser/net/pref_proxy_config_tracker.h"
[email protected]6f96cbcb2011-11-04 02:26:0735#include "chrome/browser/net/proxy_service_factory.h"
[email protected]dbbad7a2010-08-13 18:18:3636#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]dcb72d52011-04-13 12:36:5337#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]d81bb3e2013-05-02 18:17:0338#include "chrome/browser/policy/profile_policy_connector.h"
39#include "chrome/browser/policy/profile_policy_connector_factory.h"
[email protected]37858e52010-08-26 00:22:0240#include "chrome/browser/prefs/browser_prefs.h"
[email protected]c753f142013-02-10 13:14:0441#include "chrome/browser/prefs/pref_service_syncable.h"
[email protected]4850a7f2011-03-08 23:36:5942#include "chrome/browser/prerender/prerender_manager.h"
[email protected]7e75e4a2013-05-17 17:20:0343#include "chrome/browser/profiles/chrome_browser_main_extra_parts_profiles.h"
[email protected]371662e372013-10-17 22:05:2244#include "chrome/browser/profiles/profile_manager.h"
[email protected]27ddfed22012-10-30 23:22:4345#include "chrome/browser/profiles/storage_partition_descriptor.h"
[email protected]16bd088b2012-04-03 08:12:1246#include "chrome/browser/search_engines/template_url_fetcher_factory.h"
[email protected]d07edd42012-05-14 23:49:4647#include "chrome/browser/webdata/web_data_service_factory.h"
[email protected]a9afddb2009-02-12 17:49:4248#include "chrome/common/chrome_constants.h"
[email protected]9e33d7e2011-09-30 16:43:5449#include "chrome/common/chrome_switches.h"
[email protected]e000daf2013-07-31 16:50:5850#include "chrome/common/pref_names.h"
[email protected]dbbad7a2010-08-13 18:18:3651#include "chrome/common/url_constants.h"
[email protected]18a15ca82012-08-30 00:28:1652#include "chrome/test/base/history_index_restore_observer.h"
[email protected]7688968a2013-02-12 21:45:1353#include "chrome/test/base/testing_pref_service_syncable.h"
[email protected]af44e7fb2011-07-29 18:32:3254#include "chrome/test/base/ui_test_utils.h"
[email protected]a90c8ca2014-05-20 17:16:0455#include "components/bookmarks/browser/bookmark_model.h"
56#include "components/bookmarks/common/bookmark_constants.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)
[email protected]50356f82014-07-31 13:25:1885#include "chrome/browser/extensions/extension_special_storage_policy.h"
[email protected]d5a74be2014-08-08 01:01:2786#include "chrome/browser/extensions/extension_system_factory.h"
87#include "chrome/browser/extensions/test_extension_system.h"
[email protected]994fdb252014-06-26 13:26:3788#include "extensions/browser/extension_system.h"
[email protected]140d6cd92014-08-12 18:26:4689#include "extensions/browser/guest_view/guest_view_manager.h"
[email protected]1df3d972014-06-11 04:55:1890#endif
91
[email protected]bd3093c2014-06-13 07:38:1692#if defined(OS_ANDROID)
93#include "chrome/browser/signin/android_profile_oauth2_token_service.h"
94#endif
95
[email protected]e1480482013-09-11 11:49:5896#if defined(ENABLE_MANAGED_USERS)
[email protected]cce15bb2014-06-17 13:43:5197#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
98#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]e1480482013-09-11 11:49:5899#endif
100
[email protected]e1acf6f2008-10-27 20:43:33101using base::Time;
[email protected]631bb742011-11-02 11:29:39102using content::BrowserThread;
[email protected]b441a8492012-06-06 14:55:57103using content::DownloadManagerDelegate;
[email protected]3c887412010-04-19 20:30:23104using testing::NiceMock;
[email protected]4772b072010-03-30 17:45:46105using testing::Return;
[email protected]e1acf6f2008-10-27 20:43:33106
[email protected]d364c652008-08-29 19:46:56107namespace {
108
[email protected]0bfc29a2009-04-27 16:15:44109// Task used to make sure history has finished processing a request. Intended
110// for use with BlockUntilHistoryProcessesPendingRequests.
111
[email protected]d891f522013-02-08 03:24:41112class QuittingHistoryDBTask : public history::HistoryDBTask {
[email protected]0bfc29a2009-04-27 16:15:44113 public:
114 QuittingHistoryDBTask() {}
115
116 virtual bool RunOnDBThread(history::HistoryBackend* backend,
[email protected]be9826e62013-02-07 02:00:58117 history::HistoryDatabase* db) OVERRIDE {
[email protected]0bfc29a2009-04-27 16:15:44118 return true;
119 }
120
[email protected]be9826e62013-02-07 02:00:58121 virtual void DoneRunOnMainThread() OVERRIDE {
[email protected]bb024fe2013-05-10 21:33:26122 base::MessageLoop::current()->Quit();
[email protected]0bfc29a2009-04-27 16:15:44123 }
124
125 private:
[email protected]be9826e62013-02-07 02:00:58126 virtual ~QuittingHistoryDBTask() {}
[email protected]7991a232009-11-06 01:55:48127
[email protected]0bfc29a2009-04-27 16:15:44128 DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask);
129};
130
[email protected]33ad6ce92013-08-27 14:39:08131class TestExtensionURLRequestContext : public net::URLRequestContext {
132 public:
133 TestExtensionURLRequestContext() {
[email protected]7c4b66b2014-01-04 12:28:13134 net::CookieMonster* cookie_monster =
[email protected]9a6c2aa2014-01-11 22:39:39135 content::CreateCookieStore(content::CookieStoreConfig())->
136 GetCookieMonster();
[email protected]5edff3c52014-06-23 20:27:48137 const char* const schemes[] = {extensions::kExtensionScheme};
138 cookie_monster->SetCookieableSchemes(schemes, arraysize(schemes));
[email protected]33ad6ce92013-08-27 14:39:08139 set_cookie_store(cookie_monster);
140 }
141
[email protected]424559492014-07-22 00:27:40142 virtual ~TestExtensionURLRequestContext() {
143 AssertNoURLRequests();
144 }
[email protected]33ad6ce92013-08-27 14:39:08145};
146
147class TestExtensionURLRequestContextGetter
148 : public net::URLRequestContextGetter {
149 public:
150 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
151 if (!context_.get())
152 context_.reset(new TestExtensionURLRequestContext());
153 return context_.get();
154 }
155 virtual scoped_refptr<base::SingleThreadTaskRunner>
156 GetNetworkTaskRunner() const OVERRIDE {
157 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
158 }
159
160 protected:
161 virtual ~TestExtensionURLRequestContextGetter() {}
162
163 private:
164 scoped_ptr<net::URLRequestContext> context_;
165};
166
[email protected]1d51882f2013-11-12 01:59:02167#if defined(ENABLE_NOTIFICATIONS)
[email protected]540380fc2014-03-14 10:10:34168KeyedService* CreateTestDesktopNotificationService(
[email protected]c7fa4362013-04-26 18:09:02169 content::BrowserContext* profile) {
peterb1f28c8f2014-09-11 17:30:11170 return new DesktopNotificationService(static_cast<Profile*>(profile));
[email protected]dcb72d52011-04-13 12:36:53171}
[email protected]1d51882f2013-11-12 01:59:02172#endif
[email protected]dcb72d52011-04-13 12:36:53173
[email protected]d364c652008-08-29 19:46:56174} // namespace
175
[email protected]89dbb1772012-07-17 13:47:25176// static
177#if defined(OS_CHROMEOS)
178// Must be kept in sync with
179// ChromeBrowserMainPartsChromeos::PreEarlyInitialization.
[email protected]0f13fe8d2013-11-19 08:25:00180const char TestingProfile::kTestUserProfileDir[] = "test-user";
[email protected]89dbb1772012-07-17 13:47:25181#else
182const char TestingProfile::kTestUserProfileDir[] = "Default";
183#endif
184
[email protected]f25387b2008-08-21 15:20:33185TestingProfile::TestingProfile()
[email protected]ea6f76572008-12-18 00:09:55186 : start_time_(Time::Now()),
[email protected]2fb7dc982010-09-29 12:24:28187 testing_prefs_(NULL),
[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),
[email protected]9819fd02013-08-22 10:49:39206 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09207 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34208 guest_session_(false),
[email protected]49a25632011-08-31 17:03:48209 last_session_exited_cleanly_(true),
210 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53211 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44212 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35213 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17214 delegate_(NULL) {
[email protected]49a25632011-08-31 17:03:48215 Init();
[email protected]537c1082011-12-02 02:37:17216 FinishInit();
217}
218
[email protected]152ea302013-02-11 04:08:40219TestingProfile::TestingProfile(const base::FilePath& path,
[email protected]537c1082011-12-02 02:37:17220 Delegate* delegate)
221 : start_time_(Time::Now()),
222 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39223 force_incognito_(false),
[email protected]074311a2013-02-28 23:14:09224 original_profile_(NULL),
[email protected]e9476cd2013-12-04 05:55:34225 guest_session_(false),
[email protected]537c1082011-12-02 02:37:17226 last_session_exited_cleanly_(true),
227 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53228 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44229 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35230 resource_context_(NULL),
[email protected]537c1082011-12-02 02:37:17231 delegate_(delegate) {
232 Init();
233 if (delegate_) {
[email protected]bb024fe2013-05-10 21:33:26234 base::MessageLoop::current()->PostTask(
235 FROM_HERE,
236 base::Bind(&TestingProfile::FinishInit, base::Unretained(this)));
[email protected]537c1082011-12-02 02:37:17237 } else {
238 FinishInit();
239 }
[email protected]49a25632011-08-31 17:03:48240}
241
[email protected]5c4c89f2012-08-07 21:09:59242TestingProfile::TestingProfile(
[email protected]152ea302013-02-11 04:08:40243 const base::FilePath& path,
[email protected]5c4c89f2012-08-07 21:09:59244 Delegate* delegate,
[email protected]50356f82014-07-31 13:25:18245#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59246 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
[email protected]50356f82014-07-31 13:25:18247#endif
[email protected]9819fd02013-08-22 10:49:39248 scoped_ptr<PrefServiceSyncable> prefs,
tapted1ab9b7092014-09-15 23:49:40249 TestingProfile* parent,
[email protected]e9476cd2013-12-04 05:55:34250 bool guest_session,
[email protected]d20d0432014-06-12 17:14:05251 const std::string& supervised_user_id,
[email protected]a43b9a82013-11-28 16:56:41252 scoped_ptr<policy::PolicyService> policy_service,
[email protected]9819fd02013-08-22 10:49:39253 const TestingFactories& factories)
[email protected]5c4c89f2012-08-07 21:09:59254 : start_time_(Time::Now()),
255 prefs_(prefs.release()),
256 testing_prefs_(NULL),
[email protected]9819fd02013-08-22 10:49:39257 force_incognito_(false),
tapted1ab9b7092014-09-15 23:49:40258 original_profile_(parent),
[email protected]e9476cd2013-12-04 05:55:34259 guest_session_(guest_session),
[email protected]d20d0432014-06-12 17:14:05260 supervised_user_id_(supervised_user_id),
[email protected]5c4c89f2012-08-07 21:09:59261 last_session_exited_cleanly_(true),
[email protected]50356f82014-07-31 13:25:18262#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59263 extension_special_storage_policy_(extension_policy),
[email protected]50356f82014-07-31 13:25:18264#endif
[email protected]5c4c89f2012-08-07 21:09:59265 profile_path_(path),
[email protected]f0901a22013-05-27 21:37:53266 browser_context_dependency_manager_(
[email protected]f1484c52013-05-22 23:25:44267 BrowserContextDependencyManager::GetInstance()),
[email protected]37ac95b2013-07-23 23:39:35268 resource_context_(NULL),
[email protected]a43b9a82013-11-28 16:56:41269 delegate_(delegate),
270 policy_service_(policy_service.release()) {
tapted1ab9b7092014-09-15 23:49:40271 if (parent)
272 parent->SetOffTheRecordProfile(scoped_ptr<Profile>(this));
273
[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());
tapted1ab9b7092014-09-15 23:49:40345 else if (IsOffTheRecord())
346 CreateIncognitoPrefService();
[email protected]c7fa4362013-04-26 18:09:02347 else
348 CreateTestingPrefService();
[email protected]75fee372013-03-06 00:42:44349
[email protected]7567484142013-07-11 17:36:07350 if (!base::PathExists(profile_path_))
[email protected]426d1c92013-12-03 20:08:54351 base::CreateDirectory(profile_path_);
[email protected]d41e6572012-05-04 20:49:42352
[email protected]d81bb3e2013-05-02 18:17:03353 // TODO(joaodasilva): remove this once this PKS isn't created in ProfileImpl
354 // anymore, after converting the PrefService to a PKS. Until then it must
355 // be associated with a TestingProfile too.
[email protected]eb83c392013-11-07 12:53:24356 if (!IsOffTheRecord())
357 CreateProfilePolicyConnector();
[email protected]d81bb3e2013-05-02 18:17:03358
[email protected]d5a74be2014-08-08 01:01:27359#if defined(ENABLE_EXTENSIONS)
[email protected]bd306722012-07-11 20:43:59360 extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory(
361 this, extensions::TestExtensionSystem::Build);
[email protected]d5a74be2014-08-08 01:01:27362#endif
[email protected]31d8f5f22012-04-02 15:22:08363
tapted1ab9b7092014-09-15 23:49:40364 // Prefs for incognito profiles are set in CreateIncognitoPrefService() by
365 // simulating ProfileImpl::GetOffTheRecordPrefs().
366 if (!IsOffTheRecord()) {
367 DCHECK(!original_profile_);
[email protected]d45c5d52013-12-04 23:41:16368 user_prefs::PrefRegistrySyncable* pref_registry =
369 static_cast<user_prefs::PrefRegistrySyncable*>(
370 prefs_->DeprecatedGetPrefRegistry());
371 browser_context_dependency_manager_->
372 RegisterProfilePrefsForServices(this, pref_registry);
373 }
374
[email protected]9819fd02013-08-22 10:49:39375 browser_context_dependency_manager_->CreateBrowserContextServicesForTest(
[email protected]d45c5d52013-12-04 23:41:16376 this);
[email protected]49a25632011-08-31 17:03:48377
[email protected]5fd2e842012-03-01 00:29:11378#if defined(ENABLE_NOTIFICATIONS)
[email protected]dcb72d52011-04-13 12:36:53379 // Install profile keyed service factory hooks for dummy/test services
[email protected]7722653b2011-06-03 17:28:22380 DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory(
381 this, CreateTestDesktopNotificationService);
[email protected]5fd2e842012-03-01 00:29:11382#endif
[email protected]e1480482013-09-11 11:49:58383
384#if defined(ENABLE_MANAGED_USERS)
tapted1ab9b7092014-09-15 23:49:40385 if (!IsOffTheRecord()) {
386 SupervisedUserSettingsService* settings_service =
387 SupervisedUserSettingsServiceFactory::GetForProfile(this);
388 TestingPrefStore* store = new TestingPrefStore();
389 settings_service->Init(store);
390 store->SetInitializationCompleted();
391 }
[email protected]e1480482013-09-11 11:49:58392#endif
[email protected]d6001872013-09-24 18:31:26393
394 profile_name_ = "testing_profile";
[email protected]537c1082011-12-02 02:37:17395}
[email protected]12463cd2011-07-19 09:40:20396
[email protected]537c1082011-12-02 02:37:17397void TestingProfile::FinishInit() {
[email protected]080adba92011-12-07 19:00:40398 DCHECK(content::NotificationService::current());
[email protected]ad50def52011-10-19 23:17:07399 content::NotificationService::current()->Notify(
[email protected]12463cd2011-07-19 09:40:20400 chrome::NOTIFICATION_PROFILE_CREATED,
[email protected]6c2381d2011-10-19 02:52:53401 content::Source<Profile>(static_cast<Profile*>(this)),
[email protected]ad50def52011-10-19 23:17:07402 content::NotificationService::NoDetails());
[email protected]537c1082011-12-02 02:37:17403
[email protected]371662e372013-10-17 22:05:22404 ProfileManager* profile_manager = g_browser_process->profile_manager();
405 if (profile_manager)
406 profile_manager->InitProfileUserPrefs(this);
407
[email protected]537c1082011-12-02 02:37:17408 if (delegate_)
[email protected]00b33f802012-03-13 07:46:03409 delegate_->OnProfileCreated(this, true, false);
[email protected]ab23dbe2010-08-12 02:10:46410}
411
[email protected]f25387b2008-08-21 15:20:33412TestingProfile::~TestingProfile() {
[email protected]9819fd02013-08-22 10:49:39413 // Revert to non-incognito mode before shutdown.
414 force_incognito_ = false;
415
tapted1ab9b7092014-09-15 23:49:40416 // If this profile owns an incognito profile, tear it down first.
417 incognito_profile_.reset();
418
[email protected]37ac95b2013-07-23 23:39:35419 // Any objects holding live URLFetchers should be deleted before teardown.
420 TemplateURLFetcherFactory::ShutdownForProfile(this);
421
[email protected]a7c4c032012-07-27 00:36:32422 MaybeSendDestroyedNotification();
[email protected]5cdcd152011-03-31 22:48:52423
[email protected]f0901a22013-05-27 21:37:53424 browser_context_dependency_manager_->DestroyBrowserContextServices(this);
[email protected]5cdcd152011-03-31 22:48:52425
[email protected]5c6ac842013-06-02 23:37:03426 if (host_content_settings_map_.get())
[email protected]35552dc52011-07-12 09:04:38427 host_content_settings_map_->ShutdownOnUIThread();
428
[email protected]809cc4d2010-10-27 15:22:54429 DestroyTopSites();
[email protected]cdba46992011-06-07 11:51:39430
[email protected]cc5bfd42010-11-24 14:44:02431 if (pref_proxy_config_tracker_.get())
432 pref_proxy_config_tracker_->DetachFromPrefService();
[email protected]37ac95b2013-07-23 23:39:35433 // Failing a post == leaks == heapcheck failure. Make that an immediate test
434 // failure.
435 if (resource_context_) {
436 CHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE,
437 resource_context_));
438 resource_context_ = NULL;
439 content::RunAllPendingInMessageLoop(BrowserThread::IO);
440 }
[email protected]f25387b2008-08-21 15:20:33441}
initial.commit09911bf2008-07-26 23:55:29442
[email protected]540380fc2014-03-14 10:10:34443static KeyedService* BuildFaviconService(content::BrowserContext* profile) {
[email protected]0e199ed72014-07-04 13:05:22444 FaviconClient* favicon_client =
445 ChromeFaviconClientFactory::GetForProfile(static_cast<Profile*>(profile));
446 return new FaviconService(static_cast<Profile*>(profile), favicon_client);
[email protected]f3d2b312012-08-23 22:27:59447}
448
[email protected]d2879af2010-02-08 16:02:56449void TestingProfile::CreateFaviconService() {
[email protected]f3d2b312012-08-23 22:27:59450 // It is up to the caller to create the history service if one is needed.
451 FaviconServiceFactory::GetInstance()->SetTestingFactory(
452 this, BuildFaviconService);
[email protected]d2879af2010-02-08 16:02:56453}
454
[email protected]ad34610c2014-06-04 14:43:52455static KeyedService* BuildHistoryService(content::BrowserContext* context) {
456 Profile* profile = static_cast<Profile*>(context);
457 return new HistoryService(ChromeHistoryClientFactory::GetForProfile(profile),
458 profile);
[email protected]f61f4782012-06-08 21:54:21459}
460
[email protected]608e7e02013-07-24 12:23:31461bool TestingProfile::CreateHistoryService(bool delete_file, bool no_db) {
[email protected]809cc4d2010-10-27 15:22:54462 DestroyHistoryService();
[email protected]f25387b2008-08-21 15:20:33463 if (delete_file) {
[email protected]152ea302013-02-11 04:08:40464 base::FilePath path = GetPath();
[email protected]f7011fcb2009-01-28 21:54:32465 path = path.Append(chrome::kHistoryFilename);
[email protected]608e7e02013-07-24 12:23:31466 if (!base::DeleteFile(path, false) || base::PathExists(path))
467 return false;
[email protected]f25387b2008-08-21 15:20:33468 }
[email protected]7019bb6d2012-08-23 09:09:48469 // This will create and init the history service.
[email protected]f61f4782012-06-08 21:54:21470 HistoryService* history_service = static_cast<HistoryService*>(
471 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(
[email protected]a5894fe2012-11-01 14:40:56472 this, BuildHistoryService));
[email protected]ad34610c2014-06-04 14:43:52473 if (!history_service->Init(this->GetPath(), no_db)) {
[email protected]f61f4782012-06-08 21:54:21474 HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL);
[email protected]7019bb6d2012-08-23 09:09:48475 }
[email protected]a50e16a2013-04-25 14:07:17476 // Disable WebHistoryService by default, since it makes network requests.
477 WebHistoryServiceFactory::GetInstance()->SetTestingFactory(this, NULL);
[email protected]608e7e02013-07-24 12:23:31478 return true;
initial.commit09911bf2008-07-26 23:55:29479}
480
481void TestingProfile::DestroyHistoryService() {
[email protected]a5894fe2012-11-01 14:40:56482 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02483 HistoryServiceFactory::GetForProfileWithoutCreating(this);
[email protected]a5894fe2012-11-01 14:40:56484 if (!history_service)
initial.commit09911bf2008-07-26 23:55:29485 return;
486
[email protected]378970412014-06-12 07:31:17487 history_service->ClearCachedDataForContextID(0);
[email protected]bb024fe2013-05-10 21:33:26488 history_service->SetOnBackendDestroyTask(base::MessageLoop::QuitClosure());
[email protected]f61f4782012-06-08 21:54:21489 history_service->Cleanup();
490 HistoryServiceFactory::ShutdownForProfile(this);
initial.commit09911bf2008-07-26 23:55:29491
492 // Wait for the backend class to terminate before deleting the files and
493 // moving to the next test. Note: if this never terminates, somebody is
494 // probably leaking a reference to the history backend, so it never calls
495 // our destroy task.
[email protected]bb024fe2013-05-10 21:33:26496 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29497
initial.commit09911bf2008-07-26 23:55:29498 // Make sure we don't have any event pending that could disrupt the next
499 // test.
[email protected]bb024fe2013-05-10 21:33:26500 base::MessageLoop::current()->PostTask(FROM_HERE,
501 base::MessageLoop::QuitClosure());
502 base::MessageLoop::current()->Run();
initial.commit09911bf2008-07-26 23:55:29503}
[email protected]4d0cd7ce2008-08-11 16:40:57504
[email protected]809cc4d2010-10-27 15:22:54505void TestingProfile::CreateTopSites() {
506 DestroyTopSites();
[email protected]9a93d3432013-04-29 20:52:12507 top_sites_ = history::TopSites::Create(
508 this, GetPath().Append(chrome::kTopSitesFilename));
[email protected]809cc4d2010-10-27 15:22:54509}
510
511void TestingProfile::DestroyTopSites() {
512 if (top_sites_.get()) {
513 top_sites_->Shutdown();
514 top_sites_ = NULL;
[email protected]9a93d3432013-04-29 20:52:12515 // TopSitesImpl::Shutdown schedules some tasks (from TopSitesBackend) that
516 // need to be run to properly shutdown. Run all pending tasks now. This is
[email protected]809cc4d2010-10-27 15:22:54517 // normally handled by browser_process shutdown.
[email protected]bb024fe2013-05-10 21:33:26518 if (base::MessageLoop::current())
519 base::MessageLoop::current()->RunUntilIdle();
[email protected]809cc4d2010-10-27 15:22:54520 }
521}
522
[email protected]540380fc2014-03-14 10:10:34523static KeyedService* BuildBookmarkModel(content::BrowserContext* context) {
[email protected]c7fa4362013-04-26 18:09:02524 Profile* profile = static_cast<Profile*>(context);
[email protected]6a848b52014-04-26 22:06:54525 ChromeBookmarkClient* bookmark_client =
[email protected]0b0e8a12014-06-14 00:11:31526 ChromeBookmarkClientFactory::GetForProfile(profile);
mpearson56bd38e2014-08-27 22:12:57527 BookmarkModel* bookmark_model = new BookmarkModel(bookmark_client);
[email protected]0b0e8a12014-06-14 00:11:31528 bookmark_client->Init(bookmark_model);
529 bookmark_model->Load(profile->GetPrefs(),
530 profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
531 profile->GetPath(),
532 profile->GetIOTaskRunner(),
533 content::BrowserThread::GetMessageLoopProxyForThread(
534 content::BrowserThread::UI));
535 return bookmark_model;
536}
537
538static KeyedService* BuildChromeBookmarkClient(
539 content::BrowserContext* context) {
540 return new ChromeBookmarkClient(static_cast<Profile*>(context));
[email protected]f61f4782012-06-08 21:54:21541}
542
[email protected]ad34610c2014-06-04 14:43:52543static KeyedService* BuildChromeHistoryClient(
544 content::BrowserContext* context) {
545 Profile* profile = static_cast<Profile*>(context);
546 return new ChromeHistoryClient(BookmarkModelFactory::GetForProfile(profile));
547}
548
[email protected]d8e41ed2008-09-11 15:22:32549void TestingProfile::CreateBookmarkModel(bool delete_file) {
[email protected]90ef13132008-08-27 03:27:46550 if (delete_file) {
[email protected]d4a67d52014-04-12 05:17:03551 base::FilePath path = GetPath().Append(bookmarks::kBookmarksFileName);
[email protected]dd3aa792013-07-16 19:10:23552 base::DeleteFile(path, false);
[email protected]90ef13132008-08-27 03:27:46553 }
[email protected]ad34610c2014-06-04 14:43:52554 ChromeHistoryClientFactory::GetInstance()->SetTestingFactory(
555 this, BuildChromeHistoryClient);
[email protected]0b0e8a12014-06-14 00:11:31556 ChromeBookmarkClientFactory::GetInstance()->SetTestingFactory(
557 this, BuildChromeBookmarkClient);
558 // This creates the BookmarkModel.
559 ignore_result(BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse(
560 this, BuildBookmarkModel));
[email protected]4d0cd7ce2008-08-11 16:40:57561}
[email protected]d2c017a2008-08-13 21:51:45562
[email protected]540380fc2014-03-14 10:10:34563static KeyedService* BuildWebDataService(content::BrowserContext* profile) {
[email protected]c7fa4362013-04-26 18:09:02564 return new WebDataServiceWrapper(static_cast<Profile*>(profile));
[email protected]d07edd42012-05-14 23:49:46565}
[email protected]2609bc12010-01-24 08:32:55566
[email protected]d07edd42012-05-14 23:49:46567void TestingProfile::CreateWebDataService() {
[email protected]a5c65292012-05-25 00:50:01568 WebDataServiceFactory::GetInstance()->SetTestingFactory(
[email protected]d07edd42012-05-14 23:49:46569 this, BuildWebDataService);
[email protected]2609bc12010-01-24 08:32:55570}
571
[email protected]18a15ca82012-08-30 00:28:16572void TestingProfile::BlockUntilHistoryIndexIsRefreshed() {
573 // Only get the history service if it actually exists since the caller of the
574 // test should explicitly call CreateHistoryService to build it.
575 HistoryService* history_service =
576 HistoryServiceFactory::GetForProfileWithoutCreating(this);
577 DCHECK(history_service);
578 history::InMemoryURLIndex* index = history_service->InMemoryIndex();
579 if (!index || index->restored())
580 return;
581 base::RunLoop run_loop;
582 HistoryIndexRestoreObserver observer(
583 content::GetQuitTaskForRunLoop(&run_loop));
584 index->set_restore_cache_observer(&observer);
585 run_loop.Run();
586 index->set_restore_cache_observer(NULL);
587 DCHECK(index->restored());
588}
589
[email protected]120abf132011-09-27 21:38:06590// TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded?
[email protected]809cc4d2010-10-27 15:22:54591void TestingProfile::BlockUntilTopSitesLoaded() {
[email protected]a7fe9112012-07-20 02:34:45592 content::WindowedNotificationObserver top_sites_loaded_observer(
[email protected]120abf132011-09-27 21:38:06593 chrome::NOTIFICATION_TOP_SITES_LOADED,
[email protected]ad50def52011-10-19 23:17:07594 content::NotificationService::AllSources());
[email protected]120abf132011-09-27 21:38:06595 top_sites_loaded_observer.Wait();
[email protected]809cc4d2010-10-27 15:22:54596}
597
[email protected]90efc3e2014-04-22 18:39:03598void TestingProfile::SetGuestSession(bool guest) {
599 guest_session_ = guest;
600}
601
[email protected]4251165a2013-07-17 04:33:40602base::FilePath TestingProfile::GetPath() const {
[email protected]49a25632011-08-31 17:03:48603 return profile_path_;
[email protected]f9dec9482010-08-20 20:42:19604}
605
[email protected]0de615a2012-11-08 04:40:59606scoped_refptr<base::SequencedTaskRunner> TestingProfile::GetIOTaskRunner() {
[email protected]bb024fe2013-05-10 21:33:26607 return base::MessageLoop::current()->message_loop_proxy();
[email protected]0de615a2012-11-08 04:40:59608}
609
[email protected]5b199522012-12-22 17:24:44610TestingPrefServiceSyncable* TestingProfile::GetTestingPrefService() {
tapted1ab9b7092014-09-15 23:49:40611 DCHECK(prefs_);
[email protected]2fb7dc982010-09-29 12:24:28612 DCHECK(testing_prefs_);
613 return testing_prefs_;
[email protected]dbbad7a2010-08-13 18:18:36614}
615
[email protected]8e5c89a2011-06-07 18:13:33616TestingProfile* TestingProfile::AsTestingProfile() {
617 return this;
618}
619
[email protected]29d70252011-04-28 02:16:58620std::string TestingProfile::GetProfileName() {
[email protected]d6001872013-09-24 18:31:26621 return profile_name_;
[email protected]29d70252011-04-28 02:16:58622}
623
[email protected]7b7e0b32014-03-17 16:09:23624Profile::ProfileType TestingProfile::GetProfileType() const {
625 if (guest_session_)
626 return GUEST_PROFILE;
tapted1ab9b7092014-09-15 23:49:40627 if (force_incognito_ || original_profile_)
[email protected]7b7e0b32014-03-17 16:09:23628 return INCOGNITO_PROFILE;
629 return REGULAR_PROFILE;
630}
631
[email protected]27d6e852012-03-02 21:31:32632bool TestingProfile::IsOffTheRecord() const {
tapted1ab9b7092014-09-15 23:49:40633 return force_incognito_ || original_profile_;
[email protected]fadc607b62011-02-07 17:55:50634}
635
[email protected]9819fd02013-08-22 10:49:39636void TestingProfile::SetOffTheRecordProfile(scoped_ptr<Profile> profile) {
637 DCHECK(!IsOffTheRecord());
tapted1ab9b7092014-09-15 23:49:40638 DCHECK_EQ(this, profile->GetOriginalProfile());
[email protected]9819fd02013-08-22 10:49:39639 incognito_profile_ = profile.Pass();
[email protected]93dba942011-02-23 22:45:01640}
641
[email protected]fadc607b62011-02-07 17:55:50642Profile* TestingProfile::GetOffTheRecordProfile() {
[email protected]9819fd02013-08-22 10:49:39643 if (IsOffTheRecord())
644 return this;
tapted1ab9b7092014-09-15 23:49:40645 if (!incognito_profile_)
646 TestingProfile::Builder().BuildIncognito(this);
[email protected]5bb2f522011-03-25 19:04:44647 return incognito_profile_.get();
[email protected]fadc607b62011-02-07 17:55:50648}
649
650bool TestingProfile::HasOffTheRecordProfile() {
[email protected]5bb2f522011-03-25 19:04:44651 return incognito_profile_.get() != NULL;
[email protected]fadc607b62011-02-07 17:55:50652}
653
654Profile* TestingProfile::GetOriginalProfile() {
[email protected]074311a2013-02-28 23:14:09655 if (original_profile_)
656 return original_profile_;
[email protected]fadc607b62011-02-07 17:55:50657 return this;
658}
659
[email protected]d20d0432014-06-12 17:14:05660bool TestingProfile::IsSupervised() {
661 return !supervised_user_id_.empty();
[email protected]e000daf2013-07-31 16:50:58662}
663
[email protected]50356f82014-07-31 13:25:18664#if defined(ENABLE_EXTENSIONS)
[email protected]2261e7b72011-07-12 13:43:55665void TestingProfile::SetExtensionSpecialStoragePolicy(
666 ExtensionSpecialStoragePolicy* extension_special_storage_policy) {
667 extension_special_storage_policy_ = extension_special_storage_policy;
668}
[email protected]50356f82014-07-31 13:25:18669#endif
[email protected]2261e7b72011-07-12 13:43:55670
[email protected]19eb80152011-02-26 00:28:43671ExtensionSpecialStoragePolicy*
672TestingProfile::GetExtensionSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18673#if defined(ENABLE_EXTENSIONS)
[email protected]cdba46992011-06-07 11:51:39674 if (!extension_special_storage_policy_.get())
[email protected]7c5048c2011-08-19 17:07:05675 extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL);
[email protected]cdba46992011-06-07 11:51:39676 return extension_special_storage_policy_.get();
[email protected]50356f82014-07-31 13:25:18677#else
678 return NULL;
679#endif
[email protected]19eb80152011-02-26 00:28:43680}
681
[email protected]33ad6ce92013-08-27 14:39:08682net::CookieMonster* TestingProfile::GetCookieMonster() {
683 if (!GetRequestContext())
684 return NULL;
685 return GetRequestContext()->GetURLRequestContext()->cookie_store()->
686 GetCookieMonster();
687}
688
[email protected]2fb7dc982010-09-29 12:24:28689void TestingProfile::CreateTestingPrefService() {
690 DCHECK(!prefs_.get());
[email protected]5b199522012-12-22 17:24:44691 testing_prefs_ = new TestingPrefServiceSyncable();
[email protected]2fb7dc982010-09-29 12:24:28692 prefs_.reset(testing_prefs_);
[email protected]c7141feb2013-06-11 13:00:16693 user_prefs::UserPrefs::Set(this, prefs_.get());
[email protected]37ca3fe02013-07-05 15:32:44694 chrome::RegisterUserProfilePrefs(testing_prefs_->registry());
[email protected]2fb7dc982010-09-29 12:24:28695}
696
tapted1ab9b7092014-09-15 23:49:40697void TestingProfile::CreateIncognitoPrefService() {
698 DCHECK(original_profile_);
699 DCHECK(!testing_prefs_);
700 // Simplified version of ProfileImpl::GetOffTheRecordPrefs(). Note this
701 // leaves testing_prefs_ unset.
702 prefs_.reset(original_profile_->prefs_->CreateIncognitoPrefService(NULL));
703 user_prefs::UserPrefs::Set(this, prefs_.get());
704}
705
[email protected]d81bb3e2013-05-02 18:17:03706void TestingProfile::CreateProfilePolicyConnector() {
[email protected]d81bb3e2013-05-02 18:17:03707#if defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24708 schema_registry_service_ =
709 policy::SchemaRegistryServiceFactory::CreateForContext(
710 this, policy::Schema(), NULL);
711 CHECK_EQ(schema_registry_service_.get(),
712 policy::SchemaRegistryServiceFactory::GetForContext(this));
[email protected]a43b9a82013-11-28 16:56:41713#endif // defined(ENABLE_CONFIGURATION_POLICY)
[email protected]eb83c392013-11-07 12:53:24714
[email protected]a43b9a82013-11-28 16:56:41715if (!policy_service_) {
716#if defined(ENABLE_CONFIGURATION_POLICY)
717 std::vector<policy::ConfigurationPolicyProvider*> providers;
[email protected]33528402013-12-16 18:38:32718 policy_service_.reset(new policy::PolicyServiceImpl(providers));
[email protected]d81bb3e2013-05-02 18:17:03719#else
[email protected]a43b9a82013-11-28 16:56:41720 policy_service_.reset(new policy::PolicyServiceStub());
[email protected]d81bb3e2013-05-02 18:17:03721#endif
[email protected]a43b9a82013-11-28 16:56:41722 }
[email protected]24a9f1c92013-11-13 12:33:37723 profile_policy_connector_.reset(new policy::ProfilePolicyConnector());
[email protected]a43b9a82013-11-28 16:56:41724 profile_policy_connector_->InitForTesting(policy_service_.Pass());
[email protected]d81bb3e2013-05-02 18:17:03725 policy::ProfilePolicyConnectorFactory::GetInstance()->SetServiceForTesting(
726 this, profile_policy_connector_.get());
727 CHECK_EQ(profile_policy_connector_.get(),
728 policy::ProfilePolicyConnectorFactory::GetForProfile(this));
729}
730
[email protected]c753f142013-02-10 13:14:04731PrefService* TestingProfile::GetPrefs() {
tapted1ab9b7092014-09-15 23:49:40732 DCHECK(prefs_);
[email protected]dbbad7a2010-08-13 18:18:36733 return prefs_.get();
734}
735
736history::TopSites* TestingProfile::GetTopSites() {
[email protected]809cc4d2010-10-27 15:22:54737 return top_sites_.get();
[email protected]dbbad7a2010-08-13 18:18:36738}
739
[email protected]fadc607b62011-02-07 17:55:50740history::TopSites* TestingProfile::GetTopSitesWithoutCreating() {
741 return top_sites_.get();
742}
743
[email protected]b441a8492012-06-06 14:55:57744DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() {
[email protected]fadc607b62011-02-07 17:55:50745 return NULL;
746}
747
[email protected]abe2c032011-03-31 18:49:34748net::URLRequestContextGetter* TestingProfile::GetRequestContext() {
[email protected]37ac95b2013-07-23 23:39:35749 return GetDefaultStoragePartition(this)->GetURLRequestContext();
[email protected]812b3a32010-01-08 05:36:04750}
751
[email protected]6bd30072013-02-08 18:17:11752net::URLRequestContextGetter* TestingProfile::CreateRequestContext(
[email protected]7571263c2014-03-10 22:57:09753 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15754 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]37ac95b2013-07-23 23:39:35755 return new net::TestURLRequestContextGetter(
756 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
[email protected]6bd30072013-02-08 18:17:11757}
758
[email protected]da5683db2011-04-23 17:12:21759net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess(
760 int renderer_child_id) {
[email protected]fb29e6cf2012-07-12 21:27:20761 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
762 renderer_child_id);
[email protected]1bc28312012-11-08 08:31:53763 return rph->GetStoragePartition()->GetURLRequestContext();
[email protected]d9696672011-03-15 22:45:09764}
765
[email protected]10705a7b2012-08-21 19:07:08766net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() {
767 return NULL;
768}
769
770net::URLRequestContextGetter*
771TestingProfile::GetMediaRequestContextForRenderProcess(
772 int renderer_child_id) {
[email protected]fadc607b62011-02-07 17:55:50773 return NULL;
774}
775
[email protected]10eb28162012-09-18 03:04:09776net::URLRequestContextGetter*
777TestingProfile::GetMediaRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40778 const base::FilePath& partition_path,
[email protected]27ddfed22012-10-30 23:22:43779 bool in_memory) {
[email protected]10eb28162012-09-18 03:04:09780 return NULL;
781}
782
[email protected]33ad6ce92013-08-27 14:39:08783net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() {
784 if (!extensions_request_context_.get())
785 extensions_request_context_ = new TestExtensionURLRequestContextGetter();
786 return extensions_request_context_.get();
787}
788
[email protected]fadc607b62011-02-07 17:55:50789net::SSLConfigService* TestingProfile::GetSSLConfigService() {
[email protected]dc6655b2012-12-14 05:58:24790 if (!GetRequestContext())
791 return NULL;
792 return GetRequestContext()->GetURLRequestContext()->ssl_config_service();
[email protected]fadc607b62011-02-07 17:55:50793}
794
[email protected]10eb28162012-09-18 03:04:09795net::URLRequestContextGetter*
[email protected]6bd30072013-02-08 18:17:11796TestingProfile::CreateRequestContextForStoragePartition(
[email protected]152ea302013-02-11 04:08:40797 const base::FilePath& partition_path,
[email protected]6bd30072013-02-08 18:17:11798 bool in_memory,
[email protected]7571263c2014-03-10 22:57:09799 content::ProtocolHandlerMap* protocol_handlers,
[email protected]3b90aab2014-05-30 17:56:15800 content::URLRequestInterceptorScopedVector request_interceptors) {
[email protected]10eb28162012-09-18 03:04:09801 // We don't test storage partitions here yet, so returning the same dummy
[email protected]d9696672011-03-15 22:45:09802 // context is sufficient for now.
803 return GetRequestContext();
804}
805
[email protected]df02aca2012-02-09 21:03:20806content::ResourceContext* TestingProfile::GetResourceContext() {
[email protected]37ac95b2013-07-23 23:39:35807 if (!resource_context_)
808 resource_context_ = new content::MockResourceContext();
809 return resource_context_;
[email protected]e89b77d2011-04-15 18:58:10810}
811
[email protected]dbbad7a2010-08-13 18:18:36812HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() {
[email protected]35552dc52011-07-12 09:04:38813 if (!host_content_settings_map_.get()) {
[email protected]646c29cb62012-06-18 14:31:05814 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false);
[email protected]701550ad2012-09-06 23:43:29815#if defined(ENABLE_EXTENSIONS)
[email protected]994fdb252014-06-26 13:26:37816 ExtensionService* extension_service =
817 extensions::ExtensionSystem::Get(this)->extension_service();
mukai87a8d402014-09-15 20:15:09818 if (extension_service) {
819 extension_service->RegisterContentSettings(
820 host_content_settings_map_.get());
821 }
[email protected]701550ad2012-09-06 23:43:29822#endif
[email protected]35552dc52011-07-12 09:04:38823 }
[email protected]dbbad7a2010-08-13 18:18:36824 return host_content_settings_map_.get();
825}
826
[email protected]139355f2014-05-11 14:21:28827content::BrowserPluginGuestManager* TestingProfile::GetGuestManager() {
[email protected]1df3d972014-06-11 04:55:18828#if defined(ENABLE_EXTENSIONS)
[email protected]140d6cd92014-08-12 18:26:46829 return extensions::GuestViewManager::FromBrowserContext(this);
[email protected]1df3d972014-06-11 04:55:18830#else
831 return NULL;
832#endif
[email protected]24569262014-05-06 03:31:30833}
834
[email protected]c5c89d042014-06-13 14:43:37835content::PushMessagingService* TestingProfile::GetPushMessagingService() {
836 return NULL;
837}
838
[email protected]fadc607b62011-02-07 17:55:50839bool TestingProfile::IsSameProfile(Profile *p) {
840 return this == p;
841}
842
843base::Time TestingProfile::GetStartTime() const {
844 return start_time_;
845}
846
[email protected]152ea302013-02-11 04:08:40847base::FilePath TestingProfile::last_selected_directory() {
[email protected]fadc607b62011-02-07 17:55:50848 return last_selected_directory_;
849}
850
[email protected]152ea302013-02-11 04:08:40851void TestingProfile::set_last_selected_directory(const base::FilePath& path) {
[email protected]fadc607b62011-02-07 17:55:50852 last_selected_directory_ = path;
853}
854
[email protected]cc5bfd42010-11-24 14:44:02855PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() {
[email protected]6f96cbcb2011-11-04 02:26:07856 if (!pref_proxy_config_tracker_.get()) {
[email protected]e2930d0902013-07-17 05:25:42857 // TestingProfile is used in unit tests, where local state is not available.
[email protected]6f96cbcb2011-11-04 02:26:07858 pref_proxy_config_tracker_.reset(
[email protected]e2930d0902013-07-17 05:25:42859 ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(GetPrefs(),
860 NULL));
[email protected]6f96cbcb2011-11-04 02:26:07861 }
862 return pref_proxy_config_tracker_.get();
[email protected]cc5bfd42010-11-24 14:44:02863}
864
[email protected]0bfc29a2009-04-27 16:15:44865void TestingProfile::BlockUntilHistoryProcessesPendingRequests() {
[email protected]a5894fe2012-11-01 14:40:56866 HistoryService* history_service =
[email protected]6c537042012-07-13 00:59:02867 HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS);
[email protected]a5894fe2012-11-01 14:40:56868 DCHECK(history_service);
[email protected]bb024fe2013-05-10 21:33:26869 DCHECK(base::MessageLoop::current());
[email protected]0bfc29a2009-04-27 16:15:44870
[email protected]58bef622014-07-03 11:41:14871 base::CancelableTaskTracker tracker;
[email protected]6a0b6672014-07-29 12:00:23872 history_service->ScheduleDBTask(
873 scoped_ptr<history::HistoryDBTask>(
874 new QuittingHistoryDBTask()),
875 &tracker);
[email protected]bb024fe2013-05-10 21:33:26876 base::MessageLoop::current()->Run();
[email protected]0bfc29a2009-04-27 16:15:44877}
[email protected]345a8b72009-09-29 09:11:44878
[email protected]8382d8c2011-09-15 03:43:17879chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() {
880 return NULL;
881}
882
[email protected]a4205202014-06-02 16:03:08883DevToolsNetworkController* TestingProfile::GetDevToolsNetworkController() {
884 return NULL;
885}
886
[email protected]e0e1fc22012-11-05 20:25:36887void TestingProfile::ClearNetworkingHistorySince(
888 base::Time time,
889 const base::Closure& completion) {
890 if (!completion.is_null()) {
891 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
892 }
[email protected]02896a82011-09-21 18:54:32893}
894
[email protected]671f9c62011-10-28 19:22:07895GURL TestingProfile::GetHomePage() {
896 return GURL(chrome::kChromeUINewTabURL);
897}
898
[email protected]c753f142013-02-10 13:14:04899PrefService* TestingProfile::GetOffTheRecordPrefs() {
[email protected]fadc607b62011-02-07 17:55:50900 return NULL;
901}
902
[email protected]cd501a72014-08-22 19:58:31903storage::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() {
[email protected]50356f82014-07-31 13:25:18904#if defined(ENABLE_EXTENSIONS)
[email protected]36fb2c7c2011-04-04 15:49:08905 return GetExtensionSpecialStoragePolicy();
[email protected]50356f82014-07-31 13:25:18906#else
907 return NULL;
908#endif
[email protected]36fb2c7c2011-04-04 15:49:08909}
910
[email protected]c5bbe0e2014-08-01 23:23:30911content::SSLHostStateDelegate* TestingProfile::GetSSLHostStateDelegate() {
912 return NULL;
913}
914
[email protected]1bee6ed2012-03-14 06:46:36915bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) {
916 return true;
917}
[email protected]ef92e172012-04-25 19:40:41918
[email protected]4251165a2013-07-17 04:33:40919bool TestingProfile::IsGuestSession() const {
[email protected]e9476cd2013-12-04 05:55:34920 return guest_session_;
[email protected]4251165a2013-07-17 04:33:40921}
[email protected]1101dbc52013-10-05 00:19:12922
[email protected]6c0ca7fc2012-10-05 16:27:22923Profile::ExitType TestingProfile::GetLastSessionExitType() {
924 return last_session_exited_cleanly_ ? EXIT_NORMAL : EXIT_CRASHED;
[email protected]2ca1ea662012-10-04 02:26:36925}
926
[email protected]5c4c89f2012-08-07 21:09:59927TestingProfile::Builder::Builder()
928 : build_called_(false),
[email protected]9819fd02013-08-22 10:49:39929 delegate_(NULL),
[email protected]e9476cd2013-12-04 05:55:34930 guest_session_(false) {
[email protected]5c4c89f2012-08-07 21:09:59931}
932
933TestingProfile::Builder::~Builder() {
934}
935
[email protected]152ea302013-02-11 04:08:40936void TestingProfile::Builder::SetPath(const base::FilePath& path) {
[email protected]5c4c89f2012-08-07 21:09:59937 path_ = path;
938}
939
940void TestingProfile::Builder::SetDelegate(Delegate* delegate) {
941 delegate_ = delegate;
942}
943
[email protected]50356f82014-07-31 13:25:18944#if defined(ENABLE_EXTENSIONS)
[email protected]5c4c89f2012-08-07 21:09:59945void TestingProfile::Builder::SetExtensionSpecialStoragePolicy(
946 scoped_refptr<ExtensionSpecialStoragePolicy> policy) {
947 extension_policy_ = policy;
948}
[email protected]50356f82014-07-31 13:25:18949#endif
[email protected]5c4c89f2012-08-07 21:09:59950
[email protected]5b199522012-12-22 17:24:44951void TestingProfile::Builder::SetPrefService(
952 scoped_ptr<PrefServiceSyncable> prefs) {
[email protected]5c4c89f2012-08-07 21:09:59953 pref_service_ = prefs.Pass();
954}
955
[email protected]e9476cd2013-12-04 05:55:34956void TestingProfile::Builder::SetGuestSession() {
957 guest_session_ = true;
958}
959
[email protected]d20d0432014-06-12 17:14:05960void TestingProfile::Builder::SetSupervisedUserId(
961 const std::string& supervised_user_id) {
962 supervised_user_id_ = supervised_user_id;
[email protected]371662e372013-10-17 22:05:22963}
964
[email protected]a43b9a82013-11-28 16:56:41965void TestingProfile::Builder::SetPolicyService(
966 scoped_ptr<policy::PolicyService> policy_service) {
967 policy_service_ = policy_service.Pass();
968}
969
[email protected]9819fd02013-08-22 10:49:39970void TestingProfile::Builder::AddTestingFactory(
971 BrowserContextKeyedServiceFactory* service_factory,
[email protected]d45c5d52013-12-04 23:41:16972 BrowserContextKeyedServiceFactory::TestingFactoryFunction callback) {
[email protected]9819fd02013-08-22 10:49:39973 testing_factories_.push_back(std::make_pair(service_factory, callback));
974}
975
[email protected]5c4c89f2012-08-07 21:09:59976scoped_ptr<TestingProfile> TestingProfile::Builder::Build() {
977 DCHECK(!build_called_);
978 build_called_ = true;
[email protected]371662e372013-10-17 22:05:22979
tapted1ab9b7092014-09-15 23:49:40980 return scoped_ptr<TestingProfile>(new TestingProfile(path_,
981 delegate_,
[email protected]50356f82014-07-31 13:25:18982#if defined(ENABLE_EXTENSIONS)
tapted1ab9b7092014-09-15 23:49:40983 extension_policy_,
[email protected]50356f82014-07-31 13:25:18984#endif
tapted1ab9b7092014-09-15 23:49:40985 pref_service_.Pass(),
986 NULL,
987 guest_session_,
988 supervised_user_id_,
989 policy_service_.Pass(),
990 testing_factories_));
991}
992
993TestingProfile* TestingProfile::Builder::BuildIncognito(
994 TestingProfile* original_profile) {
995 DCHECK(!build_called_);
996 DCHECK(original_profile);
997 build_called_ = true;
998
999 // Note: Owned by |original_profile|.
1000 return new TestingProfile(path_,
1001 delegate_,
1002#if defined(ENABLE_EXTENSIONS)
1003 extension_policy_,
1004#endif
1005 pref_service_.Pass(),
1006 original_profile,
1007 guest_session_,
1008 supervised_user_id_,
1009 policy_service_.Pass(),
1010 testing_factories_);
[email protected]5c4c89f2012-08-07 21:09:591011}