1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_TEST_TESTING_PROFILE_H_
6 #define CHROME_TEST_TESTING_PROFILE_H_
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/scoped_temp_dir.h"
12 #include "base/timer.h"
13 #include "chrome/browser/profiles/profile.h"
16 class ResourceContextGetter
;
28 class SpecialStoragePolicy
;
31 class AutocompleteClassifier
;
35 class ExtensionPrefStore
;
36 class ExtensionPrefValueMap
;
37 class ExtensionSpecialStoragePolicy
;
40 class GeolocationContentSettingsMap
;
41 class GeolocationPermissionContext
;
43 class HostContentSettingsMap
;
45 class ProfileDependencyManager
;
46 class ProfileSyncService
;
47 class TemplateURLService
;
48 class TestingPrefService
;
53 class URLRequestContextGetter
;
56 class TestingProfile
: public Profile
{
60 virtual ~TestingProfile();
62 // Creates the favicon service. Consequent calls would recreate the service.
63 void CreateFaviconService();
65 // Creates the history service. If |delete_file| is true, the history file is
66 // deleted first, then the HistoryService is created. As TestingProfile
67 // deletes the directory containing the files used by HistoryService, this
68 // only matters if you're recreating the HistoryService. If |no_db| is true,
69 // the history backend will fail to initialize its database; this is useful
70 // for testing error conditions.
71 void CreateHistoryService(bool delete_file
, bool no_db
);
73 // Shuts down and nulls out the reference to HistoryService.
74 void DestroyHistoryService();
76 // Creates TopSites. This returns immediately, and top sites may not be
77 // loaded. Use BlockUntilTopSitesLoaded to ensure TopSites has finished
79 void CreateTopSites();
81 // Shuts down and nulls out the reference to TopSites.
82 void DestroyTopSites();
84 // Creates the BookmkarBarModel. If not invoked the bookmark bar model is
85 // NULL. If |delete_file| is true, the bookmarks file is deleted first, then
86 // the model is created. As TestingProfile deletes the directory containing
87 // the files used by HistoryService, the boolean only matters if you're
88 // recreating the BookmarkModel.
90 // NOTE: this does not block until the bookmarks are loaded. For that use
91 // BlockUntilBookmarkModelLoaded.
92 void CreateBookmarkModel(bool delete_file
);
94 // Creates an AutocompleteClassifier. If not invoked the
95 // AutocompleteClassifier is NULL.
96 void CreateAutocompleteClassifier();
98 // Creates a ProtocolHandlerRegistry. If not invoked the protocol handler
100 void CreateProtocolHandlerRegistry();
102 // Creates the webdata service. If |delete_file| is true, the webdata file is
103 // deleted first, then the WebDataService is created. As TestingProfile
104 // deletes the directory containing the files used by WebDataService, this
105 // only matters if you're recreating the WebDataService.
106 void CreateWebDataService(bool delete_file
);
108 // Blocks until the BookmarkModel finishes loaded. This is NOT invoked from
109 // CreateBookmarkModel.
110 void BlockUntilBookmarkModelLoaded();
112 // Blocks until TopSites finishes loading.
113 void BlockUntilTopSitesLoaded();
115 // Creates a TemplateURLService. If not invoked the TemplateURLService is
116 // NULL. Creates a TemplateURLFetcher. If not invoked, the
117 // TemplateURLFetcher is NULL.
118 void CreateTemplateURLFetcher();
120 // Creates a TemplateURLService. If not invoked, the TemplateURLService is
122 void CreateTemplateURLService();
124 // Creates an ExtensionService initialized with the testing profile and
125 // returns it. The profile keeps its own copy of a scoped_refptr to the
126 // ExtensionService to make sure that is still alive to be notified when the
127 // profile is destroyed.
128 ExtensionService
* CreateExtensionService(const CommandLine
* command_line
,
129 const FilePath
& install_directory
,
130 bool autoupdate_enabled
);
132 TestingPrefService
* GetTestingPrefService();
134 virtual TestingProfile
* AsTestingProfile();
136 virtual std::string
GetProfileName();
137 virtual ProfileId
GetRuntimeId();
139 virtual FilePath
GetPath();
141 // Sets whether we're incognito. Default is false.
142 void set_incognito(bool incognito
) {
143 incognito_
= incognito
;
145 virtual bool IsOffTheRecord();
146 // Assumes ownership.
147 virtual void SetOffTheRecordProfile(Profile
* profile
);
148 virtual Profile
* GetOffTheRecordProfile();
150 virtual void DestroyOffTheRecordProfile() {}
152 virtual bool HasOffTheRecordProfile();
154 virtual Profile
* GetOriginalProfile();
155 virtual ChromeAppCacheService
* GetAppCacheService();
156 virtual webkit_database::DatabaseTracker
* GetDatabaseTracker();
157 virtual VisitedLinkMaster
* GetVisitedLinkMaster();
158 virtual ExtensionService
* GetExtensionService();
159 virtual UserScriptMaster
* GetUserScriptMaster();
160 virtual ExtensionDevToolsManager
* GetExtensionDevToolsManager();
161 virtual ExtensionProcessManager
* GetExtensionProcessManager();
162 virtual ExtensionMessageService
* GetExtensionMessageService();
163 virtual ExtensionEventRouter
* GetExtensionEventRouter();
164 virtual ExtensionSpecialStoragePolicy
* GetExtensionSpecialStoragePolicy();
165 virtual SSLHostState
* GetSSLHostState();
166 virtual net::TransportSecurityState
* GetTransportSecurityState();
167 virtual FaviconService
* GetFaviconService(ServiceAccessType access
);
168 virtual HistoryService
* GetHistoryService(ServiceAccessType access
);
169 virtual HistoryService
* GetHistoryServiceWithoutCreating();
170 // The CookieMonster will only be returned if a Context has been created. Do
171 // this by calling CreateRequestContext(). See the note at GetRequestContext
172 // for more information.
173 net::CookieMonster
* GetCookieMonster();
174 virtual AutocompleteClassifier
* GetAutocompleteClassifier();
175 virtual WebDataService
* GetWebDataService(ServiceAccessType access
);
176 virtual WebDataService
* GetWebDataServiceWithoutCreating();
177 virtual PasswordStore
* GetPasswordStore(ServiceAccessType access
);
178 // Sets the profile's PrefService. If a pref service hasn't been explicitly
179 // set GetPrefs creates one, so normally you need not invoke this. If you need
180 // to set a pref service you must invoke this before GetPrefs.
181 // TestingPrefService takes ownership of |prefs|.
182 void SetPrefService(PrefService
* prefs
);
183 virtual PrefService
* GetPrefs();
184 virtual TemplateURLFetcher
* GetTemplateURLFetcher();
185 virtual history::TopSites
* GetTopSites();
186 virtual history::TopSites
* GetTopSitesWithoutCreating();
187 virtual DownloadManager
* GetDownloadManager();
188 virtual PersonalDataManager
* GetPersonalDataManager();
189 virtual fileapi::FileSystemContext
* GetFileSystemContext();
190 virtual quota::QuotaManager
* GetQuotaManager();
191 virtual BrowserSignin
* GetBrowserSignin();
192 virtual bool HasCreatedDownloadManager() const;
194 // Returns a testing ContextGetter (if one has been created via
195 // CreateRequestContext) or NULL. This is not done on-demand for two reasons:
196 // (1) Some tests depend on GetRequestContext() returning NULL. (2) Because
197 // of the special memory management considerations for the
198 // TestURLRequestContextGetter class, many tests would find themseleves
199 // leaking if they called this method without the necessary IO thread. This
200 // getter is currently only capable of returning a Context that helps test
201 // the CookieMonster. See implementation comments for more details.
202 virtual net::URLRequestContextGetter
* GetRequestContext();
203 virtual net::URLRequestContextGetter
* GetRequestContextForRenderProcess(
204 int renderer_child_id
);
205 void CreateRequestContext();
206 // Clears out the created request context (which must be done before shutting
207 // down the IO thread to avoid leaks).
208 void ResetRequestContext();
210 virtual net::URLRequestContextGetter
* GetRequestContextForMedia();
211 virtual net::URLRequestContextGetter
* GetRequestContextForExtensions();
212 virtual net::URLRequestContextGetter
* GetRequestContextForIsolatedApp(
213 const std::string
& app_id
);
215 virtual const content::ResourceContext
& GetResourceContext();
217 virtual net::SSLConfigService
* GetSSLConfigService();
218 virtual UserStyleSheetWatcher
* GetUserStyleSheetWatcher();
219 virtual FindBarState
* GetFindBarState();
220 virtual HostContentSettingsMap
* GetHostContentSettingsMap();
221 virtual GeolocationContentSettingsMap
* GetGeolocationContentSettingsMap();
222 virtual GeolocationPermissionContext
* GetGeolocationPermissionContext();
223 virtual HostZoomMap
* GetHostZoomMap();
224 virtual bool HasProfileSyncService() const;
225 virtual std::wstring
GetName();
226 virtual void SetName(const std::wstring
& name
) {}
227 virtual std::wstring
GetID();
228 virtual void SetID(const std::wstring
& id
);
229 void set_last_session_exited_cleanly(bool value
) {
230 last_session_exited_cleanly_
= value
;
232 virtual bool DidLastSessionExitCleanly();
233 virtual void MergeResourceString(int message_id
,
234 std::wstring
* output_string
) {}
235 virtual void MergeResourceInteger(int message_id
, int* output_value
) {}
236 virtual void MergeResourceBoolean(int message_id
, bool* output_value
) {}
237 virtual BookmarkModel
* GetBookmarkModel();
238 virtual bool IsSameProfile(Profile
*p
);
239 virtual base::Time
GetStartTime() const;
240 virtual ProtocolHandlerRegistry
* GetProtocolHandlerRegistry();
241 virtual SpellCheckHost
* GetSpellCheckHost();
242 virtual void ReinitializeSpellCheckHost(bool force
) { }
243 virtual WebKitContext
* GetWebKitContext();
244 virtual WebKitContext
* GetOffTheRecordWebKitContext();
245 virtual void MarkAsCleanShutdown() {}
246 virtual void InitExtensions(bool extensions_enabled
) {}
247 virtual void InitPromoResources() {}
248 virtual void InitRegisteredProtocolHandlers() {}
249 virtual NTPResourceCache
* GetNTPResourceCache();
251 virtual FilePath
last_selected_directory();
252 virtual void set_last_selected_directory(const FilePath
& path
);
253 #if defined(OS_CHROMEOS)
254 virtual void SetupChromeOSEnterpriseExtensionObserver() {
256 virtual void InitChromeOSPreferences() {
258 virtual void ChangeAppLocale(const std::string
&, AppLocaleChangedVia
) {
260 virtual void OnLogin() {
262 #endif // defined(OS_CHROMEOS)
264 virtual PrefProxyConfigTracker
* GetProxyConfigTracker();
266 // Schedules a task on the history backend and runs a nested loop until the
267 // task is processed. This has the effect of blocking the caller until the
268 // history service processes all pending requests.
269 void BlockUntilHistoryProcessesPendingRequests();
271 // Creates and initializes a profile sync service if the tests require one.
272 virtual TokenService
* GetTokenService();
273 virtual ProfileSyncService
* GetProfileSyncService();
274 virtual ProfileSyncService
* GetProfileSyncService(
275 const std::string
& cros_notes
);
276 virtual CloudPrintProxyService
* GetCloudPrintProxyService();
277 virtual ChromeBlobStorageContext
* GetBlobStorageContext();
278 virtual ExtensionInfoMap
* GetExtensionInfoMap();
279 virtual PromoCounter
* GetInstantPromoCounter();
280 virtual ChromeURLDataManager
* GetChromeURLDataManager();
281 virtual prerender::PrerenderManager
* GetPrerenderManager();
282 virtual PrefService
* GetOffTheRecordPrefs();
284 // TODO(jam): remove me once webkit_context_unittest.cc doesn't use Profile
285 // and gets the quota::SpecialStoragePolicy* from whatever ends up replacing
286 // it in the content module.
287 quota::SpecialStoragePolicy
* GetSpecialStoragePolicy();
290 base::Time start_time_
;
291 scoped_ptr
<PrefService
> prefs_
;
292 // ref only for right type, lifecycle is managed by prefs_
293 TestingPrefService
* testing_prefs_
;
296 // Destroys favicon service if it has been created.
297 void DestroyFaviconService();
299 // If the webdata service has been created, it is destroyed. This is invoked
300 // from the destructor.
301 void DestroyWebDataService();
303 // Creates a TestingPrefService and associates it with the TestingProfile.
304 void CreateTestingPrefService();
306 // The favicon service. Only created if CreateFaviconService is invoked.
307 scoped_refptr
<FaviconService
> favicon_service_
;
309 // The history service. Only created if CreateHistoryService is invoked.
310 scoped_refptr
<HistoryService
> history_service_
;
312 // The BookmarkModel. Only created if CreateBookmarkModel is invoked.
313 scoped_ptr
<BookmarkModel
> bookmark_bar_model_
;
315 // The ProtocolHandlerRegistry. Only created if CreateProtocolHandlerRegistry
317 scoped_refptr
<ProtocolHandlerRegistry
> protocol_handler_registry_
;
319 // The TokenService. Created by CreateTokenService. Filled with dummy data.
320 scoped_ptr
<TokenService
> token_service_
;
322 // The ProfileSyncService. Created by CreateProfileSyncService.
323 scoped_ptr
<ProfileSyncService
> profile_sync_service_
;
325 // The AutocompleteClassifier. Only created if CreateAutocompleteClassifier
327 scoped_ptr
<AutocompleteClassifier
> autocomplete_classifier_
;
329 // The WebDataService. Only created if CreateWebDataService is invoked.
330 scoped_refptr
<WebDataService
> web_data_service_
;
332 // The TemplateURLFetcher. Only created if CreateTemplateURLFetcher is
334 scoped_ptr
<TemplateURLFetcher
> template_url_fetcher_
;
336 scoped_ptr
<NTPResourceCache
> ntp_resource_cache_
;
338 // Internally, this is a TestURLRequestContextGetter that creates a dummy
339 // request context. Currently, only the CookieMonster is hooked up.
340 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
341 scoped_refptr
<net::URLRequestContextGetter
> extensions_request_context_
;
346 scoped_ptr
<Profile
> incognito_profile_
;
348 // Did the last session exit cleanly? Default is true.
349 bool last_session_exited_cleanly_
;
352 // FileSystemContext. Created lazily by GetFileSystemContext().
353 scoped_refptr
<fileapi::FileSystemContext
> file_system_context_
;
355 // WebKitContext, lazily initialized by GetWebKitContext().
356 scoped_refptr
<WebKitContext
> webkit_context_
;
358 // The main database tracker for this profile.
359 // Should be used only on the file thread.
360 scoped_refptr
<webkit_database::DatabaseTracker
> db_tracker_
;
362 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
363 scoped_refptr
<GeolocationContentSettingsMap
>
364 geolocation_content_settings_map_
;
365 scoped_refptr
<GeolocationPermissionContext
> geolocation_permission_context_
;
367 // Find bar state. Created lazily by GetFindBarState().
368 scoped_ptr
<FindBarState
> find_bar_state_
;
370 FilePath last_selected_directory_
;
371 scoped_refptr
<history::TopSites
> top_sites_
; // For history and thumbnails.
373 // The Extension Preferences. Only created if CreateExtensionService is
375 scoped_ptr
<ExtensionPrefs
> extension_prefs_
;
377 scoped_ptr
<ExtensionService
> extension_service_
;
379 scoped_ptr
<ExtensionPrefValueMap
> extension_pref_value_map_
;
381 scoped_refptr
<ExtensionSpecialStoragePolicy
>
382 extension_special_storage_policy_
;
385 // The proxy prefs tracker.
386 scoped_refptr
<PrefProxyConfigTracker
> pref_proxy_config_tracker_
;
388 // We use a temporary directory to store testing profile data.
389 ScopedTempDir temp_dir_
;
391 scoped_ptr
<ChromeURLDataManager
> chrome_url_data_manager_
;
393 scoped_ptr
<prerender::PrerenderManager
> prerender_manager_
;
395 // We keep a weak pointer to the dependency manager we want to notify on our
396 // death. Defaults to the Singleton implementation but overridable for
398 ProfileDependencyManager
* profile_dependency_manager_
;
401 // A profile that derives from another profile. This does not actually
402 // override anything except the GetRuntimeId() in order to test sharing of
404 class DerivedTestingProfile
: public TestingProfile
{
406 explicit DerivedTestingProfile(Profile
* profile
);
407 virtual ~DerivedTestingProfile();
409 virtual ProfileId
GetRuntimeId();
412 Profile
* original_profile_
;
415 #endif // CHROME_TEST_TESTING_PROFILE_H_