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),
68 resource_context_(new ShellResourceContext
) {
72 ShellBrowserContext::~ShellBrowserContext() {
73 if (resource_context_
) {
74 BrowserThread::DeleteSoon(
75 BrowserThread::IO
, FROM_HERE
, resource_context_
.release());
79 void ShellBrowserContext::InitWhileIOAllowed() {
80 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
81 if (cmd_line
->HasSwitch(switches::kIgnoreCertificateErrors
) ||
82 cmd_line
->HasSwitch(switches::kDumpRenderTree
)) {
83 ignore_certificate_errors_
= true;
85 if (cmd_line
->HasSwitch(switches::kContentShellDataPath
)) {
86 path_
= cmd_line
->GetSwitchValuePath(switches::kContentShellDataPath
);
90 CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA
, &path_
));
91 path_
= path_
.Append(std::wstring(L
"content_shell"));
92 #elif defined(OS_LINUX)
93 scoped_ptr
<base::Environment
> env(base::Environment::Create());
94 base::FilePath
config_dir(
95 base::nix::GetXDGDirectory(env
.get(),
96 base::nix::kXdgConfigHomeEnvVar
,
97 base::nix::kDotConfigDir
));
98 path_
= config_dir
.Append("content_shell");
99 #elif defined(OS_MACOSX)
100 CHECK(PathService::Get(base::DIR_APP_DATA
, &path_
));
101 path_
= path_
.Append("Chromium Content Shell");
102 #elif defined(OS_ANDROID)
103 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA
, &path_
));
104 path_
= path_
.Append(FILE_PATH_LITERAL("content_shell"));
109 if (!base::PathExists(path_
))
110 file_util::CreateDirectory(path_
);
113 base::FilePath
ShellBrowserContext::GetPath() const {
117 bool ShellBrowserContext::IsOffTheRecord() const {
118 return off_the_record_
;
121 DownloadManagerDelegate
* ShellBrowserContext::GetDownloadManagerDelegate() {
122 DownloadManager
* manager
= BrowserContext::GetDownloadManager(this);
124 if (!download_manager_delegate_
.get()) {
125 download_manager_delegate_
= new ShellDownloadManagerDelegate();
126 download_manager_delegate_
->SetDownloadManager(manager
);
127 CommandLine
* cmd_line
= CommandLine::ForCurrentProcess();
128 if (cmd_line
->HasSwitch(switches::kDumpRenderTree
)) {
129 download_manager_delegate_
->SetDownloadBehaviorForTesting(
130 path_
.Append(FILE_PATH_LITERAL("downloads")));
134 return download_manager_delegate_
.get();
137 net::URLRequestContextGetter
* ShellBrowserContext::GetRequestContext() {
138 return GetDefaultStoragePartition(this)->GetURLRequestContext();
141 net::URLRequestContextGetter
* ShellBrowserContext::CreateRequestContext(
142 ProtocolHandlerMap
* protocol_handlers
) {
143 DCHECK(!url_request_getter_
.get());
144 url_request_getter_
= new ShellURLRequestContextGetter(
145 ignore_certificate_errors_
,
147 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO
),
148 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
151 resource_context_
->set_url_request_context_getter(url_request_getter_
.get());
152 return url_request_getter_
.get();
155 net::URLRequestContextGetter
*
156 ShellBrowserContext::GetRequestContextForRenderProcess(
157 int renderer_child_id
) {
158 return GetRequestContext();
161 net::URLRequestContextGetter
*
162 ShellBrowserContext::GetMediaRequestContext() {
163 return GetRequestContext();
166 net::URLRequestContextGetter
*
167 ShellBrowserContext::GetMediaRequestContextForRenderProcess(
168 int renderer_child_id
) {
169 return GetRequestContext();
172 net::URLRequestContextGetter
*
173 ShellBrowserContext::GetMediaRequestContextForStoragePartition(
174 const base::FilePath
& partition_path
,
176 return GetRequestContext();
179 void ShellBrowserContext::RequestMIDISysExPermission(
180 int render_process_id
,
182 const GURL
& requesting_frame
,
183 const MIDISysExPermissionCallback
& callback
) {
184 // Always reject requests for LayoutTests for now.
185 // TODO(toyoshim): Make it programmable to improve test coverage.
186 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
)) {
190 // TODO(toyoshim): Implement. http://crbug.com/257618 .
194 net::URLRequestContextGetter
*
195 ShellBrowserContext::CreateRequestContextForStoragePartition(
196 const base::FilePath
& partition_path
,
198 ProtocolHandlerMap
* protocol_handlers
) {
202 ResourceContext
* ShellBrowserContext::GetResourceContext() {
203 return resource_context_
.get();
206 GeolocationPermissionContext
*
207 ShellBrowserContext::GetGeolocationPermissionContext() {
211 quota::SpecialStoragePolicy
* ShellBrowserContext::GetSpecialStoragePolicy() {
215 } // namespace content