| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_H_ | 5 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_H_ |
| 6 #define CHROME_TEST_BASE_TESTING_PROFILE_H_ | 6 #define CHROME_TEST_BASE_TESTING_PROFILE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 class MockResourceContext; | 17 class MockResourceContext; |
| 17 } | 18 } |
| 18 | 19 |
| 19 namespace extensions { | 20 namespace extensions { |
| 20 class ExtensionPrefs; | 21 class ExtensionPrefs; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace history { | 24 namespace history { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 52 // platforms but must be different on ChromeOS because a logged-in user cannot | 53 // platforms but must be different on ChromeOS because a logged-in user cannot |
| 53 // use "Default" as profile directory. | 54 // use "Default" as profile directory. |
| 54 // Browser- and UI tests should always use this to get to the user's profile | 55 // Browser- and UI tests should always use this to get to the user's profile |
| 55 // directory. Unit-tests, though, should use |kInitialProfile|, which is | 56 // directory. Unit-tests, though, should use |kInitialProfile|, which is |
| 56 // always "Default", because they are runnining without logged-in user. | 57 // always "Default", because they are runnining without logged-in user. |
| 57 static const char kTestUserProfileDir[]; | 58 static const char kTestUserProfileDir[]; |
| 58 | 59 |
| 59 // Default constructor that cannot be used with multi-profiles. | 60 // Default constructor that cannot be used with multi-profiles. |
| 60 TestingProfile(); | 61 TestingProfile(); |
| 61 | 62 |
| 63 typedef std::vector<std::pair< |
| 64 BrowserContextKeyedServiceFactory*, |
| 65 BrowserContextKeyedServiceFactory::FactoryFunction> > TestingFactories; |
| 66 |
| 62 // Helper class for building an instance of TestingProfile (allows injecting | 67 // Helper class for building an instance of TestingProfile (allows injecting |
| 63 // mocks for various services prior to profile initialization). | 68 // mocks for various services prior to profile initialization). |
| 64 // TODO(atwilson): Remove non-default constructors and various setters in | 69 // TODO(atwilson): Remove non-default constructors and various setters in |
| 65 // favor of using the Builder API. | 70 // favor of using the Builder API. |
| 66 class Builder { | 71 class Builder { |
| 67 public: | 72 public: |
| 68 Builder(); | 73 Builder(); |
| 69 ~Builder(); | 74 ~Builder(); |
| 70 | 75 |
| 71 // Sets a Delegate to be called back when the Profile is fully initialized. | 76 // Sets a Delegate to be called back during profile init. This causes the |
| 72 // This causes the final initialization to be performed via a task so the | 77 // final initialization to be performed via a task so the caller must run |
| 73 // caller must run a MessageLoop. Caller maintains ownership of the Delegate | 78 // a MessageLoop. Caller maintains ownership of the Delegate |
| 74 // and must manage its lifetime so it continues to exist until profile | 79 // and must manage its lifetime so it continues to exist until profile |
| 75 // initialization is complete. | 80 // initialization is complete. |
| 76 void SetDelegate(Delegate* delegate); | 81 void SetDelegate(Delegate* delegate); |
| 77 | 82 |
| 83 // Adds a testing factory to the TestingProfile. These testing factories |
| 84 // are applied before the ProfileKeyedServices are created. |
| 85 void AddTestingFactory( |
| 86 BrowserContextKeyedServiceFactory* service_factory, |
| 87 BrowserContextKeyedServiceFactory::FactoryFunction callback); |
| 88 |
| 78 // Sets the ExtensionSpecialStoragePolicy to be returned by | 89 // Sets the ExtensionSpecialStoragePolicy to be returned by |
| 79 // GetExtensionSpecialStoragePolicy(). | 90 // GetExtensionSpecialStoragePolicy(). |
| 80 void SetExtensionSpecialStoragePolicy( | 91 void SetExtensionSpecialStoragePolicy( |
| 81 scoped_refptr<ExtensionSpecialStoragePolicy> policy); | 92 scoped_refptr<ExtensionSpecialStoragePolicy> policy); |
| 82 | 93 |
| 83 // Sets the path to the directory to be used to hold profile data. | 94 // Sets the path to the directory to be used to hold profile data. |
| 84 void SetPath(const base::FilePath& path); | 95 void SetPath(const base::FilePath& path); |
| 85 | 96 |
| 86 // Sets the PrefService to be used by this profile. | 97 // Sets the PrefService to be used by this profile. |
| 87 void SetPrefService(scoped_ptr<PrefServiceSyncable> prefs); | 98 void SetPrefService(scoped_ptr<PrefServiceSyncable> prefs); |
| 88 | 99 |
| 100 // Makes the Profile being built an incognito profile. |
| 101 void SetIncognito(); |
| 102 |
| 89 // Creates the TestingProfile using previously-set settings. | 103 // Creates the TestingProfile using previously-set settings. |
| 90 scoped_ptr<TestingProfile> Build(); | 104 scoped_ptr<TestingProfile> Build(); |
| 91 | 105 |
| 92 private: | 106 private: |
| 93 // If true, Build() has already been called. | 107 // If true, Build() has already been called. |
| 94 bool build_called_; | 108 bool build_called_; |
| 95 | 109 |
| 96 // Various staging variables where values are held until Build() is invoked. | 110 // Various staging variables where values are held until Build() is invoked. |
| 97 scoped_ptr<PrefServiceSyncable> pref_service_; | 111 scoped_ptr<PrefServiceSyncable> pref_service_; |
| 98 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy_; | 112 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy_; |
| 99 base::FilePath path_; | 113 base::FilePath path_; |
| 100 Delegate* delegate_; | 114 Delegate* delegate_; |
| 115 bool incognito_; |
| 116 TestingFactories testing_factories_; |
| 101 | 117 |
| 102 DISALLOW_COPY_AND_ASSIGN(Builder); | 118 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 103 }; | 119 }; |
| 104 | 120 |
| 105 // Multi-profile aware constructor that takes the path to a directory managed | 121 // Multi-profile aware constructor that takes the path to a directory managed |
| 106 // for this profile. This constructor is meant to be used by | 122 // for this profile. This constructor is meant to be used by |
| 107 // TestingProfileManager::CreateTestingProfile. If you need to create multi- | 123 // TestingProfileManager::CreateTestingProfile. If you need to create multi- |
| 108 // profile profiles, use that factory method instead of this directly. | 124 // profile profiles, use that factory method instead of this directly. |
| 109 // Exception: if you need to create multi-profile profiles for testing the | 125 // Exception: if you need to create multi-profile profiles for testing the |
| 110 // ProfileManager, then use the constructor below instead. | 126 // ProfileManager, then use the constructor below instead. |
| 111 explicit TestingProfile(const base::FilePath& path); | 127 explicit TestingProfile(const base::FilePath& path); |
| 112 | 128 |
| 113 // Multi-profile aware constructor that takes the path to a directory managed | 129 // Multi-profile aware constructor that takes the path to a directory managed |
| 114 // for this profile and a delegate. This constructor is meant to be used | 130 // for this profile and a delegate. This constructor is meant to be used |
| 115 // for unittesting the ProfileManager. | 131 // for unittesting the ProfileManager. |
| 116 TestingProfile(const base::FilePath& path, Delegate* delegate); | 132 TestingProfile(const base::FilePath& path, Delegate* delegate); |
| 117 | 133 |
| 118 // Full constructor allowing the setting of all possible instance data. | 134 // Full constructor allowing the setting of all possible instance data. |
| 119 // Callers should use Builder::Build() instead of invoking this constructor. | 135 // Callers should use Builder::Build() instead of invoking this constructor. |
| 120 TestingProfile(const base::FilePath& path, | 136 TestingProfile(const base::FilePath& path, |
| 121 Delegate* delegate, | 137 Delegate* delegate, |
| 122 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, | 138 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, |
| 123 scoped_ptr<PrefServiceSyncable> prefs); | 139 scoped_ptr<PrefServiceSyncable> prefs, |
| 140 bool incognito, |
| 141 const TestingFactories& factories); |
| 124 | 142 |
| 125 virtual ~TestingProfile(); | 143 virtual ~TestingProfile(); |
| 126 | 144 |
| 127 // Creates the favicon service. Consequent calls would recreate the service. | 145 // Creates the favicon service. Consequent calls would recreate the service. |
| 128 void CreateFaviconService(); | 146 void CreateFaviconService(); |
| 129 | 147 |
| 130 // Creates the history service. If |delete_file| is true, the history file is | 148 // Creates the history service. If |delete_file| is true, the history file is |
| 131 // deleted first, then the HistoryService is created. As TestingProfile | 149 // deleted first, then the HistoryService is created. As TestingProfile |
| 132 // deletes the directory containing the files used by HistoryService, this | 150 // deletes the directory containing the files used by HistoryService, this |
| 133 // only matters if you're recreating the HistoryService. If |no_db| is true, | 151 // only matters if you're recreating the HistoryService. If |no_db| is true, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; | 197 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; |
| 180 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess( | 198 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess( |
| 181 int renderer_child_id) OVERRIDE; | 199 int renderer_child_id) OVERRIDE; |
| 182 virtual content::ResourceContext* GetResourceContext() OVERRIDE; | 200 virtual content::ResourceContext* GetResourceContext() OVERRIDE; |
| 183 virtual content::GeolocationPermissionContext* | 201 virtual content::GeolocationPermissionContext* |
| 184 GetGeolocationPermissionContext() OVERRIDE; | 202 GetGeolocationPermissionContext() OVERRIDE; |
| 185 virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE; | 203 virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE; |
| 186 | 204 |
| 187 virtual TestingProfile* AsTestingProfile() OVERRIDE; | 205 virtual TestingProfile* AsTestingProfile() OVERRIDE; |
| 188 virtual std::string GetProfileName() OVERRIDE; | 206 virtual std::string GetProfileName() OVERRIDE; |
| 189 void set_incognito(bool incognito) { incognito_ = incognito; } | 207 |
| 208 // Changes a profile's to/from incognito mode temporarily - profile will be |
| 209 // returned to non-incognito before destruction to allow services to |
| 210 // properly shutdown. This is only supported for legacy tests - new tests |
| 211 // should create a fresh incognito profile using Builder::SetIncognito(). |
| 212 void ForceIncognito(bool force_incognito) { |
| 213 force_incognito_ = force_incognito; |
| 214 } |
| 215 |
| 190 // Assumes ownership. | 216 // Assumes ownership. |
| 191 virtual void SetOffTheRecordProfile(Profile* profile); | 217 virtual void SetOffTheRecordProfile(scoped_ptr<Profile> profile); |
| 192 virtual void SetOriginalProfile(Profile* profile); | 218 virtual void SetOriginalProfile(Profile* profile); |
| 193 virtual Profile* GetOffTheRecordProfile() OVERRIDE; | 219 virtual Profile* GetOffTheRecordProfile() OVERRIDE; |
| 194 virtual void DestroyOffTheRecordProfile() OVERRIDE {} | 220 virtual void DestroyOffTheRecordProfile() OVERRIDE {} |
| 195 virtual bool HasOffTheRecordProfile() OVERRIDE; | 221 virtual bool HasOffTheRecordProfile() OVERRIDE; |
| 196 virtual Profile* GetOriginalProfile() OVERRIDE; | 222 virtual Profile* GetOriginalProfile() OVERRIDE; |
| 197 virtual bool IsManaged() OVERRIDE; | 223 virtual bool IsManaged() OVERRIDE; |
| 198 virtual ExtensionService* GetExtensionService() OVERRIDE; | 224 virtual ExtensionService* GetExtensionService() OVERRIDE; |
| 199 void SetExtensionSpecialStoragePolicy( | 225 void SetExtensionSpecialStoragePolicy( |
| 200 ExtensionSpecialStoragePolicy* extension_special_storage_policy); | 226 ExtensionSpecialStoragePolicy* extension_special_storage_policy); |
| 201 virtual ExtensionSpecialStoragePolicy* | 227 virtual ExtensionSpecialStoragePolicy* |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 // maps to this profile. | 325 // maps to this profile. |
| 300 void CreateProfilePolicyConnector(); | 326 void CreateProfilePolicyConnector(); |
| 301 | 327 |
| 302 // Internally, this is a TestURLRequestContextGetter that creates a dummy | 328 // Internally, this is a TestURLRequestContextGetter that creates a dummy |
| 303 // request context. Currently, only the CookieMonster is hooked up. | 329 // request context. Currently, only the CookieMonster is hooked up. |
| 304 scoped_refptr<net::URLRequestContextGetter> extensions_request_context_; | 330 scoped_refptr<net::URLRequestContextGetter> extensions_request_context_; |
| 305 | 331 |
| 306 std::wstring id_; | 332 std::wstring id_; |
| 307 | 333 |
| 308 bool incognito_; | 334 bool incognito_; |
| 335 bool force_incognito_; |
| 309 scoped_ptr<Profile> incognito_profile_; | 336 scoped_ptr<Profile> incognito_profile_; |
| 310 Profile* original_profile_; | 337 Profile* original_profile_; |
| 311 | 338 |
| 312 // Did the last session exit cleanly? Default is true. | 339 // Did the last session exit cleanly? Default is true. |
| 313 bool last_session_exited_cleanly_; | 340 bool last_session_exited_cleanly_; |
| 314 | 341 |
| 315 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; | 342 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; |
| 316 | 343 |
| 317 base::FilePath last_selected_directory_; | 344 base::FilePath last_selected_directory_; |
| 318 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. | 345 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 340 // scoped_ptr<>. | 367 // scoped_ptr<>. |
| 341 content::MockResourceContext* resource_context_; | 368 content::MockResourceContext* resource_context_; |
| 342 | 369 |
| 343 scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_; | 370 scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_; |
| 344 | 371 |
| 345 // Weak pointer to a delegate for indicating that a profile was created. | 372 // Weak pointer to a delegate for indicating that a profile was created. |
| 346 Delegate* delegate_; | 373 Delegate* delegate_; |
| 347 }; | 374 }; |
| 348 | 375 |
| 349 #endif // CHROME_TEST_BASE_TESTING_PROFILE_H_ | 376 #endif // CHROME_TEST_BASE_TESTING_PROFILE_H_ |
| OLD | NEW |