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.
5 #include "chromecast/renderer/cast_content_renderer_client.h"
7 #include <sys/sysinfo.h>
9 #include "base/command_line.h"
10 #include "base/memory/memory_pressure_listener.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chromecast/base/chromecast_switches.h"
14 #include "chromecast/crash/cast_crash_keys.h"
15 #include "chromecast/media/base/media_caps.h"
16 #include "chromecast/renderer/cast_media_load_deferrer.h"
17 #include "chromecast/renderer/cast_render_process_observer.h"
18 #include "chromecast/renderer/key_systems_cast.h"
19 #include "chromecast/renderer/media/chromecast_media_renderer_factory.h"
20 #include "components/network_hints/renderer/prescient_networking_dispatcher.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/renderer/render_frame.h"
23 #include "content/public/renderer/render_view.h"
24 #include "content/public/renderer/render_view_observer.h"
25 #include "crypto/nss_util.h"
26 #include "ipc/message_filter.h"
27 #include "third_party/WebKit/public/platform/WebColor.h"
28 #include "third_party/WebKit/public/web/WebSettings.h"
29 #include "third_party/WebKit/public/web/WebView.h"
31 namespace chromecast
{
36 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
37 // This memory threshold is set for Chromecast. See the UMA histogram
38 // Platform.MeminfoMemFree when tuning.
39 // TODO(gunsch): These should be platform/product-dependent. Look into a way
40 // to move these to platform-specific repositories.
41 const int kCriticalMinFreeMemMB
= 24;
42 const int kPollingIntervalMS
= 5000;
44 void PlatformPollFreemem(void) {
47 if (sysinfo(&sys
) == -1) {
48 LOG(ERROR
) << "platform_poll_freemem(): sysinfo failed";
50 int free_mem_mb
= static_cast<int64_t>(sys
.freeram
) *
51 sys
.mem_unit
/ (1024 * 1024);
53 if (free_mem_mb
<= kCriticalMinFreeMemMB
) {
54 // Memory is getting really low, we need to do whatever we can to
55 // prevent deadlocks and interfering with other processes.
56 base::MemoryPressureListener::NotifyMemoryPressure(
57 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
62 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
63 FROM_HERE
, base::Bind(&PlatformPollFreemem
),
64 base::TimeDelta::FromMilliseconds(kPollingIntervalMS
));
68 // Default background color to set for WebViews. WebColor is in ARGB format
69 // though the comment of WebColor says it is in RGBA.
70 const blink::WebColor kColorBlack
= 0xFF000000;
72 class CastRenderViewObserver
: content::RenderViewObserver
{
74 explicit CastRenderViewObserver(content::RenderView
* render_view
);
75 ~CastRenderViewObserver() override
{}
77 void DidClearWindowObject(blink::WebLocalFrame
* frame
) override
;
80 CastRenderViewObserver::CastRenderViewObserver(content::RenderView
* render_view
)
81 : content::RenderViewObserver(render_view
) {
84 void CastRenderViewObserver::DidClearWindowObject(blink::WebLocalFrame
* frame
) {
85 PlatformAddRendererNativeBindings(frame
);
90 CastContentRendererClient::CastContentRendererClient() {
93 CastContentRendererClient::~CastContentRendererClient() {
96 void CastContentRendererClient::RenderThreadStarted() {
97 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
98 #if defined(USE_NSS_CERTS)
99 // Note: Copied from chrome_render_process_observer.cc to fix b/8676652.
101 // On platforms where the system NSS shared libraries are used,
102 // initialize NSS now because it won't be able to load the .so's
103 // after entering the sandbox.
104 if (!command_line
->HasSwitch(switches::kSingleProcess
))
105 crypto::InitNSSSafely();
108 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
109 PlatformPollFreemem();
112 // Set the initial known codecs mask.
113 if (command_line
->HasSwitch(switches::kHdmiSinkSupportedCodecs
)) {
114 int hdmi_codecs_mask
;
115 if (base::StringToInt(command_line
->GetSwitchValueASCII(
116 switches::kHdmiSinkSupportedCodecs
),
117 &hdmi_codecs_mask
)) {
118 ::media::SetHdmiSinkCodecs(hdmi_codecs_mask
);
122 cast_observer_
.reset(
123 new CastRenderProcessObserver(PlatformGetRendererMessageFilters()));
125 prescient_networking_dispatcher_
.reset(
126 new network_hints::PrescientNetworkingDispatcher());
128 std::string last_launched_app
=
129 command_line
->GetSwitchValueNative(switches::kLastLaunchedApp
);
130 if (!last_launched_app
.empty())
131 base::debug::SetCrashKeyValue(crash_keys::kLastApp
, last_launched_app
);
133 std::string previous_app
=
134 command_line
->GetSwitchValueNative(switches::kPreviousApp
);
135 if (!previous_app
.empty())
136 base::debug::SetCrashKeyValue(crash_keys::kPreviousApp
, previous_app
);
139 void CastContentRendererClient::RenderViewCreated(
140 content::RenderView
* render_view
) {
141 blink::WebView
* webview
= render_view
->GetWebView();
143 webview
->setBaseBackgroundColor(kColorBlack
);
145 // The following settings express consistent behaviors across Cast
146 // embedders, though Android has enabled by default for mobile browsers.
147 webview
->settings()->setShrinksViewportContentToFit(false);
148 webview
->settings()->setMediaControlsOverlayPlayButtonEnabled(false);
150 // Scale 1 ensures window.innerHeight/Width match application resolution.
151 // PageScaleOverride is the 'user agent' value which overrides page
152 // settings (from meta viewport tag) - thus preventing inconsistency
153 // between Android and non-Android cast_shell.
154 webview
->setDefaultPageScaleLimits(1.f
, 1.f
);
155 webview
->setInitialPageScaleOverride(1.f
);
157 // Disable application cache as Chromecast doesn't support off-line
158 // application running.
159 webview
->settings()->setOfflineWebApplicationCacheEnabled(false);
162 // Note: RenderView will own the lifetime of its observer.
163 new CastRenderViewObserver(render_view
);
166 void CastContentRendererClient::AddKeySystems(
167 std::vector
< ::media::KeySystemInfo
>* key_systems
) {
168 AddChromecastKeySystems(key_systems
);
169 AddChromecastPlatformKeySystems(key_systems
);
172 #if !defined(OS_ANDROID)
173 scoped_ptr
<::media::RendererFactory
>
174 CastContentRendererClient::CreateMediaRendererFactory(
175 ::content::RenderFrame
* render_frame
,
176 const scoped_refptr
<::media::MediaLog
>& media_log
) {
177 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
178 if (!cmd_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
181 return scoped_ptr
<::media::RendererFactory
>(
182 new chromecast::media::ChromecastMediaRendererFactory(
183 media_log
, render_frame
->GetRoutingID()));
187 blink::WebPrescientNetworking
*
188 CastContentRendererClient::GetPrescientNetworking() {
189 return prescient_networking_dispatcher_
.get();
192 void CastContentRendererClient::DeferMediaLoad(
193 content::RenderFrame
* render_frame
,
194 const base::Closure
& closure
) {
195 if (!render_frame
->IsHidden()) {
200 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
201 new CastMediaLoadDeferrer(render_frame
, closure
);
205 } // namespace chromecast