1 // Copyright 2013 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 #include "content/shell/browser/shell_browser_context.h"
8 #include "base/command_line.h"
9 #include "base/environment.h"
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/threading/thread.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/resource_context.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/shell/browser/shell_download_manager_delegate.h"
19 #include "content/shell/browser/shell_url_request_context_getter.h"
20 #include "content/shell/common/shell_switches.h"
23 #include "base/base_paths_win.h"
24 #elif defined(OS_LINUX)
25 #include "base/nix/xdg_util.h"
26 #elif defined(OS_MACOSX)
27 #include "base/base_paths_mac.h"
32 class ShellBrowserContext::ShellResourceContext
: public ResourceContext
{
34 ShellResourceContext() : getter_(NULL
) {}
35 virtual ~ShellResourceContext() {}
37 // ResourceContext implementation:
38 virtual net::HostResolver
* GetHostResolver() OVERRIDE
{
40 return getter_
->host_resolver();
42 virtual net::URLRequestContext
* GetRequestContext() OVERRIDE
{
44 return getter_
->GetURLRequestContext();
46 virtual bool AllowMicAccess(const GURL
& origin
) OVERRIDE
{
49 virtual bool AllowCameraAccess(const GURL
& origin
) OVERRIDE
{
53 void set_url_request_context_getter(ShellURLRequestContextGetter
* getter
) {
58 ShellURLRequestContextGetter
* getter_
;
60 DISALLOW_COPY_AND_ASSIGN(ShellResourceContext
);
63 ShellBrowserContext::ShellBrowserContext(bool off_the_record
,
65 : off_the_record_(off_the_record
),
67 ignore_certificate_errors_(false),
69 resource_context_(new ShellResourceContext
) {
73 ShellBrowserContext::~ShellBrowserContext() {
74 if (resource_context_
) {
75 BrowserThread::DeleteSoon(
76 BrowserThread::IO
, FROM_HERE
, resource_context_
.release());
80 void ShellBrowserContext::InitWhileIOAllowed() {
81 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
82 if (cmd_line
->HasSwitch(switches::kIgnoreCertificateErrors
) ||
83 cmd_line
->HasSwitch(switches::kDumpRenderTree
)) {
84 ignore_certificate_errors_
= true;
86 if (cmd_line
->HasSwitch(switches::kContentShellDataPath
)) {
87 path_
= cmd_line
->GetSwitchValuePath(switches::kContentShellDataPath
);
91 CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA
, &path_
));
92 path_
= path_
.Append(std::wstring(L
"content_shell"));
93 #elif defined(OS_LINUX)
94 scoped_ptr
<base::Environment
> env(base::Environment::Create());
95 base::FilePath
config_dir(
96 base::nix::GetXDGDirectory(env
.get(),
97 base::nix::kXdgConfigHomeEnvVar
,
98 base::nix::kDotConfigDir
));
99 path_
= config_dir
.Append("content_shell");
100 #elif defined(OS_MACOSX)
101 CHECK(PathService::Get(base::DIR_APP_DATA
, &path_
));
102 path_
= path_
.Append("Chromium Content Shell");
103 #elif defined(OS_ANDROID)
104 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA
, &path_
));
105 path_
= path_
.Append(FILE_PATH_LITERAL("content_shell"));
110 if (!base::PathExists(path_
))
111 base::CreateDirectory(path_
);
114 base::FilePath
ShellBrowserContext::GetPath() const {
118 bool ShellBrowserContext::IsOffTheRecord() const {
119 return off_the_record_
;
122 DownloadManagerDelegate
* ShellBrowserContext::GetDownloadManagerDelegate() {
123 DownloadManager
* manager
= BrowserContext::GetDownloadManager(this);
125 if (!download_manager_delegate_
.get()) {
126 download_manager_delegate_
.reset(new ShellDownloadManagerDelegate());
127 download_manager_delegate_
->SetDownloadManager(manager
);
128 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
129 if (cmd_line
->HasSwitch(switches::kDumpRenderTree
)) {
130 download_manager_delegate_
->SetDownloadBehaviorForTesting(
131 path_
.Append(FILE_PATH_LITERAL("downloads")));
135 return download_manager_delegate_
.get();
138 net::URLRequestContextGetter
* ShellBrowserContext::GetRequestContext() {
139 return GetDefaultStoragePartition(this)->GetURLRequestContext();
142 net::URLRequestContextGetter
* ShellBrowserContext::CreateRequestContext(
143 ProtocolHandlerMap
* protocol_handlers
,
144 URLRequestInterceptorScopedVector request_interceptors
) {
145 DCHECK(!url_request_getter_
.get());
146 url_request_getter_
= new ShellURLRequestContextGetter(
147 ignore_certificate_errors_
,
149 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO
),
150 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
152 request_interceptors
.Pass(),
154 resource_context_
->set_url_request_context_getter(url_request_getter_
.get());
155 return url_request_getter_
.get();
158 net::URLRequestContextGetter
*
159 ShellBrowserContext::GetRequestContextForRenderProcess(
160 int renderer_child_id
) {
161 return GetRequestContext();
164 net::URLRequestContextGetter
*
165 ShellBrowserContext::GetMediaRequestContext() {
166 return GetRequestContext();
169 net::URLRequestContextGetter
*
170 ShellBrowserContext::GetMediaRequestContextForRenderProcess(
171 int renderer_child_id
) {
172 return GetRequestContext();
175 net::URLRequestContextGetter
*
176 ShellBrowserContext::GetMediaRequestContextForStoragePartition(
177 const base::FilePath
& partition_path
,
179 return GetRequestContext();
182 net::URLRequestContextGetter
*
183 ShellBrowserContext::CreateRequestContextForStoragePartition(
184 const base::FilePath
& partition_path
,
186 ProtocolHandlerMap
* protocol_handlers
,
187 URLRequestInterceptorScopedVector request_interceptors
) {
191 ResourceContext
* ShellBrowserContext::GetResourceContext() {
192 return resource_context_
.get();
195 BrowserPluginGuestManager
* ShellBrowserContext::GetGuestManager() {
196 return guest_manager_
;
199 storage::SpecialStoragePolicy
* ShellBrowserContext::GetSpecialStoragePolicy() {
203 PushMessagingService
* ShellBrowserContext::GetPushMessagingService() {
207 SSLHostStateDelegate
* ShellBrowserContext::GetSSLHostStateDelegate() {
211 } // namespace content