Implement proximity_auth::WireMessage::Serialize().
[chromium-blink-merge.git] / chromecast / renderer / cast_content_renderer_client.cc
blob1f7c4a68d1963492e191814368adeaa8ad887b65
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/location.h"
11 #include "base/memory/memory_pressure_listener.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "chromecast/base/chromecast_switches.h"
15 #include "chromecast/crash/cast_crash_keys.h"
16 #include "chromecast/media/base/media_caps.h"
17 #include "chromecast/renderer/cast_media_load_deferrer.h"
18 #include "chromecast/renderer/cast_render_process_observer.h"
19 #include "chromecast/renderer/key_systems_cast.h"
20 #include "chromecast/renderer/media/chromecast_media_renderer_factory.h"
21 #include "components/network_hints/renderer/prescient_networking_dispatcher.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/renderer/render_frame.h"
24 #include "content/public/renderer/render_view.h"
25 #include "content/public/renderer/render_view_observer.h"
26 #include "crypto/nss_util.h"
27 #include "ipc/message_filter.h"
28 #include "third_party/WebKit/public/platform/WebColor.h"
29 #include "third_party/WebKit/public/web/WebSettings.h"
30 #include "third_party/WebKit/public/web/WebView.h"
32 namespace chromecast {
33 namespace shell {
35 namespace {
37 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
38 // This memory threshold is set for Chromecast. See the UMA histogram
39 // Platform.MeminfoMemFree when tuning.
40 // TODO(gunsch): These should be platform/product-dependent. Look into a way
41 // to move these to platform-specific repositories.
42 const int kCriticalMinFreeMemMB = 24;
43 const int kPollingIntervalMS = 5000;
45 void PlatformPollFreemem(void) {
46 struct sysinfo sys;
48 if (sysinfo(&sys) == -1) {
49 LOG(ERROR) << "platform_poll_freemem(): sysinfo failed";
50 } else {
51 int free_mem_mb = static_cast<int64_t>(sys.freeram) *
52 sys.mem_unit / (1024 * 1024);
54 if (free_mem_mb <= kCriticalMinFreeMemMB) {
55 // Memory is getting really low, we need to do whatever we can to
56 // prevent deadlocks and interfering with other processes.
57 base::MemoryPressureListener::NotifyMemoryPressure(
58 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
62 // Setup next poll.
63 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
64 FROM_HERE, base::Bind(&PlatformPollFreemem),
65 base::TimeDelta::FromMilliseconds(kPollingIntervalMS));
67 #endif
69 // Default background color to set for WebViews. WebColor is in ARGB format
70 // though the comment of WebColor says it is in RGBA.
71 const blink::WebColor kColorBlack = 0xFF000000;
73 class CastRenderViewObserver : content::RenderViewObserver {
74 public:
75 CastRenderViewObserver(CastContentRendererClient* client,
76 content::RenderView* render_view);
77 ~CastRenderViewObserver() override {}
79 void DidClearWindowObject(blink::WebLocalFrame* frame) override;
81 private:
82 CastContentRendererClient* const client_;
84 DISALLOW_COPY_AND_ASSIGN(CastRenderViewObserver);
87 CastRenderViewObserver::CastRenderViewObserver(
88 CastContentRendererClient* client,
89 content::RenderView* render_view)
90 : content::RenderViewObserver(render_view),
91 client_(client) {
94 void CastRenderViewObserver::DidClearWindowObject(blink::WebLocalFrame* frame) {
95 client_->AddRendererNativeBindings(frame);
98 } // namespace
100 CastContentRendererClient::CastContentRendererClient() {
103 CastContentRendererClient::~CastContentRendererClient() {
106 void CastContentRendererClient::AddRendererNativeBindings(
107 blink::WebLocalFrame* frame) {
110 std::vector<scoped_refptr<IPC::MessageFilter>>
111 CastContentRendererClient::GetRendererMessageFilters() {
112 return std::vector<scoped_refptr<IPC::MessageFilter>>();
115 void CastContentRendererClient::RenderThreadStarted() {
116 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
117 #if !defined(USE_OPENSSL)
118 // Note: Copied from chrome_render_process_observer.cc to fix b/8676652.
120 // On platforms where the system NSS shared libraries are used,
121 // initialize NSS now because it won't be able to load the .so's
122 // after entering the sandbox.
123 if (!command_line->HasSwitch(switches::kSingleProcess))
124 crypto::InitNSSSafely();
125 #endif
127 #if defined(ARCH_CPU_ARM_FAMILY) && !defined(OS_ANDROID)
128 PlatformPollFreemem();
129 #endif
131 // Set the initial known codecs mask.
132 if (command_line->HasSwitch(switches::kHdmiSinkSupportedCodecs)) {
133 int hdmi_codecs_mask;
134 if (base::StringToInt(command_line->GetSwitchValueASCII(
135 switches::kHdmiSinkSupportedCodecs),
136 &hdmi_codecs_mask)) {
137 ::media::SetHdmiSinkCodecs(hdmi_codecs_mask);
141 cast_observer_.reset(
142 new CastRenderProcessObserver(GetRendererMessageFilters()));
144 prescient_networking_dispatcher_.reset(
145 new network_hints::PrescientNetworkingDispatcher());
147 std::string last_launched_app =
148 command_line->GetSwitchValueNative(switches::kLastLaunchedApp);
149 if (!last_launched_app.empty())
150 base::debug::SetCrashKeyValue(crash_keys::kLastApp, last_launched_app);
152 std::string previous_app =
153 command_line->GetSwitchValueNative(switches::kPreviousApp);
154 if (!previous_app.empty())
155 base::debug::SetCrashKeyValue(crash_keys::kPreviousApp, previous_app);
158 void CastContentRendererClient::RenderViewCreated(
159 content::RenderView* render_view) {
160 blink::WebView* webview = render_view->GetWebView();
161 if (webview) {
162 webview->setBaseBackgroundColor(kColorBlack);
164 // The following settings express consistent behaviors across Cast
165 // embedders, though Android has enabled by default for mobile browsers.
166 webview->settings()->setShrinksViewportContentToFit(false);
167 webview->settings()->setMediaControlsOverlayPlayButtonEnabled(false);
169 // Scale 1 ensures window.innerHeight/Width match application resolution.
170 // PageScaleOverride is the 'user agent' value which overrides page
171 // settings (from meta viewport tag) - thus preventing inconsistency
172 // between Android and non-Android cast_shell.
173 webview->setDefaultPageScaleLimits(1.f, 1.f);
174 webview->setInitialPageScaleOverride(1.f);
176 // Disable application cache as Chromecast doesn't support off-line
177 // application running.
178 webview->settings()->setOfflineWebApplicationCacheEnabled(false);
181 // Note: RenderView will own the lifetime of its observer.
182 new CastRenderViewObserver(this, render_view);
185 void CastContentRendererClient::AddKeySystems(
186 std::vector< ::media::KeySystemInfo>* key_systems) {
187 AddChromecastKeySystems(key_systems);
190 #if !defined(OS_ANDROID)
191 scoped_ptr<::media::RendererFactory>
192 CastContentRendererClient::CreateMediaRendererFactory(
193 ::content::RenderFrame* render_frame,
194 const scoped_refptr<::media::GpuVideoAcceleratorFactories>& gpu_factories,
195 const scoped_refptr<::media::MediaLog>& media_log) {
196 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
197 if (!cmd_line->HasSwitch(switches::kEnableCmaMediaPipeline))
198 return nullptr;
200 return scoped_ptr<::media::RendererFactory>(
201 new chromecast::media::ChromecastMediaRendererFactory(
202 gpu_factories, media_log, render_frame->GetRoutingID()));
204 #endif
206 blink::WebPrescientNetworking*
207 CastContentRendererClient::GetPrescientNetworking() {
208 return prescient_networking_dispatcher_.get();
211 void CastContentRendererClient::DeferMediaLoad(
212 content::RenderFrame* render_frame,
213 const base::Closure& closure) {
214 if (!render_frame->IsHidden()) {
215 closure.Run();
216 return;
219 // Lifetime is tied to |render_frame| via content::RenderFrameObserver.
220 new CastMediaLoadDeferrer(render_frame, closure);
223 } // namespace shell
224 } // namespace chromecast