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 "components/url_formatter/url_formatter.h"
25 #include "content/child/database_util.h"
26 #include "content/child/file_info_util.h"
27 #include "content/child/fileapi/webfilesystem_impl.h"
28 #include "content/child/indexed_db/webidbfactory_impl.h"
29 #include "content/child/npapi/npobject_util.h"
30 #include "content/child/quota_dispatcher.h"
31 #include "content/child/quota_message_filter.h"
32 #include "content/child/simple_webmimeregistry_impl.h"
33 #include "content/child/thread_safe_sender.h"
34 #include "content/child/web_database_observer_impl.h"
35 #include "content/child/webblobregistry_impl.h"
36 #include "content/child/webfileutilities_impl.h"
37 #include "content/child/webmessageportchannel_impl.h"
38 #include "content/common/file_utilities_messages.h"
39 #include "content/common/gpu/client/context_provider_command_buffer.h"
40 #include "content/common/gpu/client/gpu_channel_host.h"
41 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
42 #include "content/common/gpu/gpu_process_launch_causes.h"
43 #include "content/common/mime_registry_messages.h"
44 #include "content/common/view_messages.h"
45 #include "content/public/common/content_switches.h"
46 #include "content/public/common/service_registry.h"
47 #include "content/public/common/webplugininfo.h"
48 #include "content/public/renderer/content_renderer_client.h"
49 #include "content/renderer/battery_status/battery_status_dispatcher.h"
50 #include "content/renderer/cache_storage/webserviceworkercachestorage_impl.h"
51 #include "content/renderer/device_sensors/device_light_event_pump.h"
52 #include "content/renderer/device_sensors/device_motion_event_pump.h"
53 #include "content/renderer/device_sensors/device_orientation_event_pump.h"
54 #include "content/renderer/dom_storage/webstoragenamespace_impl.h"
55 #include "content/renderer/gamepad_shared_memory_reader.h"
56 #include "content/renderer/media/audio_decoder.h"
57 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
58 #include "content/renderer/media/renderer_webmidiaccessor_impl.h"
59 #include "content/renderer/render_thread_impl.h"
60 #include "content/renderer/renderer_clipboard_delegate.h"
61 #include "content/renderer/screen_orientation/screen_orientation_observer.h"
62 #include "content/renderer/webclipboard_impl.h"
63 #include "content/renderer/webgraphicscontext3d_provider_impl.h"
64 #include "content/renderer/webpublicsuffixlist_impl.h"
65 #include "gpu/config/gpu_info.h"
66 #include "ipc/ipc_sync_message_filter.h"
67 #include "media/audio/audio_output_device.h"
68 #include "media/base/audio_hardware_config.h"
69 #include "media/base/key_systems.h"
70 #include "media/base/mime_util.h"
71 #include "media/blink/webcontentdecryptionmodule_impl.h"
72 #include "media/filters/stream_parser_factory.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(
430 base::StringPiece16(key_system
)));
431 std::vector
<std::string
> strict_codecs
;
432 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, true);
434 if (!media::PrefixedIsSupportedKeySystemWithMediaMimeType(
435 mime_type_ascii
, strict_codecs
, key_system_ascii
)) {
436 return IsNotSupported
;
439 // Continue processing the mime_type and codecs.
442 // Check list of strict codecs to see if it is supported.
443 if (media::IsStrictMediaMimeType(mime_type_ascii
)) {
444 // Check if the codecs are a perfect match.
445 std::vector
<std::string
> strict_codecs
;
446 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, false);
447 return static_cast<WebMimeRegistry::SupportsType
> (
448 media::IsSupportedStrictMediaMimeType(mime_type_ascii
, strict_codecs
));
451 // If we don't recognize the codec, it's possible we support it.
452 std::vector
<std::string
> parsed_codecs
;
453 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codecs
, true);
454 if (!media::AreSupportedMediaCodecs(parsed_codecs
))
455 return MayBeSupported
;
457 // Otherwise we have a perfect match.
461 bool RendererBlinkPlatformImpl::MimeRegistry::supportsMediaSourceMIMEType(
462 const blink::WebString
& mime_type
,
463 const WebString
& codecs
) {
464 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
465 std::vector
<std::string
> parsed_codec_ids
;
466 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codec_ids
, false);
467 if (mime_type_ascii
.empty())
469 return media::StreamParserFactory::IsTypeSupported(
470 mime_type_ascii
, parsed_codec_ids
);
473 WebString
RendererBlinkPlatformImpl::MimeRegistry::mimeTypeForExtension(
474 const WebString
& file_extension
) {
475 if (IsPluginProcess())
476 return SimpleWebMimeRegistryImpl::mimeTypeForExtension(file_extension
);
478 // The sandbox restricts our access to the registry, so we need to proxy
479 // these calls over to the browser process.
480 std::string mime_type
;
481 RenderThread::Get()->Send(
482 new MimeRegistryMsg_GetMimeTypeFromExtension(
483 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
));
484 return base::ASCIIToUTF16(mime_type
);
487 WebString
RendererBlinkPlatformImpl::MimeRegistry::mimeTypeFromFile(
488 const WebString
& file_path
) {
489 if (IsPluginProcess())
490 return SimpleWebMimeRegistryImpl::mimeTypeFromFile(file_path
);
492 // The sandbox restricts our access to the registry, so we need to proxy
493 // these calls over to the browser process.
494 std::string mime_type
;
495 RenderThread::Get()->Send(new MimeRegistryMsg_GetMimeTypeFromFile(
496 base::FilePath::FromUTF16Unsafe(file_path
),
498 return base::ASCIIToUTF16(mime_type
);
501 //------------------------------------------------------------------------------
503 bool RendererBlinkPlatformImpl::FileUtilities::getFileInfo(
504 const WebString
& path
,
505 WebFileInfo
& web_file_info
) {
506 base::File::Info file_info
;
507 base::File::Error status
= base::File::FILE_ERROR_MAX
;
508 if (!SendSyncMessageFromAnyThread(new FileUtilitiesMsg_GetFileInfo(
509 base::FilePath::FromUTF16Unsafe(path
), &file_info
, &status
)) ||
510 status
!= base::File::FILE_OK
) {
513 FileInfoToWebFileInfo(file_info
, &web_file_info
);
514 web_file_info
.platformPath
= path
;
518 bool RendererBlinkPlatformImpl::FileUtilities::SendSyncMessageFromAnyThread(
519 IPC::SyncMessage
* msg
) const {
520 base::TimeTicks begin
= base::TimeTicks::Now();
521 const bool success
= thread_safe_sender_
->Send(msg
);
522 base::TimeDelta delta
= base::TimeTicks::Now() - begin
;
523 UMA_HISTOGRAM_TIMES("RendererSyncIPC.ElapsedTime", delta
);
527 //------------------------------------------------------------------------------
529 #if defined(OS_MACOSX)
531 bool RendererBlinkPlatformImpl::SandboxSupport::loadFont(NSFont
* src_font
,
534 uint32 font_data_size
;
535 FontDescriptor
src_font_descriptor(src_font
);
536 base::SharedMemoryHandle font_data
;
537 if (!RenderThread::Get()->Send(new ViewHostMsg_LoadFont(
538 src_font_descriptor
, &font_data_size
, &font_data
, font_id
))) {
544 if (font_data_size
== 0 || font_data
== base::SharedMemory::NULLHandle() ||
546 LOG(ERROR
) << "Bad response from ViewHostMsg_LoadFont() for " <<
547 src_font_descriptor
.font_name
;
553 // TODO(jeremy): Need to call back into WebKit to make sure that the font
554 // isn't already activated, based on the font id. If it's already
555 // activated, don't reactivate it here - crbug.com/72727 .
557 return FontLoader::CGFontRefFromBuffer(font_data
, font_data_size
, out
);
560 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
562 void RendererBlinkPlatformImpl::SandboxSupport::getFallbackFontForCharacter(
563 blink::WebUChar32 character
,
564 const char* preferred_locale
,
565 blink::WebFallbackFont
* fallbackFont
) {
566 base::AutoLock
lock(unicode_font_families_mutex_
);
567 const std::map
<int32_t, blink::WebFallbackFont
>::const_iterator iter
=
568 unicode_font_families_
.find(character
);
569 if (iter
!= unicode_font_families_
.end()) {
570 fallbackFont
->name
= iter
->second
.name
;
571 fallbackFont
->filename
= iter
->second
.filename
;
572 fallbackFont
->fontconfigInterfaceId
= iter
->second
.fontconfigInterfaceId
;
573 fallbackFont
->ttcIndex
= iter
->second
.ttcIndex
;
574 fallbackFont
->isBold
= iter
->second
.isBold
;
575 fallbackFont
->isItalic
= iter
->second
.isItalic
;
579 GetFallbackFontForCharacter(character
, preferred_locale
, fallbackFont
);
580 unicode_font_families_
.insert(std::make_pair(character
, *fallbackFont
));
583 void RendererBlinkPlatformImpl::SandboxSupport::getRenderStyleForStrike(
586 blink::WebFontRenderStyle
* out
) {
587 GetRenderStyleForStrike(family
, sizeAndStyle
, out
);
592 //------------------------------------------------------------------------------
594 Platform::FileHandle
RendererBlinkPlatformImpl::databaseOpenFile(
595 const WebString
& vfs_file_name
,
597 return DatabaseUtil::DatabaseOpenFile(
598 vfs_file_name
, desired_flags
, sync_message_filter_
.get());
601 int RendererBlinkPlatformImpl::databaseDeleteFile(
602 const WebString
& vfs_file_name
,
604 return DatabaseUtil::DatabaseDeleteFile(
605 vfs_file_name
, sync_dir
, sync_message_filter_
.get());
608 long RendererBlinkPlatformImpl::databaseGetFileAttributes(
609 const WebString
& vfs_file_name
) {
610 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name
,
611 sync_message_filter_
.get());
614 long long RendererBlinkPlatformImpl::databaseGetFileSize(
615 const WebString
& vfs_file_name
) {
616 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name
,
617 sync_message_filter_
.get());
620 long long RendererBlinkPlatformImpl::databaseGetSpaceAvailableForOrigin(
621 const WebString
& origin_identifier
) {
622 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier
,
623 sync_message_filter_
.get());
626 bool RendererBlinkPlatformImpl::databaseSetFileSize(
627 const WebString
& vfs_file_name
, long long size
) {
628 return DatabaseUtil::DatabaseSetFileSize(
629 vfs_file_name
, size
, sync_message_filter_
.get());
632 bool RendererBlinkPlatformImpl::canAccelerate2dCanvas() {
633 #if defined(OS_ANDROID)
634 SynchronousCompositorFactory
* factory
=
635 SynchronousCompositorFactory::GetInstance();
636 if (factory
&& factory
->OverrideWithFactory()) {
637 return factory
->GetGPUInfo().SupportsAccelerated2dCanvas();
641 RenderThreadImpl
* thread
= RenderThreadImpl::current();
642 GpuChannelHost
* host
= thread
->EstablishGpuChannelSync(
643 CAUSE_FOR_GPU_LAUNCH_CANVAS_2D
);
647 return host
->gpu_info().SupportsAccelerated2dCanvas();
650 bool RendererBlinkPlatformImpl::isThreadedCompositingEnabled() {
651 RenderThreadImpl
* thread
= RenderThreadImpl::current();
652 // thread can be NULL in tests.
653 return thread
&& thread
->compositor_task_runner().get();
656 bool RendererBlinkPlatformImpl::isThreadedAnimationEnabled() {
657 RenderThreadImpl
* thread
= RenderThreadImpl::current();
658 return thread
? thread
->IsThreadedAnimationEnabled() : true;
661 double RendererBlinkPlatformImpl::audioHardwareSampleRate() {
662 RenderThreadImpl
* thread
= RenderThreadImpl::current();
663 return thread
->GetAudioHardwareConfig()->GetOutputSampleRate();
666 size_t RendererBlinkPlatformImpl::audioHardwareBufferSize() {
667 RenderThreadImpl
* thread
= RenderThreadImpl::current();
668 return thread
->GetAudioHardwareConfig()->GetOutputBufferSize();
671 unsigned RendererBlinkPlatformImpl::audioHardwareOutputChannels() {
672 RenderThreadImpl
* thread
= RenderThreadImpl::current();
673 return thread
->GetAudioHardwareConfig()->GetOutputChannels();
676 WebDatabaseObserver
* RendererBlinkPlatformImpl::databaseObserver() {
677 return web_database_observer_impl_
.get();
680 WebAudioDevice
* RendererBlinkPlatformImpl::createAudioDevice(
682 unsigned input_channels
,
685 WebAudioDevice::RenderCallback
* callback
,
686 const blink::WebString
& input_device_id
) {
687 // Use a mock for testing.
688 blink::WebAudioDevice
* mock_device
=
689 GetContentClient()->renderer()->OverrideCreateAudioDevice(sample_rate
);
693 // The |channels| does not exactly identify the channel layout of the
694 // device. The switch statement below assigns a best guess to the channel
695 // layout based on number of channels.
696 media::ChannelLayout layout
= media::CHANNEL_LAYOUT_UNSUPPORTED
;
699 layout
= media::CHANNEL_LAYOUT_MONO
;
702 layout
= media::CHANNEL_LAYOUT_STEREO
;
705 layout
= media::CHANNEL_LAYOUT_2_1
;
708 layout
= media::CHANNEL_LAYOUT_4_0
;
711 layout
= media::CHANNEL_LAYOUT_5_0
;
714 layout
= media::CHANNEL_LAYOUT_5_1
;
717 layout
= media::CHANNEL_LAYOUT_7_0
;
720 layout
= media::CHANNEL_LAYOUT_7_1
;
723 // If the layout is not supported (more than 9 channels), falls back to
725 layout
= media::CHANNEL_LAYOUT_DISCRETE
;
729 if (input_device_id
.isNull() ||
730 !base::StringToInt(base::UTF16ToUTF8(
731 base::StringPiece16(input_device_id
)), &session_id
)) {
732 if (input_channels
> 0)
733 DLOG(WARNING
) << "createAudioDevice(): request for audio input ignored";
738 // For CHANNEL_LAYOUT_DISCRETE, pass the explicit channel count along with
739 // the channel layout when creating an |AudioParameters| object.
740 media::AudioParameters
params(
741 media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
742 layout
, channels
, static_cast<int>(sample_rate
), 16, buffer_size
,
743 media::AudioParameters::NO_EFFECTS
);
745 return new RendererWebAudioDeviceImpl(params
, callback
, session_id
);
748 #if defined(OS_ANDROID)
749 bool RendererBlinkPlatformImpl::loadAudioResource(
750 blink::WebAudioBus
* destination_bus
,
751 const char* audio_file_data
,
753 return DecodeAudioFileData(destination_bus
,
756 thread_safe_sender_
);
759 bool RendererBlinkPlatformImpl::loadAudioResource(
760 blink::WebAudioBus
* destination_bus
,
761 const char* audio_file_data
,
763 return DecodeAudioFileData(
764 destination_bus
, audio_file_data
, data_size
);
766 #endif // defined(OS_ANDROID)
768 //------------------------------------------------------------------------------
770 blink::WebMIDIAccessor
* RendererBlinkPlatformImpl::createMIDIAccessor(
771 blink::WebMIDIAccessorClient
* client
) {
772 blink::WebMIDIAccessor
* accessor
=
773 GetContentClient()->renderer()->OverrideCreateMIDIAccessor(client
);
777 return new RendererWebMIDIAccessorImpl(client
);
780 void RendererBlinkPlatformImpl::getPluginList(
782 blink::WebPluginListBuilder
* builder
) {
783 #if defined(ENABLE_PLUGINS)
784 std::vector
<WebPluginInfo
> plugins
;
785 if (!plugin_refresh_allowed_
)
787 RenderThread::Get()->Send(
788 new ViewHostMsg_GetPlugins(refresh
, &plugins
));
789 for (size_t i
= 0; i
< plugins
.size(); ++i
) {
790 const WebPluginInfo
& plugin
= plugins
[i
];
793 plugin
.name
, plugin
.desc
,
794 plugin
.path
.BaseName().AsUTF16Unsafe());
796 for (size_t j
= 0; j
< plugin
.mime_types
.size(); ++j
) {
797 const WebPluginMimeType
& mime_type
= plugin
.mime_types
[j
];
799 builder
->addMediaTypeToLastPlugin(
800 WebString::fromUTF8(mime_type
.mime_type
), mime_type
.description
);
802 for (size_t k
= 0; k
< mime_type
.file_extensions
.size(); ++k
) {
803 builder
->addFileExtensionToLastMediaType(
804 WebString::fromUTF8(mime_type
.file_extensions
[k
]));
811 //------------------------------------------------------------------------------
813 blink::WebPublicSuffixList
* RendererBlinkPlatformImpl::publicSuffixList() {
814 return &public_suffix_list_
;
817 //------------------------------------------------------------------------------
819 blink::WebString
RendererBlinkPlatformImpl::signedPublicKeyAndChallengeString(
820 unsigned key_size_index
,
821 const blink::WebString
& challenge
,
822 const blink::WebURL
& url
) {
823 std::string signed_public_key
;
824 RenderThread::Get()->Send(new ViewHostMsg_Keygen(
825 static_cast<uint32
>(key_size_index
),
828 &signed_public_key
));
829 return WebString::fromUTF8(signed_public_key
);
832 //------------------------------------------------------------------------------
834 void RendererBlinkPlatformImpl::screenColorProfile(
835 WebVector
<char>* to_profile
) {
837 // On Windows screen color profile is only available in the browser.
838 std::vector
<char> profile
;
839 // This Send() can be called from any impl-side thread. Use a thread
840 // safe send to avoid crashing trying to access RenderThread::Get(),
841 // which is not accessible from arbitrary threads.
842 thread_safe_sender_
->Send(
843 new ViewHostMsg_GetMonitorColorProfile(&profile
));
844 *to_profile
= profile
;
846 // On other platforms, the primary monitor color profile can be read
848 gfx::ColorProfile profile
;
849 *to_profile
= profile
.profile();
853 //------------------------------------------------------------------------------
855 blink::WebScrollbarBehavior
* RendererBlinkPlatformImpl::scrollbarBehavior() {
856 return web_scrollbar_behavior_
.get();
859 //------------------------------------------------------------------------------
861 WebBlobRegistry
* RendererBlinkPlatformImpl::blobRegistry() {
862 // blob_registry_ can be NULL when running some tests.
863 return blob_registry_
.get();
866 //------------------------------------------------------------------------------
868 void RendererBlinkPlatformImpl::sampleGamepads(WebGamepads
& gamepads
) {
869 PlatformEventObserverBase
* observer
=
870 platform_event_observers_
.Lookup(blink::WebPlatformEventGamepad
);
873 static_cast<RendererGamepadProvider
*>(observer
)->SampleGamepads(gamepads
);
876 //------------------------------------------------------------------------------
878 WebRTCPeerConnectionHandler
*
879 RendererBlinkPlatformImpl::createRTCPeerConnectionHandler(
880 WebRTCPeerConnectionHandlerClient
* client
) {
881 RenderThreadImpl
* render_thread
= RenderThreadImpl::current();
882 DCHECK(render_thread
);
886 #if defined(ENABLE_WEBRTC)
887 WebRTCPeerConnectionHandler
* peer_connection_handler
=
888 GetContentClient()->renderer()->OverrideCreateWebRTCPeerConnectionHandler(
890 if (peer_connection_handler
)
891 return peer_connection_handler
;
893 PeerConnectionDependencyFactory
* rtc_dependency_factory
=
894 render_thread
->GetPeerConnectionDependencyFactory();
895 return rtc_dependency_factory
->CreateRTCPeerConnectionHandler(client
);
898 #endif // defined(ENABLE_WEBRTC)
901 //------------------------------------------------------------------------------
903 WebMediaStreamCenter
* RendererBlinkPlatformImpl::createMediaStreamCenter(
904 WebMediaStreamCenterClient
* client
) {
905 RenderThreadImpl
* render_thread
= RenderThreadImpl::current();
906 DCHECK(render_thread
);
909 return render_thread
->CreateMediaStreamCenter(client
);
913 bool RendererBlinkPlatformImpl::SetSandboxEnabledForTesting(bool enable
) {
914 bool was_enabled
= g_sandbox_enabled
;
915 g_sandbox_enabled
= enable
;
919 //------------------------------------------------------------------------------
921 blink::WebSpeechSynthesizer
* RendererBlinkPlatformImpl::createSpeechSynthesizer(
922 blink::WebSpeechSynthesizerClient
* client
) {
923 return GetContentClient()->renderer()->OverrideSpeechSynthesizer(client
);
926 //------------------------------------------------------------------------------
928 bool RendererBlinkPlatformImpl::processMemorySizesInBytes(
929 size_t* private_bytes
,
930 size_t* shared_bytes
) {
931 content::RenderThread::Get()->Send(
932 new ViewHostMsg_GetProcessMemorySizes(private_bytes
, shared_bytes
));
936 //------------------------------------------------------------------------------
938 blink::WebGraphicsContext3D
*
939 RendererBlinkPlatformImpl::createOffscreenGraphicsContext3D(
940 const blink::WebGraphicsContext3D::Attributes
& attributes
) {
941 return createOffscreenGraphicsContext3D(attributes
, NULL
);
944 blink::WebGraphicsContext3D
*
945 RendererBlinkPlatformImpl::createOffscreenGraphicsContext3D(
946 const blink::WebGraphicsContext3D::Attributes
& attributes
,
947 blink::WebGraphicsContext3D
* share_context
) {
948 return createOffscreenGraphicsContext3D(attributes
, share_context
, NULL
);
951 blink::WebGraphicsContext3D
*
952 RendererBlinkPlatformImpl::createOffscreenGraphicsContext3D(
953 const blink::WebGraphicsContext3D::Attributes
& attributes
,
954 blink::WebGraphicsContext3D
* share_context
,
955 blink::WebGLInfo
* gl_info
) {
956 if (!RenderThreadImpl::current())
959 #if defined(OS_ANDROID)
960 SynchronousCompositorFactory
* factory
=
961 SynchronousCompositorFactory::GetInstance();
962 if (factory
&& factory
->OverrideWithFactory()) {
963 scoped_ptr
<gpu_blink::WebGraphicsContext3DInProcessCommandBufferImpl
>
965 factory
->CreateOffscreenGraphicsContext3D(attributes
));
966 if (!in_process_context
|| !in_process_context
->InitializeOnCurrentThread())
968 return in_process_context
.release();
972 scoped_refptr
<GpuChannelHost
> gpu_channel_host(
973 RenderThreadImpl::current()->EstablishGpuChannelSync(
974 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE
));
976 if (gpu_channel_host
.get() && gl_info
) {
977 const gpu::GPUInfo
& gpu_info
= gpu_channel_host
->gpu_info();
978 switch (gpu_info
.context_info_state
) {
979 case gpu::kCollectInfoSuccess
:
980 case gpu::kCollectInfoNonFatalFailure
:
981 gl_info
->vendorInfo
.assign(
982 blink::WebString::fromUTF8(gpu_info
.gl_vendor
));
983 gl_info
->rendererInfo
.assign(
984 blink::WebString::fromUTF8(gpu_info
.gl_renderer
));
985 gl_info
->driverVersion
.assign(
986 blink::WebString::fromUTF8(gpu_info
.driver_version
));
987 gl_info
->vendorId
= gpu_info
.gpu
.vendor_id
;
988 gl_info
->deviceId
= gpu_info
.gpu
.device_id
;
990 case gpu::kCollectInfoFatalFailure
:
991 case gpu::kCollectInfoNone
:
992 gl_info
->contextInfoCollectionFailure
.assign(blink::WebString::fromUTF8(
993 "GPUInfoCollectionFailure: GPU initialization Failed. GPU "
994 "Info not Collected."));
1001 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits
;
1002 bool lose_context_when_out_of_memory
= false;
1003 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> context(
1004 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
1005 gpu_channel_host
.get(),
1007 lose_context_when_out_of_memory
,
1008 GURL(attributes
.topDocumentURL
),
1010 static_cast<WebGraphicsContext3DCommandBufferImpl
*>(share_context
)));
1012 // Most likely the GPU process exited and the attempt to reconnect to it
1013 // failed. Need to try to restore the context again later.
1014 if (!context
|| !context
->InitializeOnCurrentThread())
1016 return context
.release();
1019 //------------------------------------------------------------------------------
1021 blink::WebGraphicsContext3DProvider
*
1022 RendererBlinkPlatformImpl::createSharedOffscreenGraphicsContext3DProvider() {
1023 scoped_refptr
<cc_blink::ContextProviderWebContext
> provider
=
1024 RenderThreadImpl::current()->SharedMainThreadContextProvider();
1025 if (!provider
.get())
1027 return new WebGraphicsContext3DProviderImpl(provider
);
1030 //------------------------------------------------------------------------------
1032 blink::WebCompositorSupport
* RendererBlinkPlatformImpl::compositorSupport() {
1033 return &compositor_support_
;
1036 //------------------------------------------------------------------------------
1038 blink::WebString
RendererBlinkPlatformImpl::convertIDNToUnicode(
1039 const blink::WebString
& host
,
1040 const blink::WebString
& languages
) {
1041 return url_formatter::IDNToUnicode(host
.utf8(), languages
.utf8());
1044 //------------------------------------------------------------------------------
1046 void RendererBlinkPlatformImpl::recordRappor(const char* metric
,
1047 const blink::WebString
& sample
) {
1048 GetContentClient()->renderer()->RecordRappor(metric
, sample
.utf8());
1051 void RendererBlinkPlatformImpl::recordRapporURL(const char* metric
,
1052 const blink::WebURL
& url
) {
1053 GetContentClient()->renderer()->RecordRapporURL(metric
, url
);
1056 //------------------------------------------------------------------------------
1059 void RendererBlinkPlatformImpl::SetMockDeviceLightDataForTesting(double data
) {
1060 g_test_device_light_data
= data
;
1063 //------------------------------------------------------------------------------
1066 void RendererBlinkPlatformImpl::SetMockDeviceMotionDataForTesting(
1067 const blink::WebDeviceMotionData
& data
) {
1068 g_test_device_motion_data
.Get() = data
;
1071 //------------------------------------------------------------------------------
1074 void RendererBlinkPlatformImpl::SetMockDeviceOrientationDataForTesting(
1075 const blink::WebDeviceOrientationData
& data
) {
1076 g_test_device_orientation_data
.Get() = data
;
1079 //------------------------------------------------------------------------------
1081 void RendererBlinkPlatformImpl::vibrate(unsigned int milliseconds
) {
1082 GetConnectedVibrationManagerService()->Vibrate(
1083 base::checked_cast
<int64
>(milliseconds
));
1084 vibration_manager_
.reset();
1087 void RendererBlinkPlatformImpl::cancelVibration() {
1088 GetConnectedVibrationManagerService()->Cancel();
1089 vibration_manager_
.reset();
1092 device::VibrationManagerPtr
&
1093 RendererBlinkPlatformImpl::GetConnectedVibrationManagerService() {
1094 if (!vibration_manager_
) {
1095 RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService(
1096 mojo::GetProxy(&vibration_manager_
));
1098 return vibration_manager_
;
1101 //------------------------------------------------------------------------------
1104 PlatformEventObserverBase
*
1105 RendererBlinkPlatformImpl::CreatePlatformEventObserverFromType(
1106 blink::WebPlatformEventType type
) {
1107 RenderThread
* thread
= RenderThreadImpl::current();
1109 // When running layout tests, those observers should not listen to the actual
1110 // hardware changes. In order to make that happen, they will receive a null
1112 if (thread
&& RenderThreadImpl::current()->layout_test_mode())
1116 case blink::WebPlatformEventDeviceMotion
:
1117 return new DeviceMotionEventPump(thread
);
1118 case blink::WebPlatformEventDeviceOrientation
:
1119 return new DeviceOrientationEventPump(thread
);
1120 case blink::WebPlatformEventDeviceLight
:
1121 return new DeviceLightEventPump(thread
);
1122 case blink::WebPlatformEventGamepad
:
1123 return new GamepadSharedMemoryReader(thread
);
1124 case blink::WebPlatformEventScreenOrientation
:
1125 return new ScreenOrientationObserver();
1127 // A default statement is required to prevent compilation errors when
1128 // Blink adds a new type.
1129 DVLOG(1) << "RendererBlinkPlatformImpl::startListening() with "
1136 void RendererBlinkPlatformImpl::SetPlatformEventObserverForTesting(
1137 blink::WebPlatformEventType type
,
1138 scoped_ptr
<PlatformEventObserverBase
> observer
) {
1139 DCHECK(type
!= blink::WebPlatformEventBattery
);
1141 if (platform_event_observers_
.Lookup(type
))
1142 platform_event_observers_
.Remove(type
);
1143 platform_event_observers_
.AddWithID(observer
.release(), type
);
1146 void RendererBlinkPlatformImpl::startListening(
1147 blink::WebPlatformEventType type
,
1148 blink::WebPlatformEventListener
* listener
) {
1149 if (type
== blink::WebPlatformEventBattery
) {
1150 if (RenderThreadImpl::current() &&
1151 RenderThreadImpl::current()->layout_test_mode()) {
1152 g_test_battery_status_listener
=
1153 static_cast<blink::WebBatteryStatusListener
*>(listener
);
1155 battery_status_dispatcher_
.reset(new BatteryStatusDispatcher(
1156 static_cast<blink::WebBatteryStatusListener
*>(listener
)));
1161 PlatformEventObserverBase
* observer
= platform_event_observers_
.Lookup(type
);
1163 observer
= CreatePlatformEventObserverFromType(type
);
1166 platform_event_observers_
.AddWithID(observer
, static_cast<int32
>(type
));
1168 observer
->Start(listener
);
1170 // Device events (motion, orientation and light) expect to get an event fired
1171 // as soon as a listener is registered if a fake data was passed before.
1172 // TODO(mlamouri,timvolodine): make those send mock values directly instead of
1173 // using this broken pattern.
1174 if (RenderThreadImpl::current() &&
1175 RenderThreadImpl::current()->layout_test_mode() &&
1176 (type
== blink::WebPlatformEventDeviceMotion
||
1177 type
== blink::WebPlatformEventDeviceOrientation
||
1178 type
== blink::WebPlatformEventDeviceLight
)) {
1179 SendFakeDeviceEventDataForTesting(type
);
1183 void RendererBlinkPlatformImpl::SendFakeDeviceEventDataForTesting(
1184 blink::WebPlatformEventType type
) {
1185 PlatformEventObserverBase
* observer
= platform_event_observers_
.Lookup(type
);
1191 case blink::WebPlatformEventDeviceMotion
:
1192 if (!(g_test_device_motion_data
== 0))
1193 data
= &g_test_device_motion_data
.Get();
1195 case blink::WebPlatformEventDeviceOrientation
:
1196 if (!(g_test_device_orientation_data
== 0))
1197 data
= &g_test_device_orientation_data
.Get();
1199 case blink::WebPlatformEventDeviceLight
:
1200 if (g_test_device_light_data
>= 0)
1201 data
= &g_test_device_light_data
;
1211 base::ThreadTaskRunnerHandle::Get()->PostTask(
1212 FROM_HERE
, base::Bind(&PlatformEventObserverBase::SendFakeDataForTesting
,
1213 base::Unretained(observer
), data
));
1216 void RendererBlinkPlatformImpl::stopListening(
1217 blink::WebPlatformEventType type
) {
1218 if (type
== blink::WebPlatformEventBattery
) {
1219 g_test_battery_status_listener
= nullptr;
1220 battery_status_dispatcher_
.reset();
1224 PlatformEventObserverBase
* observer
= platform_event_observers_
.Lookup(type
);
1230 //------------------------------------------------------------------------------
1232 void RendererBlinkPlatformImpl::queryStorageUsageAndQuota(
1233 const blink::WebURL
& storage_partition
,
1234 blink::WebStorageQuotaType type
,
1235 blink::WebStorageQuotaCallbacks callbacks
) {
1236 if (!thread_safe_sender_
.get() || !quota_message_filter_
.get())
1238 QuotaDispatcher::ThreadSpecificInstance(thread_safe_sender_
.get(),
1239 quota_message_filter_
.get())
1240 ->QueryStorageUsageAndQuota(
1242 static_cast<storage::StorageType
>(type
),
1243 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks
));
1246 //------------------------------------------------------------------------------
1248 void RendererBlinkPlatformImpl::MockBatteryStatusChangedForTesting(
1249 const blink::WebBatteryStatus
& status
) {
1250 if (!g_test_battery_status_listener
)
1252 g_test_battery_status_listener
->updateBatteryStatus(status
);
1255 } // namespace content