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_OFF_THE_RECORD_PROFILE_IO_DATA_H_
6 #define CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/containers/hash_tables.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
15 #include "chrome/browser/profiles/profile_io_data.h"
16 #include "chrome/browser/profiles/storage_partition_descriptor.h"
18 class ChromeURLRequestContextGetter;
22 class FtpTransactionFactory;
23 class HttpTransactionFactory;
25 class URLRequestContext;
28 // OffTheRecordProfile owns a OffTheRecordProfileIOData::Handle, which holds a
29 // reference to the OffTheRecordProfileIOData. OffTheRecordProfileIOData is
30 // intended to own all the objects owned by OffTheRecordProfile which live on
31 // the IO thread, such as, but not limited to, network objects like
32 // CookieMonster, HttpTransactionFactory, etc. OffTheRecordProfileIOData is
33 // owned by the OffTheRecordProfile and OffTheRecordProfileIOData's
34 // ChromeURLRequestContexts. When all of them go away, then ProfileIOData will
35 // be deleted. Note that the OffTheRecordProfileIOData will typically outlive
36 // the Profile it is "owned" by, so it's important for OffTheRecordProfileIOData
37 // not to hold any references to the Profile beyond what's used by LazyParams
38 // (which should be deleted after lazy initialization).
40 class OffTheRecordProfileIOData : public ProfileIOData {
44 explicit Handle(Profile* profile);
47 content::ResourceContext* GetResourceContext() const;
48 // GetResourceContextNoInit() does not call LazyInitialize() so it can be
49 // safely be used during initialization.
50 content::ResourceContext* GetResourceContextNoInit() const;
51 scoped_refptr<ChromeURLRequestContextGetter> CreateMainRequestContextGetter(
52 content::ProtocolHandlerMap* protocol_handlers,
53 content::URLRequestInterceptorScopedVector request_interceptors) const;
54 scoped_refptr<ChromeURLRequestContextGetter>
55 GetExtensionsRequestContextGetter() const;
56 scoped_refptr<ChromeURLRequestContextGetter>
57 GetIsolatedAppRequestContextGetter(
58 const base::FilePath& partition_path,
59 bool in_memory) const;
60 scoped_refptr<ChromeURLRequestContextGetter>
61 CreateIsolatedAppRequestContextGetter(
62 const base::FilePath& partition_path,
64 content::ProtocolHandlerMap* protocol_handlers,
65 content::URLRequestInterceptorScopedVector
66 request_interceptors) const;
68 // Returns the DevToolsNetworkController attached to ProfileIOData.
69 DevToolsNetworkController* GetDevToolsNetworkController() const;
72 typedef std::map<StoragePartitionDescriptor,
73 scoped_refptr<ChromeURLRequestContextGetter>,
74 StoragePartitionDescriptorLess>
75 ChromeURLRequestContextGetterMap;
77 // Lazily initialize ProfileParams. We do this on the calls to
78 // Get*RequestContextGetter(), so we only initialize ProfileParams right
79 // before posting a task to the IO thread to start using them. This prevents
80 // objects that are supposed to be deleted on the IO thread, but are created
81 // on the UI thread from being unnecessarily initialized.
82 void LazyInitialize() const;
84 // Ordering is important here. Do not reorder unless you know what you're
85 // doing. We need to release |io_data_| *before* the getters, because we
86 // want to make sure that the last reference for |io_data_| is on the IO
87 // thread. The getters will be deleted on the IO thread, so they will
88 // release their refs to their contexts, which will release the last refs to
89 // the ProfileIOData on the IO thread.
90 mutable scoped_refptr<ChromeURLRequestContextGetter>
91 main_request_context_getter_;
92 mutable scoped_refptr<ChromeURLRequestContextGetter>
93 extensions_request_context_getter_;
94 mutable ChromeURLRequestContextGetterMap
95 app_request_context_getter_map_;
96 OffTheRecordProfileIOData* const io_data_;
98 Profile* const profile_;
100 mutable bool initialized_;
102 DISALLOW_COPY_AND_ASSIGN(Handle);
106 friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
108 explicit OffTheRecordProfileIOData(Profile::ProfileType profile_type);
109 virtual ~OffTheRecordProfileIOData();
111 virtual void InitializeInternal(
112 ProfileParams* profile_params,
113 content::ProtocolHandlerMap* protocol_handlers,
114 content::URLRequestInterceptorScopedVector request_interceptors)
116 virtual void InitializeExtensionsRequestContext(
117 ProfileParams* profile_params) const OVERRIDE;
118 virtual net::URLRequestContext* InitializeAppRequestContext(
119 net::URLRequestContext* main_context,
120 const StoragePartitionDescriptor& partition_descriptor,
121 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
122 protocol_handler_interceptor,
123 content::ProtocolHandlerMap* protocol_handlers,
124 content::URLRequestInterceptorScopedVector request_interceptors)
126 virtual net::URLRequestContext* InitializeMediaRequestContext(
127 net::URLRequestContext* original_context,
128 const StoragePartitionDescriptor& partition_descriptor) const OVERRIDE;
129 virtual net::URLRequestContext*
130 AcquireMediaRequestContext() const OVERRIDE;
131 virtual net::URLRequestContext* AcquireIsolatedAppRequestContext(
132 net::URLRequestContext* main_context,
133 const StoragePartitionDescriptor& partition_descriptor,
134 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
135 protocol_handler_interceptor,
136 content::ProtocolHandlerMap* protocol_handlers,
137 content::URLRequestInterceptorScopedVector request_interceptors)
139 virtual net::URLRequestContext*
140 AcquireIsolatedMediaRequestContext(
141 net::URLRequestContext* app_context,
142 const StoragePartitionDescriptor& partition_descriptor)
145 mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
146 mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
148 mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
149 mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
151 mutable scoped_ptr<net::SdchManager> sdch_manager_;
153 DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
156 #endif // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_