1// Copyright 2014 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 "chromecast/shell/browser/cast_browser_context.h"
6
7#include "base/command_line.h"
8#include "base/files/file_util.h"
9#include "base/macros.h"
10#include "base/path_service.h"
11#include "chromecast/common/cast_paths.h"
12#include "chromecast/shell/browser/cast_download_manager_delegate.h"
13#include "chromecast/shell/browser/url_request_context_factory.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 "net/url_request/url_request_context.h"
19#include "net/url_request/url_request_context_getter.h"
20
21namespace chromecast {
22namespace shell {
23
24class CastBrowserContext::CastResourceContext :
25    public content::ResourceContext {
26 public:
27  explicit CastResourceContext(
28      URLRequestContextFactory* url_request_context_factory) :
29    url_request_context_factory_(url_request_context_factory) {}
30  virtual ~CastResourceContext() {}
31
32  // ResourceContext implementation:
33  virtual net::HostResolver* GetHostResolver() OVERRIDE {
34    return url_request_context_factory_->GetMainGetter()->
35        GetURLRequestContext()->host_resolver();
36  }
37
38  virtual net::URLRequestContext* GetRequestContext() OVERRIDE {
39    return url_request_context_factory_->GetMainGetter()->
40        GetURLRequestContext();
41  }
42
43 private:
44  URLRequestContextFactory* url_request_context_factory_;
45
46  DISALLOW_COPY_AND_ASSIGN(CastResourceContext);
47};
48
49CastBrowserContext::CastBrowserContext(
50    URLRequestContextFactory* url_request_context_factory)
51    : url_request_context_factory_(url_request_context_factory),
52      resource_context_(new CastResourceContext(url_request_context_factory)),
53      download_manager_delegate_(new CastDownloadManagerDelegate()) {
54  InitWhileIOAllowed();
55}
56
57CastBrowserContext::~CastBrowserContext() {
58  content::BrowserThread::DeleteSoon(
59      content::BrowserThread::IO,
60      FROM_HERE,
61      resource_context_.release());
62}
63
64void CastBrowserContext::InitWhileIOAllowed() {
65#if defined(OS_ANDROID)
66  CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &path_));
67  path_ = path_.Append(FILE_PATH_LITERAL("cast_shell"));
68
69  if (!base::PathExists(path_))
70    base::CreateDirectory(path_);
71#else
72  // Chromecast doesn't support user profiles nor does it have
73  // incognito mode.  This means that all of the persistent
74  // data (currently only cookies and local storage) will be
75  // shared in a single location as defined here.
76  CHECK(PathService::Get(DIR_CAST_HOME, &path_));
77#endif  // defined(OS_ANDROID)
78}
79
80base::FilePath CastBrowserContext::GetPath() const {
81  return path_;
82}
83
84bool CastBrowserContext::IsOffTheRecord() const {
85  return false;
86}
87
88net::URLRequestContextGetter* CastBrowserContext::GetRequestContext() {
89  return GetDefaultStoragePartition(this)->GetURLRequestContext();
90}
91
92net::URLRequestContextGetter*
93CastBrowserContext::GetRequestContextForRenderProcess(int renderer_child_id) {
94  return GetRequestContext();
95}
96
97net::URLRequestContextGetter* CastBrowserContext::GetMediaRequestContext() {
98  return url_request_context_factory_->GetMediaGetter();
99}
100
101net::URLRequestContextGetter*
102CastBrowserContext::GetMediaRequestContextForRenderProcess(
103    int renderer_child_id) {
104  return GetMediaRequestContext();
105}
106
107net::URLRequestContextGetter*
108CastBrowserContext::GetMediaRequestContextForStoragePartition(
109    const base::FilePath& partition_path, bool in_memory) {
110  return GetMediaRequestContext();
111}
112
113content::ResourceContext* CastBrowserContext::GetResourceContext() {
114  return resource_context_.get();
115}
116
117content::DownloadManagerDelegate*
118CastBrowserContext::GetDownloadManagerDelegate() {
119  return download_manager_delegate_.get();
120}
121
122content::BrowserPluginGuestManager* CastBrowserContext::GetGuestManager() {
123  NOTIMPLEMENTED();
124  return NULL;
125}
126
127storage::SpecialStoragePolicy* CastBrowserContext::GetSpecialStoragePolicy() {
128  NOTIMPLEMENTED();
129  return NULL;
130}
131
132content::PushMessagingService* CastBrowserContext::GetPushMessagingService() {
133  NOTIMPLEMENTED();
134  return NULL;
135}
136
137content::SSLHostStateDelegate* CastBrowserContext::GetSSLHostStateDelegate() {
138  NOTIMPLEMENTED();
139  return NULL;
140}
141
142}  // namespace shell
143}  // namespace chromecast
144