blob: 29fb8daef715a17da03ef94d4c9608576b02be72 [file] [log] [blame]
[email protected]de7d61ff2013-08-20 11:30:411// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]b0f146f2011-09-15 22:14:252// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de7d61ff2013-08-20 11:30:415#include "content/shell/browser/shell_browser_context.h"
[email protected]b0f146f2011-09-15 22:14:256
[email protected]037edb52011-11-15 21:14:067#include "base/bind.h"
[email protected]2f2b9512012-06-08 17:31:008#include "base/command_line.h"
[email protected]5cba3bf2012-01-14 02:40:359#include "base/environment.h"
[email protected]b0f146f2011-09-15 22:14:2510#include "base/file_util.h"
11#include "base/logging.h"
12#include "base/path_service.h"
[email protected]2e5b60a22011-11-28 15:56:4113#include "base/threading/thread.h"
[email protected]c38831a12011-10-28 12:44:4914#include "content/public/browser/browser_thread.h"
[email protected]6bd30072013-02-08 18:17:1115#include "content/public/browser/resource_context.h"
16#include "content/public/browser/storage_partition.h"
17#include "content/public/common/content_switches.h"
[email protected]de7d61ff2013-08-20 11:30:4118#include "content/shell/browser/shell_download_manager_delegate.h"
19#include "content/shell/browser/shell_url_request_context_getter.h"
[email protected]b7c504c2013-05-07 14:42:1220#include "content/shell/common/shell_switches.h"
[email protected]b0f146f2011-09-15 22:14:2521
22#if defined(OS_WIN)
23#include "base/base_paths_win.h"
[email protected]5cba3bf2012-01-14 02:40:3524#elif defined(OS_LINUX)
25#include "base/nix/xdg_util.h"
[email protected]80ec0b72012-03-22 22:45:2726#elif defined(OS_MACOSX)
27#include "base/base_paths_mac.h"
[email protected]b0f146f2011-09-15 22:14:2528#endif
29
[email protected]810ddc52012-01-24 01:00:3530namespace content {
31
[email protected]6bd30072013-02-08 18:17:1132class ShellBrowserContext::ShellResourceContext : public ResourceContext {
33 public:
34 ShellResourceContext() : getter_(NULL) {}
35 virtual ~ShellResourceContext() {}
36
37 // ResourceContext implementation:
38 virtual net::HostResolver* GetHostResolver() OVERRIDE {
39 CHECK(getter_);
40 return getter_->host_resolver();
41 }
42 virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
43 CHECK(getter_);
44 return getter_->GetURLRequestContext();
45 }
[email protected]d07190a2013-08-06 22:12:3946 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE {
47 return false;
48 }
49 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE {
50 return false;
51 }
[email protected]6bd30072013-02-08 18:17:1152
53 void set_url_request_context_getter(ShellURLRequestContextGetter* getter) {
54 getter_ = getter;
55 }
56
57 private:
58 ShellURLRequestContextGetter* getter_;
59
60 DISALLOW_COPY_AND_ASSIGN(ShellResourceContext);
61};
62
[email protected]d5869bf2013-07-03 16:21:4763ShellBrowserContext::ShellBrowserContext(bool off_the_record,
64 net::NetLog* net_log)
[email protected]c2dad292012-09-07 21:27:3565 : off_the_record_(off_the_record),
[email protected]d5869bf2013-07-03 16:21:4766 net_log_(net_log),
[email protected]6bd30072013-02-08 18:17:1167 ignore_certificate_errors_(false),
68 resource_context_(new ShellResourceContext) {
[email protected]11a65b692012-03-30 11:29:1669 InitWhileIOAllowed();
[email protected]b0f146f2011-09-15 22:14:2570}
71
72ShellBrowserContext::~ShellBrowserContext() {
[email protected]59383c782013-04-17 16:43:2773 if (resource_context_) {
[email protected]e99ca5112011-09-26 17:22:5474 BrowserThread::DeleteSoon(
75 BrowserThread::IO, FROM_HERE, resource_context_.release());
76 }
[email protected]b0f146f2011-09-15 22:14:2577}
78
[email protected]11a65b692012-03-30 11:29:1679void ShellBrowserContext::InitWhileIOAllowed() {
[email protected]aae34062012-07-19 15:52:1580 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]d22dc092012-11-19 20:35:5681 if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors) ||
82 cmd_line->HasSwitch(switches::kDumpRenderTree)) {
[email protected]c2dad292012-09-07 21:27:3583 ignore_certificate_errors_ = true;
[email protected]d22dc092012-11-19 20:35:5684 }
[email protected]e1cf85a22012-08-17 22:39:2385 if (cmd_line->HasSwitch(switches::kContentShellDataPath)) {
86 path_ = cmd_line->GetSwitchValuePath(switches::kContentShellDataPath);
87 return;
88 }
[email protected]b0f146f2011-09-15 22:14:2589#if defined(OS_WIN)
[email protected]e99ca5112011-09-26 17:22:5490 CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA, &path_));
91 path_ = path_.Append(std::wstring(L"content_shell"));
[email protected]5cba3bf2012-01-14 02:40:3592#elif defined(OS_LINUX)
93 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]d30a36f2013-02-07 04:16:2694 base::FilePath config_dir(
[email protected]9528c9a2012-06-13 23:20:2295 base::nix::GetXDGDirectory(env.get(),
96 base::nix::kXdgConfigHomeEnvVar,
97 base::nix::kDotConfigDir));
[email protected]5cba3bf2012-01-14 02:40:3598 path_ = config_dir.Append("content_shell");
[email protected]80ec0b72012-03-22 22:45:2799#elif defined(OS_MACOSX)
100 CHECK(PathService::Get(base::DIR_APP_DATA, &path_));
101 path_ = path_.Append("Chromium Content Shell");
[email protected]70a7f1192012-06-08 01:31:34102#elif defined(OS_ANDROID)
[email protected]c3bf75252013-03-18 22:11:48103 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &path_));
[email protected]70a7f1192012-06-08 01:31:34104 path_ = path_.Append(FILE_PATH_LITERAL("content_shell"));
[email protected]b0f146f2011-09-15 22:14:25105#else
106 NOTIMPLEMENTED();
107#endif
108
[email protected]7567484142013-07-11 17:36:07109 if (!base::PathExists(path_))
[email protected]426d1c92013-12-03 20:08:54110 base::CreateDirectory(path_);
[email protected]11a65b692012-03-30 11:29:16111}
[email protected]b0f146f2011-09-15 22:14:25112
[email protected]4251165a2013-07-17 04:33:40113base::FilePath ShellBrowserContext::GetPath() const {
[email protected]e99ca5112011-09-26 17:22:54114 return path_;
[email protected]b0f146f2011-09-15 22:14:25115}
116
[email protected]27d6e852012-03-02 21:31:32117bool ShellBrowserContext::IsOffTheRecord() const {
[email protected]71d504f2012-07-25 17:15:28118 return off_the_record_;
[email protected]b0f146f2011-09-15 22:14:25119}
120
[email protected]b441a8492012-06-06 14:55:57121DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate() {
[email protected]08c92ac2012-08-21 23:41:40122 DownloadManager* manager = BrowserContext::GetDownloadManager(this);
123
[email protected]fc72bb12013-06-02 21:13:46124 if (!download_manager_delegate_.get()) {
[email protected]7cef82172013-12-17 06:58:37125 download_manager_delegate_.reset(new ShellDownloadManagerDelegate());
[email protected]59002092012-08-27 19:42:56126 download_manager_delegate_->SetDownloadManager(manager);
[email protected]17bc408b22013-01-10 18:40:08127 CommandLine* cmd_line = CommandLine::ForCurrentProcess();
128 if (cmd_line->HasSwitch(switches::kDumpRenderTree)) {
129 download_manager_delegate_->SetDownloadBehaviorForTesting(
130 path_.Append(FILE_PATH_LITERAL("downloads")));
131 }
[email protected]59002092012-08-27 19:42:56132 }
[email protected]08c92ac2012-08-21 23:41:40133
[email protected]b441a8492012-06-06 14:55:57134 return download_manager_delegate_.get();
[email protected]b0f146f2011-09-15 22:14:25135}
136
[email protected]b0f146f2011-09-15 22:14:25137net::URLRequestContextGetter* ShellBrowserContext::GetRequestContext() {
[email protected]6bd30072013-02-08 18:17:11138 return GetDefaultStoragePartition(this)->GetURLRequestContext();
139}
140
141net::URLRequestContextGetter* ShellBrowserContext::CreateRequestContext(
[email protected]672c8c12013-03-07 12:30:06142 ProtocolHandlerMap* protocol_handlers) {
[email protected]fc72bb12013-06-02 21:13:46143 DCHECK(!url_request_getter_.get());
[email protected]6bd30072013-02-08 18:17:11144 url_request_getter_ = new ShellURLRequestContextGetter(
145 ignore_certificate_errors_,
146 GetPath(),
147 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
148 BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
[email protected]d5869bf2013-07-03 16:21:47149 protocol_handlers,
150 net_log_);
[email protected]6bd30072013-02-08 18:17:11151 resource_context_->set_url_request_context_getter(url_request_getter_.get());
152 return url_request_getter_.get();
[email protected]b0f146f2011-09-15 22:14:25153}
154
155net::URLRequestContextGetter*
156 ShellBrowserContext::GetRequestContextForRenderProcess(
157 int renderer_child_id) {
158 return GetRequestContext();
159}
160
161net::URLRequestContextGetter*
[email protected]10705a7b2012-08-21 19:07:08162 ShellBrowserContext::GetMediaRequestContext() {
163 return GetRequestContext();
164}
165
166net::URLRequestContextGetter*
167 ShellBrowserContext::GetMediaRequestContextForRenderProcess(
168 int renderer_child_id) {
[email protected]b0f146f2011-09-15 22:14:25169 return GetRequestContext();
170}
171
[email protected]55c0eca2012-09-15 05:12:34172net::URLRequestContextGetter*
[email protected]10eb28162012-09-18 03:04:09173 ShellBrowserContext::GetMediaRequestContextForStoragePartition(
[email protected]d30a36f2013-02-07 04:16:26174 const base::FilePath& partition_path,
[email protected]27ddfed22012-10-30 23:22:43175 bool in_memory) {
[email protected]10eb28162012-09-18 03:04:09176 return GetRequestContext();
177}
178
[email protected]8aca7252013-07-12 19:18:59179void ShellBrowserContext::RequestMIDISysExPermission(
180 int render_process_id,
181 int render_view_id,
[email protected]00880a02013-10-30 03:18:30182 int bridge_id,
[email protected]8aca7252013-07-12 19:18:59183 const GURL& requesting_frame,
184 const MIDISysExPermissionCallback& callback) {
[email protected]19027322013-07-23 11:46:52185 // Always reject requests for LayoutTests for now.
[email protected]8aca7252013-07-12 19:18:59186 // TODO(toyoshim): Make it programmable to improve test coverage.
[email protected]19027322013-07-23 11:46:52187 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)) {
188 callback.Run(false);
189 return;
190 }
[email protected]bccdcd92014-01-03 05:30:03191 callback.Run(true);
[email protected]8aca7252013-07-12 19:18:59192}
193
[email protected]00880a02013-10-30 03:18:30194void ShellBrowserContext::CancelMIDISysExPermissionRequest(
195 int render_process_id,
196 int render_view_id,
197 int bridge_id,
198 const GURL& requesting_frame) {
199}
200
[email protected]10eb28162012-09-18 03:04:09201net::URLRequestContextGetter*
[email protected]6bd30072013-02-08 18:17:11202 ShellBrowserContext::CreateRequestContextForStoragePartition(
[email protected]d30a36f2013-02-07 04:16:26203 const base::FilePath& partition_path,
[email protected]6bd30072013-02-08 18:17:11204 bool in_memory,
[email protected]672c8c12013-03-07 12:30:06205 ProtocolHandlerMap* protocol_handlers) {
[email protected]55c0eca2012-09-15 05:12:34206 return NULL;
207}
208
[email protected]df02aca2012-02-09 21:03:20209ResourceContext* ShellBrowserContext::GetResourceContext() {
[email protected]df02aca2012-02-09 21:03:20210 return resource_context_.get();
[email protected]b0f146f2011-09-15 22:14:25211}
212
[email protected]b0f146f2011-09-15 22:14:25213GeolocationPermissionContext*
214 ShellBrowserContext::GetGeolocationPermissionContext() {
[email protected]30e3e9a62012-06-07 20:46:57215 return NULL;
[email protected]b0f146f2011-09-15 22:14:25216}
217
[email protected]55eb70e762012-02-20 17:38:39218quota::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() {
219 return NULL;
[email protected]b0f146f2011-09-15 22:14:25220}
221
222} // namespace content