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_content_browser_client.h"
6
7#include "base/command_line.h"
8#include "base/i18n/rtl.h"
9#include "base/path_service.h"
10#include "chromecast/common/cast_paths.h"
11#include "chromecast/common/global_descriptors.h"
12#include "chromecast/shell/browser/cast_browser_context.h"
13#include "chromecast/shell/browser/cast_browser_main_parts.h"
14#include "chromecast/shell/browser/cast_browser_process.h"
15#include "chromecast/shell/browser/devtools/cast_dev_tools_delegate.h"
16#include "chromecast/shell/browser/geolocation/cast_access_token_store.h"
17#include "chromecast/shell/browser/url_request_context_factory.h"
18#include "content/public/browser/browser_thread.h"
19#include "content/public/browser/certificate_request_result_type.h"
20#include "content/public/browser/file_descriptor_info.h"
21#include "content/public/browser/render_process_host.h"
22#include "content/public/common/content_descriptors.h"
23#include "content/public/common/content_switches.h"
24#include "content/public/common/url_constants.h"
25#include "content/public/common/web_preferences.h"
26
27namespace chromecast {
28namespace shell {
29
30CastContentBrowserClient::CastContentBrowserClient()
31    : url_request_context_factory_(new URLRequestContextFactory()) {
32}
33
34CastContentBrowserClient::~CastContentBrowserClient() {
35  content::BrowserThread::DeleteSoon(
36      content::BrowserThread::IO,
37      FROM_HERE,
38      url_request_context_factory_.release());
39}
40
41content::BrowserMainParts* CastContentBrowserClient::CreateBrowserMainParts(
42    const content::MainFunctionParams& parameters) {
43  return new CastBrowserMainParts(parameters,
44                                  url_request_context_factory_.get());
45}
46
47void CastContentBrowserClient::RenderProcessWillLaunch(
48    content::RenderProcessHost* host) {
49}
50
51net::URLRequestContextGetter* CastContentBrowserClient::CreateRequestContext(
52    content::BrowserContext* browser_context,
53    content::ProtocolHandlerMap* protocol_handlers,
54    content::URLRequestInterceptorScopedVector request_interceptors) {
55  return url_request_context_factory_->CreateMainGetter(
56      browser_context,
57      protocol_handlers,
58      request_interceptors.Pass());
59}
60
61bool CastContentBrowserClient::IsHandledURL(const GURL& url) {
62  if (!url.is_valid())
63    return false;
64
65  static const char* const kProtocolList[] = {
66      url::kBlobScheme,
67      url::kFileSystemScheme,
68      content::kChromeUIScheme,
69      content::kChromeDevToolsScheme,
70      url::kDataScheme,
71#if defined(OS_ANDROID)
72      url::kFileScheme,
73#endif  // defined(OS_ANDROID)
74  };
75
76  const std::string& scheme = url.scheme();
77  for (size_t i = 0; i < arraysize(kProtocolList); ++i) {
78    if (scheme == kProtocolList[i])
79      return true;
80  }
81  return false;
82}
83
84void CastContentBrowserClient::AppendExtraCommandLineSwitches(
85    base::CommandLine* command_line,
86    int child_process_id) {
87
88  std::string process_type =
89      command_line->GetSwitchValueNative(switches::kProcessType);
90  // Renderer process comamndline
91  if (process_type == switches::kRendererProcess) {
92    // Any browser command-line switches that should be propagated to
93    // the renderer go here.
94  }
95}
96
97content::AccessTokenStore* CastContentBrowserClient::CreateAccessTokenStore() {
98  return new CastAccessTokenStore(
99      CastBrowserProcess::GetInstance()->browser_context());
100}
101
102void CastContentBrowserClient::OverrideWebkitPrefs(
103    content::RenderViewHost* render_view_host,
104    const GURL& url,
105    content::WebPreferences* prefs) {
106  prefs->allow_scripts_to_close_windows = true;
107  // TODO(lcwu): http://crbug.com/391089. This pref is set to true by default
108  // because some content providers such as YouTube use plain http requests
109  // to retrieve media data chunks while running in a https page. This pref
110  // should be disabled once all the content providers are no longer doing that.
111  prefs->allow_running_insecure_content = true;
112}
113
114std::string CastContentBrowserClient::GetApplicationLocale() {
115  const std::string locale(base::i18n::GetConfiguredLocale());
116  return locale.empty() ? "en-US" : locale;
117}
118
119void CastContentBrowserClient::AllowCertificateError(
120    int render_process_id,
121    int render_view_id,
122    int cert_error,
123    const net::SSLInfo& ssl_info,
124    const GURL& request_url,
125    content::ResourceType resource_type,
126    bool overridable,
127    bool strict_enforcement,
128    bool expired_previous_decision,
129    const base::Callback<void(bool)>& callback,
130    content::CertificateRequestResultType* result) {
131  // Allow developers to override certificate errors.
132  // Otherwise, any fatal certificate errors will cause an abort.
133  *result = content::CERTIFICATE_REQUEST_RESULT_TYPE_CANCEL;
134  return;
135}
136
137bool CastContentBrowserClient::CanCreateWindow(
138    const GURL& opener_url,
139    const GURL& opener_top_level_frame_url,
140    const GURL& source_origin,
141    WindowContainerType container_type,
142    const GURL& target_url,
143    const content::Referrer& referrer,
144    WindowOpenDisposition disposition,
145    const blink::WebWindowFeatures& features,
146    bool user_gesture,
147    bool opener_suppressed,
148    content::ResourceContext* context,
149    int render_process_id,
150    int opener_id,
151    bool* no_javascript_access) {
152  *no_javascript_access = true;
153  return false;
154}
155
156content::DevToolsManagerDelegate*
157CastContentBrowserClient::GetDevToolsManagerDelegate() {
158  return new CastDevToolsManagerDelegate();
159}
160
161void CastContentBrowserClient::GetAdditionalMappedFilesForChildProcess(
162    const base::CommandLine& command_line,
163    int child_process_id,
164    std::vector<content::FileDescriptorInfo>* mappings) {
165#if defined(OS_ANDROID)
166  int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
167  base::FilePath pak_file;
168  CHECK(PathService::Get(FILE_CAST_PAK, &pak_file));
169  base::File pak_with_flags(pak_file, flags);
170  if (!pak_with_flags.IsValid()) {
171    NOTREACHED() << "Failed to open file when creating renderer process: "
172                 << "cast_shell.pak";
173  }
174  mappings->push_back(content::FileDescriptorInfo(
175      kAndroidPakDescriptor,
176      base::FileDescriptor(base::File(pak_file, flags))));
177#endif  // defined(OS_ANDROID)
178}
179
180}  // namespace shell
181}  // namespace chromecast
182