Dismiss EME infobar when WebMediaPlayer is destroyed.
[chromium-blink-merge.git] / chrome / test / base / testing_profile.h
blob9f963891272ee141901ef4718fe6da626ab97291
1 // Copyright (c) 2012 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_BASE_TESTING_PROFILE_H_
6 #define CHROME_TEST_BASE_TESTING_PROFILE_H_
8 #include <string>
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
16 namespace content {
17 class MockResourceContext;
20 namespace extensions {
21 class ExtensionPrefs;
24 namespace history {
25 class TopSites;
28 namespace net {
29 class CookieMonster;
30 class URLRequestContextGetter;
33 namespace policy {
34 class PolicyService;
35 class ProfilePolicyConnector;
36 class SchemaRegistryService;
39 namespace quota {
40 class SpecialStoragePolicy;
43 class BrowserContextDependencyManager;
44 class CommandLine;
45 class ExtensionSpecialStoragePolicy;
46 class HostContentSettingsMap;
47 class PrefServiceSyncable;
48 class ProfileSyncService;
49 class TemplateURLService;
50 class TestingPrefServiceSyncable;
52 class TestingProfile : public Profile {
53 public:
54 // Profile directory name for the test user. This is "Default" on most
55 // platforms but must be different on ChromeOS because a logged-in user cannot
56 // use "Default" as profile directory.
57 // Browser- and UI tests should always use this to get to the user's profile
58 // directory. Unit-tests, though, should use |kInitialProfile|, which is
59 // always "Default", because they are runnining without logged-in user.
60 static const char kTestUserProfileDir[];
62 // Default constructor that cannot be used with multi-profiles.
63 TestingProfile();
65 typedef std::vector<std::pair<
66 BrowserContextKeyedServiceFactory*,
67 BrowserContextKeyedServiceFactory::TestingFactoryFunction> >
68 TestingFactories;
70 // Helper class for building an instance of TestingProfile (allows injecting
71 // mocks for various services prior to profile initialization).
72 // TODO(atwilson): Remove non-default constructors and various setters in
73 // favor of using the Builder API.
74 class Builder {
75 public:
76 Builder();
77 ~Builder();
79 // Sets a Delegate to be called back during profile init. This causes the
80 // final initialization to be performed via a task so the caller must run
81 // a MessageLoop. Caller maintains ownership of the Delegate
82 // and must manage its lifetime so it continues to exist until profile
83 // initialization is complete.
84 void SetDelegate(Delegate* delegate);
86 // Adds a testing factory to the TestingProfile. These testing factories
87 // are applied before the ProfileKeyedServices are created.
88 void AddTestingFactory(
89 BrowserContextKeyedServiceFactory* service_factory,
90 BrowserContextKeyedServiceFactory::TestingFactoryFunction callback);
92 // Sets the ExtensionSpecialStoragePolicy to be returned by
93 // GetExtensionSpecialStoragePolicy().
94 void SetExtensionSpecialStoragePolicy(
95 scoped_refptr<ExtensionSpecialStoragePolicy> policy);
97 // Sets the path to the directory to be used to hold profile data.
98 void SetPath(const base::FilePath& path);
100 // Sets the PrefService to be used by this profile.
101 void SetPrefService(scoped_ptr<PrefServiceSyncable> prefs);
103 // Makes the Profile being built an incognito profile.
104 void SetIncognito();
106 // Makes the Profile being built a guest profile.
107 void SetGuestSession();
109 // Sets the managed user ID (which is empty by default). If it is set to a
110 // non-empty string, the profile is managed.
111 void SetManagedUserId(const std::string& managed_user_id);
113 // Sets the PolicyService to be used by this profile.
114 void SetPolicyService(scoped_ptr<policy::PolicyService> policy_service);
116 // Creates the TestingProfile using previously-set settings.
117 scoped_ptr<TestingProfile> Build();
119 private:
120 // If true, Build() has already been called.
121 bool build_called_;
123 // Various staging variables where values are held until Build() is invoked.
124 scoped_ptr<PrefServiceSyncable> pref_service_;
125 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy_;
126 base::FilePath path_;
127 Delegate* delegate_;
128 bool incognito_;
129 bool guest_session_;
130 std::string managed_user_id_;
131 scoped_ptr<policy::PolicyService> policy_service_;
132 TestingFactories testing_factories_;
134 DISALLOW_COPY_AND_ASSIGN(Builder);
137 // Multi-profile aware constructor that takes the path to a directory managed
138 // for this profile. This constructor is meant to be used by
139 // TestingProfileManager::CreateTestingProfile. If you need to create multi-
140 // profile profiles, use that factory method instead of this directly.
141 // Exception: if you need to create multi-profile profiles for testing the
142 // ProfileManager, then use the constructor below instead.
143 explicit TestingProfile(const base::FilePath& path);
145 // Multi-profile aware constructor that takes the path to a directory managed
146 // for this profile and a delegate. This constructor is meant to be used
147 // for unittesting the ProfileManager.
148 TestingProfile(const base::FilePath& path, Delegate* delegate);
150 // Full constructor allowing the setting of all possible instance data.
151 // Callers should use Builder::Build() instead of invoking this constructor.
152 TestingProfile(const base::FilePath& path,
153 Delegate* delegate,
154 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy,
155 scoped_ptr<PrefServiceSyncable> prefs,
156 bool incognito,
157 bool guest_session,
158 const std::string& managed_user_id,
159 scoped_ptr<policy::PolicyService> policy_service,
160 const TestingFactories& factories);
162 virtual ~TestingProfile();
164 // Creates the favicon service. Consequent calls would recreate the service.
165 void CreateFaviconService();
167 // Creates the history service. If |delete_file| is true, the history file is
168 // deleted first, then the HistoryService is created. As TestingProfile
169 // deletes the directory containing the files used by HistoryService, this
170 // only matters if you're recreating the HistoryService. If |no_db| is true,
171 // the history backend will fail to initialize its database; this is useful
172 // for testing error conditions. Returns true on success.
173 bool CreateHistoryService(bool delete_file, bool no_db) WARN_UNUSED_RESULT;
175 // Shuts down and nulls out the reference to HistoryService.
176 void DestroyHistoryService();
178 // Creates TopSites. This returns immediately, and top sites may not be
179 // loaded. Use BlockUntilTopSitesLoaded to ensure TopSites has finished
180 // loading.
181 void CreateTopSites();
183 // Shuts down and nulls out the reference to TopSites.
184 void DestroyTopSites();
186 // Creates the BookmkarBarModel. If not invoked the bookmark bar model is
187 // NULL. If |delete_file| is true, the bookmarks file is deleted first, then
188 // the model is created. As TestingProfile deletes the directory containing
189 // the files used by HistoryService, the boolean only matters if you're
190 // recreating the BookmarkModel.
192 // NOTE: this does not block until the bookmarks are loaded. For that use
193 // WaitForBookmarkModelToLoad().
194 void CreateBookmarkModel(bool delete_file);
196 // Creates a WebDataService. If not invoked, the web data service is NULL.
197 void CreateWebDataService();
199 // Blocks until the HistoryService finishes restoring its in-memory cache.
200 // This is NOT invoked from CreateHistoryService.
201 void BlockUntilHistoryIndexIsRefreshed();
203 // Blocks until TopSites finishes loading.
204 void BlockUntilTopSitesLoaded();
206 TestingPrefServiceSyncable* GetTestingPrefService();
208 // content::BrowserContext
209 virtual base::FilePath GetPath() const OVERRIDE;
210 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE;
211 virtual bool IsOffTheRecord() const OVERRIDE;
212 virtual content::DownloadManagerDelegate*
213 GetDownloadManagerDelegate() OVERRIDE;
214 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE;
215 virtual net::URLRequestContextGetter* CreateRequestContext(
216 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE;
217 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
218 int renderer_child_id) OVERRIDE;
219 virtual content::ResourceContext* GetResourceContext() OVERRIDE;
220 virtual content::GeolocationPermissionContext*
221 GetGeolocationPermissionContext() OVERRIDE;
222 virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE;
224 virtual TestingProfile* AsTestingProfile() OVERRIDE;
225 virtual std::string GetProfileName() OVERRIDE;
227 // DEPRECATED, because it's fragile to change a profile from non-incognito
228 // to incognito after the ProfileKeyedServices have been created (some
229 // ProfileKeyedServices either should not exist in incognito mode, or will
230 // crash when they try to get references to other services they depend on,
231 // but do not exist in incognito mode).
232 // TODO(atwilson): Remove this API (http://crbug.com/277296).
234 // Changes a profile's to/from incognito mode temporarily - profile will be
235 // returned to non-incognito before destruction to allow services to
236 // properly shutdown. This is only supported for legacy tests - new tests
237 // should create a true incognito profile using Builder::SetIncognito() or
238 // by using the TestingProfile constructor that allows setting the incognito
239 // flag.
240 void ForceIncognito(bool force_incognito) {
241 force_incognito_ = force_incognito;
244 // Assumes ownership.
245 virtual void SetOffTheRecordProfile(scoped_ptr<Profile> profile);
246 virtual void SetOriginalProfile(Profile* profile);
247 virtual Profile* GetOffTheRecordProfile() OVERRIDE;
248 virtual void DestroyOffTheRecordProfile() OVERRIDE {}
249 virtual bool HasOffTheRecordProfile() OVERRIDE;
250 virtual Profile* GetOriginalProfile() OVERRIDE;
251 virtual bool IsManaged() OVERRIDE;
252 virtual ExtensionService* GetExtensionService() OVERRIDE;
253 void SetExtensionSpecialStoragePolicy(
254 ExtensionSpecialStoragePolicy* extension_special_storage_policy);
255 virtual ExtensionSpecialStoragePolicy*
256 GetExtensionSpecialStoragePolicy() OVERRIDE;
257 // TODO(ajwong): Remove this API in favor of directly retrieving the
258 // CookieStore from the StoragePartition after ExtensionURLRequestContext
259 // has been removed.
260 net::CookieMonster* GetCookieMonster();
262 virtual PrefService* GetPrefs() OVERRIDE;
264 virtual history::TopSites* GetTopSites() OVERRIDE;
265 virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE;
267 virtual net::URLRequestContextGetter* GetMediaRequestContext() OVERRIDE;
268 virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
269 int renderer_child_id) OVERRIDE;
270 virtual net::URLRequestContextGetter*
271 GetRequestContextForExtensions() OVERRIDE;
272 virtual net::URLRequestContextGetter*
273 GetMediaRequestContextForStoragePartition(
274 const base::FilePath& partition_path,
275 bool in_memory) OVERRIDE;
276 virtual void RequestMIDISysExPermission(
277 int render_process_id,
278 int render_view_id,
279 int bridge_id,
280 const GURL& requesting_frame,
281 const MIDISysExPermissionCallback& callback) OVERRIDE;
282 virtual void CancelMIDISysExPermissionRequest(
283 int render_process_id,
284 int render_view_id,
285 int bridge_id,
286 const GURL& requesting_frame) OVERRIDE;
287 virtual void RequestProtectedMediaIdentifierPermission(
288 int render_process_id,
289 int render_view_id,
290 int bridge_id,
291 int group_id,
292 const GURL& requesting_frame,
293 const ProtectedMediaIdentifierPermissionCallback& callback) OVERRIDE;
294 virtual void CancelProtectedMediaIdentifierPermissionRequests(
295 int group_id) OVERRIDE;
296 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
297 const base::FilePath& partition_path,
298 bool in_memory,
299 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE;
300 virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE;
301 virtual HostContentSettingsMap* GetHostContentSettingsMap() OVERRIDE;
302 virtual std::wstring GetName();
303 virtual void SetName(const std::wstring& name) {}
304 virtual std::wstring GetID();
305 virtual void SetID(const std::wstring& id);
306 void set_last_session_exited_cleanly(bool value) {
307 last_session_exited_cleanly_ = value;
309 virtual void MergeResourceString(int message_id,
310 std::wstring* output_string) {}
311 virtual void MergeResourceInteger(int message_id, int* output_value) {}
312 virtual void MergeResourceBoolean(int message_id, bool* output_value) {}
313 virtual bool IsSameProfile(Profile *p) OVERRIDE;
314 virtual base::Time GetStartTime() const OVERRIDE;
315 virtual base::FilePath last_selected_directory() OVERRIDE;
316 virtual void set_last_selected_directory(const base::FilePath& path) OVERRIDE;
317 virtual bool WasCreatedByVersionOrLater(const std::string& version) OVERRIDE;
318 virtual bool IsGuestSession() const OVERRIDE;
319 virtual void SetExitType(ExitType exit_type) OVERRIDE {}
320 virtual ExitType GetLastSessionExitType() OVERRIDE;
321 #if defined(OS_CHROMEOS)
322 virtual void ChangeAppLocale(const std::string&,
323 AppLocaleChangedVia) OVERRIDE {
325 virtual void OnLogin() OVERRIDE {
327 virtual void InitChromeOSPreferences() OVERRIDE {
329 #endif // defined(OS_CHROMEOS)
331 virtual PrefProxyConfigTracker* GetProxyConfigTracker() OVERRIDE;
333 // Schedules a task on the history backend and runs a nested loop until the
334 // task is processed. This has the effect of blocking the caller until the
335 // history service processes all pending requests.
336 void BlockUntilHistoryProcessesPendingRequests();
338 virtual chrome_browser_net::Predictor* GetNetworkPredictor() OVERRIDE;
339 virtual void ClearNetworkingHistorySince(
340 base::Time time,
341 const base::Closure& completion) OVERRIDE;
342 virtual GURL GetHomePage() OVERRIDE;
344 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE;
346 void set_profile_name(const std::string& profile_name) {
347 profile_name_ = profile_name;
350 protected:
351 base::Time start_time_;
352 scoped_ptr<PrefServiceSyncable> prefs_;
353 // ref only for right type, lifecycle is managed by prefs_
354 TestingPrefServiceSyncable* testing_prefs_;
356 private:
357 // Creates a temporary directory for use by this profile.
358 void CreateTempProfileDir();
360 // Common initialization between the two constructors.
361 void Init();
363 // Finishes initialization when a profile is created asynchronously.
364 void FinishInit();
366 // Creates a TestingPrefService and associates it with the TestingProfile.
367 void CreateTestingPrefService();
369 // Creates a ProfilePolicyConnector that the ProfilePolicyConnectorFactory
370 // maps to this profile.
371 void CreateProfilePolicyConnector();
373 // Internally, this is a TestURLRequestContextGetter that creates a dummy
374 // request context. Currently, only the CookieMonster is hooked up.
375 scoped_refptr<net::URLRequestContextGetter> extensions_request_context_;
377 std::wstring id_;
379 bool incognito_;
380 bool force_incognito_;
381 scoped_ptr<Profile> incognito_profile_;
382 Profile* original_profile_;
384 bool guest_session_;
386 std::string managed_user_id_;
388 // Did the last session exit cleanly? Default is true.
389 bool last_session_exited_cleanly_;
391 scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
393 base::FilePath last_selected_directory_;
394 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails.
396 scoped_refptr<ExtensionSpecialStoragePolicy>
397 extension_special_storage_policy_;
399 // The proxy prefs tracker.
400 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_;
402 // We use a temporary directory to store testing profile data. In a multi-
403 // profile environment, this is invalid and the directory is managed by the
404 // TestingProfileManager.
405 base::ScopedTempDir temp_dir_;
406 // The path to this profile. This will be valid in either of the two above
407 // cases.
408 base::FilePath profile_path_;
410 // We keep a weak pointer to the dependency manager we want to notify on our
411 // death. Defaults to the Singleton implementation but overridable for
412 // testing.
413 BrowserContextDependencyManager* browser_context_dependency_manager_;
415 // Owned, but must be deleted on the IO thread so not placing in a
416 // scoped_ptr<>.
417 content::MockResourceContext* resource_context_;
419 #if defined(ENABLE_CONFIGURATION_POLICY)
420 scoped_ptr<policy::SchemaRegistryService> schema_registry_service_;
421 #endif
422 scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_;
424 // Weak pointer to a delegate for indicating that a profile was created.
425 Delegate* delegate_;
427 std::string profile_name_;
429 scoped_ptr<policy::PolicyService> policy_service_;
432 #endif // CHROME_TEST_BASE_TESTING_PROFILE_H_