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
18class ChromeURLRequestContextGetter;
19class Profile;
20
21namespace net {
22class FtpTransactionFactory;
23class HttpTransactionFactory;
24class SdchManager;
25class 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
40class 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    // Collect references to context getters in reverse order, i.e. last item
85    // will be main request getter. This list is passed to |io_data_|
86    // for invalidation on IO thread.
87    scoped_ptr<ChromeURLRequestContextGetterVector> GetAllContextGetters();
88
89    // The getters will be invalidated on the IO thread before
90    // ProfileIOData instance is deleted.
91    mutable scoped_refptr<ChromeURLRequestContextGetter>
92        main_request_context_getter_;
93    mutable scoped_refptr<ChromeURLRequestContextGetter>
94        extensions_request_context_getter_;
95    mutable ChromeURLRequestContextGetterMap
96        app_request_context_getter_map_;
97    OffTheRecordProfileIOData* const io_data_;
98
99    Profile* const profile_;
100
101    mutable bool initialized_;
102
103    DISALLOW_COPY_AND_ASSIGN(Handle);
104  };
105
106 private:
107  friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
108
109  explicit OffTheRecordProfileIOData(Profile::ProfileType profile_type);
110  virtual ~OffTheRecordProfileIOData();
111
112  virtual void InitializeInternal(
113      ProfileParams* profile_params,
114      content::ProtocolHandlerMap* protocol_handlers,
115      content::URLRequestInterceptorScopedVector request_interceptors)
116          const OVERRIDE;
117  virtual void InitializeExtensionsRequestContext(
118      ProfileParams* profile_params) const OVERRIDE;
119  virtual net::URLRequestContext* InitializeAppRequestContext(
120      net::URLRequestContext* main_context,
121      const StoragePartitionDescriptor& partition_descriptor,
122      scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
123          protocol_handler_interceptor,
124      content::ProtocolHandlerMap* protocol_handlers,
125      content::URLRequestInterceptorScopedVector request_interceptors)
126          const OVERRIDE;
127  virtual net::URLRequestContext* InitializeMediaRequestContext(
128      net::URLRequestContext* original_context,
129      const StoragePartitionDescriptor& partition_descriptor) const OVERRIDE;
130  virtual net::URLRequestContext*
131      AcquireMediaRequestContext() const OVERRIDE;
132  virtual net::URLRequestContext* AcquireIsolatedAppRequestContext(
133      net::URLRequestContext* main_context,
134      const StoragePartitionDescriptor& partition_descriptor,
135      scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
136          protocol_handler_interceptor,
137      content::ProtocolHandlerMap* protocol_handlers,
138      content::URLRequestInterceptorScopedVector request_interceptors)
139          const OVERRIDE;
140  virtual net::URLRequestContext*
141      AcquireIsolatedMediaRequestContext(
142          net::URLRequestContext* app_context,
143          const StoragePartitionDescriptor& partition_descriptor)
144              const OVERRIDE;
145
146  mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
147  mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
148
149  mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
150  mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
151
152  mutable scoped_ptr<net::SdchManager> sdch_manager_;
153
154  DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
155};
156
157#endif  // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
158