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