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 "chromecast/common/chromecast_switches.h"
13 #include "chromecast/crash/cast_crash_keys.h"
14 #include "chromecast/media/base/media_caps.h"
15 #include "chromecast/renderer/cast_media_load_deferrer.h"
16 #include "chromecast/renderer/cast_render_process_observer.h"
17 #include "chromecast/renderer/key_systems_cast.h"
18 #include "chromecast/renderer/media/chromecast_media_renderer_factory.h"
19 #include "components/network_hints/renderer/prescient_networking_dispatcher.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/renderer/render_frame.h"
22 #include "content/public/renderer/render_view.h"
23 #include "content/public/renderer/render_view_observer.h"
24 #include "crypto/nss_util.h"
25 #include "ipc/message_filter.h"
26 #include "third_party/WebKit/public/platform/WebColor.h"
27 #include "third_party/WebKit/public/web/WebSettings.h"
28 #include "third_party/WebKit/public/web/WebView.h"
30 namespace chromecast
{
35 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
36 // This memory threshold is set for Chromecast. See the UMA histogram
37 // Platform.MeminfoMemFree when tuning.
38 // TODO(gunsch): These should be platform/product-dependent. Look into a way
39 // to move these to platform-specific repositories.
40 const int kCriticalMinFreeMemMB
= 24;
41 const int kPollingIntervalMS
= 5000;
43 void PlatformPollFreemem(void) {
46 if (sysinfo(&sys
) == -1) {
47 LOG(ERROR
) << "platform_poll_freemem(): sysinfo failed";
49 int free_mem_mb
= static_cast<int64_t>(sys
.freeram
) *
50 sys
.mem_unit
/ (1024 * 1024);
52 if (free_mem_mb
<= kCriticalMinFreeMemMB
) {
53 // Memory is getting really low, we need to do whatever we can to
54 // prevent deadlocks and interfering with other processes.
55 base::MemoryPressureListener::NotifyMemoryPressure(
56 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
61 base::MessageLoopProxy::current()->PostDelayedTask(
63 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();
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 // Disable application cache as Chromecast doesn't support off-line
151 // application running.
152 webview
->settings()->setOfflineWebApplicationCacheEnabled(false);
155 // Note: RenderView will own the lifetime of its observer.
156 new CastRenderViewObserver(render_view
);
159 void CastContentRendererClient::AddKeySystems(
160 std::vector
< ::media::KeySystemInfo
>* key_systems
) {
161 AddChromecastKeySystems(key_systems
);
162 AddChromecastPlatformKeySystems(key_systems
);
165 #if !defined(OS_ANDROID)
166 scoped_ptr
<::media::RendererFactory
>
167 CastContentRendererClient::CreateMediaRendererFactory(
168 ::content::RenderFrame
* render_frame
,
169 const scoped_refptr
<::media::MediaLog
>& media_log
) {
170 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
171 if (!cmd_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
174 return scoped_ptr
<::media::RendererFactory
>(
175 new chromecast::media::ChromecastMediaRendererFactory(
176 media_log
, render_frame
->GetRoutingID()));
180 blink::WebPrescientNetworking
*
181 CastContentRendererClient::GetPrescientNetworking() {
182 return prescient_networking_dispatcher_
.get();
185 void CastContentRendererClient::DeferMediaLoad(
186 content::RenderFrame
* render_frame
,
187 const base::Closure
& closure
) {
188 if (!render_frame
->IsHidden()) {
193 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
194 new CastMediaLoadDeferrer(render_frame
, closure
);
198 } // namespace chromecast