blob: 34d8df45b025f574a189305d8742bd6aeeb0a5bc [file] [log] [blame]
[email protected]b0f146f2011-09-15 22:14:251// Copyright (c) 2011 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#include "content/shell/shell_browser_context.h"
6
7#include "base/file_util.h"
8#include "base/logging.h"
9#include "base/path_service.h"
10#include "base/string_split.h"
11#include "content/browser/appcache/chrome_appcache_service.h"
12#include "content/browser/browser_thread.h"
13#include "content/browser/chrome_blob_storage_context.h"
14#include "content/browser/download/download_manager.h"
15#include "content/browser/download/download_status_updater.h"
16#include "content/browser/download/mock_download_manager_delegate.h"
17#include "content/browser/file_system/browser_file_system_helper.h"
18#include "content/browser/geolocation/geolocation_permission_context.h"
19#include "content/browser/host_zoom_map.h"
20#include "content/browser/in_process_webkit/webkit_context.h"
21#include "content/browser/resource_context.h"
22#include "content/browser/ssl/ssl_host_state.h"
23#include "content/shell/shell_browser_main.h"
24#include "net/base/cert_verifier.h"
25#include "net/base/default_origin_bound_cert_store.h"
26#include "net/base/dnsrr_resolver.h"
27#include "net/base/host_resolver.h"
28#include "net/http/http_auth_handler_factory.h"
29#include "net/http/http_cache.h"
30#include "net/base/origin_bound_cert_service.h"
31#include "net/base/ssl_config_service_defaults.h"
32#include "net/proxy/proxy_service.h"
33#include "net/url_request/url_request_context.h"
34#include "net/url_request/url_request_context_getter.h"
35#include "net/url_request/url_request_job_factory.h"
36#include "webkit/database/database_tracker.h"
37#include "webkit/quota/quota_manager.h"
38
39#if defined(OS_WIN)
40#include "base/base_paths_win.h"
41#endif
42
43namespace {
44
45class ShellURLRequestContextGetter : public net::URLRequestContextGetter {
46 public:
47 ShellURLRequestContextGetter(
48 const FilePath& base_path_,
49 MessageLoop* io_loop,
50 MessageLoop* file_loop)
51 : io_loop_(io_loop),
52 file_loop_(file_loop) {
53 }
54
55 // net::URLRequestContextGetter implementation.
56 virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
58
59 if (!url_request_context_) {
60 FilePath cache_path = base_path_.Append(FILE_PATH_LITERAL("Cache"));
61 net::HttpCache::DefaultBackend* main_backend =
62 new net::HttpCache::DefaultBackend(
63 net::DISK_CACHE,
64 cache_path,
65 0,
66 BrowserThread::GetMessageLoopProxyForThread(
67 BrowserThread::CACHE));
68
69 net::NetLog* net_log = NULL;
70 host_resolver_.reset(net::CreateSystemHostResolver(
71 net::HostResolver::kDefaultParallelism,
72 net::HostResolver::kDefaultRetryAttempts,
73 net_log));
74
75 cert_verifier_.reset(new net::CertVerifier());
76
77 origin_bound_cert_service_.reset(new net::OriginBoundCertService(
78 new net::DefaultOriginBoundCertStore(NULL)));
79
80 dnsrr_resolver_.reset(new net::DnsRRResolver());
81
82 proxy_config_service_.reset(
83 net::ProxyService::CreateSystemProxyConfigService(
84 io_loop_,
85 file_loop_));
86
87 // TODO(jam): use v8 if possible, look at chrome code.
88 proxy_service_.reset(
89 net::ProxyService::CreateUsingSystemProxyResolver(
90 proxy_config_service_.get(),
91 0,
92 net_log));
93
94 url_security_manager_.reset(net::URLSecurityManager::Create(NULL, NULL));
95
96 std::vector<std::string> supported_schemes;
97 base::SplitString("basic,digest,ntlm,negotiate", ',', &supported_schemes);
98 http_auth_handler_factory_.reset(
99 net::HttpAuthHandlerRegistryFactory::Create(
100 supported_schemes,
101 url_security_manager_.get(),
102 host_resolver_.get(),
103 std::string(), // gssapi_library_name
104 false, // negotiate_disable_cname_lookup
105 false)); // negotiate_enable_port
106
107 net::HttpCache* main_cache = new net::HttpCache(
108 host_resolver_.get(),
109 cert_verifier_.get(),
110 origin_bound_cert_service_.get(),
111 dnsrr_resolver_.get(),
112 NULL, //dns_cert_checker
113 proxy_service_.get(),
114 new net::SSLConfigServiceDefaults(),
115 http_auth_handler_factory_.get(),
116 NULL, // network_delegate
117 net_log,
118 main_backend);
119 main_http_factory_.reset(main_cache);
120
121 url_request_context_ = new net::URLRequestContext();
122 job_factory_.reset(new net::URLRequestJobFactory);
123 url_request_context_->set_job_factory(job_factory_.get());
124 url_request_context_->set_http_transaction_factory(main_cache);
125 url_request_context_->set_origin_bound_cert_service(
126 origin_bound_cert_service_.get());
127 url_request_context_->set_dnsrr_resolver(dnsrr_resolver_.get());
128 url_request_context_->set_proxy_service(proxy_service_.get());
129 }
130
131 return url_request_context_;
132 }
133
134 virtual net::CookieStore* DONTUSEME_GetCookieStore() OVERRIDE {
135 if (BrowserThread::CurrentlyOn(BrowserThread::IO))
136 return GetURLRequestContext()->cookie_store();
137 NOTIMPLEMENTED();
138 return NULL;
139 }
140
141 virtual scoped_refptr<base::MessageLoopProxy>
142 GetIOMessageLoopProxy() const OVERRIDE {
143 return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
144 }
145
146 net::HostResolver* host_resolver() { return host_resolver_.get(); }
147
148 private:
149 FilePath base_path_;
150 MessageLoop* io_loop_;
151 MessageLoop* file_loop_;
152
153 scoped_ptr<net::URLRequestJobFactory> job_factory_;
154 scoped_refptr<net::URLRequestContext> url_request_context_;
155
156 scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
157 scoped_ptr<net::HostResolver> host_resolver_;
158 scoped_ptr<net::CertVerifier> cert_verifier_;
159 scoped_ptr<net::OriginBoundCertService> origin_bound_cert_service_;
160 scoped_ptr<net::DnsRRResolver> dnsrr_resolver_;
161 scoped_ptr<net::ProxyConfigService> proxy_config_service_;
162 scoped_ptr<net::ProxyService> proxy_service_;
163 scoped_ptr<net::HttpAuthHandlerFactory> http_auth_handler_factory_;
164 scoped_ptr<net::URLSecurityManager> url_security_manager_;
165};
166
167class ShellResourceContext : public content::ResourceContext {
168 public:
169 ShellResourceContext(ShellURLRequestContextGetter* getter)
170 : getter_(getter) {
171 }
172
173 private:
174 virtual void EnsureInitialized() const OVERRIDE {
175 const_cast<ShellResourceContext*>(this)->InitializeInternal();
176 }
177
178 void InitializeInternal() {
179 set_request_context(getter_->GetURLRequestContext());
180 set_host_resolver(getter_->host_resolver());
181 }
182
183 scoped_refptr<ShellURLRequestContextGetter> getter_;
184};
185
186class ShellGeolocationPermissionContext : public GeolocationPermissionContext {
187 public:
188 ShellGeolocationPermissionContext() {
189 }
190
191 // GeolocationPermissionContext implementation).
192 virtual void RequestGeolocationPermission(
193 int render_process_id,
194 int render_view_id,
195 int bridge_id,
196 const GURL& requesting_frame) OVERRIDE {
197 NOTIMPLEMENTED();
198 }
199
200 virtual void CancelGeolocationPermissionRequest(
201 int render_process_id,
202 int render_view_id,
203 int bridge_id,
204 const GURL& requesting_frame) OVERRIDE {
205 NOTIMPLEMENTED();
206 }
207
208 private:
209 DISALLOW_COPY_AND_ASSIGN(ShellGeolocationPermissionContext);
210};
211
212} // namespace
213
214namespace content {
215
216ShellBrowserContext::ShellBrowserContext(
217 ShellBrowserMainParts* shell_main_parts)
218 : shell_main_parts_(shell_main_parts) {
219}
220
221ShellBrowserContext::~ShellBrowserContext() {
222}
223
224FilePath ShellBrowserContext::GetPath() {
225 FilePath result;
226
227#if defined(OS_WIN)
228 CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA, &result));
229 result.Append(std::wstring(L"content_shell"));
230#else
231 NOTIMPLEMENTED();
232#endif
233
234 if (!file_util::PathExists(result))
235 file_util::CreateDirectory(result);
236
237 return result;
238}
239
240bool ShellBrowserContext::IsOffTheRecord() {
241 return false;
242}
243
244SSLHostState* ShellBrowserContext::GetSSLHostState() {
245 if (!ssl_host_state_.get())
246 ssl_host_state_.reset(new SSLHostState());
247 return ssl_host_state_.get();
248}
249
250DownloadManager* ShellBrowserContext::GetDownloadManager() {
251 if (!download_manager_.get()) {
252 download_status_updater_.reset(new DownloadStatusUpdater());
253
254 download_manager_delegate_.reset(new MockDownloadManagerDelegate());
255 download_manager_ = new DownloadManager(download_manager_delegate_.get(),
256 download_status_updater_.get());
257 download_manager_->Init(this);
258 }
259 return download_manager_.get();
260}
261
262bool ShellBrowserContext::HasCreatedDownloadManager() const {
263 return download_manager_.get() != NULL;
264}
265
266net::URLRequestContextGetter* ShellBrowserContext::GetRequestContext() {
267 if (!url_request_getter_) {
268 url_request_getter_ = new ShellURLRequestContextGetter(
269 GetPath(),
270 shell_main_parts_->io_thread()->message_loop(),
271 shell_main_parts_->file_thread()->message_loop());
272 }
273 return url_request_getter_;
274}
275
276net::URLRequestContextGetter*
277 ShellBrowserContext::GetRequestContextForRenderProcess(
278 int renderer_child_id) {
279 return GetRequestContext();
280}
281
282net::URLRequestContextGetter*
283 ShellBrowserContext::GetRequestContextForMedia() {
284 return GetRequestContext();
285}
286
287const ResourceContext& ShellBrowserContext::GetResourceContext() {
288 if (!resource_context_.get()) {
289 resource_context_.reset(new ShellResourceContext(
290 static_cast<ShellURLRequestContextGetter*>(GetRequestContext())));
291 }
292 return *resource_context_.get();
293}
294
295HostZoomMap* ShellBrowserContext::GetHostZoomMap() {
296 if (!host_zoom_map_)
297 host_zoom_map_ = new HostZoomMap();
298 return host_zoom_map_.get();
299}
300
301GeolocationPermissionContext*
302 ShellBrowserContext::GetGeolocationPermissionContext() {
303 if (!geolocation_permission_context_) {
304 geolocation_permission_context_ =
305 new ShellGeolocationPermissionContext();
306 }
307 return geolocation_permission_context_;
308}
309
310bool ShellBrowserContext::DidLastSessionExitCleanly() {
311 return true;
312}
313
314quota::QuotaManager* ShellBrowserContext::GetQuotaManager() {
315 CreateQuotaManagerAndClients();
316 return quota_manager_.get();
317}
318
319WebKitContext* ShellBrowserContext::GetWebKitContext() {
320 CreateQuotaManagerAndClients();
321 return webkit_context_.get();
322}
323
324webkit_database::DatabaseTracker* ShellBrowserContext::GetDatabaseTracker() {
325 CreateQuotaManagerAndClients();
326 return db_tracker_;
327}
328
329ChromeBlobStorageContext* ShellBrowserContext::GetBlobStorageContext() {
330 if (!blob_storage_context_) {
331 blob_storage_context_ = new ChromeBlobStorageContext();
332 BrowserThread::PostTask(
333 BrowserThread::IO, FROM_HERE,
334 NewRunnableMethod(
335 blob_storage_context_.get(),
336 &ChromeBlobStorageContext::InitializeOnIOThread));
337 }
338 return blob_storage_context_;
339}
340
341ChromeAppCacheService* ShellBrowserContext::GetAppCacheService() {
342 CreateQuotaManagerAndClients();
343 return appcache_service_;
344}
345
346fileapi::FileSystemContext* ShellBrowserContext::GetFileSystemContext() {
347 CreateQuotaManagerAndClients();
348 return file_system_context_.get();
349}
350
351void ShellBrowserContext::CreateQuotaManagerAndClients() {
352 quota_manager_ = new quota::QuotaManager(
353 IsOffTheRecord(),
354 GetPath(),
355 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
356 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
357 NULL);
358
359 file_system_context_ = CreateFileSystemContext(
360 GetPath(), IsOffTheRecord(), NULL, quota_manager_->proxy());
361 db_tracker_ = new webkit_database::DatabaseTracker(
362 GetPath(), IsOffTheRecord(), false, NULL, quota_manager_->proxy(),
363 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE));
364 webkit_context_ = new WebKitContext(
365 IsOffTheRecord(), GetPath(), NULL, false, quota_manager_->proxy(),
366 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::WEBKIT));
367 appcache_service_ = new ChromeAppCacheService(quota_manager_->proxy());
368 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy;
369 BrowserThread::PostTask(
370 BrowserThread::IO, FROM_HERE,
371 NewRunnableMethod(
372 appcache_service_.get(),
373 &ChromeAppCacheService::InitializeOnIOThread,
374 IsOffTheRecord()
375 ? FilePath() : GetPath().Append(FILE_PATH_LITERAL("AppCache")),
376 &GetResourceContext(),
377 special_storage_policy));
378}
379
380} // namespace content