• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
ShellResourceContext()34   ShellResourceContext() : getter_(NULL) {}
~ShellResourceContext()35   virtual ~ShellResourceContext() {}
36 
37   // ResourceContext implementation:
GetHostResolver()38   virtual net::HostResolver* GetHostResolver() OVERRIDE {
39     CHECK(getter_);
40     return getter_->host_resolver();
41   }
GetRequestContext()42   virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
43     CHECK(getter_);
44     return getter_->GetURLRequestContext();
45   }
AllowMicAccess(const GURL & origin)46   virtual bool AllowMicAccess(const GURL& origin) OVERRIDE {
47     return false;
48   }
AllowCameraAccess(const GURL & origin)49   virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE {
50     return false;
51   }
52 
set_url_request_context_getter(ShellURLRequestContextGetter * getter)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 
ShellBrowserContext(bool off_the_record,net::NetLog * net_log)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 
~ShellBrowserContext()72 ShellBrowserContext::~ShellBrowserContext() {
73   if (resource_context_) {
74     BrowserThread::DeleteSoon(
75       BrowserThread::IO, FROM_HERE, resource_context_.release());
76   }
77 }
78 
InitWhileIOAllowed()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 
GetPath() const113 base::FilePath ShellBrowserContext::GetPath() const {
114   return path_;
115 }
116 
IsOffTheRecord() const117 bool ShellBrowserContext::IsOffTheRecord() const {
118   return off_the_record_;
119 }
120 
GetDownloadManagerDelegate()121 DownloadManagerDelegate* ShellBrowserContext::GetDownloadManagerDelegate()  {
122   DownloadManager* manager = BrowserContext::GetDownloadManager(this);
123 
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")));
131     }
132   }
133 
134   return download_manager_delegate_.get();
135 }
136 
GetRequestContext()137 net::URLRequestContextGetter* ShellBrowserContext::GetRequestContext()  {
138   return GetDefaultStoragePartition(this)->GetURLRequestContext();
139 }
140 
CreateRequestContext(ProtocolHandlerMap * protocol_handlers)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*
GetRequestContextForRenderProcess(int renderer_child_id)156     ShellBrowserContext::GetRequestContextForRenderProcess(
157         int renderer_child_id)  {
158   return GetRequestContext();
159 }
160 
161 net::URLRequestContextGetter*
GetMediaRequestContext()162     ShellBrowserContext::GetMediaRequestContext()  {
163   return GetRequestContext();
164 }
165 
166 net::URLRequestContextGetter*
GetMediaRequestContextForRenderProcess(int renderer_child_id)167     ShellBrowserContext::GetMediaRequestContextForRenderProcess(
168         int renderer_child_id)  {
169   return GetRequestContext();
170 }
171 
172 net::URLRequestContextGetter*
GetMediaRequestContextForStoragePartition(const base::FilePath & partition_path,bool in_memory)173     ShellBrowserContext::GetMediaRequestContextForStoragePartition(
174         const base::FilePath& partition_path,
175         bool in_memory) {
176   return GetRequestContext();
177 }
178 
RequestMIDISysExPermission(int render_process_id,int render_view_id,int bridge_id,const GURL & requesting_frame,const MIDISysExPermissionCallback & callback)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(switches::kDumpRenderTree)) {
188     callback.Run(false);
189     return;
190   }
191   // TODO(toyoshim): Implement. http://crbug.com/257618 .
192   callback.Run(false);
193 }
194 
CancelMIDISysExPermissionRequest(int render_process_id,int render_view_id,int bridge_id,const GURL & requesting_frame)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 net::URLRequestContextGetter*
CreateRequestContextForStoragePartition(const base::FilePath & partition_path,bool in_memory,ProtocolHandlerMap * protocol_handlers)203     ShellBrowserContext::CreateRequestContextForStoragePartition(
204         const base::FilePath& partition_path,
205         bool in_memory,
206         ProtocolHandlerMap* protocol_handlers) {
207   return NULL;
208 }
209 
GetResourceContext()210 ResourceContext* ShellBrowserContext::GetResourceContext()  {
211   return resource_context_.get();
212 }
213 
214 GeolocationPermissionContext*
GetGeolocationPermissionContext()215     ShellBrowserContext::GetGeolocationPermissionContext()  {
216   return NULL;
217 }
218 
GetSpecialStoragePolicy()219 quota::SpecialStoragePolicy* ShellBrowserContext::GetSpecialStoragePolicy() {
220   return NULL;
221 }
222 
223 }  // namespace content
224