Upstream version 5.34.104.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/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
30 namespace content {
31
32 class 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   virtual bool AllowMicAccess(const GURL& origin) OVERRIDE {
47     return false;
48   }
49   virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE {
50     return false;
51   }
52
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
63 ShellBrowserContext::ShellBrowserContext(bool off_the_record,
64                                          net::NetLog* net_log)
65     : off_the_record_(off_the_record),
66       net_log_(net_log),
67       ignore_certificate_errors_(false),
68       resource_context_(new ShellResourceContext) {
69   InitWhileIOAllowed();
70 }
71
72 ShellBrowserContext::~ShellBrowserContext() {
73   if (resource_context_) {
74     BrowserThread::DeleteSoon(
75       BrowserThread::IO, FROM_HERE, resource_context_.release());
76   }
77 }
78
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;
84   }
85   if (cmd_line->HasSwitch(switches::kContentShellDataPath)) {
86     path_ = cmd_line->GetSwitchValuePath(switches::kContentShellDataPath);
87     return;
88   }
89 #if defined(OS_WIN)
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"));
105 #else
106   NOTIMPLEMENTED();
107 #endif
108
109   if (!base::PathExists(path_))
110     base::CreateDirectory(path_);
111 }
112
113 base::FilePath ShellBrowserContext::GetPath() const {
114   return path_;
115 }
116
117 bool ShellBrowserContext::IsOffTheRecord() const {
118   return off_the_record_;
119 }
120
121 DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate()  {
122   DownloadManager* manager = BrowserContext::GetDownloadManager(this);
123
124   if (!download_manager_delegate_.get()) {
125     download_manager_delegate_.reset(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")));
131     }
132   }
133
134   return download_manager_delegate_.get();
135 }
136
137 net::URLRequestContextGetter* ShellBrowserContext::GetRequestContext()  {
138   return GetDefaultStoragePartition(this)->GetURLRequestContext();
139 }
140
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_,
146       GetPath(),
147       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
148       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
149       protocol_handlers,
150       net_log_);
151   resource_context_->set_url_request_context_getter(url_request_getter_.get());
152   return url_request_getter_.get();
153 }
154
155 net::URLRequestContextGetter*
156     ShellBrowserContext::GetRequestContextForRenderProcess(
157         int renderer_child_id)  {
158   return GetRequestContext();
159 }
160
161 net::URLRequestContextGetter*
162     ShellBrowserContext::GetMediaRequestContext()  {
163   return GetRequestContext();
164 }
165
166 net::URLRequestContextGetter*
167     ShellBrowserContext::GetMediaRequestContextForRenderProcess(
168         int renderer_child_id)  {
169   return GetRequestContext();
170 }
171
172 net::URLRequestContextGetter*
173     ShellBrowserContext::GetMediaRequestContextForStoragePartition(
174         const base::FilePath& partition_path,
175         bool in_memory) {
176   return GetRequestContext();
177 }
178
179 void ShellBrowserContext::RequestMidiSysExPermission(
180       int render_process_id,
181       int render_view_id,
182       int bridge_id,
183       const GURL& requesting_frame,
184       const MidiSysExPermissionCallback& callback) {
185   // Always reject requests for LayoutTests for now.
186   // TODO(toyoshim): Make it programmable to improve test coverage.
187   if (!CommandLine::ForCurrentProcess()->HasSwitch(
188       switches::kDumpRenderTree)) {
189     callback.Run(false);
190     return;
191   }
192   callback.Run(true);
193 }
194
195 void ShellBrowserContext::CancelMidiSysExPermissionRequest(
196     int render_process_id,
197     int render_view_id,
198     int bridge_id,
199     const GURL& requesting_frame) {
200 }
201
202 void ShellBrowserContext::RequestProtectedMediaIdentifierPermission(
203     int render_process_id,
204     int render_view_id,
205     int bridge_id,
206     int group_id,
207     const GURL& requesting_frame,
208     const ProtectedMediaIdentifierPermissionCallback& callback) {
209   callback.Run(true);
210 }
211
212 void ShellBrowserContext::CancelProtectedMediaIdentifierPermissionRequests(
213     int group_id) {
214 }
215
216 net::URLRequestContextGetter*
217     ShellBrowserContext::CreateRequestContextForStoragePartition(
218         const base::FilePath& partition_path,
219         bool in_memory,
220         ProtocolHandlerMap* protocol_handlers) {
221   return NULL;
222 }
223
224 ResourceContext* ShellBrowserContext::GetResourceContext()  {
225   return resource_context_.get();
226 }
227
228 GeolocationPermissionContext*
229     ShellBrowserContext::GetGeolocationPermissionContext()  {
230   return NULL;
231 }
232
233 quota::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() {
234   return NULL;
235 }
236
237 }  // namespace content