Upstream version 6.35.121.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 ChromeURLRequestContext;
19 class ChromeURLRequestContextGetter;
20 class Profile;
21
22 namespace net {
23 class FtpTransactionFactory;
24 class HttpTransactionFactory;
25 }  // namespace net
26
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).
38
39 class OffTheRecordProfileIOData : public ProfileIOData {
40  public:
41   class Handle {
42    public:
43     explicit Handle(Profile* profile);
44     ~Handle();
45
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,
62             bool in_memory,
63             content::ProtocolHandlerMap* protocol_handlers,
64             content::ProtocolHandlerScopedVector protocol_interceptors) const;
65
66    private:
67     typedef std::map<StoragePartitionDescriptor,
68                      scoped_refptr<ChromeURLRequestContextGetter>,
69                      StoragePartitionDescriptorLess>
70       ChromeURLRequestContextGetterMap;
71
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;
78
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_;
92
93     Profile* const profile_;
94
95     mutable bool initialized_;
96
97     DISALLOW_COPY_AND_ASSIGN(Handle);
98   };
99
100  private:
101   friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
102
103   explicit OffTheRecordProfileIOData(Profile::ProfileType profile_type);
104   virtual ~OffTheRecordProfileIOData();
105
106   virtual void InitializeInternal(
107       ProfileParams* profile_params,
108       content::ProtocolHandlerMap* protocol_handlers,
109       content::ProtocolHandlerScopedVector protocol_interceptors)
110           const OVERRIDE;
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)
120           const OVERRIDE;
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)
133           const OVERRIDE;
134   virtual ChromeURLRequestContext*
135       AcquireIsolatedMediaRequestContext(
136           ChromeURLRequestContext* app_context,
137           const StoragePartitionDescriptor& partition_descriptor)
138               const OVERRIDE;
139
140   mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
141   mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
142
143   mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
144   mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
145
146   DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
147 };
148
149 #endif  // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_