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_NET_CHROME_URL_REQUEST_CONTEXT_H_ 6 #define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_ 7 8 #include <string> 9 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" 15 16 class ChromeURLRequestContextFactory; 17 class IOThread; 18 class Profile; 19 class ProfileIOData; 20 struct StoragePartitionDescriptor; 21 22 namespace chrome_browser_net { 23 class LoadTimeStats; 24 } 25 26 // Subclass of net::URLRequestContext which can be used to store extra 27 // information for requests. 28 // 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 { 32 public: 33 enum ContextType { 34 CONTEXT_TYPE_MAIN, 35 CONTEXT_TYPE_MEDIA, 36 CONTEXT_TYPE_EXTENSIONS, 37 CONTEXT_TYPE_APP 38 }; 39 ChromeURLRequestContext(ContextType type, 40 chrome_browser_net::LoadTimeStats* load_time_stats); 41 virtual ~ChromeURLRequestContext(); 42 43 base::WeakPtr<ChromeURLRequestContext> GetWeakPtr() { 44 return weak_factory_.GetWeakPtr(); 45 } 46 47 // Copies the state from |other| into this context. 48 void CopyFrom(ChromeURLRequestContext* other); 49 50 private: 51 base::WeakPtrFactory<ChromeURLRequestContext> weak_factory_; 52 53 // --------------------------------------------------------------------------- 54 // Important: When adding any new members below, consider whether they need to 55 // be added to CopyFrom. 56 // --------------------------------------------------------------------------- 57 58 chrome_browser_net::LoadTimeStats* load_time_stats_; 59 60 // --------------------------------------------------------------------------- 61 // Important: When adding any new members above, consider whether they need to 62 // be added to CopyFrom. 63 // --------------------------------------------------------------------------- 64 65 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContext); 66 }; 67 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. 71 // 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 { 75 public: 76 // Constructs a ChromeURLRequestContextGetter that will use |factory| to 77 // create the ChromeURLRequestContext. 78 explicit ChromeURLRequestContextGetter( 79 ChromeURLRequestContextFactory* factory); 80 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. 84 // 85 // net::URLRequestContextGetter implementation. 86 virtual ChromeURLRequestContext* GetURLRequestContext() OVERRIDE; 87 virtual scoped_refptr<base::SingleThreadTaskRunner> 88 GetNetworkTaskRunner() const OVERRIDE; 89 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( 93 Profile* profile, 94 const ProfileIOData* profile_io_data, 95 content::ProtocolHandlerMap* protocol_handlers); 96 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); 102 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); 107 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( 111 Profile* profile, 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); 117 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( 121 Profile* profile, 122 ChromeURLRequestContextGetter* app_context, 123 const ProfileIOData* profile_io_data, 124 const StoragePartitionDescriptor& partition_descriptor); 125 126 private: 127 virtual ~ChromeURLRequestContextGetter(); 128 129 // Deferred logic for creating a ChromeURLRequestContext. 130 // Access only from the IO thread. 131 scoped_ptr<ChromeURLRequestContextFactory> factory_; 132 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_; 137 138 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter); 139 }; 140 141 #endif // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_ 142