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.
4
5#include "content/shell/browser/shell_browser_context.h"
6
7#include "base/bind.h"
8#include "base/command_line.h"
9#include "base/environment.h"
10#include "base/files/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"
21
22#if defined(OS_WIN)
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"
28#endif
29
30namespace content {
31
32class 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  }
46
47  void set_url_request_context_getter(ShellURLRequestContextGetter* getter) {
48    getter_ = getter;
49  }
50
51 private:
52  ShellURLRequestContextGetter* getter_;
53
54  DISALLOW_COPY_AND_ASSIGN(ShellResourceContext);
55};
56
57ShellBrowserContext::ShellBrowserContext(bool off_the_record,
58                                         net::NetLog* net_log)
59    : off_the_record_(off_the_record),
60      net_log_(net_log),
61      ignore_certificate_errors_(false),
62      guest_manager_(NULL),
63      resource_context_(new ShellResourceContext) {
64  InitWhileIOAllowed();
65}
66
67ShellBrowserContext::~ShellBrowserContext() {
68  if (resource_context_) {
69    BrowserThread::DeleteSoon(
70      BrowserThread::IO, FROM_HERE, resource_context_.release());
71  }
72}
73
74void ShellBrowserContext::InitWhileIOAllowed() {
75  CommandLine* cmd_line = CommandLine::ForCurrentProcess();
76  if (cmd_line->HasSwitch(switches::kIgnoreCertificateErrors) ||
77      cmd_line->HasSwitch(switches::kDumpRenderTree)) {
78    ignore_certificate_errors_ = true;
79  }
80  if (cmd_line->HasSwitch(switches::kContentShellDataPath)) {
81    path_ = cmd_line->GetSwitchValuePath(switches::kContentShellDataPath);
82    return;
83  }
84#if defined(OS_WIN)
85  CHECK(PathService::Get(base::DIR_LOCAL_APP_DATA, &path_));
86  path_ = path_.Append(std::wstring(L"content_shell"));
87#elif defined(OS_LINUX)
88  scoped_ptr<base::Environment> env(base::Environment::Create());
89  base::FilePath config_dir(
90      base::nix::GetXDGDirectory(env.get(),
91                                 base::nix::kXdgConfigHomeEnvVar,
92                                 base::nix::kDotConfigDir));
93  path_ = config_dir.Append("content_shell");
94#elif defined(OS_MACOSX)
95  CHECK(PathService::Get(base::DIR_APP_DATA, &path_));
96  path_ = path_.Append("Chromium Content Shell");
97#elif defined(OS_ANDROID)
98  CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &path_));
99  path_ = path_.Append(FILE_PATH_LITERAL("content_shell"));
100#else
101  NOTIMPLEMENTED();
102#endif
103
104  if (!base::PathExists(path_))
105    base::CreateDirectory(path_);
106}
107
108base::FilePath ShellBrowserContext::GetPath() const {
109  return path_;
110}
111
112bool ShellBrowserContext::IsOffTheRecord() const {
113  return off_the_record_;
114}
115
116DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate()  {
117  DownloadManager* manager = BrowserContext::GetDownloadManager(this);
118
119  if (!download_manager_delegate_.get()) {
120    download_manager_delegate_.reset(new ShellDownloadManagerDelegate());
121    download_manager_delegate_->SetDownloadManager(manager);
122    CommandLine* cmd_line = CommandLine::ForCurrentProcess();
123    if (cmd_line->HasSwitch(switches::kDumpRenderTree)) {
124      download_manager_delegate_->SetDownloadBehaviorForTesting(
125          path_.Append(FILE_PATH_LITERAL("downloads")));
126    }
127  }
128
129  return download_manager_delegate_.get();
130}
131
132net::URLRequestContextGetter* ShellBrowserContext::GetRequestContext()  {
133  return GetDefaultStoragePartition(this)->GetURLRequestContext();
134}
135
136net::URLRequestContextGetter* ShellBrowserContext::CreateRequestContext(
137    ProtocolHandlerMap* protocol_handlers,
138    URLRequestInterceptorScopedVector request_interceptors) {
139  DCHECK(!url_request_getter_.get());
140  url_request_getter_ = new ShellURLRequestContextGetter(
141      ignore_certificate_errors_,
142      GetPath(),
143      BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
144      BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
145      protocol_handlers,
146      request_interceptors.Pass(),
147      net_log_);
148  resource_context_->set_url_request_context_getter(url_request_getter_.get());
149  return url_request_getter_.get();
150}
151
152net::URLRequestContextGetter*
153    ShellBrowserContext::GetRequestContextForRenderProcess(
154        int renderer_child_id)  {
155  return GetRequestContext();
156}
157
158net::URLRequestContextGetter*
159    ShellBrowserContext::GetMediaRequestContext()  {
160  return GetRequestContext();
161}
162
163net::URLRequestContextGetter*
164    ShellBrowserContext::GetMediaRequestContextForRenderProcess(
165        int renderer_child_id)  {
166  return GetRequestContext();
167}
168
169net::URLRequestContextGetter*
170    ShellBrowserContext::GetMediaRequestContextForStoragePartition(
171        const base::FilePath& partition_path,
172        bool in_memory) {
173  return GetRequestContext();
174}
175
176net::URLRequestContextGetter*
177ShellBrowserContext::CreateRequestContextForStoragePartition(
178    const base::FilePath& partition_path,
179    bool in_memory,
180    ProtocolHandlerMap* protocol_handlers,
181    URLRequestInterceptorScopedVector request_interceptors) {
182  return NULL;
183}
184
185ResourceContext* ShellBrowserContext::GetResourceContext()  {
186  return resource_context_.get();
187}
188
189BrowserPluginGuestManager* ShellBrowserContext::GetGuestManager() {
190  return guest_manager_;
191}
192
193storage::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() {
194  return NULL;
195}
196
197PushMessagingService* ShellBrowserContext::GetPushMessagingService() {
198  return NULL;
199}
200
201SSLHostStateDelegate* ShellBrowserContext::GetSSLHostStateDelegate() {
202  return NULL;
203}
204
205}  // namespace content
206