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/renderer/cast_media_load_deferrer.h"
13 #include "chromecast/renderer/cast_render_process_observer.h"
14 #include "chromecast/renderer/key_systems_cast.h"
15 #include "chromecast/renderer/media/cma_media_renderer_factory.h"
16 #include "components/dns_prefetch/renderer/prescient_networking_dispatcher.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/renderer/render_frame.h"
19 #include "content/public/renderer/render_view.h"
20 #include "crypto/nss_util.h"
21 #include "third_party/WebKit/public/platform/WebColor.h"
22 #include "third_party/WebKit/public/web/WebSettings.h"
23 #include "third_party/WebKit/public/web/WebView.h"
25 namespace chromecast
{
30 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
31 // These memory thresholds are set for Chromecast. See the UMA histogram
32 // Platform.MeminfoMemFree when tuning.
33 // TODO(gunsch): These should be platform/product-dependent. Look into a way
34 // to move these to platform-specific repositories.
35 const int kCriticalMinFreeMemMB
= 24;
36 const int kModerateMinFreeMemMB
= 48;
37 const int kPollingIntervalMS
= 5000;
39 void PlatformPollFreemem(void) {
42 if (sysinfo(&sys
) == -1) {
43 LOG(ERROR
) << "platform_poll_freemem(): sysinfo failed";
45 int free_mem_mb
= static_cast<int64_t>(sys
.freeram
) *
46 sys
.mem_unit
/ (1024 * 1024);
48 if (free_mem_mb
<= kModerateMinFreeMemMB
) {
49 if (free_mem_mb
<= kCriticalMinFreeMemMB
) {
50 // Memory is getting really low, we need to do whatever we can to
51 // prevent deadlocks and interfering with other processes.
52 base::MemoryPressureListener::NotifyMemoryPressure(
53 base::MemoryPressureListener::MEMORY_PRESSURE_CRITICAL
);
55 // There is enough memory, but it is starting to get low.
56 base::MemoryPressureListener::NotifyMemoryPressure(
57 base::MemoryPressureListener::MEMORY_PRESSURE_MODERATE
);
63 base::MessageLoopProxy::current()->PostDelayedTask(
65 base::Bind(&PlatformPollFreemem
),
66 base::TimeDelta::FromMilliseconds(kPollingIntervalMS
));
70 // Default background color to set for WebViews. WebColor is in ARGB format
71 // though the comment of WebColor says it is in RGBA.
72 const blink::WebColor kColorBlack
= 0xFF000000;
76 CastContentRendererClient::CastContentRendererClient() {
79 CastContentRendererClient::~CastContentRendererClient() {
82 void CastContentRendererClient::RenderThreadStarted() {
84 // Note: Copied from chrome_render_process_observer.cc to fix b/8676652.
86 // On platforms where the system NSS shared libraries are used,
87 // initialize NSS now because it won't be able to load the .so's
88 // after entering the sandbox.
89 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
90 if (!command_line
->HasSwitch(switches::kSingleProcess
))
91 crypto::InitNSSSafely();
94 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
95 PlatformPollFreemem();
98 cast_observer_
.reset(new CastRenderProcessObserver());
100 prescient_networking_dispatcher_
.reset(
101 new dns_prefetch::PrescientNetworkingDispatcher());
104 void CastContentRendererClient::RenderViewCreated(
105 content::RenderView
* render_view
) {
106 blink::WebView
* webview
= render_view
->GetWebView();
108 webview
->setBaseBackgroundColor(kColorBlack
);
110 // The following settings express consistent behaviors across Cast
111 // embedders, though Android has enabled by default for mobile browsers.
112 webview
->settings()->setShrinksViewportContentToFit(false);
113 webview
->settings()->setMediaControlsOverlayPlayButtonEnabled(false);
117 void CastContentRendererClient::AddKeySystems(
118 std::vector
< ::media::KeySystemInfo
>* key_systems
) {
119 AddChromecastKeySystems(key_systems
);
120 AddChromecastPlatformKeySystems(key_systems
);
123 #if !defined(OS_ANDROID)
124 scoped_ptr
<::media::RendererFactory
>
125 CastContentRendererClient::CreateMediaRendererFactory(
126 ::content::RenderFrame
* render_frame
) {
127 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
128 if (!cmd_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
131 return scoped_ptr
<::media::RendererFactory
>(
132 new chromecast::media::CmaMediaRendererFactory(
133 render_frame
->GetRoutingID()));
137 blink::WebPrescientNetworking
*
138 CastContentRendererClient::GetPrescientNetworking() {
139 return prescient_networking_dispatcher_
.get();
142 void CastContentRendererClient::DeferMediaLoad(
143 content::RenderFrame
* render_frame
,
144 const base::Closure
& closure
) {
145 if (!render_frame
->IsHidden()) {
150 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
151 new CastMediaLoadDeferrer(render_frame
, closure
);
155 } // namespace chromecast