Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / off_the_record_profile_io_data.h
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_OFF_THE_RECORD_PROFILE_IO_DATA_H_
6 #define CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
7
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"
17
18 class ChromeURLRequestContextGetter;
19 class Profile;
20
21 namespace net {
22 class FtpTransactionFactory;
23 class HttpTransactionFactory;
24 class SdchManager;
25 class URLRequestContext;
26 }  // namespace net
27
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).
39
40 class OffTheRecordProfileIOData : public ProfileIOData {
41  public:
42   class Handle {
43    public:
44     explicit Handle(Profile* profile);
45     ~Handle();
46
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,
63             bool in_memory,
64             content::ProtocolHandlerMap* protocol_handlers,
65             content::URLRequestInterceptorScopedVector
66                 request_interceptors) const;
67
68     // Returns the DevToolsNetworkController attached to ProfileIOData.
69     DevToolsNetworkController* GetDevToolsNetworkController() const;
70
71    private:
72     typedef std::map<StoragePartitionDescriptor,
73                      scoped_refptr<ChromeURLRequestContextGetter>,
74                      StoragePartitionDescriptorLess>
75       ChromeURLRequestContextGetterMap;
76
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;
83
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_;
97
98     Profile* const profile_;
99
100     mutable bool initialized_;
101
102     DISALLOW_COPY_AND_ASSIGN(Handle);
103   };
104
105  private:
106   friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
107
108   explicit OffTheRecordProfileIOData(Profile::ProfileType profile_type);
109   virtual ~OffTheRecordProfileIOData();
110
111   virtual void InitializeInternal(
112       ProfileParams* profile_params,
113       content::ProtocolHandlerMap* protocol_handlers,
114       content::URLRequestInterceptorScopedVector request_interceptors)
115           const OVERRIDE;
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)
125           const OVERRIDE;
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)
138           const OVERRIDE;
139   virtual net::URLRequestContext*
140       AcquireIsolatedMediaRequestContext(
141           net::URLRequestContext* app_context,
142           const StoragePartitionDescriptor& partition_descriptor)
143               const OVERRIDE;
144
145   mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
146   mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
147
148   mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
149   mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
150
151   mutable scoped_ptr<net::SdchManager> sdch_manager_;
152
153   DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
154 };
155
156 #endif  // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_