base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / chromecast / renderer / cast_content_renderer_client.cc
blob521602d8d74f382240aeb10f42b351c5190fe59e
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 {
26 namespace shell {
28 namespace {
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) {
40 struct sysinfo sys;
42 if (sysinfo(&sys) == -1) {
43 LOG(ERROR) << "platform_poll_freemem(): sysinfo failed";
44 } else {
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);
54 } else {
55 // There is enough memory, but it is starting to get low.
56 base::MemoryPressureListener::NotifyMemoryPressure(
57 base::MemoryPressureListener::MEMORY_PRESSURE_MODERATE);
62 // Setup next poll.
63 base::MessageLoopProxy::current()->PostDelayedTask(
64 FROM_HERE,
65 base::Bind(&PlatformPollFreemem),
66 base::TimeDelta::FromMilliseconds(kPollingIntervalMS));
68 #endif
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;
74 } // namespace
76 CastContentRendererClient::CastContentRendererClient() {
79 CastContentRendererClient::~CastContentRendererClient() {
82 void CastContentRendererClient::RenderThreadStarted() {
83 #if defined(USE_NSS)
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();
92 #endif
94 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
95 PlatformPollFreemem();
96 #endif
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();
107 if (webview) {
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))
129 return nullptr;
131 return scoped_ptr<::media::RendererFactory>(
132 new chromecast::media::CmaMediaRendererFactory(
133 render_frame->GetRoutingID()));
135 #endif
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()) {
146 closure.Run();
147 return;
150 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
151 new CastMediaLoadDeferrer(render_frame, closure);
154 } // namespace shell
155 } // namespace chromecast