blob: ad7d5496bcbb7207c8776738e5e1d93dae92cefc [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
#define CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_
#include "base/basictypes.h"
#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/browser/profiles/storage_partition_descriptor.h"
class ChromeURLRequestContextGetter;
class Profile;
namespace net {
class FtpTransactionFactory;
class HttpTransactionFactory;
class SdchManager;
class URLRequestContext;
} // namespace net
// OffTheRecordProfile owns a OffTheRecordProfileIOData::Handle, which holds a
// reference to the OffTheRecordProfileIOData. OffTheRecordProfileIOData is
// intended to own all the objects owned by OffTheRecordProfile which live on
// the IO thread, such as, but not limited to, network objects like
// CookieMonster, HttpTransactionFactory, etc. OffTheRecordProfileIOData is
// owned by the OffTheRecordProfile and OffTheRecordProfileIOData's
// ChromeURLRequestContexts. When all of them go away, then ProfileIOData will
// be deleted. Note that the OffTheRecordProfileIOData will typically outlive
// the Profile it is "owned" by, so it's important for OffTheRecordProfileIOData
// not to hold any references to the Profile beyond what's used by LazyParams
// (which should be deleted after lazy initialization).
class OffTheRecordProfileIOData : public ProfileIOData {
public:
class Handle {
public:
explicit Handle(Profile* profile);
~Handle();
content::ResourceContext* GetResourceContext() const;
// GetResourceContextNoInit() does not call LazyInitialize() so it can be
// safely be used during initialization.
content::ResourceContext* GetResourceContextNoInit() const;
scoped_refptr<ChromeURLRequestContextGetter> CreateMainRequestContextGetter(
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors) const;
scoped_refptr<ChromeURLRequestContextGetter>
GetExtensionsRequestContextGetter() const;
scoped_refptr<ChromeURLRequestContextGetter>
GetIsolatedAppRequestContextGetter(
const base::FilePath& partition_path,
bool in_memory) const;
scoped_refptr<ChromeURLRequestContextGetter>
CreateIsolatedAppRequestContextGetter(
const base::FilePath& partition_path,
bool in_memory,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector
request_interceptors) const;
// Returns the DevToolsNetworkController attached to ProfileIOData.
DevToolsNetworkController* GetDevToolsNetworkController() const;
private:
typedef std::map<StoragePartitionDescriptor,
scoped_refptr<ChromeURLRequestContextGetter>,
StoragePartitionDescriptorLess>
ChromeURLRequestContextGetterMap;
// Lazily initialize ProfileParams. We do this on the calls to
// Get*RequestContextGetter(), so we only initialize ProfileParams right
// before posting a task to the IO thread to start using them. This prevents
// objects that are supposed to be deleted on the IO thread, but are created
// on the UI thread from being unnecessarily initialized.
void LazyInitialize() const;
// Ordering is important here. Do not reorder unless you know what you're
// doing. We need to release |io_data_| *before* the getters, because we
// want to make sure that the last reference for |io_data_| is on the IO
// thread. The getters will be deleted on the IO thread, so they will
// release their refs to their contexts, which will release the last refs to
// the ProfileIOData on the IO thread.
mutable scoped_refptr<ChromeURLRequestContextGetter>
main_request_context_getter_;
mutable scoped_refptr<ChromeURLRequestContextGetter>
extensions_request_context_getter_;
mutable ChromeURLRequestContextGetterMap
app_request_context_getter_map_;
OffTheRecordProfileIOData* const io_data_;
Profile* const profile_;
mutable bool initialized_;
DISALLOW_COPY_AND_ASSIGN(Handle);
};
private:
friend class base::RefCountedThreadSafe<OffTheRecordProfileIOData>;
explicit OffTheRecordProfileIOData(Profile::ProfileType profile_type);
virtual ~OffTheRecordProfileIOData();
virtual void InitializeInternal(
ProfileParams* profile_params,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
const OVERRIDE;
virtual void InitializeExtensionsRequestContext(
ProfileParams* profile_params) const OVERRIDE;
virtual net::URLRequestContext* InitializeAppRequestContext(
net::URLRequestContext* main_context,
const StoragePartitionDescriptor& partition_descriptor,
scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
protocol_handler_interceptor,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
const OVERRIDE;
virtual net::URLRequestContext* InitializeMediaRequestContext(
net::URLRequestContext* original_context,
const StoragePartitionDescriptor& partition_descriptor) const OVERRIDE;
virtual net::URLRequestContext*
AcquireMediaRequestContext() const OVERRIDE;
virtual net::URLRequestContext* AcquireIsolatedAppRequestContext(
net::URLRequestContext* main_context,
const StoragePartitionDescriptor& partition_descriptor,
scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
protocol_handler_interceptor,
content::ProtocolHandlerMap* protocol_handlers,
content::URLRequestInterceptorScopedVector request_interceptors)
const OVERRIDE;
virtual net::URLRequestContext*
AcquireIsolatedMediaRequestContext(
net::URLRequestContext* app_context,
const StoragePartitionDescriptor& partition_descriptor)
const OVERRIDE;
mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
mutable scoped_ptr<net::SdchManager> sdch_manager_;
DISALLOW_COPY_AND_ASSIGN(OffTheRecordProfileIOData);
};
#endif // CHROME_BROWSER_PROFILES_OFF_THE_RECORD_PROFILE_IO_DATA_H_