1 // Copyright (c) 2012 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 "content/renderer/renderer_blink_platform_impl.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/lazy_instance.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/metrics/histogram.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "build/build_config.h"
20 #include "cc/blink/context_provider_web_context.h"
21 #include "components/scheduler/child/web_scheduler_impl.h"
22 #include "components/scheduler/renderer/renderer_scheduler.h"
23 #include "components/scheduler/renderer/webthread_impl_for_renderer_scheduler.h"
24 #include "content/child/database_util.h"
25 #include "content/child/file_info_util.h"
26 #include "content/child/fileapi/webfilesystem_impl.h"
27 #include "content/child/indexed_db/webidbfactory_impl.h"
28 #include "content/child/npapi/npobject_util.h"
29 #include "content/child/quota_dispatcher.h"
30 #include "content/child/quota_message_filter.h"
31 #include "content/child/simple_webmimeregistry_impl.h"
32 #include "content/child/thread_safe_sender.h"
33 #include "content/child/web_database_observer_impl.h"
34 #include "content/child/webblobregistry_impl.h"
35 #include "content/child/webfileutilities_impl.h"
36 #include "content/child/webmessageportchannel_impl.h"
37 #include "content/common/file_utilities_messages.h"
38 #include "content/common/gpu/client/context_provider_command_buffer.h"
39 #include "content/common/gpu/client/gpu_channel_host.h"
40 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
41 #include "content/common/gpu/gpu_process_launch_causes.h"
42 #include "content/common/mime_registry_messages.h"
43 #include "content/common/view_messages.h"
44 #include "content/public/common/content_switches.h"
45 #include "content/public/common/service_registry.h"
46 #include "content/public/common/webplugininfo.h"
47 #include "content/public/renderer/content_renderer_client.h"
48 #include "content/renderer/battery_status/battery_status_dispatcher.h"
49 #include "content/renderer/cache_storage/webserviceworkercachestorage_impl.h"
50 #include "content/renderer/device_sensors/device_light_event_pump.h"
51 #include "content/renderer/device_sensors/device_motion_event_pump.h"
52 #include "content/renderer/device_sensors/device_orientation_event_pump.h"
53 #include "content/renderer/dom_storage/webstoragenamespace_impl.h"
54 #include "content/renderer/gamepad_shared_memory_reader.h"
55 #include "content/renderer/media/audio_decoder.h"
56 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
57 #include "content/renderer/media/renderer_webmidiaccessor_impl.h"
58 #include "content/renderer/render_thread_impl.h"
59 #include "content/renderer/renderer_clipboard_delegate.h"
60 #include "content/renderer/screen_orientation/screen_orientation_observer.h"
61 #include "content/renderer/webclipboard_impl.h"
62 #include "content/renderer/webgraphicscontext3d_provider_impl.h"
63 #include "content/renderer/webpublicsuffixlist_impl.h"
64 #include "gpu/config/gpu_info.h"
65 #include "ipc/ipc_sync_message_filter.h"
66 #include "media/audio/audio_output_device.h"
67 #include "media/base/audio_hardware_config.h"
68 #include "media/base/key_systems.h"
69 #include "media/base/mime_util.h"
70 #include "media/blink/webcontentdecryptionmodule_impl.h"
71 #include "media/filters/stream_parser_factory.h"
72 #include "net/base/net_util.h"
73 #include "storage/common/database/database_identifier.h"
74 #include "storage/common/quota/quota_types.h"
75 #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h"
76 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
77 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h"
78 #include "third_party/WebKit/public/platform/WebFileInfo.h"
79 #include "third_party/WebKit/public/platform/WebGamepads.h"
80 #include "third_party/WebKit/public/platform/WebMediaStreamCenter.h"
81 #include "third_party/WebKit/public/platform/WebMediaStreamCenterClient.h"
82 #include "third_party/WebKit/public/platform/WebPluginListBuilder.h"
83 #include "third_party/WebKit/public/platform/WebURL.h"
84 #include "third_party/WebKit/public/platform/WebVector.h"
85 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceMotionListener.h"
86 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationListener.h"
87 #include "ui/gfx/color_profile.h"
90 #if defined(OS_ANDROID)
91 #include "content/renderer/android/synchronous_compositor_factory.h"
92 #include "content/renderer/media/android/audio_decoder_android.h"
93 #include "gpu/blink/webgraphicscontext3d_in_process_command_buffer_impl.h"
96 #if defined(OS_MACOSX)
97 #include "content/common/mac/font_descriptor.h"
98 #include "content/common/mac/font_loader.h"
99 #include "content/renderer/webscrollbarbehavior_impl_mac.h"
100 #include "third_party/WebKit/public/platform/mac/WebSandboxSupport.h"
103 #if defined(OS_POSIX)
104 #include "base/file_descriptor_posix.h"
105 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
109 #include "base/synchronization/lock.h"
110 #include "content/common/child_process_sandbox_support_impl_linux.h"
111 #include "third_party/WebKit/public/platform/linux/WebFallbackFont.h"
112 #include "third_party/WebKit/public/platform/linux/WebSandboxSupport.h"
113 #include "third_party/icu/source/common/unicode/utf16.h"
118 #include "content/common/child_process_messages.h"
121 #if defined(USE_AURA)
122 #include "content/renderer/webscrollbarbehavior_impl_gtkoraura.h"
123 #elif !defined(OS_MACOSX)
124 #include "third_party/WebKit/public/platform/WebScrollbarBehavior.h"
125 #define WebScrollbarBehaviorImpl blink::WebScrollbarBehavior
128 #if defined(ENABLE_WEBRTC)
129 #include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
132 using blink::Platform
;
133 using blink::WebAudioDevice
;
134 using blink::WebBlobRegistry
;
135 using blink::WebDatabaseObserver
;
136 using blink::WebFileInfo
;
137 using blink::WebFileSystem
;
138 using blink::WebGamepad
;
139 using blink::WebGamepads
;
140 using blink::WebIDBFactory
;
141 using blink::WebMIDIAccessor
;
142 using blink::WebMediaStreamCenter
;
143 using blink::WebMediaStreamCenterClient
;
144 using blink::WebMimeRegistry
;
145 using blink::WebRTCPeerConnectionHandler
;
146 using blink::WebRTCPeerConnectionHandlerClient
;
147 using blink::WebStorageNamespace
;
148 using blink::WebString
;
150 using blink::WebVector
;
156 bool g_sandbox_enabled
= true;
157 double g_test_device_light_data
= -1;
158 base::LazyInstance
<blink::WebDeviceMotionData
>::Leaky
159 g_test_device_motion_data
= LAZY_INSTANCE_INITIALIZER
;
160 base::LazyInstance
<blink::WebDeviceOrientationData
>::Leaky
161 g_test_device_orientation_data
= LAZY_INSTANCE_INITIALIZER
;
162 // Set in startListening() when running layout tests, unset in stopListening(),
164 blink::WebBatteryStatusListener
* g_test_battery_status_listener
= nullptr;
168 //------------------------------------------------------------------------------
170 class RendererBlinkPlatformImpl::MimeRegistry
171 : public SimpleWebMimeRegistryImpl
{
173 virtual blink::WebMimeRegistry::SupportsType
supportsMediaMIMEType(
174 const blink::WebString
& mime_type
,
175 const blink::WebString
& codecs
,
176 const blink::WebString
& key_system
);
177 virtual bool supportsMediaSourceMIMEType(const blink::WebString
& mime_type
,
178 const blink::WebString
& codecs
);
179 virtual blink::WebString
mimeTypeForExtension(
180 const blink::WebString
& file_extension
);
181 virtual blink::WebString
mimeTypeFromFile(
182 const blink::WebString
& file_path
);
185 class RendererBlinkPlatformImpl::FileUtilities
: public WebFileUtilitiesImpl
{
187 explicit FileUtilities(ThreadSafeSender
* sender
)
188 : thread_safe_sender_(sender
) {}
189 virtual bool getFileInfo(const WebString
& path
, WebFileInfo
& result
);
191 bool SendSyncMessageFromAnyThread(IPC::SyncMessage
* msg
) const;
192 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
195 #if !defined(OS_ANDROID) && !defined(OS_WIN)
196 class RendererBlinkPlatformImpl::SandboxSupport
197 : public blink::WebSandboxSupport
{
199 virtual ~SandboxSupport() {}
201 #if defined(OS_MACOSX)
202 virtual bool loadFont(
204 CGFontRef
* container
,
206 #elif defined(OS_POSIX)
207 virtual void getFallbackFontForCharacter(
208 blink::WebUChar32 character
,
209 const char* preferred_locale
,
210 blink::WebFallbackFont
* fallbackFont
);
211 virtual void getRenderStyleForStrike(
212 const char* family
, int sizeAndStyle
, blink::WebFontRenderStyle
* out
);
215 // WebKit likes to ask us for the correct font family to use for a set of
216 // unicode code points. It needs this information frequently so we cache it
218 base::Lock unicode_font_families_mutex_
;
219 std::map
<int32_t, blink::WebFallbackFont
> unicode_font_families_
;
222 #endif // !defined(OS_ANDROID) && !defined(OS_WIN)
224 //------------------------------------------------------------------------------
226 RendererBlinkPlatformImpl::RendererBlinkPlatformImpl(
227 scheduler::RendererScheduler
* renderer_scheduler
)
228 : BlinkPlatformImpl(renderer_scheduler
->DefaultTaskRunner()),
230 new scheduler::WebThreadImplForRendererScheduler(renderer_scheduler
)),
231 clipboard_delegate_(new RendererClipboardDelegate
),
232 clipboard_(new WebClipboardImpl(clipboard_delegate_
.get())),
233 mime_registry_(new RendererBlinkPlatformImpl::MimeRegistry
),
234 sudden_termination_disables_(0),
235 plugin_refresh_allowed_(true),
236 default_task_runner_(renderer_scheduler
->DefaultTaskRunner()),
237 web_scrollbar_behavior_(new WebScrollbarBehaviorImpl
) {
238 #if !defined(OS_ANDROID) && !defined(OS_WIN)
239 if (g_sandbox_enabled
&& sandboxEnabled()) {
240 sandbox_support_
.reset(new RendererBlinkPlatformImpl::SandboxSupport
);
242 DVLOG(1) << "Disabling sandbox support for testing.";
246 // ChildThread may not exist in some tests.
247 if (ChildThreadImpl::current()) {
248 sync_message_filter_
= ChildThreadImpl::current()->sync_message_filter();
249 thread_safe_sender_
= ChildThreadImpl::current()->thread_safe_sender();
250 quota_message_filter_
= ChildThreadImpl::current()->quota_message_filter();
251 blob_registry_
.reset(new WebBlobRegistryImpl(thread_safe_sender_
.get()));
252 web_idb_factory_
.reset(new WebIDBFactoryImpl(thread_safe_sender_
.get()));
253 web_database_observer_impl_
.reset(
254 new WebDatabaseObserverImpl(sync_message_filter_
.get()));
258 RendererBlinkPlatformImpl::~RendererBlinkPlatformImpl() {
259 WebFileSystemImpl::DeleteThreadSpecificInstance();
262 //------------------------------------------------------------------------------
264 blink::WebThread
* RendererBlinkPlatformImpl::currentThread() {
265 if (main_thread_
->isCurrentThread())
266 return main_thread_
.get();
267 return BlinkPlatformImpl::currentThread();
270 blink::WebClipboard
* RendererBlinkPlatformImpl::clipboard() {
271 blink::WebClipboard
* clipboard
=
272 GetContentClient()->renderer()->OverrideWebClipboard();
275 return clipboard_
.get();
278 blink::WebMimeRegistry
* RendererBlinkPlatformImpl::mimeRegistry() {
279 return mime_registry_
.get();
282 blink::WebFileUtilities
* RendererBlinkPlatformImpl::fileUtilities() {
283 if (!file_utilities_
) {
284 file_utilities_
.reset(new FileUtilities(thread_safe_sender_
.get()));
285 file_utilities_
->set_sandbox_enabled(sandboxEnabled());
287 return file_utilities_
.get();
290 blink::WebSandboxSupport
* RendererBlinkPlatformImpl::sandboxSupport() {
291 #if defined(OS_ANDROID) || defined(OS_WIN)
292 // These platforms do not require sandbox support.
295 return sandbox_support_
.get();
299 blink::WebCookieJar
* RendererBlinkPlatformImpl::cookieJar() {
300 NOTREACHED() << "Use WebFrameClient::cookieJar() instead!";
304 blink::WebThemeEngine
* RendererBlinkPlatformImpl::themeEngine() {
305 blink::WebThemeEngine
* theme_engine
=
306 GetContentClient()->renderer()->OverrideThemeEngine();
309 return BlinkPlatformImpl::themeEngine();
312 bool RendererBlinkPlatformImpl::sandboxEnabled() {
313 // As explained in Platform.h, this function is used to decide
314 // whether to allow file system operations to come out of WebKit or not.
315 // Even if the sandbox is disabled, there's no reason why the code should
316 // act any differently...unless we're in single process mode. In which
317 // case, we have no other choice. Platform.h discourages using
318 // this switch unless absolutely necessary, so hopefully we won't end up
319 // with too many code paths being different in single-process mode.
320 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
321 switches::kSingleProcess
);
324 unsigned long long RendererBlinkPlatformImpl::visitedLinkHash(
325 const char* canonical_url
,
327 return GetContentClient()->renderer()->VisitedLinkHash(canonical_url
, length
);
330 bool RendererBlinkPlatformImpl::isLinkVisited(unsigned long long link_hash
) {
331 return GetContentClient()->renderer()->IsLinkVisited(link_hash
);
334 void RendererBlinkPlatformImpl::createMessageChannel(
335 blink::WebMessagePortChannel
** channel1
,
336 blink::WebMessagePortChannel
** channel2
) {
337 WebMessagePortChannelImpl::CreatePair(
338 default_task_runner_
, channel1
, channel2
);
341 blink::WebPrescientNetworking
*
342 RendererBlinkPlatformImpl::prescientNetworking() {
343 return GetContentClient()->renderer()->GetPrescientNetworking();
346 void RendererBlinkPlatformImpl::cacheMetadata(const blink::WebURL
& url
,
350 // Let the browser know we generated cacheable metadata for this resource. The
351 // browser may cache it and return it on subsequent responses to speed
352 // the processing of this resource.
353 std::vector
<char> copy(data
, data
+ size
);
354 RenderThread::Get()->Send(new ViewHostMsg_DidGenerateCacheableMetadata(
355 url
, base::Time::FromInternalValue(response_time
), copy
));
358 WebString
RendererBlinkPlatformImpl::defaultLocale() {
359 return base::ASCIIToUTF16(RenderThread::Get()->GetLocale());
362 void RendererBlinkPlatformImpl::suddenTerminationChanged(bool enabled
) {
364 // We should not get more enables than disables, but we want it to be a
365 // non-fatal error if it does happen.
366 DCHECK_GT(sudden_termination_disables_
, 0);
367 sudden_termination_disables_
= std::max(sudden_termination_disables_
- 1,
369 if (sudden_termination_disables_
!= 0)
372 sudden_termination_disables_
++;
373 if (sudden_termination_disables_
!= 1)
377 RenderThread
* thread
= RenderThread::Get();
378 if (thread
) // NULL in unittests.
379 thread
->Send(new ViewHostMsg_SuddenTerminationChanged(enabled
));
382 WebStorageNamespace
* RendererBlinkPlatformImpl::createLocalStorageNamespace() {
383 return new WebStorageNamespaceImpl();
387 //------------------------------------------------------------------------------
389 WebIDBFactory
* RendererBlinkPlatformImpl::idbFactory() {
390 return web_idb_factory_
.get();
393 //------------------------------------------------------------------------------
395 blink::WebServiceWorkerCacheStorage
* RendererBlinkPlatformImpl::cacheStorage(
396 const WebString
& origin_identifier
) {
398 storage::GetOriginFromIdentifier(origin_identifier
.utf8());
399 return new WebServiceWorkerCacheStorageImpl(thread_safe_sender_
.get(),
403 //------------------------------------------------------------------------------
405 WebFileSystem
* RendererBlinkPlatformImpl::fileSystem() {
406 return WebFileSystemImpl::ThreadSpecificInstance(default_task_runner_
);
409 //------------------------------------------------------------------------------
411 WebMimeRegistry::SupportsType
412 RendererBlinkPlatformImpl::MimeRegistry::supportsMediaMIMEType(
413 const WebString
& mime_type
,
414 const WebString
& codecs
,
415 const WebString
& key_system
) {
416 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
417 // Not supporting the container is a flat-out no.
418 if (!media::IsSupportedMediaMimeType(mime_type_ascii
))
419 return IsNotSupported
;
421 if (!key_system
.isEmpty()) {
422 // Check whether the key system is supported with the mime_type and codecs.
424 // Chromium only supports ASCII parameters.
425 if (!base::IsStringASCII(key_system
))
426 return IsNotSupported
;
428 std::string key_system_ascii
=
429 media::GetUnprefixedKeySystemName(base::UTF16ToASCII(key_system
));
430 std::vector
<std::string
> strict_codecs
;
431 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, true);
433 if (!media::PrefixedIsSupportedKeySystemWithMediaMimeType(
434 mime_type_ascii
, strict_codecs
, key_system_ascii
)) {
435 return IsNotSupported
;
438 // Continue processing the mime_type and codecs.
441 // Check list of strict codecs to see if it is supported.
442 if (media::IsStrictMediaMimeType(mime_type_ascii
)) {
443 // Check if the codecs are a perfect match.
444 std::vector
<std::string
> strict_codecs
;
445 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, false);
446 return static_cast<WebMimeRegistry::SupportsType
> (
447 media::IsSupportedStrictMediaMimeType(mime_type_ascii
, strict_codecs
));
450 // If we don't recognize the codec, it's possible we support it.
451 std::vector
<std::string
> parsed_codecs
;
452 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codecs
, true);
453 if (!media::AreSupportedMediaCodecs(parsed_codecs
))
454 return MayBeSupported
;
456 // Otherwise we have a perfect match.
460 bool RendererBlinkPlatformImpl::MimeRegistry::supportsMediaSourceMIMEType(
461 const blink::WebString
& mime_type
,
462 const WebString
& codecs
) {
463 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
464 std::vector
<std::string
> parsed_codec_ids
;
465 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codec_ids
, false);
466 if (mime_type_ascii
.empty())
468 return media::StreamParserFactory::IsTypeSupported(
469 mime_type_ascii
, parsed_codec_ids
);
472 WebString
RendererBlinkPlatformImpl::MimeRegistry::mimeTypeForExtension(
473 const WebString
& file_extension
) {
474 if (IsPluginProcess())
475 return SimpleWebMimeRegistryImpl::mimeTypeForExtension(file_extension
);
477 // The sandbox restricts our access to the registry, so we need to proxy
478 // these calls over to the browser process.
479 std::string mime_type
;
480 RenderThread::Get()->Send(
481 new MimeRegistryMsg_GetMimeTypeFromExtension(
482 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
));
483 return base::ASCIIToUTF16(mime_type
);
486 WebString
RendererBlinkPlatformImpl::MimeRegistry::mimeTypeFromFile(
487 const WebString
& file_path
) {
488 if (IsPluginProcess())
489 return SimpleWebMimeRegistryImpl::mimeTypeFromFile(file_path
);
491 // The sandbox restricts our access to the registry, so we need to proxy
492 // these calls over to the browser process.
493 std::string mime_type
;
494 RenderThread::Get()->Send(new MimeRegistryMsg_GetMimeTypeFromFile(
495 base::FilePath::FromUTF16Unsafe(file_path
),
497 return base::ASCIIToUTF16(mime_type
);
500 //------------------------------------------------------------------------------
502 bool RendererBlinkPlatformImpl::FileUtilities::getFileInfo(
503 const WebString
& path
,
504 WebFileInfo
& web_file_info
) {
505 base::File::Info file_info
;
506 base::File::Error status
= base::File::FILE_ERROR_MAX
;
507 if (!SendSyncMessageFromAnyThread(new FileUtilitiesMsg_GetFileInfo(
508 base::FilePath::FromUTF16Unsafe(path
), &file_info
, &status
)) ||
509 status
!= base::File::FILE_OK
) {
512 FileInfoToWebFileInfo(file_info
, &web_file_info
);
513 web_file_info
.platformPath
= path
;
517 bool RendererBlinkPlatformImpl::FileUtilities::SendSyncMessageFromAnyThread(
518 IPC::SyncMessage
* msg
) const {
519 base::TimeTicks begin
= base::TimeTicks::Now();
520 const bool success
= thread_safe_sender_
->Send(msg
);
521 base::TimeDelta delta
= base::TimeTicks::Now() - begin
;
522 UMA_HISTOGRAM_TIMES("RendererSyncIPC.ElapsedTime", delta
);
526 //------------------------------------------------------------------------------
528 #if defined(OS_MACOSX)
530 bool RendererBlinkPlatformImpl::SandboxSupport::loadFont(NSFont
* src_font
,
533 uint32 font_data_size
;
534 FontDescriptor
src_font_descriptor(src_font
);
535 base::SharedMemoryHandle font_data
;
536 if (!RenderThread::Get()->Send(new ViewHostMsg_LoadFont(
537 src_font_descriptor
, &font_data_size
, &font_data
, font_id
))) {
543 if (font_data_size
== 0 || font_data
== base::SharedMemory::NULLHandle() ||
545 LOG(ERROR
) << "Bad response from ViewHostMsg_LoadFont() for " <<
546 src_font_descriptor
.font_name
;
552 // TODO(jeremy): Need to call back into WebKit to make sure that the font
553 // isn't already activated, based on the font id. If it's already
554 // activated, don't reactivate it here - crbug.com/72727 .
556 return FontLoader::CGFontRefFromBuffer(font_data
, font_data_size
, out
);
559 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
561 void RendererBlinkPlatformImpl::SandboxSupport::getFallbackFontForCharacter(
562 blink::WebUChar32 character
,
563 const char* preferred_locale
,
564 blink::WebFallbackFont
* fallbackFont
) {
565 base::AutoLock
lock(unicode_font_families_mutex_
);
566 const std::map
<int32_t, blink::WebFallbackFont
>::const_iterator iter
=
567 unicode_font_families_
.find(character
);
568 if (iter
!= unicode_font_families_
.end()) {
569 fallbackFont
->name
= iter
->second
.name
;
570 fallbackFont
->filename
= iter
->second
.filename
;
571 fallbackFont
->fontconfigInterfaceId
= iter
->second
.fontconfigInterfaceId
;
572 fallbackFont
->ttcIndex
= iter
->second
.ttcIndex
;
573 fallbackFont
->isBold
= iter
->second
.isBold
;
574 fallbackFont
->isItalic
= iter
->second
.isItalic
;
578 GetFallbackFontForCharacter(character
, preferred_locale
, fallbackFont
);
579 unicode_font_families_
.insert(std::make_pair(character
, *fallbackFont
));
582 void RendererBlinkPlatformImpl::SandboxSupport::getRenderStyleForStrike(
585 blink::WebFontRenderStyle
* out
) {
586 GetRenderStyleForStrike(family
, sizeAndStyle
, out
);
591 //------------------------------------------------------------------------------
593 Platform::FileHandle
RendererBlinkPlatformImpl::databaseOpenFile(
594 const WebString
& vfs_file_name
,
596 return DatabaseUtil::DatabaseOpenFile(
597 vfs_file_name
, desired_flags
, sync_message_filter_
.get());
600 int RendererBlinkPlatformImpl::databaseDeleteFile(
601 const WebString
& vfs_file_name
,
603 return DatabaseUtil::DatabaseDeleteFile(
604 vfs_file_name
, sync_dir
, sync_message_filter_
.get());
607 long RendererBlinkPlatformImpl::databaseGetFileAttributes(
608 const WebString
& vfs_file_name
) {
609 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name
,
610 sync_message_filter_
.get());
613 long long RendererBlinkPlatformImpl::databaseGetFileSize(
614 const WebString
& vfs_file_name
) {
615 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name
,
616 sync_message_filter_
.get());
619 long long RendererBlinkPlatformImpl::databaseGetSpaceAvailableForOrigin(
620 const WebString
& origin_identifier
) {
621 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier
,
622 sync_message_filter_
.get());
625 bool RendererBlinkPlatformImpl::databaseSetFileSize(
626 const WebString
& vfs_file_name
, long long size
) {
627 return DatabaseUtil::DatabaseSetFileSize(
628 vfs_file_name
, size
, sync_message_filter_
.get());
631 bool RendererBlinkPlatformImpl::canAccelerate2dCanvas() {
632 RenderThreadImpl
* thread
= RenderThreadImpl::current();
633 GpuChannelHost
* host
= thread
->EstablishGpuChannelSync(
634 CAUSE_FOR_GPU_LAUNCH_CANVAS_2D
);
638 return host
->gpu_info().SupportsAccelerated2dCanvas();
641 bool RendererBlinkPlatformImpl::isThreadedCompositingEnabled() {
642 RenderThreadImpl
* thread
= RenderThreadImpl::current();
643 // thread can be NULL in tests.
644 return thread
&& thread
->compositor_task_runner().get();
647 double RendererBlinkPlatformImpl::audioHardwareSampleRate() {
648 RenderThreadImpl
* thread
= RenderThreadImpl::current();
649 return thread
->GetAudioHardwareConfig()->GetOutputSampleRate();
652 size_t RendererBlinkPlatformImpl::audioHardwareBufferSize() {
653 RenderThreadImpl
* thread
= RenderThreadImpl::current();
654 return thread
->GetAudioHardwareConfig()->GetOutputBufferSize();
657 unsigned RendererBlinkPlatformImpl::audioHardwareOutputChannels() {
658 RenderThreadImpl
* thread
= RenderThreadImpl::current();
659 return thread
->GetAudioHardwareConfig()->GetOutputChannels();
662 WebDatabaseObserver
* RendererBlinkPlatformImpl::databaseObserver() {
663 return web_database_observer_impl_
.get();
666 WebAudioDevice
* RendererBlinkPlatformImpl::createAudioDevice(
668 unsigned input_channels
,
671 WebAudioDevice::RenderCallback
* callback
,
672 const blink::WebString
& input_device_id
) {
673 // Use a mock for testing.
674 blink::WebAudioDevice
* mock_device
=
675 GetContentClient()->renderer()->OverrideCreateAudioDevice(sample_rate
);
679 // The |channels| does not exactly identify the channel layout of the
680 // device. The switch statement below assigns a best guess to the channel
681 // layout based on number of channels.
682 // TODO(crogers): WebKit should give the channel layout instead of the hard
684 media::ChannelLayout layout
= media::CHANNEL_LAYOUT_UNSUPPORTED
;
687 layout
= media::CHANNEL_LAYOUT_MONO
;
690 layout
= media::CHANNEL_LAYOUT_STEREO
;
693 layout
= media::CHANNEL_LAYOUT_2_1
;
696 layout
= media::CHANNEL_LAYOUT_4_0
;
699 layout
= media::CHANNEL_LAYOUT_5_0
;
702 layout
= media::CHANNEL_LAYOUT_5_1
;
705 layout
= media::CHANNEL_LAYOUT_7_0
;
708 layout
= media::CHANNEL_LAYOUT_7_1
;
711 layout
= media::CHANNEL_LAYOUT_STEREO
;
715 if (input_device_id
.isNull() ||
716 !base::StringToInt(base::UTF16ToUTF8(input_device_id
), &session_id
)) {
717 if (input_channels
> 0)
718 DLOG(WARNING
) << "createAudioDevice(): request for audio input ignored";
723 media::AudioParameters
params(
724 media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
725 layout
, static_cast<int>(sample_rate
), 16, buffer_size
,
726 media::AudioParameters::NO_EFFECTS
);
728 return new RendererWebAudioDeviceImpl(params
, callback
, session_id
);
731 #if defined(OS_ANDROID)
732 bool RendererBlinkPlatformImpl::loadAudioResource(
733 blink::WebAudioBus
* destination_bus
,
734 const char* audio_file_data
,
736 return DecodeAudioFileData(destination_bus
,
739 thread_safe_sender_
);
742 bool RendererBlinkPlatformImpl::loadAudioResource(
743 blink::WebAudioBus
* destination_bus
,
744 const char* audio_file_data
,
746 return DecodeAudioFileData(
747 destination_bus
, audio_file_data
, data_size
);
749 #endif // defined(OS_ANDROID)
751 //------------------------------------------------------------------------------
753 blink::WebMIDIAccessor
* RendererBlinkPlatformImpl::createMIDIAccessor(
754 blink::WebMIDIAccessorClient
* client
) {
755 blink::WebMIDIAccessor
* accessor
=
756 GetContentClient()->renderer()->OverrideCreateMIDIAccessor(client
);
760 return new RendererWebMIDIAccessorImpl(client
);
763 void RendererBlinkPlatformImpl::getPluginList(
765 blink::WebPluginListBuilder
* builder
) {
766 #if defined(ENABLE_PLUGINS)
767 std::vector
<WebPluginInfo
> plugins
;
768 if (!plugin_refresh_allowed_
)
770 RenderThread::Get()->Send(
771 new ViewHostMsg_GetPlugins(refresh
, &plugins
));
772 for (size_t i
= 0; i
< plugins
.size(); ++i
) {
773 const WebPluginInfo
& plugin
= plugins
[i
];
776 plugin
.name
, plugin
.desc
,
777 plugin
.path
.BaseName().AsUTF16Unsafe());
779 for (size_t j
= 0; j
< plugin
.mime_types
.size(); ++j
) {
780 const WebPluginMimeType
& mime_type
= plugin
.mime_types
[j
];
782 builder
->addMediaTypeToLastPlugin(
783 WebString::fromUTF8(mime_type
.mime_type
), mime_type
.description
);
785 for (size_t k
= 0; k
< mime_type
.file_extensions
.size(); ++k
) {
786 builder
->addFileExtensionToLastMediaType(
787 WebString::fromUTF8(mime_type
.file_extensions
[k
]));
794 //------------------------------------------------------------------------------
796 blink::WebPublicSuffixList
* RendererBlinkPlatformImpl::publicSuffixList() {
797 return &public_suffix_list_
;
800 //------------------------------------------------------------------------------
802 blink::WebString
RendererBlinkPlatformImpl::signedPublicKeyAndChallengeString(
803 unsigned key_size_index
,
804 const blink::WebString
& challenge
,
805 const blink::WebURL
& url
) {
806 std::string signed_public_key
;
807 RenderThread::Get()->Send(new ViewHostMsg_Keygen(
808 static_cast<uint32
>(key_size_index
),
811 &signed_public_key
));
812 return WebString::fromUTF8(signed_public_key
);
815 //------------------------------------------------------------------------------
817 void RendererBlinkPlatformImpl::screenColorProfile(
818 WebVector
<char>* to_profile
) {
820 // On Windows screen color profile is only available in the browser.
821 std::vector
<char> profile
;
822 // This Send() can be called from any impl-side thread. Use a thread
823 // safe send to avoid crashing trying to access RenderThread::Get(),
824 // which is not accessible from arbitrary threads.
825 thread_safe_sender_
->Send(
826 new ViewHostMsg_GetMonitorColorProfile(&profile
));
827 *to_profile
= profile
;
829 // On other platforms, the primary monitor color profile can be read
831 gfx::ColorProfile profile
;
832 *to_profile
= profile
.profile();
836 //------------------------------------------------------------------------------
838 blink::WebScrollbarBehavior
* RendererBlinkPlatformImpl::scrollbarBehavior() {
839 return web_scrollbar_behavior_
.get();
842 //------------------------------------------------------------------------------
844 WebBlobRegistry
* RendererBlinkPlatformImpl::blobRegistry() {
845 // blob_registry_ can be NULL when running some tests.
846 return blob_registry_
.get();
849 //------------------------------------------------------------------------------
851 void RendererBlinkPlatformImpl::sampleGamepads(WebGamepads
& gamepads
) {
852 PlatformEventObserverBase
* observer
=
853 platform_event_observers_
.Lookup(blink::WebPlatformEventGamepad
);
856 static_cast<RendererGamepadProvider
*>(observer
)->SampleGamepads(gamepads
);
859 //------------------------------------------------------------------------------
861 WebRTCPeerConnectionHandler
*
862 RendererBlinkPlatformImpl::createRTCPeerConnectionHandler(
863 WebRTCPeerConnectionHandlerClient
* client
) {
864 RenderThreadImpl
* render_thread
= RenderThreadImpl::current();
865 DCHECK(render_thread
);
869 #if defined(ENABLE_WEBRTC)
870 WebRTCPeerConnectionHandler
* peer_connection_handler
=
871 GetContentClient()->renderer()->OverrideCreateWebRTCPeerConnectionHandler(
873 if (peer_connection_handler
)
874 return peer_connection_handler
;
876 PeerConnectionDependencyFactory
* rtc_dependency_factory
=
877 render_thread
->GetPeerConnectionDependencyFactory();
878 return rtc_dependency_factory
->CreateRTCPeerConnectionHandler(client
);
881 #endif // defined(ENABLE_WEBRTC)
884 //------------------------------------------------------------------------------
886 WebMediaStreamCenter
* RendererBlinkPlatformImpl::createMediaStreamCenter(
887 WebMediaStreamCenterClient
* client
) {
888 RenderThreadImpl
* render_thread
= RenderThreadImpl::current();
889 DCHECK(render_thread
);
892 return render_thread
->CreateMediaStreamCenter(client
);
896 bool RendererBlinkPlatformImpl::SetSandboxEnabledForTesting(bool enable
) {
897 bool was_enabled
= g_sandbox_enabled
;
898 g_sandbox_enabled
= enable
;
902 //------------------------------------------------------------------------------
904 blink::WebSpeechSynthesizer
* RendererBlinkPlatformImpl::createSpeechSynthesizer(
905 blink::WebSpeechSynthesizerClient
* client
) {
906 return GetContentClient()->renderer()->OverrideSpeechSynthesizer(client
);
909 //------------------------------------------------------------------------------
911 bool RendererBlinkPlatformImpl::processMemorySizesInBytes(
912 size_t* private_bytes
,
913 size_t* shared_bytes
) {
914 content::RenderThread::Get()->Send(
915 new ViewHostMsg_GetProcessMemorySizes(private_bytes
, shared_bytes
));
919 //------------------------------------------------------------------------------
921 blink::WebGraphicsContext3D
*
922 RendererBlinkPlatformImpl::createOffscreenGraphicsContext3D(
923 const blink::WebGraphicsContext3D::Attributes
& attributes
) {
924 return createOffscreenGraphicsContext3D(attributes
, NULL
);
927 blink::WebGraphicsContext3D
*
928 RendererBlinkPlatformImpl::createOffscreenGraphicsContext3D(
929 const blink::WebGraphicsContext3D::Attributes
& attributes
,
930 blink::WebGraphicsContext3D
* share_context
) {
931 return createOffscreenGraphicsContext3D(attributes
, share_context
, NULL
);
934 blink::WebGraphicsContext3D
*
935 RendererBlinkPlatformImpl::createOffscreenGraphicsContext3D(
936 const blink::WebGraphicsContext3D::Attributes
& attributes
,
937 blink::WebGraphicsContext3D
* share_context
,
938 blink::WebGLInfo
* gl_info
) {
939 if (!RenderThreadImpl::current())
942 #if defined(OS_ANDROID)
943 if (SynchronousCompositorFactory
* factory
=
944 SynchronousCompositorFactory::GetInstance()) {
945 scoped_ptr
<gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl
>
947 factory
->CreateOffscreenGraphicsContext3D(attributes
));
948 if (!in_process_context
||
949 !in_process_context
->InitializeOnCurrentThread())
951 return in_process_context
.release();
955 scoped_refptr
<GpuChannelHost
> gpu_channel_host(
956 RenderThreadImpl::current()->EstablishGpuChannelSync(
957 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
));
959 if (gpu_channel_host
.get() && gl_info
) {
960 const gpu::GPUInfo
& gpu_info
= gpu_channel_host
->gpu_info();
961 switch (gpu_info
.context_info_state
) {
962 case gpu::kCollectInfoSuccess
:
963 case gpu::kCollectInfoNonFatalFailure
:
964 gl_info
->vendorInfo
.assign(
965 blink::WebString::fromUTF8(gpu_info
.gl_vendor
));
966 gl_info
->rendererInfo
.assign(
967 blink::WebString::fromUTF8(gpu_info
.gl_renderer
));
968 gl_info
->driverVersion
.assign(
969 blink::WebString::fromUTF8(gpu_info
.driver_version
));
970 gl_info
->vendorId
= gpu_info
.gpu
.vendor_id
;
971 gl_info
->deviceId
= gpu_info
.gpu
.device_id
;
973 case gpu::kCollectInfoFatalFailure
:
974 case gpu::kCollectInfoNone
:
975 gl_info
->contextInfoCollectionFailure
.assign(blink::WebString::fromUTF8(
976 "GPUInfoCollectionFailure: GPU initialization Failed. GPU "
977 "Info not Collected."));
984 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits
;
985 bool lose_context_when_out_of_memory
= false;
986 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context(
987 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
988 gpu_channel_host
.get(),
990 lose_context_when_out_of_memory
,
991 GURL(attributes
.topDocumentURL
),
993 static_cast<WebGraphicsContext3DCommandBufferImpl
*>(share_context
)));
995 // Most likely the GPU process exited and the attempt to reconnect to it
996 // failed. Need to try to restore the context again later.
997 if (!context
|| !context
->InitializeOnCurrentThread())
999 return context
.release();
1002 //------------------------------------------------------------------------------
1004 blink::WebGraphicsContext3DProvider
*
1005 RendererBlinkPlatformImpl::createSharedOffscreenGraphicsContext3DProvider() {
1006 scoped_refptr
<cc_blink::ContextProviderWebContext
> provider
=
1007 RenderThreadImpl::current()->SharedMainThreadContextProvider();
1008 if (!provider
.get())
1010 return new WebGraphicsContext3DProviderImpl(provider
);
1013 //------------------------------------------------------------------------------
1015 blink::WebCompositorSupport
* RendererBlinkPlatformImpl::compositorSupport() {
1016 return &compositor_support_
;
1019 //------------------------------------------------------------------------------
1021 blink::WebString
RendererBlinkPlatformImpl::convertIDNToUnicode(
1022 const blink::WebString
& host
,
1023 const blink::WebString
& languages
) {
1024 return net::IDNToUnicode(host
.utf8(), languages
.utf8());
1027 //------------------------------------------------------------------------------
1029 void RendererBlinkPlatformImpl::recordRappor(const char* metric
,
1030 const blink::WebString
& sample
) {
1031 GetContentClient()->renderer()->RecordRappor(metric
, sample
.utf8());
1034 void RendererBlinkPlatformImpl::recordRapporURL(const char* metric
,
1035 const blink::WebURL
& url
) {
1036 GetContentClient()->renderer()->RecordRapporURL(metric
, url
);
1039 //------------------------------------------------------------------------------
1042 void RendererBlinkPlatformImpl::SetMockDeviceLightDataForTesting(double data
) {
1043 g_test_device_light_data
= data
;
1046 //------------------------------------------------------------------------------
1049 void RendererBlinkPlatformImpl::SetMockDeviceMotionDataForTesting(
1050 const blink::WebDeviceMotionData
& data
) {
1051 g_test_device_motion_data
.Get() = data
;
1054 //------------------------------------------------------------------------------
1057 void RendererBlinkPlatformImpl::SetMockDeviceOrientationDataForTesting(
1058 const blink::WebDeviceOrientationData
& data
) {
1059 g_test_device_orientation_data
.Get() = data
;
1062 //------------------------------------------------------------------------------
1064 void RendererBlinkPlatformImpl::vibrate(unsigned int milliseconds
) {
1065 GetConnectedVibrationManagerService()->Vibrate(
1066 base::checked_cast
<int64
>(milliseconds
));
1067 vibration_manager_
.reset();
1070 void RendererBlinkPlatformImpl::cancelVibration() {
1071 GetConnectedVibrationManagerService()->Cancel();
1072 vibration_manager_
.reset();
1075 device::VibrationManagerPtr
&
1076 RendererBlinkPlatformImpl::GetConnectedVibrationManagerService() {
1077 if (!vibration_manager_
) {
1078 RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService(
1079 mojo::GetProxy(&vibration_manager_
));
1081 return vibration_manager_
;
1084 //------------------------------------------------------------------------------
1087 PlatformEventObserverBase
*
1088 RendererBlinkPlatformImpl::CreatePlatformEventObserverFromType(
1089 blink::WebPlatformEventType type
) {
1090 RenderThread
* thread
= RenderThreadImpl::current();
1092 // When running layout tests, those observers should not listen to the actual
1093 // hardware changes. In order to make that happen, they will receive a null
1095 if (thread
&& RenderThreadImpl::current()->layout_test_mode())
1099 case blink::WebPlatformEventDeviceMotion
:
1100 return new DeviceMotionEventPump(thread
);
1101 case blink::WebPlatformEventDeviceOrientation
:
1102 return new DeviceOrientationEventPump(thread
);
1103 case blink::WebPlatformEventDeviceLight
:
1104 return new DeviceLightEventPump(thread
);
1105 case blink::WebPlatformEventGamepad
:
1106 return new GamepadSharedMemoryReader(thread
);
1107 case blink::WebPlatformEventScreenOrientation
:
1108 return new ScreenOrientationObserver();
1110 // A default statement is required to prevent compilation errors when
1111 // Blink adds a new type.
1112 DVLOG(1) << "RendererBlinkPlatformImpl::startListening() with "
1119 void RendererBlinkPlatformImpl::SetPlatformEventObserverForTesting(
1120 blink::WebPlatformEventType type
,
1121 scoped_ptr
<PlatformEventObserverBase
> observer
) {
1122 DCHECK(type
!= blink::WebPlatformEventBattery
);
1124 if (platform_event_observers_
.Lookup(type
))
1125 platform_event_observers_
.Remove(type
);
1126 platform_event_observers_
.AddWithID(observer
.release(), type
);
1129 void RendererBlinkPlatformImpl::startListening(
1130 blink::WebPlatformEventType type
,
1131 blink::WebPlatformEventListener
* listener
) {
1132 if (type
== blink::WebPlatformEventBattery
) {
1133 if (RenderThreadImpl::current() &&
1134 RenderThreadImpl::current()->layout_test_mode()) {
1135 g_test_battery_status_listener
=
1136 static_cast<blink::WebBatteryStatusListener
*>(listener
);
1138 battery_status_dispatcher_
.reset(new BatteryStatusDispatcher(
1139 static_cast<blink::WebBatteryStatusListener
*>(listener
)));
1144 PlatformEventObserverBase
* observer
= platform_event_observers_
.Lookup(type
);
1146 observer
= CreatePlatformEventObserverFromType(type
);
1149 platform_event_observers_
.AddWithID(observer
, static_cast<int32
>(type
));
1151 observer
->Start(listener
);
1153 // Device events (motion, orientation and light) expect to get an event fired
1154 // as soon as a listener is registered if a fake data was passed before.
1155 // TODO(mlamouri,timvolodine): make those send mock values directly instead of
1156 // using this broken pattern.
1157 if (RenderThreadImpl::current() &&
1158 RenderThreadImpl::current()->layout_test_mode() &&
1159 (type
== blink::WebPlatformEventDeviceMotion
||
1160 type
== blink::WebPlatformEventDeviceOrientation
||
1161 type
== blink::WebPlatformEventDeviceLight
)) {
1162 SendFakeDeviceEventDataForTesting(type
);
1166 void RendererBlinkPlatformImpl::SendFakeDeviceEventDataForTesting(
1167 blink::WebPlatformEventType type
) {
1168 PlatformEventObserverBase
* observer
= platform_event_observers_
.Lookup(type
);
1174 case blink::WebPlatformEventDeviceMotion
:
1175 if (!(g_test_device_motion_data
== 0))
1176 data
= &g_test_device_motion_data
.Get();
1178 case blink::WebPlatformEventDeviceOrientation
:
1179 if (!(g_test_device_orientation_data
== 0))
1180 data
= &g_test_device_orientation_data
.Get();
1182 case blink::WebPlatformEventDeviceLight
:
1183 if (g_test_device_light_data
>= 0)
1184 data
= &g_test_device_light_data
;
1194 base::ThreadTaskRunnerHandle::Get()->PostTask(
1195 FROM_HERE
, base::Bind(&PlatformEventObserverBase::SendFakeDataForTesting
,
1196 base::Unretained(observer
), data
));
1199 void RendererBlinkPlatformImpl::stopListening(
1200 blink::WebPlatformEventType type
) {
1201 if (type
== blink::WebPlatformEventBattery
) {
1202 g_test_battery_status_listener
= nullptr;
1203 battery_status_dispatcher_
.reset();
1207 PlatformEventObserverBase
* observer
= platform_event_observers_
.Lookup(type
);
1213 //------------------------------------------------------------------------------
1215 void RendererBlinkPlatformImpl::queryStorageUsageAndQuota(
1216 const blink::WebURL
& storage_partition
,
1217 blink::WebStorageQuotaType type
,
1218 blink::WebStorageQuotaCallbacks callbacks
) {
1219 if (!thread_safe_sender_
.get() || !quota_message_filter_
.get())
1221 QuotaDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get(),
1222 quota_message_filter_
.get())
1223 ->QueryStorageUsageAndQuota(
1225 static_cast<storage::StorageType
>(type
),
1226 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks
));
1229 //------------------------------------------------------------------------------
1231 void RendererBlinkPlatformImpl::MockBatteryStatusChangedForTesting(
1232 const blink::WebBatteryStatus
& status
) {
1233 if (!g_test_battery_status_listener
)
1235 g_test_battery_status_listener
->updateBatteryStatus(status
);
1238 } // namespace content