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 ChromeURLRequestContext;
19 class ChromeURLRequestContextGetter;
23 class FtpTransactionFactory;
24 class HttpTransactionFactory;
27 // OffTheRecordProfile owns a OffTheRecordProfileIOData::Handle, which holds a
28 // reference to the OffTheRecordProfileIOData. OffTheRecordProfileIOData is
29 // intended to own all the objects owned by OffTheRecordProfile which live on
30 // the IO thread, such as, but not limited to, network objects like
31 // CookieMonster, HttpTransactionFactory, etc. OffTheRecordProfileIOData is
32 // owned by the OffTheRecordProfile and OffTheRecordProfileIOData's
33 // ChromeURLRequestContexts. When all of them go away, then ProfileIOData will
34 // be deleted. Note that the OffTheRecordProfileIOData will typically outlive
35 // the Profile it is "owned" by, so it's important for OffTheRecordProfileIOData
36 // not to hold any references to the Profile beyond what's used by LazyParams
37 // (which should be deleted after lazy initialization).
39 class OffTheRecordProfileIOData : public ProfileIOData {
43 explicit Handle(Profile* profile);
46 content::ResourceContext* GetResourceContext() const;
47 // GetResourceContextNoInit() does not call LazyInitialize() so it can be
48 // safely be used during initialization.
49 content::ResourceContext* GetResourceContextNoInit() const;
50 scoped_refptr<ChromeURLRequestContextGetter> CreateMainRequestContextGetter(
51 content::ProtocolHandlerMap* protocol_handlers,
52 content::ProtocolHandlerScopedVector protocol_interceptors) const;
53 scoped_refptr<ChromeURLRequestContextGetter>
54 GetExtensionsRequestContextGetter() const;
55 scoped_refptr<ChromeURLRequestContextGetter>
56 GetIsolatedAppRequestContextGetter(
57 const base::FilePath& partition_path,
58 bool in_memory) const;
59 scoped_refptr<ChromeURLRequestContextGetter>
60 CreateIsolatedAppRequestContextGetter(
61 const base::FilePath& partition_path,
63 content::ProtocolHandlerMap* protocol_handlers,
64 content::ProtocolHandlerScopedVector protocol_interceptors) const;
67 typedef std::map<StoragePartitionDescriptor,
68 scoped_refptr<ChromeURLRequestContextGetter>,
69 StoragePartitionDescriptorLess>
70 ChromeURLRequestContextGetterMap;
72 // Lazily initialize ProfileParams. We do this on the calls to
73 // Get*RequestContextGetter(), so we only initialize ProfileParams right
74 // before posting a task to the IO thread to start using them. This prevents
75 // objects that are supposed to be deleted on the IO thread, but are created
76 // on the UI thread from being unnecessarily initialized.
77 void LazyInitialize() const;
79 // Ordering is important here. Do not reorder unless you know what you're
80 // doing. We need to release |io_data_| *before* the getters, because we
81 // want to make sure that the last reference for |io_data_| is on the IO
82 // thread. The getters will be deleted on the IO thread, so they will
83 // release their refs to their contexts, which will release the last refs to
84 // the ProfileIOData on the IO thread.
85 mutable scoped_refptr<ChromeURLRequestContextGetter>
86 main_request_context_getter_;
87 mutable scoped_refptr<ChromeURLRequestContextGetter>
88 extensions_request_context_getter_;
89 mutable ChromeURLRequestContextGetterMap
90 app_request_context_getter_map_;
91 OffTheRecordProfileIOData* const io_data_;
93 Profile* const profile_;
95 mutable bool initialized_;
97 DISALLOW_COPY_AND_ASSIGN(Handle);
101 friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
103 explicit OffTheRecordProfileIOData(Profile::ProfileType profile_type);
104 virtual ~OffTheRecordProfileIOData();
106 virtual void InitializeInternal(
107 ProfileParams* profile_params,
108 content::ProtocolHandlerMap* protocol_handlers,
109 content::ProtocolHandlerScopedVector protocol_interceptors)
111 virtual void InitializeExtensionsRequestContext(
112 ProfileParams* profile_params) const OVERRIDE;
113 virtual ChromeURLRequestContext* InitializeAppRequestContext(
114 ChromeURLRequestContext* main_context,
115 const StoragePartitionDescriptor& partition_descriptor,
116 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
117 protocol_handler_interceptor,
118 content::ProtocolHandlerMap* protocol_handlers,
119 content::ProtocolHandlerScopedVector protocol_interceptors)
121 virtual ChromeURLRequestContext* InitializeMediaRequestContext(
122 ChromeURLRequestContext* original_context,
123 const StoragePartitionDescriptor& partition_descriptor) const OVERRIDE;
124 virtual ChromeURLRequestContext*
125 AcquireMediaRequestContext() const OVERRIDE;
126 virtual ChromeURLRequestContext* AcquireIsolatedAppRequestContext(
127 ChromeURLRequestContext* main_context,
128 const StoragePartitionDescriptor& partition_descriptor,
129 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
130 protocol_handler_interceptor,
131 content::ProtocolHandlerMap* protocol_handlers,
132 content::ProtocolHandlerScopedVector protocol_interceptors)
134 virtual ChromeURLRequestContext*
135 AcquireIsolatedMediaRequestContext(
136 ChromeURLRequestContext* app_context,
137 const StoragePartitionDescriptor& partition_descriptor)
140 mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
141 mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
143 mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
144 mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
146 DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
149 #endif // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_