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_NET_CHROME_URL_REQUEST_CONTEXT_H_
6 #define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
12 #include "net/url_request/url_request_context.h"
13 #include "net/url_request/url_request_context_getter.h"
14 #include "net/url_request/url_request_job_factory.h"
16 class ChromeURLRequestContextFactory;
20 struct StoragePartitionDescriptor;
22 namespace chrome_browser_net {
26 // Subclass of net::URLRequestContext which can be used to store extra
27 // information for requests.
29 // All methods of this class must be called from the IO thread,
30 // including the constructor and destructor.
31 class ChromeURLRequestContext : public net::URLRequestContext {
36 CONTEXT_TYPE_EXTENSIONS,
39 ChromeURLRequestContext(ContextType type,
40 chrome_browser_net::LoadTimeStats* load_time_stats);
41 virtual ~ChromeURLRequestContext();
43 base::WeakPtr<ChromeURLRequestContext> GetWeakPtr() {
44 return weak_factory_.GetWeakPtr();
47 // Copies the state from |other| into this context.
48 void CopyFrom(ChromeURLRequestContext* other);
51 base::WeakPtrFactory<ChromeURLRequestContext> weak_factory_;
53 // ---------------------------------------------------------------------------
54 // Important: When adding any new members below, consider whether they need to
55 // be added to CopyFrom.
56 // ---------------------------------------------------------------------------
58 chrome_browser_net::LoadTimeStats* load_time_stats_;
60 // ---------------------------------------------------------------------------
61 // Important: When adding any new members above, consider whether they need to
62 // be added to CopyFrom.
63 // ---------------------------------------------------------------------------
65 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContext);
68 // A net::URLRequestContextGetter subclass used by the browser. This returns a
69 // subclass of net::URLRequestContext which can be used to store extra
70 // information about requests.
72 // Most methods are expected to be called on the UI thread, except for
73 // the destructor and GetURLRequestContext().
74 class ChromeURLRequestContextGetter : public net::URLRequestContextGetter {
76 // Constructs a ChromeURLRequestContextGetter that will use |factory| to
77 // create the ChromeURLRequestContext.
78 explicit ChromeURLRequestContextGetter(
79 ChromeURLRequestContextFactory* factory);
81 // Note that GetURLRequestContext() can only be called from the IO
82 // thread (it will assert otherwise).
83 // GetIOMessageLoopProxy however can be called from any thread.
85 // net::URLRequestContextGetter implementation.
86 virtual ChromeURLRequestContext* GetURLRequestContext() OVERRIDE;
87 virtual scoped_refptr<base::SingleThreadTaskRunner>
88 GetNetworkTaskRunner() const OVERRIDE;
90 // Create an instance for use with an 'original' (non-OTR) profile. This is
91 // expected to get called on the UI thread.
92 static ChromeURLRequestContextGetter* Create(
94 const ProfileIOData* profile_io_data,
95 content::ProtocolHandlerMap* protocol_handlers);
97 // Create an instance for an original profile for media. This is expected to
98 // get called on UI thread. This method takes a profile and reuses the
99 // 'original' net::URLRequestContext for common files.
100 static ChromeURLRequestContextGetter* CreateForMedia(
101 Profile* profile, const ProfileIOData* profile_io_data);
103 // Create an instance for an original profile for extensions. This is expected
104 // to get called on UI thread.
105 static ChromeURLRequestContextGetter* CreateForExtensions(
106 Profile* profile, const ProfileIOData* profile_io_data);
108 // Create an instance for an original profile for an app with isolated
109 // storage. This is expected to get called on UI thread.
110 static ChromeURLRequestContextGetter* CreateForIsolatedApp(
112 const ProfileIOData* profile_io_data,
113 const StoragePartitionDescriptor& partition_descriptor,
114 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
115 protocol_handler_interceptor,
116 content::ProtocolHandlerMap* protocol_handlers);
118 // Create an instance for an original profile for media with isolated
119 // storage. This is expected to get called on UI thread.
120 static ChromeURLRequestContextGetter* CreateForIsolatedMedia(
122 ChromeURLRequestContextGetter* app_context,
123 const ProfileIOData* profile_io_data,
124 const StoragePartitionDescriptor& partition_descriptor);
127 virtual ~ChromeURLRequestContextGetter();
129 // Deferred logic for creating a ChromeURLRequestContext.
130 // Access only from the IO thread.
131 scoped_ptr<ChromeURLRequestContextFactory> factory_;
133 // NULL if not yet initialized. Otherwise, it is the ChromeURLRequestContext
134 // instance that was lazily created by GetURLRequestContext().
135 // Access only from the IO thread.
136 base::WeakPtr<ChromeURLRequestContext> url_request_context_;
138 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter);
141 #endif // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_