[email protected] | e7e4673 | 2012-01-05 11:45:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 5 | #include "chrome/test/base/testing_profile.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | a42b559 | 2009-09-03 16:52:23 | [diff] [blame] | 7 | #include "build/build_config.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 8 | |
| 9 | #include "base/base_paths.h" |
[email protected] | e3e43d9 | 2010-02-26 22:02:38 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 11 | #include "base/file_util.h" |
[email protected] | 83a7d2eb | 2010-05-03 21:46:19 | [diff] [blame] | 12 | #include "base/message_loop_proxy.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 13 | #include "base/path_service.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 15 | #include "base/string_number_conversions.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 16 | #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 17 | #include "chrome/browser/bookmarks/bookmark_model.h" |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 18 | #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
[email protected] | 1459fb6 | 2011-05-25 19:03:27 | [diff] [blame] | 19 | #include "chrome/browser/browser_process.h" |
[email protected] | 9d01a6a | 2010-11-30 12:03:33 | [diff] [blame] | 20 | #include "chrome/browser/content_settings/host_content_settings_map.h" |
[email protected] | 2f69b38 | 2011-02-19 00:34:25 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_special_storage_policy.h" |
[email protected] | 31d8f5f2 | 2012-04-02 15:22:08 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_system.h" |
| 24 | #include "chrome/browser/extensions/extension_system_factory.h" |
| 25 | #include "chrome/browser/extensions/test_extension_system.h" |
[email protected] | ee2ed42c | 2011-04-28 22:19:14 | [diff] [blame] | 26 | #include "chrome/browser/favicon/favicon_service.h" |
[email protected] | c476e63 | 2011-06-23 11:18:04 | [diff] [blame] | 27 | #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 28 | #include "chrome/browser/history/history.h" |
[email protected] | 075ae73 | 2009-02-11 23:58:31 | [diff] [blame] | 29 | #include "chrome/browser/history/history_backend.h" |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 30 | #include "chrome/browser/history/history_service_factory.h" |
[email protected] | 572f40c9b | 2012-07-06 20:35:33 | [diff] [blame] | 31 | #include "chrome/browser/history/shortcuts_backend.h" |
| 32 | #include "chrome/browser/history/shortcuts_backend_factory.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 33 | #include "chrome/browser/history/top_sites.h" |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 34 | #include "chrome/browser/net/proxy_service_factory.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 35 | #include "chrome/browser/notifications/desktop_notification_service.h" |
[email protected] | dcb72d5 | 2011-04-13 12:36:53 | [diff] [blame] | 36 | #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
[email protected] | 5c4c89f | 2012-08-07 21:09:59 | [diff] [blame] | 37 | #include "chrome/browser/policy/user_cloud_policy_manager.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 38 | #include "chrome/browser/prefs/browser_prefs.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 39 | #include "chrome/browser/prefs/testing_pref_store.h" |
[email protected] | 4850a7f | 2011-03-08 23:36:59 | [diff] [blame] | 40 | #include "chrome/browser/prerender/prerender_manager.h" |
[email protected] | 5cdcd15 | 2011-03-31 22:48:52 | [diff] [blame] | 41 | #include "chrome/browser/profiles/profile_dependency_manager.h" |
[email protected] | ee78d8a | 2012-03-15 16:53:48 | [diff] [blame] | 42 | #include "chrome/browser/protector/protector_service_factory.h" |
[email protected] | 16bd088b | 2012-04-03 08:12:12 | [diff] [blame] | 43 | #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
[email protected] | c52b289 | 2012-03-07 11:01:02 | [diff] [blame] | 44 | #include "chrome/browser/speech/chrome_speech_recognition_preferences.h" |
[email protected] | 810ffba | 2012-06-12 01:07:48 | [diff] [blame] | 45 | #include "chrome/browser/webdata/web_data_service.h" |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 46 | #include "chrome/browser/webdata/web_data_service_factory.h" |
[email protected] | a9afddb | 2009-02-12 17:49:42 | [diff] [blame] | 47 | #include "chrome/common/chrome_constants.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 48 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | 9e33d7e | 2011-09-30 16:43:54 | [diff] [blame] | 49 | #include "chrome/common/chrome_switches.h" |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 50 | #include "chrome/common/url_constants.h" |
[email protected] | 7bc6068 | 2011-07-29 20:55:59 | [diff] [blame] | 51 | #include "chrome/test/base/bookmark_load_observer.h" |
[email protected] | 8ad3636e | 2011-08-01 22:31:40 | [diff] [blame] | 52 | #include "chrome/test/base/testing_pref_service.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 53 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 54 | #include "content/public/browser/browser_thread.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 55 | #include "content/public/browser/notification_service.h" |
[email protected] | fb29e6cf | 2012-07-12 21:27:20 | [diff] [blame] | 56 | #include "content/public/browser/render_process_host.h" |
[email protected] | 08a932d5 | 2012-06-03 21:42:12 | [diff] [blame] | 57 | #include "content/public/test/mock_resource_context.h" |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 58 | #include "content/public/test/test_utils.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 59 | #include "net/cookies/cookie_monster.h" |
[email protected] | 812b3a3 | 2010-01-08 05:36:04 | [diff] [blame] | 60 | #include "net/url_request/url_request_context.h" |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 61 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 62 | #include "net/url_request/url_request_test_util.h" |
[email protected] | 4772b07 | 2010-03-30 17:45:46 | [diff] [blame] | 63 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 64 | |
[email protected] | 21d3a88 | 2012-05-31 14:41:55 | [diff] [blame] | 65 | #if defined(ENABLE_CONFIGURATION_POLICY) |
| 66 | #include "chrome/browser/policy/policy_service_impl.h" |
| 67 | #else |
| 68 | #include "chrome/browser/policy/policy_service_stub.h" |
| 69 | #endif // defined(ENABLE_CONFIGURATION_POLICY) |
| 70 | |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 71 | #if defined(OS_CHROMEOS) |
| 72 | #include "chrome/browser/chromeos/proxy_config_service_impl.h" |
| 73 | #endif // defined(OS_CHROMEOS) |
| 74 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 75 | using base::Time; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 76 | using content::BrowserThread; |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 77 | using content::DownloadManagerDelegate; |
[email protected] | 3c88741 | 2010-04-19 20:30:23 | [diff] [blame] | 78 | using testing::NiceMock; |
[email protected] | 4772b07 | 2010-03-30 17:45:46 | [diff] [blame] | 79 | using testing::Return; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 80 | |
[email protected] | d364c65 | 2008-08-29 19:46:56 | [diff] [blame] | 81 | namespace { |
| 82 | |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 83 | // Task used to make sure history has finished processing a request. Intended |
| 84 | // for use with BlockUntilHistoryProcessesPendingRequests. |
| 85 | |
| 86 | class QuittingHistoryDBTask : public HistoryDBTask { |
| 87 | public: |
| 88 | QuittingHistoryDBTask() {} |
| 89 | |
| 90 | virtual bool RunOnDBThread(history::HistoryBackend* backend, |
| 91 | history::HistoryDatabase* db) { |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | virtual void DoneRunOnMainThread() { |
| 96 | MessageLoop::current()->Quit(); |
| 97 | } |
| 98 | |
| 99 | private: |
[email protected] | 7991a23 | 2009-11-06 01:55:48 | [diff] [blame] | 100 | ~QuittingHistoryDBTask() {} |
| 101 | |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 102 | DISALLOW_COPY_AND_ASSIGN(QuittingHistoryDBTask); |
| 103 | }; |
| 104 | |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 105 | class TestExtensionURLRequestContext : public net::URLRequestContext { |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 106 | public: |
| 107 | TestExtensionURLRequestContext() { |
| 108 | net::CookieMonster* cookie_monster = new net::CookieMonster(NULL, NULL); |
| 109 | const char* schemes[] = {chrome::kExtensionScheme}; |
| 110 | cookie_monster->SetCookieableSchemes(schemes, 1); |
[email protected] | f6c21cb | 2011-02-16 19:45:41 | [diff] [blame] | 111 | set_cookie_store(cookie_monster); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 112 | } |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 113 | |
[email protected] | 7b2720b | 2012-04-25 16:59:11 | [diff] [blame] | 114 | virtual ~TestExtensionURLRequestContext() {} |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 115 | }; |
| 116 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 117 | class TestExtensionURLRequestContextGetter |
| 118 | : public net::URLRequestContextGetter { |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 119 | public: |
[email protected] | aeb53f0 | 2011-01-15 00:21:34 | [diff] [blame] | 120 | virtual net::URLRequestContext* GetURLRequestContext() { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 121 | if (!context_.get()) |
| 122 | context_.reset(new TestExtensionURLRequestContext()); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 123 | return context_.get(); |
| 124 | } |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 125 | virtual scoped_refptr<base::SingleThreadTaskRunner> |
| 126 | GetNetworkTaskRunner() const OVERRIDE { |
[email protected] | 0c7d74f | 2010-10-11 11:55:26 | [diff] [blame] | 127 | return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO); |
[email protected] | 83a7d2eb | 2010-05-03 21:46:19 | [diff] [blame] | 128 | } |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 129 | |
[email protected] | 13ed17f8 | 2012-04-06 02:27:18 | [diff] [blame] | 130 | protected: |
| 131 | virtual ~TestExtensionURLRequestContextGetter() {} |
| 132 | |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 133 | private: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 134 | scoped_ptr<net::URLRequestContext> context_; |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 135 | }; |
| 136 | |
[email protected] | abc7724 | 2012-03-16 04:32:31 | [diff] [blame] | 137 | ProfileKeyedService* CreateTestDesktopNotificationService(Profile* profile) { |
[email protected] | 29cebbd | 2012-06-06 21:43:42 | [diff] [blame] | 138 | #if defined(ENABLE_NOTIFICATIONS) |
[email protected] | dcb72d5 | 2011-04-13 12:36:53 | [diff] [blame] | 139 | return new DesktopNotificationService(profile, NULL); |
[email protected] | 29cebbd | 2012-06-06 21:43:42 | [diff] [blame] | 140 | #else |
| 141 | return NULL; |
| 142 | #endif |
[email protected] | dcb72d5 | 2011-04-13 12:36:53 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | d364c65 | 2008-08-29 19:46:56 | [diff] [blame] | 145 | } // namespace |
| 146 | |
[email protected] | 89dbb177 | 2012-07-17 13:47:25 | [diff] [blame] | 147 | // static |
| 148 | #if defined(OS_CHROMEOS) |
| 149 | // Must be kept in sync with |
| 150 | // ChromeBrowserMainPartsChromeos::PreEarlyInitialization. |
| 151 | const char TestingProfile::kTestUserProfileDir[] = "test-user"; |
| 152 | #else |
| 153 | const char TestingProfile::kTestUserProfileDir[] = "Default"; |
| 154 | #endif |
| 155 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 156 | TestingProfile::TestingProfile() |
[email protected] | ea6f7657 | 2008-12-18 00:09:55 | [diff] [blame] | 157 | : start_time_(Time::Now()), |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 158 | testing_prefs_(NULL), |
[email protected] | 5bb2f52 | 2011-03-25 19:04:44 | [diff] [blame] | 159 | incognito_(false), |
[email protected] | 5cdcd15 | 2011-03-31 22:48:52 | [diff] [blame] | 160 | last_session_exited_cleanly_(true), |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 161 | profile_dependency_manager_(ProfileDependencyManager::GetInstance()), |
| 162 | delegate_(NULL) { |
[email protected] | 5c4c89f | 2012-08-07 21:09:59 | [diff] [blame] | 163 | CreateTempProfileDir(); |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 164 | profile_path_ = temp_dir_.path(); |
| 165 | |
| 166 | Init(); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 167 | FinishInit(); |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TestingProfile::TestingProfile(const FilePath& path) |
| 171 | : start_time_(Time::Now()), |
| 172 | testing_prefs_(NULL), |
| 173 | incognito_(false), |
| 174 | last_session_exited_cleanly_(true), |
| 175 | profile_path_(path), |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 176 | profile_dependency_manager_(ProfileDependencyManager::GetInstance()), |
| 177 | delegate_(NULL) { |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 178 | Init(); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 179 | FinishInit(); |
| 180 | } |
| 181 | |
| 182 | TestingProfile::TestingProfile(const FilePath& path, |
| 183 | Delegate* delegate) |
| 184 | : start_time_(Time::Now()), |
| 185 | testing_prefs_(NULL), |
| 186 | incognito_(false), |
| 187 | last_session_exited_cleanly_(true), |
| 188 | profile_path_(path), |
| 189 | profile_dependency_manager_(ProfileDependencyManager::GetInstance()), |
| 190 | delegate_(delegate) { |
| 191 | Init(); |
| 192 | if (delegate_) { |
| 193 | MessageLoop::current()->PostTask(FROM_HERE, |
| 194 | base::Bind(&TestingProfile::FinishInit, |
| 195 | base::Unretained(this))); |
| 196 | } else { |
| 197 | FinishInit(); |
| 198 | } |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | 5c4c89f | 2012-08-07 21:09:59 | [diff] [blame] | 201 | TestingProfile::TestingProfile( |
| 202 | const FilePath& path, |
| 203 | Delegate* delegate, |
| 204 | scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, |
| 205 | scoped_ptr<PrefService> prefs, |
| 206 | scoped_ptr<policy::UserCloudPolicyManager> user_cloud_policy_manager) |
| 207 | : start_time_(Time::Now()), |
| 208 | prefs_(prefs.release()), |
| 209 | testing_prefs_(NULL), |
| 210 | incognito_(false), |
| 211 | last_session_exited_cleanly_(true), |
| 212 | extension_special_storage_policy_(extension_policy), |
| 213 | user_cloud_policy_manager_(user_cloud_policy_manager.release()), |
| 214 | profile_path_(path), |
| 215 | profile_dependency_manager_(ProfileDependencyManager::GetInstance()), |
| 216 | delegate_(delegate) { |
| 217 | |
| 218 | // If no profile path was supplied, create one. |
| 219 | if (profile_path_.empty()) { |
| 220 | CreateTempProfileDir(); |
| 221 | profile_path_ = temp_dir_.path(); |
| 222 | } |
| 223 | |
| 224 | Init(); |
| 225 | // If caller supplied a delegate, delay the FinishInit invocation until other |
| 226 | // tasks have run. |
| 227 | // TODO(atwilson): See if this is still required once we convert the current |
| 228 | // users of the constructor that takes a Delegate* param. |
| 229 | if (delegate_) { |
| 230 | MessageLoop::current()->PostTask(FROM_HERE, |
| 231 | base::Bind(&TestingProfile::FinishInit, |
| 232 | base::Unretained(this))); |
| 233 | } else { |
| 234 | FinishInit(); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void TestingProfile::CreateTempProfileDir() { |
| 239 | if (!temp_dir_.CreateUniqueTempDir()) { |
| 240 | LOG(ERROR) << "Failed to create unique temporary directory."; |
| 241 | |
| 242 | // Fallback logic in case we fail to create unique temporary directory. |
| 243 | FilePath system_tmp_dir; |
| 244 | bool success = PathService::Get(base::DIR_TEMP, &system_tmp_dir); |
| 245 | |
| 246 | // We're severly screwed if we can't get the system temporary |
| 247 | // directory. Die now to avoid writing to the filesystem root |
| 248 | // or other bad places. |
| 249 | CHECK(success); |
| 250 | |
| 251 | FilePath fallback_dir(system_tmp_dir.AppendASCII("TestingProfilePath")); |
| 252 | file_util::Delete(fallback_dir, true); |
| 253 | file_util::CreateDirectory(fallback_dir); |
| 254 | if (!temp_dir_.Set(fallback_dir)) { |
| 255 | // That shouldn't happen, but if it does, try to recover. |
| 256 | LOG(ERROR) << "Failed to use a fallback temporary directory."; |
| 257 | |
| 258 | // We're screwed if this fails, see CHECK above. |
| 259 | CHECK(temp_dir_.Set(system_tmp_dir)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 264 | void TestingProfile::Init() { |
[email protected] | d41e657 | 2012-05-04 20:49:42 | [diff] [blame] | 265 | if (!file_util::PathExists(profile_path_)) |
| 266 | file_util::CreateDirectory(profile_path_); |
| 267 | |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 268 | extensions::ExtensionSystemFactory::GetInstance()->SetTestingFactory( |
| 269 | this, extensions::TestExtensionSystem::Build); |
[email protected] | 31d8f5f2 | 2012-04-02 15:22:08 | [diff] [blame] | 270 | |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 271 | profile_dependency_manager_->CreateProfileServices(this, true); |
| 272 | |
[email protected] | 5fd2e84 | 2012-03-01 00:29:11 | [diff] [blame] | 273 | #if defined(ENABLE_NOTIFICATIONS) |
[email protected] | dcb72d5 | 2011-04-13 12:36:53 | [diff] [blame] | 274 | // Install profile keyed service factory hooks for dummy/test services |
[email protected] | 7722653b | 2011-06-03 17:28:22 | [diff] [blame] | 275 | DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory( |
| 276 | this, CreateTestDesktopNotificationService); |
[email protected] | 5fd2e84 | 2012-03-01 00:29:11 | [diff] [blame] | 277 | #endif |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 278 | } |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 279 | |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 280 | void TestingProfile::FinishInit() { |
[email protected] | 080adba9 | 2011-12-07 19:00:40 | [diff] [blame] | 281 | DCHECK(content::NotificationService::current()); |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 282 | content::NotificationService::current()->Notify( |
[email protected] | 12463cd | 2011-07-19 09:40:20 | [diff] [blame] | 283 | chrome::NOTIFICATION_PROFILE_CREATED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 284 | content::Source<Profile>(static_cast<Profile*>(this)), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 285 | content::NotificationService::NoDetails()); |
[email protected] | 537c108 | 2011-12-02 02:37:17 | [diff] [blame] | 286 | |
| 287 | if (delegate_) |
[email protected] | 00b33f80 | 2012-03-13 07:46:03 | [diff] [blame] | 288 | delegate_->OnProfileCreated(this, true, false); |
[email protected] | ab23dbe | 2010-08-12 02:10:46 | [diff] [blame] | 289 | } |
| 290 | |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 291 | TestingProfile::~TestingProfile() { |
[email protected] | a7c4c03 | 2012-07-27 00:36:32 | [diff] [blame] | 292 | MaybeSendDestroyedNotification(); |
[email protected] | 5cdcd15 | 2011-03-31 22:48:52 | [diff] [blame] | 293 | |
| 294 | profile_dependency_manager_->DestroyProfileServices(this); |
| 295 | |
[email protected] | 35552dc5 | 2011-07-12 09:04:38 | [diff] [blame] | 296 | if (host_content_settings_map_) |
| 297 | host_content_settings_map_->ShutdownOnUIThread(); |
| 298 | |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 299 | DestroyTopSites(); |
[email protected] | 1facac2 | 2012-08-23 07:20:58 | [diff] [blame] | 300 | DestroyFaviconService(); |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame] | 301 | |
[email protected] | cc5bfd4 | 2010-11-24 14:44:02 | [diff] [blame] | 302 | if (pref_proxy_config_tracker_.get()) |
| 303 | pref_proxy_config_tracker_->DetachFromPrefService(); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 304 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | |
[email protected] | d2879af | 2010-02-08 16:02:56 | [diff] [blame] | 306 | void TestingProfile::CreateFaviconService() { |
[email protected] | 1facac2 | 2012-08-23 07:20:58 | [diff] [blame] | 307 | favicon_service_.reset(new FaviconService(this)); |
[email protected] | d2879af | 2010-02-08 16:02:56 | [diff] [blame] | 308 | } |
| 309 | |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 310 | static scoped_refptr<RefcountedProfileKeyedService> BuildHistoryService( |
| 311 | Profile* profile) { |
| 312 | return new HistoryService(profile); |
| 313 | } |
| 314 | |
[email protected] | d486a085 | 2009-11-02 21:40:00 | [diff] [blame] | 315 | void TestingProfile::CreateHistoryService(bool delete_file, bool no_db) { |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 316 | DestroyHistoryService(); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 317 | if (delete_file) { |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 318 | FilePath path = GetPath(); |
| 319 | path = path.Append(chrome::kHistoryFilename); |
[email protected] | f25387b | 2008-08-21 15:20:33 | [diff] [blame] | 320 | file_util::Delete(path, false); |
| 321 | } |
[email protected] | 7019bb6d | 2012-08-23 09:09:48 | [diff] [blame^] | 322 | // This will create and init the history service. |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 323 | HistoryService* history_service = static_cast<HistoryService*>( |
| 324 | HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 325 | this, BuildHistoryService).get()); |
[email protected] | 7019bb6d | 2012-08-23 09:09:48 | [diff] [blame^] | 326 | if (!history_service->Init(this->GetPath(), |
| 327 | BookmarkModelFactory::GetForProfile(this), |
| 328 | no_db)) { |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 329 | HistoryServiceFactory::GetInstance()->SetTestingFactoryAndUse(this, NULL); |
[email protected] | 7019bb6d | 2012-08-23 09:09:48 | [diff] [blame^] | 330 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void TestingProfile::DestroyHistoryService() { |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 334 | scoped_refptr<HistoryService> history_service = |
[email protected] | 6c53704 | 2012-07-13 00:59:02 | [diff] [blame] | 335 | HistoryServiceFactory::GetForProfileWithoutCreating(this); |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 336 | if (!history_service.get()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | return; |
| 338 | |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 339 | history_service->NotifyRenderProcessHostDestruction(0); |
| 340 | history_service->SetOnBackendDestroyTask(MessageLoop::QuitClosure()); |
| 341 | history_service->Cleanup(); |
| 342 | HistoryServiceFactory::ShutdownForProfile(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | |
| 344 | // Wait for the backend class to terminate before deleting the files and |
| 345 | // moving to the next test. Note: if this never terminates, somebody is |
| 346 | // probably leaking a reference to the history backend, so it never calls |
| 347 | // our destroy task. |
| 348 | MessageLoop::current()->Run(); |
| 349 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | // Make sure we don't have any event pending that could disrupt the next |
| 351 | // test. |
[email protected] | 1cb1a24 | 2011-12-10 18:36:51 | [diff] [blame] | 352 | MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | MessageLoop::current()->Run(); |
| 354 | } |
[email protected] | 4d0cd7ce | 2008-08-11 16:40:57 | [diff] [blame] | 355 | |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 356 | void TestingProfile::CreateTopSites() { |
| 357 | DestroyTopSites(); |
| 358 | top_sites_ = new history::TopSites(this); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 359 | FilePath file_name = GetPath().Append(chrome::kTopSitesFilename); |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 360 | top_sites_->Init(file_name); |
| 361 | } |
| 362 | |
| 363 | void TestingProfile::DestroyTopSites() { |
| 364 | if (top_sites_.get()) { |
| 365 | top_sites_->Shutdown(); |
| 366 | top_sites_ = NULL; |
| 367 | // TopSites::Shutdown schedules some tasks (from TopSitesBackend) that need |
| 368 | // to be run to properly shutdown. Run all pending tasks now. This is |
| 369 | // normally handled by browser_process shutdown. |
| 370 | if (MessageLoop::current()) |
| 371 | MessageLoop::current()->RunAllPending(); |
| 372 | } |
| 373 | } |
| 374 | |
[email protected] | 1facac2 | 2012-08-23 07:20:58 | [diff] [blame] | 375 | void TestingProfile::DestroyFaviconService() { |
| 376 | favicon_service_.reset(); |
| 377 | } |
| 378 | |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 379 | static ProfileKeyedService* BuildBookmarkModel(Profile* profile) { |
| 380 | BookmarkModel* bookmark_model = new BookmarkModel(profile); |
| 381 | bookmark_model->Load(); |
| 382 | return bookmark_model; |
| 383 | } |
| 384 | |
| 385 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 386 | void TestingProfile::CreateBookmarkModel(bool delete_file) { |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 387 | |
| 388 | if (delete_file) { |
[email protected] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 389 | FilePath path = GetPath(); |
| 390 | path = path.Append(chrome::kBookmarksFileName); |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 391 | file_util::Delete(path, false); |
| 392 | } |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 393 | // This will create a bookmark model. |
| 394 | BookmarkModel* bookmark_service = |
| 395 | static_cast<BookmarkModel*>( |
| 396 | BookmarkModelFactory::GetInstance()->SetTestingFactoryAndUse( |
| 397 | this, BuildBookmarkModel)); |
| 398 | |
| 399 | HistoryService* history_service = |
[email protected] | 6c53704 | 2012-07-13 00:59:02 | [diff] [blame] | 400 | HistoryServiceFactory::GetForProfileWithoutCreating(this).get(); |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 401 | if (history_service) { |
| 402 | history_service->history_backend_->bookmark_service_ = |
| 403 | bookmark_service; |
| 404 | history_service->history_backend_->expirer_.bookmark_service_ = |
| 405 | bookmark_service; |
[email protected] | 90ef1313 | 2008-08-27 03:27:46 | [diff] [blame] | 406 | } |
[email protected] | 4d0cd7ce | 2008-08-11 16:40:57 | [diff] [blame] | 407 | } |
[email protected] | d2c017a | 2008-08-13 21:51:45 | [diff] [blame] | 408 | |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 409 | static scoped_refptr<RefcountedProfileKeyedService> BuildWebDataService( |
| 410 | Profile* profile) { |
| 411 | WebDataService* web_data_service = new WebDataService(); |
| 412 | if (web_data_service) |
| 413 | web_data_service->Init(profile->GetPath()); |
[email protected] | a5c6529 | 2012-05-25 00:50:01 | [diff] [blame] | 414 | return web_data_service; |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 415 | } |
[email protected] | 2609bc1 | 2010-01-24 08:32:55 | [diff] [blame] | 416 | |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 417 | void TestingProfile::CreateWebDataService() { |
[email protected] | a5c6529 | 2012-05-25 00:50:01 | [diff] [blame] | 418 | WebDataServiceFactory::GetInstance()->SetTestingFactory( |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 419 | this, BuildWebDataService); |
[email protected] | 2609bc1 | 2010-01-24 08:32:55 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | d364c65 | 2008-08-29 19:46:56 | [diff] [blame] | 422 | void TestingProfile::BlockUntilBookmarkModelLoaded() { |
[email protected] | d7f7f75 | 2012-08-08 02:02:55 | [diff] [blame] | 423 | // Only get the bookmark model if it actually exists since the caller of the |
| 424 | // test should explicitly call CreateBookmarkModel to build it. |
| 425 | BookmarkModel* bookmark_model = |
| 426 | BookmarkModelFactory::GetForProfileIfExists(this); |
| 427 | DCHECK(bookmark_model); |
| 428 | if (bookmark_model->IsLoaded()) |
[email protected] | d364c65 | 2008-08-29 19:46:56 | [diff] [blame] | 429 | return; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 430 | base::RunLoop run_loop; |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 431 | BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop)); |
[email protected] | d7f7f75 | 2012-08-08 02:02:55 | [diff] [blame] | 432 | bookmark_model->AddObserver(&observer); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 433 | run_loop.Run(); |
[email protected] | d7f7f75 | 2012-08-08 02:02:55 | [diff] [blame] | 434 | bookmark_model->RemoveObserver(&observer); |
| 435 | DCHECK(bookmark_model->IsLoaded()); |
[email protected] | d364c65 | 2008-08-29 19:46:56 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 438 | // TODO(phajdan.jr): Doesn't this hang if Top Sites are already loaded? |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 439 | void TestingProfile::BlockUntilTopSitesLoaded() { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 440 | content::WindowedNotificationObserver top_sites_loaded_observer( |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 441 | chrome::NOTIFICATION_TOP_SITES_LOADED, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 442 | content::NotificationService::AllSources()); |
[email protected] | d7f7f75 | 2012-08-08 02:02:55 | [diff] [blame] | 443 | if (!HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS)) |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 444 | GetTopSites()->HistoryLoaded(); |
[email protected] | 120abf13 | 2011-09-27 21:38:06 | [diff] [blame] | 445 | top_sites_loaded_observer.Wait(); |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 446 | } |
| 447 | |
[email protected] | f9dec948 | 2010-08-20 20:42:19 | [diff] [blame] | 448 | FilePath TestingProfile::GetPath() { |
[email protected] | 49a2563 | 2011-08-31 17:03:48 | [diff] [blame] | 449 | return profile_path_; |
[email protected] | f9dec948 | 2010-08-20 20:42:19 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 452 | TestingPrefService* TestingProfile::GetTestingPrefService() { |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 453 | if (!prefs_.get()) |
| 454 | CreateTestingPrefService(); |
| 455 | DCHECK(testing_prefs_); |
| 456 | return testing_prefs_; |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | 8e5c89a | 2011-06-07 18:13:33 | [diff] [blame] | 459 | TestingProfile* TestingProfile::AsTestingProfile() { |
| 460 | return this; |
| 461 | } |
| 462 | |
[email protected] | 29d7025 | 2011-04-28 02:16:58 | [diff] [blame] | 463 | std::string TestingProfile::GetProfileName() { |
| 464 | return std::string("testing_profile"); |
| 465 | } |
| 466 | |
[email protected] | 27d6e85 | 2012-03-02 21:31:32 | [diff] [blame] | 467 | bool TestingProfile::IsOffTheRecord() const { |
[email protected] | 5bb2f52 | 2011-03-25 19:04:44 | [diff] [blame] | 468 | return incognito_; |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | 93dba94 | 2011-02-23 22:45:01 | [diff] [blame] | 471 | void TestingProfile::SetOffTheRecordProfile(Profile* profile) { |
[email protected] | 5bb2f52 | 2011-03-25 19:04:44 | [diff] [blame] | 472 | incognito_profile_.reset(profile); |
[email protected] | 93dba94 | 2011-02-23 22:45:01 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 475 | Profile* TestingProfile::GetOffTheRecordProfile() { |
[email protected] | 5bb2f52 | 2011-03-25 19:04:44 | [diff] [blame] | 476 | return incognito_profile_.get(); |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 477 | } |
| 478 | |
[email protected] | d4f5d116 | 2011-11-30 01:41:52 | [diff] [blame] | 479 | GAIAInfoUpdateService* TestingProfile::GetGAIAInfoUpdateService() { |
| 480 | return NULL; |
| 481 | } |
| 482 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 483 | bool TestingProfile::HasOffTheRecordProfile() { |
[email protected] | 5bb2f52 | 2011-03-25 19:04:44 | [diff] [blame] | 484 | return incognito_profile_.get() != NULL; |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | Profile* TestingProfile::GetOriginalProfile() { |
| 488 | return this; |
| 489 | } |
| 490 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 491 | VisitedLinkMaster* TestingProfile::GetVisitedLinkMaster() { |
| 492 | return NULL; |
| 493 | } |
| 494 | |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 495 | ExtensionService* TestingProfile::GetExtensionService() { |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 496 | return extensions::ExtensionSystem::Get(this)->extension_service(); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 497 | } |
| 498 | |
[email protected] | 20f97c9 | 2012-07-13 23:12:37 | [diff] [blame] | 499 | extensions::UserScriptMaster* TestingProfile::GetUserScriptMaster() { |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 500 | return extensions::ExtensionSystem::Get(this)->user_script_master(); |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | ExtensionProcessManager* TestingProfile::GetExtensionProcessManager() { |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 504 | return extensions::ExtensionSystem::Get(this)->process_manager(); |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 507 | extensions::EventRouter* TestingProfile::GetExtensionEventRouter() { |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 508 | return extensions::ExtensionSystem::Get(this)->event_router(); |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 509 | } |
| 510 | |
[email protected] | 2261e7b7 | 2011-07-12 13:43:55 | [diff] [blame] | 511 | void TestingProfile::SetExtensionSpecialStoragePolicy( |
| 512 | ExtensionSpecialStoragePolicy* extension_special_storage_policy) { |
| 513 | extension_special_storage_policy_ = extension_special_storage_policy; |
| 514 | } |
| 515 | |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 516 | ExtensionSpecialStoragePolicy* |
| 517 | TestingProfile::GetExtensionSpecialStoragePolicy() { |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame] | 518 | if (!extension_special_storage_policy_.get()) |
[email protected] | 7c5048c | 2011-08-19 17:07:05 | [diff] [blame] | 519 | extension_special_storage_policy_ = new ExtensionSpecialStoragePolicy(NULL); |
[email protected] | cdba4699 | 2011-06-07 11:51:39 | [diff] [blame] | 520 | return extension_special_storage_policy_.get(); |
[email protected] | 19eb8015 | 2011-02-26 00:28:43 | [diff] [blame] | 521 | } |
| 522 | |
[email protected] | 1facac2 | 2012-08-23 07:20:58 | [diff] [blame] | 523 | FaviconService* TestingProfile::GetFaviconService(ServiceAccessType access) { |
| 524 | return favicon_service_.get(); |
| 525 | } |
| 526 | |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 527 | net::CookieMonster* TestingProfile::GetCookieMonster() { |
| 528 | if (!GetRequestContext()) |
| 529 | return NULL; |
[email protected] | 277ec26 | 2011-03-30 21:09:40 | [diff] [blame] | 530 | return GetRequestContext()->GetURLRequestContext()->cookie_store()-> |
| 531 | GetCookieMonster(); |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 532 | } |
| 533 | |
[email protected] | 5c4c89f | 2012-08-07 21:09:59 | [diff] [blame] | 534 | policy::UserCloudPolicyManager* TestingProfile::GetUserCloudPolicyManager() { |
| 535 | return user_cloud_policy_manager_.get(); |
| 536 | } |
| 537 | |
[email protected] | 21d3a88 | 2012-05-31 14:41:55 | [diff] [blame] | 538 | policy::PolicyService* TestingProfile::GetPolicyService() { |
| 539 | if (!policy_service_.get()) { |
| 540 | #if defined(ENABLE_CONFIGURATION_POLICY) |
| 541 | policy::PolicyServiceImpl::Providers providers; |
| 542 | policy_service_.reset(new policy::PolicyServiceImpl(providers)); |
| 543 | #else |
| 544 | policy_service_.reset(new policy::PolicyServiceStub()); |
| 545 | #endif |
| 546 | } |
| 547 | return policy_service_.get(); |
| 548 | } |
| 549 | |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 550 | void TestingProfile::SetPrefService(PrefService* prefs) { |
[email protected] | ddcaa41 | 2012-03-30 19:57:29 | [diff] [blame] | 551 | #if defined(ENABLE_PROTECTOR_SERVICE) |
[email protected] | ee78d8a | 2012-03-15 16:53:48 | [diff] [blame] | 552 | // ProtectorService binds itself very closely to the PrefService at the moment |
| 553 | // of Profile creation and watches pref changes to update their backup. |
| 554 | // For tests that replace the PrefService after TestingProfile creation, |
| 555 | // ProtectorService is disabled to prevent further invalid memory accesses. |
| 556 | protector::ProtectorServiceFactory::GetInstance()-> |
| 557 | SetTestingFactory(this, NULL); |
[email protected] | ddcaa41 | 2012-03-30 19:57:29 | [diff] [blame] | 558 | #endif |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 559 | prefs_.reset(prefs); |
| 560 | } |
| 561 | |
| 562 | void TestingProfile::CreateTestingPrefService() { |
| 563 | DCHECK(!prefs_.get()); |
| 564 | testing_prefs_ = new TestingPrefService(); |
| 565 | prefs_.reset(testing_prefs_); |
| 566 | Profile::RegisterUserPrefs(prefs_.get()); |
[email protected] | 572f40c9b | 2012-07-06 20:35:33 | [diff] [blame] | 567 | chrome::RegisterUserPrefs(prefs_.get()); |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 568 | } |
| 569 | |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 570 | PrefService* TestingProfile::GetPrefs() { |
| 571 | if (!prefs_.get()) { |
[email protected] | 2fb7dc98 | 2010-09-29 12:24:28 | [diff] [blame] | 572 | CreateTestingPrefService(); |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 573 | } |
| 574 | return prefs_.get(); |
| 575 | } |
| 576 | |
| 577 | history::TopSites* TestingProfile::GetTopSites() { |
[email protected] | 809cc4d | 2010-10-27 15:22:54 | [diff] [blame] | 578 | return top_sites_.get(); |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 579 | } |
| 580 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 581 | history::TopSites* TestingProfile::GetTopSitesWithoutCreating() { |
| 582 | return top_sites_.get(); |
| 583 | } |
| 584 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 585 | DownloadManagerDelegate* TestingProfile::GetDownloadManagerDelegate() { |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 586 | return NULL; |
| 587 | } |
| 588 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 589 | net::URLRequestContextGetter* TestingProfile::GetRequestContext() { |
[email protected] | 812b3a3 | 2010-01-08 05:36:04 | [diff] [blame] | 590 | return request_context_.get(); |
| 591 | } |
| 592 | |
[email protected] | da5683db | 2011-04-23 17:12:21 | [diff] [blame] | 593 | net::URLRequestContextGetter* TestingProfile::GetRequestContextForRenderProcess( |
| 594 | int renderer_child_id) { |
[email protected] | 31d8f5f2 | 2012-04-02 15:22:08 | [diff] [blame] | 595 | ExtensionService* extension_service = |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 596 | extensions::ExtensionSystem::Get(this)->extension_service(); |
[email protected] | 31d8f5f2 | 2012-04-02 15:22:08 | [diff] [blame] | 597 | if (extension_service) { |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 598 | const extensions::Extension* installed_app = extension_service-> |
[email protected] | da5683db | 2011-04-23 17:12:21 | [diff] [blame] | 599 | GetInstalledAppForRenderer(renderer_child_id); |
| 600 | if (installed_app != NULL && installed_app->is_storage_isolated()) |
| 601 | return GetRequestContextForIsolatedApp(installed_app->id()); |
| 602 | } |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 603 | |
[email protected] | fb29e6cf | 2012-07-12 21:27:20 | [diff] [blame] | 604 | content::RenderProcessHost* rph = content::RenderProcessHost::FromID( |
| 605 | renderer_child_id); |
| 606 | if (rph && rph->IsGuest()) { |
| 607 | // For guest processes (used by the browser tag), we need to isolate the |
| 608 | // storage. |
| 609 | // TODO(nasko): Until we have proper storage partitions, create a |
| 610 | // non-persistent context using the RPH's id. |
| 611 | std::string id("guest-"); |
| 612 | id.append(base::IntToString(renderer_child_id)); |
| 613 | return GetRequestContextForIsolatedApp(id); |
| 614 | } |
| 615 | |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 616 | return GetRequestContext(); |
| 617 | } |
| 618 | |
[email protected] | 812b3a3 | 2010-01-08 05:36:04 | [diff] [blame] | 619 | void TestingProfile::CreateRequestContext() { |
| 620 | if (!request_context_) |
[email protected] | 4473860 | 2012-03-02 22:56:01 | [diff] [blame] | 621 | request_context_ = |
| 622 | new TestURLRequestContextGetter( |
| 623 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)); |
[email protected] | 812b3a3 | 2010-01-08 05:36:04 | [diff] [blame] | 624 | } |
| 625 | |
[email protected] | 7c89320 | 2010-10-07 20:18:02 | [diff] [blame] | 626 | void TestingProfile::ResetRequestContext() { |
[email protected] | 3931fd8 | 2012-03-12 21:18:48 | [diff] [blame] | 627 | // Any objects holding live URLFetchers should be deleted before the request |
| 628 | // context is shut down. |
[email protected] | 16bd088b | 2012-04-03 08:12:12 | [diff] [blame] | 629 | TemplateURLFetcherFactory::ShutdownForProfile(this); |
[email protected] | 3931fd8 | 2012-03-12 21:18:48 | [diff] [blame] | 630 | |
[email protected] | 7c89320 | 2010-10-07 20:18:02 | [diff] [blame] | 631 | request_context_ = NULL; |
| 632 | } |
| 633 | |
[email protected] | 10705a7b | 2012-08-21 19:07:08 | [diff] [blame] | 634 | net::URLRequestContextGetter* TestingProfile::GetMediaRequestContext() { |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | net::URLRequestContextGetter* |
| 639 | TestingProfile::GetMediaRequestContextForRenderProcess( |
| 640 | int renderer_child_id) { |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 644 | net::URLRequestContextGetter* TestingProfile::GetRequestContextForExtensions() { |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 645 | if (!extensions_request_context_) |
[email protected] | 00dea958 | 2012-05-09 15:20:05 | [diff] [blame] | 646 | extensions_request_context_ = new TestExtensionURLRequestContextGetter(); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 647 | return extensions_request_context_.get(); |
| 648 | } |
| 649 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 650 | net::SSLConfigService* TestingProfile::GetSSLConfigService() { |
| 651 | return NULL; |
| 652 | } |
| 653 | |
[email protected] | abe2c03 | 2011-03-31 18:49:34 | [diff] [blame] | 654 | net::URLRequestContextGetter* TestingProfile::GetRequestContextForIsolatedApp( |
[email protected] | d969667 | 2011-03-15 22:45:09 | [diff] [blame] | 655 | const std::string& app_id) { |
| 656 | // We don't test isolated app storage here yet, so returning the same dummy |
| 657 | // context is sufficient for now. |
| 658 | return GetRequestContext(); |
| 659 | } |
| 660 | |
[email protected] | df02aca | 2012-02-09 21:03:20 | [diff] [blame] | 661 | content::ResourceContext* TestingProfile::GetResourceContext() { |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 662 | if (!resource_context_.get()) |
| 663 | resource_context_.reset(new content::MockResourceContext()); |
| 664 | return resource_context_.get(); |
[email protected] | e89b77d | 2011-04-15 18:58:10 | [diff] [blame] | 665 | } |
| 666 | |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 667 | HostContentSettingsMap* TestingProfile::GetHostContentSettingsMap() { |
[email protected] | 35552dc5 | 2011-07-12 09:04:38 | [diff] [blame] | 668 | if (!host_content_settings_map_.get()) { |
[email protected] | 646c29cb6 | 2012-06-18 14:31:05 | [diff] [blame] | 669 | host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), false); |
| 670 | ExtensionService* extension_service = GetExtensionService(); |
| 671 | if (extension_service) |
| 672 | host_content_settings_map_->RegisterExtensionService(extension_service); |
[email protected] | 35552dc5 | 2011-07-12 09:04:38 | [diff] [blame] | 673 | } |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 674 | return host_content_settings_map_.get(); |
| 675 | } |
| 676 | |
[email protected] | 810ddc5 | 2012-01-24 01:00:35 | [diff] [blame] | 677 | content::GeolocationPermissionContext* |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 678 | TestingProfile::GetGeolocationPermissionContext() { |
| 679 | if (!geolocation_permission_context_.get()) { |
| 680 | geolocation_permission_context_ = |
[email protected] | c476e63 | 2011-06-23 11:18:04 | [diff] [blame] | 681 | new ChromeGeolocationPermissionContext(this); |
[email protected] | dbbad7a | 2010-08-13 18:18:36 | [diff] [blame] | 682 | } |
| 683 | return geolocation_permission_context_.get(); |
| 684 | } |
| 685 | |
[email protected] | c52b289 | 2012-03-07 11:01:02 | [diff] [blame] | 686 | content::SpeechRecognitionPreferences* |
| 687 | TestingProfile::GetSpeechRecognitionPreferences() { |
[email protected] | 3297132a | 2012-02-21 23:27:59 | [diff] [blame] | 688 | #if defined(ENABLE_INPUT_SPEECH) |
[email protected] | 750e957 | 2012-06-06 15:21:24 | [diff] [blame] | 689 | return ChromeSpeechRecognitionPreferences::GetForProfile(this); |
[email protected] | 3297132a | 2012-02-21 23:27:59 | [diff] [blame] | 690 | #else |
| 691 | return NULL; |
| 692 | #endif |
[email protected] | 8238dd6 | 2011-09-29 15:13:01 | [diff] [blame] | 693 | } |
| 694 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 695 | std::wstring TestingProfile::GetName() { |
| 696 | return std::wstring(); |
| 697 | } |
| 698 | |
| 699 | std::wstring TestingProfile::GetID() { |
| 700 | return id_; |
| 701 | } |
| 702 | |
| 703 | void TestingProfile::SetID(const std::wstring& id) { |
| 704 | id_ = id; |
| 705 | } |
| 706 | |
| 707 | bool TestingProfile::DidLastSessionExitCleanly() { |
| 708 | return last_session_exited_cleanly_; |
| 709 | } |
| 710 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 711 | bool TestingProfile::IsSameProfile(Profile *p) { |
| 712 | return this == p; |
| 713 | } |
| 714 | |
| 715 | base::Time TestingProfile::GetStartTime() const { |
| 716 | return start_time_; |
| 717 | } |
| 718 | |
[email protected] | a6d36cc | 2011-02-23 00:39:48 | [diff] [blame] | 719 | ProtocolHandlerRegistry* TestingProfile::GetProtocolHandlerRegistry() { |
[email protected] | ab563ff | 2012-07-21 00:26:32 | [diff] [blame] | 720 | return NULL; |
[email protected] | a6d36cc | 2011-02-23 00:39:48 | [diff] [blame] | 721 | } |
| 722 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 723 | FilePath TestingProfile::last_selected_directory() { |
| 724 | return last_selected_directory_; |
| 725 | } |
| 726 | |
| 727 | void TestingProfile::set_last_selected_directory(const FilePath& path) { |
| 728 | last_selected_directory_ = path; |
| 729 | } |
| 730 | |
[email protected] | cc5bfd4 | 2010-11-24 14:44:02 | [diff] [blame] | 731 | PrefProxyConfigTracker* TestingProfile::GetProxyConfigTracker() { |
[email protected] | 6f96cbcb | 2011-11-04 02:26:07 | [diff] [blame] | 732 | if (!pref_proxy_config_tracker_.get()) { |
| 733 | pref_proxy_config_tracker_.reset( |
| 734 | ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); |
| 735 | } |
| 736 | return pref_proxy_config_tracker_.get(); |
[email protected] | cc5bfd4 | 2010-11-24 14:44:02 | [diff] [blame] | 737 | } |
| 738 | |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 739 | void TestingProfile::BlockUntilHistoryProcessesPendingRequests() { |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 740 | scoped_refptr<HistoryService> history_service = |
[email protected] | 6c53704 | 2012-07-13 00:59:02 | [diff] [blame] | 741 | HistoryServiceFactory::GetForProfile(this, Profile::EXPLICIT_ACCESS); |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 742 | DCHECK(history_service.get()); |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 743 | DCHECK(MessageLoop::current()); |
| 744 | |
| 745 | CancelableRequestConsumer consumer; |
[email protected] | f61f478 | 2012-06-08 21:54:21 | [diff] [blame] | 746 | history_service->ScheduleDBTask(new QuittingHistoryDBTask(), &consumer); |
[email protected] | 0bfc29a | 2009-04-27 16:15:44 | [diff] [blame] | 747 | MessageLoop::current()->Run(); |
| 748 | } |
[email protected] | 345a8b7 | 2009-09-29 09:11:44 | [diff] [blame] | 749 | |
[email protected] | 8382d8c | 2011-09-15 03:43:17 | [diff] [blame] | 750 | chrome_browser_net::Predictor* TestingProfile::GetNetworkPredictor() { |
| 751 | return NULL; |
| 752 | } |
| 753 | |
[email protected] | db96a88 | 2011-10-09 02:01:54 | [diff] [blame] | 754 | void TestingProfile::ClearNetworkingHistorySince(base::Time time) { |
[email protected] | 02896a8 | 2011-09-21 18:54:32 | [diff] [blame] | 755 | NOTIMPLEMENTED(); |
| 756 | } |
| 757 | |
[email protected] | 671f9c6 | 2011-10-28 19:22:07 | [diff] [blame] | 758 | GURL TestingProfile::GetHomePage() { |
| 759 | return GURL(chrome::kChromeUINewTabURL); |
| 760 | } |
| 761 | |
[email protected] | fadc607b6 | 2011-02-07 17:55:50 | [diff] [blame] | 762 | PrefService* TestingProfile::GetOffTheRecordPrefs() { |
| 763 | return NULL; |
| 764 | } |
| 765 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 766 | quota::SpecialStoragePolicy* TestingProfile::GetSpecialStoragePolicy() { |
| 767 | return GetExtensionSpecialStoragePolicy(); |
| 768 | } |
| 769 | |
[email protected] | 1bee6ed | 2012-03-14 06:46:36 | [diff] [blame] | 770 | bool TestingProfile::WasCreatedByVersionOrLater(const std::string& version) { |
| 771 | return true; |
| 772 | } |
[email protected] | ef92e17 | 2012-04-25 19:40:41 | [diff] [blame] | 773 | |
| 774 | base::Callback<ChromeURLDataManagerBackend*(void)> |
| 775 | TestingProfile::GetChromeURLDataManagerBackendGetter() const { |
| 776 | return base::Callback<ChromeURLDataManagerBackend*(void)>(); |
| 777 | } |
[email protected] | 5c4c89f | 2012-08-07 21:09:59 | [diff] [blame] | 778 | |
| 779 | TestingProfile::Builder::Builder() |
| 780 | : build_called_(false), |
| 781 | delegate_(NULL) { |
| 782 | } |
| 783 | |
| 784 | TestingProfile::Builder::~Builder() { |
| 785 | } |
| 786 | |
| 787 | void TestingProfile::Builder::SetPath(const FilePath& path) { |
| 788 | path_ = path; |
| 789 | } |
| 790 | |
| 791 | void TestingProfile::Builder::SetDelegate(Delegate* delegate) { |
| 792 | delegate_ = delegate; |
| 793 | } |
| 794 | |
| 795 | void TestingProfile::Builder::SetExtensionSpecialStoragePolicy( |
| 796 | scoped_refptr<ExtensionSpecialStoragePolicy> policy) { |
| 797 | extension_policy_ = policy; |
| 798 | } |
| 799 | |
| 800 | void TestingProfile::Builder::SetPrefService(scoped_ptr<PrefService> prefs) { |
| 801 | pref_service_ = prefs.Pass(); |
| 802 | } |
| 803 | |
| 804 | void TestingProfile::Builder::SetUserCloudPolicyManager( |
| 805 | scoped_ptr<policy::UserCloudPolicyManager> manager) { |
| 806 | user_cloud_policy_manager_ = manager.Pass(); |
| 807 | } |
| 808 | |
| 809 | scoped_ptr<TestingProfile> TestingProfile::Builder::Build() { |
| 810 | DCHECK(!build_called_); |
| 811 | build_called_ = true; |
| 812 | return scoped_ptr<TestingProfile>(new TestingProfile( |
| 813 | path_, |
| 814 | delegate_, |
| 815 | extension_policy_, |
| 816 | pref_service_.Pass(), |
| 817 | user_cloud_policy_manager_.Pass())); |
| 818 | } |
| 819 | |
| 820 | |