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. 4 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_ 6 #define CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_ 7 8 #include "base/basictypes.h" 9 #include "base/callback.h" 10 #include "base/containers/hash_tables.h" 11 #include "base/memory/ref_counted.h" 12 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" 13 #include "chrome/browser/profiles/profile_io_data.h" 14 15 namespace chrome_browser_net { 16 class HttpServerPropertiesManager; 17 class Predictor; 18 } // namespace chrome_browser_net 19 20 namespace net { 21 class FtpTransactionFactory; 22 class HttpServerProperties; 23 class HttpTransactionFactory; 24 } // namespace net 25 26 namespace quota { 27 class SpecialStoragePolicy; 28 } // namespace quota 29 30 class ProfileImplIOData : public ProfileIOData { 31 public: 32 class Handle { 33 public: 34 explicit Handle(Profile* profile); 35 ~Handle(); 36 37 // Init() must be called before ~Handle(). It records most of the 38 // parameters needed to construct a ChromeURLRequestContextGetter. 39 void Init(const base::FilePath& cookie_path, 40 const base::FilePath& server_bound_cert_path, 41 const base::FilePath& cache_path, 42 int cache_max_size, 43 const base::FilePath& media_cache_path, 44 int media_cache_max_size, 45 const base::FilePath& extensions_cookie_path, 46 const base::FilePath& profile_path, 47 const base::FilePath& infinite_cache_path, 48 chrome_browser_net::Predictor* predictor, 49 bool restore_old_session_cookies, 50 quota::SpecialStoragePolicy* special_storage_policy); 51 52 // These Create*ContextGetter() functions are only exposed because the 53 // circular relationship between Profile, ProfileIOData::Handle, and the 54 // ChromeURLRequestContextGetter factories requires Profile be able to call 55 // these functions. 56 scoped_refptr<ChromeURLRequestContextGetter> 57 CreateMainRequestContextGetter( 58 content::ProtocolHandlerMap* protocol_handlers, 59 PrefService* local_state, 60 IOThread* io_thread) const; 61 scoped_refptr<ChromeURLRequestContextGetter> 62 CreateIsolatedAppRequestContextGetter( 63 const base::FilePath& partition_path, 64 bool in_memory, 65 content::ProtocolHandlerMap* protocol_handlers) const; 66 67 content::ResourceContext* GetResourceContext() const; 68 // GetResourceContextNoInit() does not call LazyInitialize() so it can be 69 // safely be used during initialization. 70 content::ResourceContext* GetResourceContextNoInit() const; 71 scoped_refptr<ChromeURLRequestContextGetter> 72 GetMediaRequestContextGetter() const; 73 scoped_refptr<ChromeURLRequestContextGetter> 74 GetExtensionsRequestContextGetter() const; 75 scoped_refptr<ChromeURLRequestContextGetter> 76 GetIsolatedMediaRequestContextGetter( 77 const base::FilePath& partition_path, 78 bool in_memory) const; 79 80 // Deletes all network related data since |time|. It deletes transport 81 // security state since |time| and also deletes HttpServerProperties data. 82 // Works asynchronously, however if the |completion| callback is non-null, 83 // it will be posted on the UI thread once the removal process completes. 84 void ClearNetworkingHistorySince(base::Time time, 85 const base::Closure& completion); 86 87 private: 88 typedef std::map<StoragePartitionDescriptor, 89 scoped_refptr<ChromeURLRequestContextGetter>, 90 StoragePartitionDescriptorLess> 91 ChromeURLRequestContextGetterMap; 92 93 // Lazily initialize ProfileParams. We do this on the calls to 94 // Get*RequestContextGetter(), so we only initialize ProfileParams right 95 // before posting a task to the IO thread to start using them. This prevents 96 // objects that are supposed to be deleted on the IO thread, but are created 97 // on the UI thread from being unnecessarily initialized. 98 void LazyInitialize() const; 99 100 // Ordering is important here. Do not reorder unless you know what you're 101 // doing. We need to release |io_data_| *before* the getters, because we 102 // want to make sure that the last reference for |io_data_| is on the IO 103 // thread. The getters will be deleted on the IO thread, so they will 104 // release their refs to their contexts, which will release the last refs to 105 // the ProfileIOData on the IO thread. 106 mutable scoped_refptr<ChromeURLRequestContextGetter> 107 main_request_context_getter_; 108 mutable scoped_refptr<ChromeURLRequestContextGetter> 109 media_request_context_getter_; 110 mutable scoped_refptr<ChromeURLRequestContextGetter> 111 extensions_request_context_getter_; 112 mutable ChromeURLRequestContextGetterMap app_request_context_getter_map_; 113 mutable ChromeURLRequestContextGetterMap 114 isolated_media_request_context_getter_map_; 115 ProfileImplIOData* const io_data_; 116 117 Profile* const profile_; 118 119 mutable bool initialized_; 120 121 DISALLOW_COPY_AND_ASSIGN(Handle); 122 }; 123 124 private: 125 friend class base::RefCountedThreadSafe<ProfileImplIOData>; 126 127 struct LazyParams { 128 LazyParams(); 129 ~LazyParams(); 130 131 // All of these parameters are intended to be read on the IO thread. 132 base::FilePath cookie_path; 133 base::FilePath server_bound_cert_path; 134 base::FilePath cache_path; 135 int cache_max_size; 136 base::FilePath media_cache_path; 137 int media_cache_max_size; 138 base::FilePath extensions_cookie_path; 139 base::FilePath infinite_cache_path; 140 bool restore_old_session_cookies; 141 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy; 142 }; 143 144 typedef base::hash_map<std::string, net::HttpTransactionFactory* > 145 HttpTransactionFactoryMap; 146 147 ProfileImplIOData(); 148 virtual ~ProfileImplIOData(); 149 150 virtual void InitializeInternal( 151 ProfileParams* profile_params, 152 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE; 153 virtual void InitializeExtensionsRequestContext( 154 ProfileParams* profile_params) const OVERRIDE; 155 virtual ChromeURLRequestContext* InitializeAppRequestContext( 156 ChromeURLRequestContext* main_context, 157 const StoragePartitionDescriptor& partition_descriptor, 158 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 159 protocol_handler_interceptor, 160 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE; 161 virtual ChromeURLRequestContext* InitializeMediaRequestContext( 162 ChromeURLRequestContext* original_context, 163 const StoragePartitionDescriptor& partition_descriptor) const OVERRIDE; 164 virtual ChromeURLRequestContext* 165 AcquireMediaRequestContext() const OVERRIDE; 166 virtual ChromeURLRequestContext* 167 AcquireIsolatedAppRequestContext( 168 ChromeURLRequestContext* main_context, 169 const StoragePartitionDescriptor& partition_descriptor, 170 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 171 protocol_handler_interceptor, 172 content::ProtocolHandlerMap* protocol_handlers) const OVERRIDE; 173 virtual ChromeURLRequestContext* 174 AcquireIsolatedMediaRequestContext( 175 ChromeURLRequestContext* app_context, 176 const StoragePartitionDescriptor& partition_descriptor) 177 const OVERRIDE; 178 virtual chrome_browser_net::LoadTimeStats* GetLoadTimeStats( 179 IOThread::Globals* io_thread_globals) const OVERRIDE; 180 181 // Deletes all network related data since |time|. It deletes transport 182 // security state since |time| and also deletes HttpServerProperties data. 183 // Works asynchronously, however if the |completion| callback is non-null, 184 // it will be posted on the UI thread once the removal process completes. 185 void ClearNetworkingHistorySinceOnIOThread(base::Time time, 186 const base::Closure& completion); 187 188 // Lazy initialization params. 189 mutable scoped_ptr<LazyParams> lazy_params_; 190 191 mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_; 192 mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_; 193 194 // Same as |ProfileIOData::http_server_properties_|, owned there to maintain 195 // destruction ordering. 196 mutable chrome_browser_net::HttpServerPropertiesManager* 197 http_server_properties_manager_; 198 199 mutable scoped_ptr<chrome_browser_net::Predictor> predictor_; 200 201 mutable scoped_ptr<ChromeURLRequestContext> media_request_context_; 202 203 mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_; 204 mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_; 205 206 // Parameters needed for isolated apps. 207 base::FilePath profile_path_; 208 int app_cache_max_size_; 209 int app_media_cache_max_size_; 210 211 DISALLOW_COPY_AND_ASSIGN(ProfileImplIOData); 212 }; 213 214 #endif // CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_ 215