OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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. |
| 4 |
| 5 #ifndef IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ |
| 6 #define IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ |
| 7 |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "components/keyed_service/ios/browser_state_keyed_service_factory.h" |
| 13 #include "components/keyed_service/ios/refcounted_browser_state_keyed_service_fa
ctory.h" |
| 14 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 15 #include "ios/chrome/browser/net/net_types.h" |
| 16 |
| 17 namespace syncable_prefs { |
| 18 class PrefServiceSyncable; |
| 19 class TestingPrefServiceSyncable; |
| 20 } |
| 21 |
| 22 // This class is the implementation of ChromeBrowserState used for testing. |
| 23 class TestChromeBrowserState : public ios::ChromeBrowserState { |
| 24 public: |
| 25 typedef std::vector< |
| 26 std::pair<BrowserStateKeyedServiceFactory*, |
| 27 BrowserStateKeyedServiceFactory::TestingFactoryFunction>> |
| 28 TestingFactories; |
| 29 |
| 30 typedef std::vector<std::pair< |
| 31 RefcountedBrowserStateKeyedServiceFactory*, |
| 32 RefcountedBrowserStateKeyedServiceFactory::TestingFactoryFunction>> |
| 33 RefcountedTestingFactories; |
| 34 |
| 35 ~TestChromeBrowserState() override; |
| 36 |
| 37 // BrowserState: |
| 38 bool IsOffTheRecord() const override; |
| 39 base::FilePath GetStatePath() const override; |
| 40 |
| 41 // ChromeBrowserState: |
| 42 scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() override; |
| 43 ios::ChromeBrowserState* GetOriginalChromeBrowserState() override; |
| 44 bool HasOffTheRecordChromeBrowserState() const override; |
| 45 ios::ChromeBrowserState* GetOffTheRecordChromeBrowserState() override; |
| 46 PrefProxyConfigTracker* GetProxyConfigTracker() override; |
| 47 net::SSLConfigService* GetSSLConfigService() override; |
| 48 PrefService* GetPrefs() override; |
| 49 PrefService* GetOffTheRecordPrefs() override; |
| 50 ChromeBrowserStateIOData* GetIOData() override; |
| 51 void ClearNetworkingHistorySince(base::Time time, |
| 52 const base::Closure& completion) override; |
| 53 net::URLRequestContextGetter* CreateRequestContext( |
| 54 ProtocolHandlerMap* protocol_handlers, |
| 55 URLRequestInterceptorScopedVector request_interceptors) override; |
| 56 net::URLRequestContextGetter* CreateIsolatedRequestContext( |
| 57 const base::FilePath& partition_path) override; |
| 58 TestChromeBrowserState* AsTestChromeBrowserState() override; |
| 59 |
| 60 // This method is defined as empty following the paradigm of |
| 61 // TestingProfile::DestroyOffTheRecordProfile(). |
| 62 void DestroyOffTheRecordChromeBrowserState() override {} |
| 63 |
| 64 // Creates a WebDataService. If not invoked, the web data service is null. |
| 65 void CreateWebDataService(); |
| 66 |
| 67 // Creates the BookmkarBarModel. If not invoked the bookmark bar model is |
| 68 // NULL. If |delete_file| is true, the bookmarks file is deleted first, then |
| 69 // the model is created. As TestChromeBrowserState deletes the directory |
| 70 // containing the files used by HistoryService, the boolean only matters if |
| 71 // you're recreating the BookmarkModel. |
| 72 // |
| 73 // NOTE: this does not block until the bookmarks are loaded. |
| 74 // TODO(shreyasv): If needed, write a version that blocks. |
| 75 void CreateBookmarkModel(bool delete_file); |
| 76 |
| 77 // Creates the history service. If |delete_file| is true, the history file is |
| 78 // deleted first, then the HistoryService is created. As |
| 79 // TestChromeBrowserState deletes the directory containing the files used by |
| 80 // HistoryService, this only matters if you're recreating the HistoryService. |
| 81 bool CreateHistoryService(bool delete_file) WARN_UNUSED_RESULT; |
| 82 |
| 83 // Shuts down and nulls out the reference to HistoryService. |
| 84 void DestroyHistoryService(); |
| 85 |
| 86 // Returns the preferences as a TestingPrefServiceSyncable if possible or |
| 87 // null. Returns null for off-the-record TestChromeBrowserState and also |
| 88 // for TestChromeBrowserState initialized with a custom pref service. |
| 89 syncable_prefs::TestingPrefServiceSyncable* GetTestingPrefService(); |
| 90 |
| 91 // Helper class that allows for parameterizing the building |
| 92 // of TestChromeBrowserStates. |
| 93 class Builder { |
| 94 public: |
| 95 Builder(); |
| 96 ~Builder(); |
| 97 |
| 98 // Adds a testing factory to the TestChromeBrowserState. These testing |
| 99 // factories are installed before the ProfileKeyedServices are created. |
| 100 void AddTestingFactory( |
| 101 BrowserStateKeyedServiceFactory* service_factory, |
| 102 BrowserStateKeyedServiceFactory::TestingFactoryFunction cb); |
| 103 void AddTestingFactory( |
| 104 RefcountedBrowserStateKeyedServiceFactory* service_factory, |
| 105 RefcountedBrowserStateKeyedServiceFactory::TestingFactoryFunction cb); |
| 106 |
| 107 // Sets the path to the directory to be used to hold ChromeBrowserState |
| 108 // data. |
| 109 void SetPath(const base::FilePath& path); |
| 110 |
| 111 // Sets the PrefService to be used by the ChromeBrowserState. |
| 112 void SetPrefService(scoped_ptr<syncable_prefs::PrefServiceSyncable> prefs); |
| 113 |
| 114 // Creates the TestChromeBrowserState using previously-set settings. |
| 115 scoped_ptr<TestChromeBrowserState> Build(); |
| 116 |
| 117 private: |
| 118 // If true, Build() has been called. |
| 119 bool build_called_; |
| 120 |
| 121 // Various staging variables where values are held until Build() is invoked. |
| 122 base::FilePath state_path_; |
| 123 scoped_ptr<syncable_prefs::PrefServiceSyncable> pref_service_; |
| 124 |
| 125 TestingFactories testing_factories_; |
| 126 RefcountedTestingFactories refcounted_testing_factories_; |
| 127 |
| 128 DISALLOW_COPY_AND_ASSIGN(Builder); |
| 129 }; |
| 130 |
| 131 protected: |
| 132 // Used to create the principal TestChromeBrowserState. |
| 133 TestChromeBrowserState( |
| 134 const base::FilePath& path, |
| 135 scoped_ptr<syncable_prefs::PrefServiceSyncable> prefs, |
| 136 const TestingFactories& testing_factories, |
| 137 const RefcountedTestingFactories& refcounted_testing_factories); |
| 138 |
| 139 private: |
| 140 friend class Builder; |
| 141 |
| 142 // Used to create the incognito TestChromeBrowserState. |
| 143 explicit TestChromeBrowserState( |
| 144 TestChromeBrowserState* original_browser_state); |
| 145 |
| 146 // Initialization of the TestChromeBrowserState. This is a separate method |
| 147 // as it needs to be called after the bi-directional link between original |
| 148 // and off-the-record TestChromeBrowserState has been created. |
| 149 void Init(); |
| 150 |
| 151 // We use a temporary directory to store testing browser state data. |
| 152 // This must be declared before anything that may make use of the |
| 153 // directory so as to ensure files are closed before cleanup. |
| 154 base::ScopedTempDir temp_dir_; |
| 155 |
| 156 // The path to this browser state. |
| 157 base::FilePath state_path_; |
| 158 |
| 159 // If non-null, |testing_prefs_| points to |prefs_|. It is there to avoid |
| 160 // casting as |prefs_| may not be a TestingPrefServiceSyncable. |
| 161 scoped_ptr<syncable_prefs::PrefServiceSyncable> prefs_; |
| 162 syncable_prefs::TestingPrefServiceSyncable* testing_prefs_; |
| 163 |
| 164 // The incognito ChromeBrowserState instance that is associated with this |
| 165 // non-incognito ChromeBrowserState instance. |
| 166 scoped_ptr<TestChromeBrowserState> otr_browser_state_; |
| 167 TestChromeBrowserState* original_browser_state_; |
| 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(TestChromeBrowserState); |
| 170 }; |
| 171 |
| 172 #endif // IOS_CHROME_BROWSER_BROWSER_STATE_TEST_CHROME_BROWSER_STATE_H_ |
OLD | NEW |