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 "chromecast/common/chromecast_switches.h"
12 #include "chromecast/crash/cast_crash_keys.h"
13 #include "chromecast/renderer/cast_media_load_deferrer.h"
14 #include "chromecast/renderer/cast_render_process_observer.h"
15 #include "chromecast/renderer/key_systems_cast.h"
16 #include "chromecast/renderer/media/cma_media_renderer_factory.h"
17 #include "components/network_hints/renderer/prescient_networking_dispatcher.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/renderer/render_frame.h"
20 #include "content/public/renderer/render_view.h"
21 #include "content/public/renderer/render_view_observer.h"
22 #include "crypto/nss_util.h"
23 #include "ipc/message_filter.h"
24 #include "third_party/WebKit/public/platform/WebColor.h"
25 #include "third_party/WebKit/public/web/WebSettings.h"
26 #include "third_party/WebKit/public/web/WebView.h"
28 namespace chromecast
{
33 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
34 // This memory threshold is set for Chromecast. See the UMA histogram
35 // Platform.MeminfoMemFree when tuning.
36 // TODO(gunsch): These should be platform/product-dependent. Look into a way
37 // to move these to platform-specific repositories.
38 const int kCriticalMinFreeMemMB
= 24;
39 const int kPollingIntervalMS
= 5000;
41 void PlatformPollFreemem(void) {
44 if (sysinfo(&sys
) == -1) {
45 LOG(ERROR
) << "platform_poll_freemem(): sysinfo failed";
47 int free_mem_mb
= static_cast<int64_t>(sys
.freeram
) *
48 sys
.mem_unit
/ (1024 * 1024);
50 if (free_mem_mb
<= kCriticalMinFreeMemMB
) {
51 // Memory is getting really low, we need to do whatever we can to
52 // prevent deadlocks and interfering with other processes.
53 base::MemoryPressureListener::NotifyMemoryPressure(
54 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
59 base::MessageLoopProxy::current()->PostDelayedTask(
61 base::Bind(&PlatformPollFreemem
),
62 base::TimeDelta::FromMilliseconds(kPollingIntervalMS
));
66 // Default background color to set for WebViews. WebColor is in ARGB format
67 // though the comment of WebColor says it is in RGBA.
68 const blink::WebColor kColorBlack
= 0xFF000000;
70 class CastRenderViewObserver
: content::RenderViewObserver
{
72 explicit CastRenderViewObserver(content::RenderView
* render_view
);
73 ~CastRenderViewObserver() override
{}
75 void DidClearWindowObject(blink::WebLocalFrame
* frame
) override
;
78 CastRenderViewObserver::CastRenderViewObserver(content::RenderView
* render_view
)
79 : content::RenderViewObserver(render_view
) {
82 void CastRenderViewObserver::DidClearWindowObject(blink::WebLocalFrame
* frame
) {
83 PlatformAddRendererNativeBindings(frame
);
88 CastContentRendererClient::CastContentRendererClient() {
91 CastContentRendererClient::~CastContentRendererClient() {
94 void CastContentRendererClient::RenderThreadStarted() {
95 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
97 // Note: Copied from chrome_render_process_observer.cc to fix b/8676652.
99 // On platforms where the system NSS shared libraries are used,
100 // initialize NSS now because it won't be able to load the .so's
101 // after entering the sandbox.
102 if (!command_line
->HasSwitch(switches::kSingleProcess
))
103 crypto::InitNSSSafely();
106 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
107 PlatformPollFreemem();
110 cast_observer_
.reset(
111 new CastRenderProcessObserver(PlatformGetRendererMessageFilters()));
113 prescient_networking_dispatcher_
.reset(
114 new network_hints::PrescientNetworkingDispatcher());
116 std::string last_launched_app
=
117 command_line
->GetSwitchValueNative(switches::kLastLaunchedApp
);
118 if (!last_launched_app
.empty())
119 base::debug::SetCrashKeyValue(crash_keys::kLastApp
, last_launched_app
);
121 std::string previous_app
=
122 command_line
->GetSwitchValueNative(switches::kPreviousApp
);
123 if (!previous_app
.empty())
124 base::debug::SetCrashKeyValue(crash_keys::kPreviousApp
, previous_app
);
127 void CastContentRendererClient::RenderViewCreated(
128 content::RenderView
* render_view
) {
129 blink::WebView
* webview
= render_view
->GetWebView();
131 webview
->setBaseBackgroundColor(kColorBlack
);
133 // The following settings express consistent behaviors across Cast
134 // embedders, though Android has enabled by default for mobile browsers.
135 webview
->settings()->setShrinksViewportContentToFit(false);
136 webview
->settings()->setMediaControlsOverlayPlayButtonEnabled(false);
138 // Disable application cache as Chromecast doesn't support off-line
139 // application running.
140 webview
->settings()->setOfflineWebApplicationCacheEnabled(false);
143 // Note: RenderView will own the lifetime of its observer.
144 new CastRenderViewObserver(render_view
);
147 void CastContentRendererClient::AddKeySystems(
148 std::vector
< ::media::KeySystemInfo
>* key_systems
) {
149 AddChromecastKeySystems(key_systems
);
150 AddChromecastPlatformKeySystems(key_systems
);
153 #if !defined(OS_ANDROID)
154 scoped_ptr
<::media::RendererFactory
>
155 CastContentRendererClient::CreateMediaRendererFactory(
156 ::content::RenderFrame
* render_frame
,
157 const scoped_refptr
<::media::MediaLog
>& media_log
) {
158 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
159 if (!cmd_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
162 return scoped_ptr
<::media::RendererFactory
>(
163 new chromecast::media::CmaMediaRendererFactory(
164 render_frame
->GetRoutingID()));
168 blink::WebPrescientNetworking
*
169 CastContentRendererClient::GetPrescientNetworking() {
170 return prescient_networking_dispatcher_
.get();
173 void CastContentRendererClient::DeferMediaLoad(
174 content::RenderFrame
* render_frame
,
175 const base::Closure
& closure
) {
176 if (!render_frame
->IsHidden()) {
181 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
182 new CastMediaLoadDeferrer(render_frame
, closure
);
186 } // namespace chromecast