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