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_BROWSER_PROFILES_PROFILE_IO_DATA_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/synchronization/lock.h"
17 #include "chrome/browser/api/prefs/pref_member.h"
18 #include "chrome/browser/io_thread.h"
19 #include "chrome/browser/net/chrome_url_request_context.h"
20 #include "chrome/browser/profiles/storage_partition_descriptor.h"
21 #include "content/public/browser/resource_context.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/http/http_network_session.h"
24 #include "net/url_request/url_request_job_factory.h"
26 class ChromeHttpUserAgentSettings
;
28 class DesktopNotificationService
;
29 class ExtensionInfoMap
;
30 class HostContentSettingsMap
;
32 class ProtocolHandlerRegistry
;
33 class SigninNamesOnIOThread
;
34 class TransportSecurityPersister
;
36 namespace chrome_browser_net
{
38 class HttpServerPropertiesManager
;
39 class ResourcePrefetchPredictorObserver
;
44 class FraudulentCertificateReporter
;
45 class HttpTransactionFactory
;
46 class ServerBoundCertService
;
47 class ProxyConfigService
;
49 class SSLConfigService
;
50 class TransportSecurityState
;
51 class URLRequestJobFactoryImpl
;
55 class URLBlacklistManager
;
58 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
59 // thread that is owned by a Profile, such as, but not limited to, network
60 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
61 // ProfileIOData, but will make sure to delete it on the IO thread (except
62 // possibly in unit tests where there is no IO thread).
65 virtual ~ProfileIOData();
67 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
69 // Returns true if |scheme| is handled in Chrome, or by default handlers in
71 static bool IsHandledProtocol(const std::string
& scheme
);
73 // Returns true if |url| is handled in Chrome, or by default handlers in
75 static bool IsHandledURL(const GURL
& url
);
78 content::ResourceContext
* GetResourceContext() const;
79 ChromeURLDataManagerBackend
* GetChromeURLDataManagerBackend() const;
81 // These should only be called at most once each. Ownership is reversed when
82 // they get called, from ProfileIOData owning ChromeURLRequestContext to vice
84 ChromeURLRequestContext
* GetMainRequestContext() const;
85 ChromeURLRequestContext
* GetMediaRequestContext() const;
86 ChromeURLRequestContext
* GetExtensionsRequestContext() const;
87 ChromeURLRequestContext
* GetIsolatedAppRequestContext(
88 ChromeURLRequestContext
* main_context
,
89 const StoragePartitionDescriptor
& partition_descriptor
,
90 scoped_ptr
<net::URLRequestJobFactory::Interceptor
>
91 protocol_handler_interceptor
) const;
92 ChromeURLRequestContext
* GetIsolatedMediaRequestContext(
93 ChromeURLRequestContext
* app_context
,
94 const StoragePartitionDescriptor
& partition_descriptor
) const;
96 // These are useful when the Chrome layer is called from the content layer
97 // with a content::ResourceContext, and they want access to Chrome data for
99 ExtensionInfoMap
* GetExtensionInfoMap() const;
100 CookieSettings
* GetCookieSettings() const;
102 #if defined(ENABLE_NOTIFICATIONS)
103 DesktopNotificationService
* GetNotificationService() const;
106 IntegerPrefMember
* session_startup_pref() const {
107 return &session_startup_pref_
;
110 SigninNamesOnIOThread
* signin_names() const {
111 return signin_names_
.get();
114 StringPrefMember
* google_services_username() const {
115 return &google_services_username_
;
118 StringPrefMember
* google_services_username_pattern() const {
119 return &google_services_username_pattern_
;
122 BooleanPrefMember
* reverse_autologin_enabled() const {
123 return &reverse_autologin_enabled_
;
126 StringListPrefMember
* one_click_signin_rejected_email_list() const {
127 return &one_click_signin_rejected_email_list_
;
130 ChromeURLRequestContext
* extensions_request_context() const {
131 return extensions_request_context_
.get();
134 BooleanPrefMember
* safe_browsing_enabled() const {
135 return &safe_browsing_enabled_
;
138 BooleanPrefMember
* printing_enabled() const {
139 return &printing_enabled_
;
142 net::TransportSecurityState
* transport_security_state() const {
143 return transport_security_state_
.get();
146 chrome_browser_net::HttpServerPropertiesManager
*
147 http_server_properties_manager() const;
149 bool is_incognito() const {
150 return is_incognito_
;
153 chrome_browser_net::ResourcePrefetchPredictorObserver
*
154 resource_prefetch_predictor_observer() const {
155 return resource_prefetch_predictor_observer_
.get();
158 // Initialize the member needed to track the metrics enabled state. This is
159 // only to be called on the UI thread.
160 void InitializeMetricsEnabledStateOnUIThread();
162 // Returns whether or not metrics reporting is enabled in the browser instance
163 // on which this profile resides. This is safe for use from the IO thread, and
164 // should only be called from there.
165 bool GetMetricsEnabledStateOnIOThread() const;
168 // A URLRequestContext for media that owns its HTTP factory, to ensure
170 class MediaRequestContext
: public ChromeURLRequestContext
{
172 explicit MediaRequestContext(
173 chrome_browser_net::LoadTimeStats
* load_time_stats
);
175 void SetHttpTransactionFactory(
176 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
179 virtual ~MediaRequestContext();
181 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
184 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
185 // to ensure they are deleted.
186 class AppRequestContext
: public ChromeURLRequestContext
{
188 explicit AppRequestContext(
189 chrome_browser_net::LoadTimeStats
* load_time_stats
);
191 void SetCookieStore(net::CookieStore
* cookie_store
);
192 void SetHttpTransactionFactory(
193 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
194 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
197 virtual ~AppRequestContext();
199 scoped_refptr
<net::CookieStore
> cookie_store_
;
200 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
201 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
204 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
206 struct ProfileParams
{
212 scoped_refptr
<CookieSettings
> cookie_settings
;
213 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
214 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
215 scoped_refptr
<ExtensionInfoMap
> extension_info_map
;
216 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
217 resource_prefetch_predictor_observer_
;
219 #if defined(ENABLE_NOTIFICATIONS)
220 DesktopNotificationService
* notification_service
;
223 // This pointer exists only as a means of conveying a url interceptor
224 // pointer from the protocol handler registry on the UI thread to the
225 // the URLRequestJobFactory on the IO thread. The consumer MUST take
226 // ownership of the object by calling release() on this pointer.
227 scoped_ptr
<net::URLRequestJobFactory::Interceptor
>
228 protocol_handler_interceptor
;
230 // We need to initialize the ProxyConfigService from the UI thread
231 // because on linux it relies on initializing things through gconf,
232 // and needs to be on the main thread.
233 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
234 // The profile this struct was populated from. It's passed as a void* to
235 // ensure it's not accidently used on the IO thread. Before using it on the
236 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
240 explicit ProfileIOData(bool is_incognito
);
242 static std::string
GetSSLSessionCacheShard();
244 void InitializeOnUIThread(Profile
* profile
);
245 void ApplyProfileParamsToContext(ChromeURLRequestContext
* context
) const;
247 void SetUpJobFactoryDefaults(
248 net::URLRequestJobFactoryImpl
* job_factory
,
249 scoped_ptr
<net::URLRequestJobFactory::Interceptor
>
250 protocol_handler_interceptor
,
251 net::NetworkDelegate
* network_delegate
,
252 net::FtpTransactionFactory
* ftp_transaction_factory
,
253 net::FtpAuthCache
* ftp_auth_cache
) const;
255 // Lazy initializes the ProfileIOData object the first time a request context
256 // is requested. The lazy logic is implemented here. The actual initialization
257 // is done in LazyInitializeInternal(), implemented by subtypes. Static helper
258 // functions have been provided to assist in common operations.
259 void LazyInitialize() const;
261 // Called when the profile is destroyed.
262 void ShutdownOnUIThread();
264 ChromeURLDataManagerBackend
* chrome_url_data_manager_backend() const {
265 return chrome_url_data_manager_backend_
.get();
268 // A ServerBoundCertService object is created by a derived class of
269 // ProfileIOData, and the derived class calls this method to set the
270 // server_bound_cert_service_ member and transfers ownership to the base
272 void set_server_bound_cert_service(
273 net::ServerBoundCertService
* server_bound_cert_service
) const;
275 net::NetworkDelegate
* network_delegate() const {
276 return network_delegate_
.get();
279 net::FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
280 return fraudulent_certificate_reporter_
.get();
283 net::ProxyService
* proxy_service() const {
284 return proxy_service_
.get();
287 void set_http_server_properties_manager(
288 chrome_browser_net::HttpServerPropertiesManager
* manager
) const;
290 ChromeURLRequestContext
* main_request_context() const {
291 return main_request_context_
.get();
294 chrome_browser_net::LoadTimeStats
* load_time_stats() const {
295 return load_time_stats_
;
298 // Destroys the ResourceContext first, to cancel any URLRequests that are
299 // using it still, before we destroy the member variables that those
300 // URLRequests may be accessing.
301 void DestroyResourceContext();
303 // Fills in fields of params using values from main_request_context_ and the
304 // IOThread associated with profile_params.
305 void PopulateNetworkSessionParams(
306 const ProfileParams
* profile_params
,
307 net::HttpNetworkSession::Params
* params
) const;
309 void SetCookieSettingsForTesting(CookieSettings
* cookie_settings
);
311 void set_signin_names_for_testing(SigninNamesOnIOThread
* signin_names
);
314 class ResourceContext
: public content::ResourceContext
{
316 explicit ResourceContext(ProfileIOData
* io_data
);
317 virtual ~ResourceContext();
319 // ResourceContext implementation:
320 virtual net::HostResolver
* GetHostResolver() OVERRIDE
;
321 virtual net::URLRequestContext
* GetRequestContext() OVERRIDE
;
324 friend class ProfileIOData
;
326 void EnsureInitialized();
328 ProfileIOData
* const io_data_
;
330 net::HostResolver
* host_resolver_
;
331 net::URLRequestContext
* request_context_
;
334 typedef std::map
<StoragePartitionDescriptor
,
335 ChromeURLRequestContext
*,
336 StoragePartitionDescriptorLess
>
337 URLRequestContextMap
;
339 // --------------------------------------------
340 // Virtual interface for subtypes to implement:
341 // --------------------------------------------
343 // Does the actual initialization of the ProfileIOData subtype. Subtypes
344 // should use the static helper functions above to implement this.
345 virtual void LazyInitializeInternal(ProfileParams
* profile_params
) const = 0;
347 // Initializes the RequestContext for extensions.
348 virtual void InitializeExtensionsRequestContext(
349 ProfileParams
* profile_params
) const = 0;
350 // Does an on-demand initialization of a RequestContext for the given
352 virtual ChromeURLRequestContext
* InitializeAppRequestContext(
353 ChromeURLRequestContext
* main_context
,
354 const StoragePartitionDescriptor
& details
,
355 scoped_ptr
<net::URLRequestJobFactory::Interceptor
>
356 protocol_handler_interceptor
) const = 0;
358 // Does an on-demand initialization of a media RequestContext for the given
360 virtual ChromeURLRequestContext
* InitializeMediaRequestContext(
361 ChromeURLRequestContext
* original_context
,
362 const StoragePartitionDescriptor
& details
) const = 0;
364 // These functions are used to transfer ownership of the lazily initialized
365 // context from ProfileIOData to the URLRequestContextGetter.
366 virtual ChromeURLRequestContext
*
367 AcquireMediaRequestContext() const = 0;
368 virtual ChromeURLRequestContext
*
369 AcquireIsolatedAppRequestContext(
370 ChromeURLRequestContext
* main_context
,
371 const StoragePartitionDescriptor
& partition_descriptor
,
372 scoped_ptr
<net::URLRequestJobFactory::Interceptor
>
373 protocol_handler_interceptor
) const = 0;
374 virtual ChromeURLRequestContext
*
375 AcquireIsolatedMediaRequestContext(
376 ChromeURLRequestContext
* app_context
,
377 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
379 // Returns the LoadTimeStats object to be used for this profile.
380 virtual chrome_browser_net::LoadTimeStats
* GetLoadTimeStats(
381 IOThread::Globals
* io_thread_globals
) const = 0;
383 // The order *DOES* matter for the majority of these member variables, so
384 // don't move them around unless you know what you're doing!
386 // * ResourceContext references the URLRequestContexts, so
387 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
388 // should be destroyed first.
389 // * URLRequestContexts reference a whole bunch of members, so
390 // URLRequestContext needs to be destroyed before them.
391 // * Therefore, ResourceContext should be listed last, and then the
392 // URLRequestContexts, and then the URLRequestContext members.
393 // * Note that URLRequestContext members have a directed dependency graph
394 // too, so they must themselves be ordered correctly.
396 // Tracks whether or not we've been lazily initialized.
397 mutable bool initialized_
;
399 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
400 // Deleted after lazy initialization.
401 mutable scoped_ptr
<ProfileParams
> profile_params_
;
403 // Provides access to the email addresses of all signed in profiles.
404 mutable scoped_ptr
<SigninNamesOnIOThread
> signin_names_
;
406 mutable StringPrefMember google_services_username_
;
407 mutable StringPrefMember google_services_username_pattern_
;
408 mutable BooleanPrefMember reverse_autologin_enabled_
;
409 mutable StringListPrefMember one_click_signin_rejected_email_list_
;
411 // Member variables which are pointed to by the various context objects.
412 mutable BooleanPrefMember enable_referrers_
;
413 mutable BooleanPrefMember enable_do_not_track_
;
414 mutable BooleanPrefMember force_safesearch_
;
415 mutable BooleanPrefMember safe_browsing_enabled_
;
416 mutable BooleanPrefMember printing_enabled_
;
417 // TODO(marja): Remove session_startup_pref_ if no longer needed.
418 mutable IntegerPrefMember session_startup_pref_
;
420 // The state of metrics reporting in the browser that this profile runs on.
421 // Unfortunately, since ChromeOS has a separate representation of this state,
422 // we need to make one available based on the platform.
423 #if defined(OS_CHROMEOS)
424 bool enable_metrics_
;
426 BooleanPrefMember enable_metrics_
;
429 // Pointed to by NetworkDelegate.
430 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
432 // Pointed to by URLRequestContext.
433 mutable scoped_refptr
<ExtensionInfoMap
> extension_info_map_
;
434 mutable scoped_ptr
<ChromeURLDataManagerBackend
>
435 chrome_url_data_manager_backend_
;
436 mutable scoped_ptr
<net::ServerBoundCertService
> server_bound_cert_service_
;
437 mutable scoped_ptr
<net::NetworkDelegate
> network_delegate_
;
438 mutable scoped_ptr
<net::FraudulentCertificateReporter
>
439 fraudulent_certificate_reporter_
;
440 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
441 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
442 mutable scoped_ptr
<chrome_browser_net::HttpServerPropertiesManager
>
443 http_server_properties_manager_
;
445 #if defined(ENABLE_NOTIFICATIONS)
446 mutable DesktopNotificationService
* notification_service_
;
449 mutable scoped_ptr
<TransportSecurityPersister
>
450 transport_security_persister_
;
452 // These are only valid in between LazyInitialize() and their accessor being
454 mutable scoped_ptr
<ChromeURLRequestContext
> main_request_context_
;
455 mutable scoped_ptr
<ChromeURLRequestContext
> extensions_request_context_
;
456 // One URLRequestContext per isolated app for main and media requests.
457 mutable URLRequestContextMap app_request_context_map_
;
458 mutable URLRequestContextMap isolated_media_request_context_map_
;
460 mutable scoped_ptr
<ResourceContext
> resource_context_
;
462 mutable scoped_refptr
<CookieSettings
> cookie_settings_
;
464 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
465 resource_prefetch_predictor_observer_
;
467 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
468 chrome_http_user_agent_settings_
;
470 mutable chrome_browser_net::LoadTimeStats
* load_time_stats_
;
472 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
473 bool initialized_on_UI_thread_
;
477 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
480 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_