OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/resource_context_impl.h" | 5 #include "content/browser/resource_context_impl.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "content/browser/appcache/chrome_appcache_service.h" | 8 #include "content/browser/appcache/chrome_appcache_service.h" |
8 #include "content/browser/fileapi/browser_file_system_helper.h" | 9 #include "content/browser/fileapi/browser_file_system_helper.h" |
9 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 10 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
10 #include "content/browser/host_zoom_map_impl.h" | 11 #include "content/browser/host_zoom_map_impl.h" |
11 #include "content/browser/in_process_webkit/indexed_db_context_impl.h" | 12 #include "content/browser/in_process_webkit/indexed_db_context_impl.h" |
| 13 #include "content/browser/net/view_blob_internals_job_factory.h" |
| 14 #include "content/browser/net/view_http_cache_job_factory.h" |
| 15 #include "content/browser/renderer_host/resource_request_info_impl.h" |
12 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/common/url_constants.h" |
| 19 #include "net/url_request/url_request.h" |
| 20 #include "net/url_request/url_request_context.h" |
| 21 #include "net/url_request/url_request_context_getter.h" |
| 22 #include "webkit/appcache/appcache_service.h" |
| 23 #include "webkit/appcache/view_appcache_internals_job.h" |
| 24 #include "webkit/blob/blob_data.h" |
| 25 #include "webkit/blob/blob_url_request_job_factory.h" |
14 #include "webkit/database/database_tracker.h" | 26 #include "webkit/database/database_tracker.h" |
| 27 #include "webkit/fileapi/file_system_url_request_job_factory.h" |
15 | 28 |
16 // Key names on ResourceContext. | 29 // Key names on ResourceContext. |
17 static const char* kAppCacheServicKeyName = "content_appcache_service_tracker"; | 30 static const char* kAppCacheServicKeyName = "content_appcache_service_tracker"; |
18 static const char* kBlobStorageContextKeyName = "content_blob_storage_context"; | 31 static const char* kBlobStorageContextKeyName = "content_blob_storage_context"; |
19 static const char* kDatabaseTrackerKeyName = "content_database_tracker"; | 32 static const char* kDatabaseTrackerKeyName = "content_database_tracker"; |
20 static const char* kFileSystemContextKeyName = "content_file_system_context"; | 33 static const char* kFileSystemContextKeyName = "content_file_system_context"; |
21 static const char* kIndexedDBContextKeyName = "content_indexed_db_context"; | 34 static const char* kIndexedDBContextKeyName = "content_indexed_db_context"; |
22 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; | 35 static const char* kHostZoomMapKeyName = "content_host_zoom_map"; |
23 | 36 |
24 using appcache::AppCacheService; | 37 using appcache::AppCacheService; |
25 using base::UserDataAdapter; | 38 using base::UserDataAdapter; |
26 using content::BrowserThread; | 39 using content::BrowserThread; |
27 using fileapi::FileSystemContext; | 40 using fileapi::FileSystemContext; |
28 using webkit_blob::BlobStorageController; | 41 using webkit_blob::BlobStorageController; |
29 using webkit_database::DatabaseTracker; | 42 using webkit_database::DatabaseTracker; |
30 | 43 |
31 namespace content { | 44 namespace content { |
32 | 45 |
| 46 namespace { |
| 47 |
33 class NonOwningZoomData : public base::SupportsUserData::Data { | 48 class NonOwningZoomData : public base::SupportsUserData::Data { |
34 public: | 49 public: |
35 explicit NonOwningZoomData(HostZoomMap* hzm) : host_zoom_map_(hzm) {} | 50 explicit NonOwningZoomData(HostZoomMap* hzm) : host_zoom_map_(hzm) {} |
36 HostZoomMap* host_zoom_map() { return host_zoom_map_; } | 51 HostZoomMap* host_zoom_map() { return host_zoom_map_; } |
37 | 52 |
38 private: | 53 private: |
39 HostZoomMap* host_zoom_map_; | 54 HostZoomMap* host_zoom_map_; |
40 }; | 55 }; |
41 | 56 |
| 57 class BlobProtocolHandler : public webkit_blob::BlobProtocolHandler { |
| 58 public: |
| 59 BlobProtocolHandler( |
| 60 webkit_blob::BlobStorageController* blob_storage_controller, |
| 61 base::MessageLoopProxy* loop_proxy) |
| 62 : webkit_blob::BlobProtocolHandler(blob_storage_controller, |
| 63 loop_proxy) {} |
| 64 |
| 65 virtual ~BlobProtocolHandler() {} |
| 66 |
| 67 private: |
| 68 virtual scoped_refptr<webkit_blob::BlobData> |
| 69 LookupBlobData(net::URLRequest* request) const { |
| 70 const ResourceRequestInfoImpl* info = |
| 71 ResourceRequestInfoImpl::ForRequest(request); |
| 72 if (!info) |
| 73 return NULL; |
| 74 return info->requested_blob_data(); |
| 75 } |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(BlobProtocolHandler); |
| 78 }; |
| 79 |
| 80 // Adds a bunch of debugging urls. We use an interceptor instead of a protocol |
| 81 // handler because we want to reuse the chrome://scheme (everyone is familiar |
| 82 // with it, and no need to expose the content/chrome separation through our UI). |
| 83 class DeveloperProtocolHandler |
| 84 : public net::URLRequestJobFactory::Interceptor { |
| 85 public: |
| 86 DeveloperProtocolHandler( |
| 87 AppCacheService* appcache_service, |
| 88 BlobStorageController* blob_storage_controller) |
| 89 : appcache_service_(appcache_service), |
| 90 blob_storage_controller_(blob_storage_controller) {} |
| 91 virtual ~DeveloperProtocolHandler() {} |
| 92 |
| 93 virtual net::URLRequestJob* MaybeIntercept( |
| 94 net::URLRequest* request) const OVERRIDE { |
| 95 // Check for chrome://view-http-cache/*, which uses its own job type. |
| 96 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url())) |
| 97 return ViewHttpCacheJobFactory::CreateJobForRequest(request); |
| 98 |
| 99 // Next check for chrome://appcache-internals/, which uses its own job type. |
| 100 if (request->url().SchemeIs(chrome::kChromeUIScheme) && |
| 101 request->url().host() == chrome::kChromeUIAppCacheInternalsHost) { |
| 102 return appcache::ViewAppCacheInternalsJobFactory::CreateJobForRequest( |
| 103 request, appcache_service_); |
| 104 } |
| 105 |
| 106 // Next check for chrome://blob-internals/, which uses its own job type. |
| 107 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url())) { |
| 108 return ViewBlobInternalsJobFactory::CreateJobForRequest( |
| 109 request, blob_storage_controller_); |
| 110 } |
| 111 return NULL; |
| 112 } |
| 113 |
| 114 virtual net::URLRequestJob* MaybeInterceptRedirect( |
| 115 const GURL& location, |
| 116 net::URLRequest* request) const OVERRIDE { |
| 117 return NULL; |
| 118 } |
| 119 |
| 120 virtual net::URLRequestJob* MaybeInterceptResponse( |
| 121 net::URLRequest* request) const OVERRIDE { |
| 122 return NULL; |
| 123 } |
| 124 |
| 125 private: |
| 126 AppCacheService* appcache_service_; |
| 127 BlobStorageController* blob_storage_controller_; |
| 128 }; |
| 129 |
| 130 void InitializeRequestContext( |
| 131 ResourceContext* resource_context, |
| 132 scoped_refptr<net::URLRequestContextGetter> context_getter) { |
| 133 if (!context_getter) |
| 134 return; // tests. |
| 135 net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
| 136 net::URLRequestJobFactory* job_factory = |
| 137 const_cast<net::URLRequestJobFactory*>(context->job_factory()); |
| 138 if (job_factory->IsHandledProtocol(chrome::kBlobScheme)) |
| 139 return; // Already initialized this RequestContext. |
| 140 |
| 141 bool set_protocol = job_factory->SetProtocolHandler( |
| 142 chrome::kBlobScheme, |
| 143 new BlobProtocolHandler( |
| 144 GetBlobStorageControllerForResourceContext(resource_context), |
| 145 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); |
| 146 DCHECK(set_protocol); |
| 147 set_protocol = job_factory->SetProtocolHandler( |
| 148 chrome::kFileSystemScheme, |
| 149 CreateFileSystemProtocolHandler( |
| 150 GetFileSystemContextForResourceContext(resource_context), |
| 151 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); |
| 152 DCHECK(set_protocol); |
| 153 |
| 154 job_factory->AddInterceptor(new DeveloperProtocolHandler( |
| 155 ResourceContext::GetAppCacheService(resource_context), |
| 156 GetBlobStorageControllerForResourceContext(resource_context))); |
| 157 |
| 158 // TODO(jam): Add the ProtocolHandlerRegistryIntercepter here! |
| 159 } |
| 160 |
| 161 } // namespace |
| 162 |
42 AppCacheService* ResourceContext::GetAppCacheService(ResourceContext* context) { | 163 AppCacheService* ResourceContext::GetAppCacheService(ResourceContext* context) { |
43 return UserDataAdapter<ChromeAppCacheService>::Get( | 164 return UserDataAdapter<ChromeAppCacheService>::Get( |
44 context, kAppCacheServicKeyName); | 165 context, kAppCacheServicKeyName); |
45 } | 166 } |
46 | 167 |
47 FileSystemContext* ResourceContext::GetFileSystemContext( | 168 BlobStorageController* GetBlobStorageControllerForResourceContext( |
48 ResourceContext* resource_context) { | |
49 return UserDataAdapter<FileSystemContext>::Get( | |
50 resource_context, kFileSystemContextKeyName); | |
51 } | |
52 | |
53 BlobStorageController* ResourceContext::GetBlobStorageController( | |
54 ResourceContext* resource_context) { | 169 ResourceContext* resource_context) { |
55 return GetChromeBlobStorageContextForResourceContext(resource_context)-> | 170 return GetChromeBlobStorageContextForResourceContext(resource_context)-> |
56 controller(); | 171 controller(); |
57 } | 172 } |
58 | 173 |
59 DatabaseTracker* GetDatabaseTrackerForResourceContext( | 174 DatabaseTracker* GetDatabaseTrackerForResourceContext( |
60 ResourceContext* resource_context) { | 175 ResourceContext* resource_context) { |
61 return UserDataAdapter<DatabaseTracker>::Get( | 176 return UserDataAdapter<DatabaseTracker>::Get( |
62 resource_context, kDatabaseTrackerKeyName); | 177 resource_context, kDatabaseTrackerKeyName); |
63 } | 178 } |
64 | 179 |
| 180 FileSystemContext* GetFileSystemContextForResourceContext( |
| 181 ResourceContext* resource_context) { |
| 182 return UserDataAdapter<FileSystemContext>::Get( |
| 183 resource_context, kFileSystemContextKeyName); |
| 184 } |
| 185 |
65 IndexedDBContextImpl* GetIndexedDBContextForResourceContext( | 186 IndexedDBContextImpl* GetIndexedDBContextForResourceContext( |
66 ResourceContext* resource_context) { | 187 ResourceContext* resource_context) { |
67 return UserDataAdapter<IndexedDBContextImpl>::Get( | 188 return UserDataAdapter<IndexedDBContextImpl>::Get( |
68 resource_context, kIndexedDBContextKeyName); | 189 resource_context, kIndexedDBContextKeyName); |
69 } | 190 } |
70 | 191 |
71 ChromeBlobStorageContext* GetChromeBlobStorageContextForResourceContext( | 192 ChromeBlobStorageContext* GetChromeBlobStorageContextForResourceContext( |
72 ResourceContext* resource_context) { | 193 ResourceContext* resource_context) { |
73 return UserDataAdapter<ChromeBlobStorageContext>::Get( | 194 return UserDataAdapter<ChromeBlobStorageContext>::Get( |
74 resource_context, kBlobStorageContextKeyName); | 195 resource_context, kBlobStorageContextKeyName); |
(...skipping 30 matching lines...) Expand all Loading... |
105 kBlobStorageContextKeyName, | 226 kBlobStorageContextKeyName, |
106 new UserDataAdapter<ChromeBlobStorageContext>( | 227 new UserDataAdapter<ChromeBlobStorageContext>( |
107 ChromeBlobStorageContext::GetFor(browser_context))); | 228 ChromeBlobStorageContext::GetFor(browser_context))); |
108 | 229 |
109 // This object is owned by the BrowserContext and not ResourceContext, so | 230 // This object is owned by the BrowserContext and not ResourceContext, so |
110 // store a non-owning pointer here. | 231 // store a non-owning pointer here. |
111 resource_context->SetUserData( | 232 resource_context->SetUserData( |
112 kHostZoomMapKeyName, | 233 kHostZoomMapKeyName, |
113 new NonOwningZoomData( | 234 new NonOwningZoomData( |
114 HostZoomMap::GetForBrowserContext(browser_context))); | 235 HostZoomMap::GetForBrowserContext(browser_context))); |
| 236 |
| 237 // Add content's URLRequestContext's hooks. |
| 238 // Check first to avoid memory leak in unittests. |
| 239 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 240 BrowserThread::PostTask( |
| 241 BrowserThread::IO, FROM_HERE, |
| 242 base::Bind(&InitializeRequestContext, |
| 243 resource_context, |
| 244 make_scoped_refptr(browser_context->GetRequestContext()))); |
| 245 BrowserThread::PostTask( |
| 246 BrowserThread::IO, FROM_HERE, |
| 247 base::Bind(&InitializeRequestContext, |
| 248 resource_context, |
| 249 make_scoped_refptr( |
| 250 browser_context->GetRequestContextForMedia()))); |
| 251 } |
115 } | 252 } |
116 | 253 |
117 } // namespace content | 254 } // namespace content |
OLD | NEW |