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_webkitplatformsupport_impl.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/lazy_instance.h"
11 #include "base/metrics/histogram.h"
12 #include "base/platform_file.h"
13 #include "base/shared_memory.h"
14 #include "base/utf_string_conversions.h"
15 #include "content/common/database_util.h"
16 #include "content/common/file_utilities_messages.h"
17 #include "content/common/fileapi/webblobregistry_impl.h"
18 #include "content/common/fileapi/webfilesystem_impl.h"
19 #include "content/common/gpu/client/context_provider_command_buffer.h"
20 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
21 #include "content/common/indexed_db/proxy_webidbfactory_impl.h"
22 #include "content/common/mime_registry_messages.h"
23 #include "content/common/npobject_util.h"
24 #include "content/common/thread_safe_sender.h"
25 #include "content/common/view_messages.h"
26 #include "content/common/webmessageportchannel_impl.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/common/gpu_info.h"
29 #include "content/public/renderer/content_renderer_client.h"
30 #include "content/renderer/dom_storage/webstoragenamespace_impl.h"
31 #include "content/renderer/gamepad_shared_memory_reader.h"
32 #include "content/renderer/hyphenator/hyphenator.h"
33 #include "content/renderer/media/media_stream_dependency_factory.h"
34 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
35 #include "content/renderer/render_thread_impl.h"
36 #include "content/renderer/renderer_clipboard_client.h"
37 #include "content/renderer/websharedworkerrepository_impl.h"
38 #include "googleurl/src/gurl.h"
39 #include "ipc/ipc_sync_message_filter.h"
40 #include "media/audio/audio_output_device.h"
41 #include "media/base/audio_hardware_config.h"
42 #include "third_party/WebKit/Source/Platform/chromium/public/WebBlobRegistry.h"
43 #include "third_party/WebKit/Source/Platform/chromium/public/WebFileInfo.h"
44 #include "third_party/WebKit/Source/Platform/chromium/public/WebGamepads.h"
45 #include "third_party/WebKit/Source/Platform/chromium/public/WebHyphenator.h"
46 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamCenter.h"
47 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamCenterClient.h"
48 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h"
49 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
50 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
51 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
52 #include "webkit/base/file_path_string_conversions.h"
53 #include "webkit/glue/simple_webmimeregistry_impl.h"
54 #include "webkit/glue/webclipboard_impl.h"
55 #include "webkit/glue/webfileutilities_impl.h"
56 #include "webkit/glue/webkit_glue.h"
59 #include "content/common/child_process_messages.h"
60 #include "third_party/WebKit/Source/Platform/chromium/public/win/WebSandboxSupport.h"
63 #if defined(OS_MACOSX)
64 #include "content/common/mac/font_descriptor.h"
65 #include "content/common/mac/font_loader.h"
66 #include "third_party/WebKit/Source/Platform/chromium/public/mac/WebSandboxSupport.h"
69 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
73 #include "base/synchronization/lock.h"
74 #include "content/common/child_process_sandbox_support_impl_linux.h"
75 #include "third_party/WebKit/Source/Platform/chromium/public/linux/WebFontFamily.h"
76 #include "third_party/WebKit/Source/Platform/chromium/public/linux/WebSandboxSupport.h"
80 #include "base/file_descriptor_posix.h"
83 using WebKit::WebAudioDevice
;
84 using WebKit::WebBlobRegistry
;
85 using WebKit::WebFileInfo
;
86 using WebKit::WebFileSystem
;
87 using WebKit::WebFrame
;
88 using WebKit::WebGamepads
;
89 using WebKit::WebIDBFactory
;
90 using WebKit::Platform
;
91 using WebKit::WebMediaStreamCenter
;
92 using WebKit::WebMediaStreamCenterClient
;
93 using WebKit::WebRTCPeerConnectionHandler
;
94 using WebKit::WebRTCPeerConnectionHandlerClient
;
95 using WebKit::WebStorageNamespace
;
96 using WebKit::WebString
;
98 using WebKit::WebVector
;
102 static bool g_sandbox_enabled
= true;
103 base::LazyInstance
<WebGamepads
>::Leaky g_test_gamepads
=
104 LAZY_INSTANCE_INITIALIZER
;
106 //------------------------------------------------------------------------------
108 class RendererWebKitPlatformSupportImpl::MimeRegistry
109 : public webkit_glue::SimpleWebMimeRegistryImpl
{
111 virtual WebKit::WebString
mimeTypeForExtension(const WebKit::WebString
&);
112 virtual WebKit::WebString
mimeTypeFromFile(const WebKit::WebString
&);
113 virtual WebKit::WebString
preferredExtensionForMIMEType(
114 const WebKit::WebString
&);
117 class RendererWebKitPlatformSupportImpl::FileUtilities
118 : public webkit_glue::WebFileUtilitiesImpl
{
120 explicit FileUtilities(ThreadSafeSender
* sender
)
121 : thread_safe_sender_(sender
) {}
122 virtual bool getFileInfo(const WebString
& path
, WebFileInfo
& result
);
123 virtual base::PlatformFile
openFile(const WebKit::WebString
& path
,
126 bool SendSyncMessageFromAnyThread(IPC::SyncMessage
* msg
) const;
127 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
130 class RendererWebKitPlatformSupportImpl::Hyphenator
131 : public WebKit::WebHyphenator
{
134 virtual ~Hyphenator();
136 virtual bool canHyphenate(const WebKit::WebString
& locale
) OVERRIDE
;
137 virtual size_t computeLastHyphenLocation(
138 const char16
* characters
,
141 const WebKit::WebString
& locale
) OVERRIDE
;
143 scoped_ptr
<content::Hyphenator
> hyphenator_
;
145 DISALLOW_COPY_AND_ASSIGN(Hyphenator
);
148 #if defined(OS_ANDROID)
149 // WebKit doesn't use WebSandboxSupport on android so we don't need to
150 // implement anything here.
151 class RendererWebKitPlatformSupportImpl::SandboxSupport
{
154 class RendererWebKitPlatformSupportImpl::SandboxSupport
155 : public WebKit::WebSandboxSupport
{
157 virtual ~SandboxSupport() {}
160 virtual bool ensureFontLoaded(HFONT
);
161 #elif defined(OS_MACOSX)
162 virtual bool loadFont(
164 CGFontRef
* container
,
166 #elif defined(OS_POSIX)
167 virtual void getFontFamilyForCharacters(
168 const WebKit::WebUChar
* characters
,
169 size_t numCharacters
,
170 const char* preferred_locale
,
171 WebKit::WebFontFamily
* family
);
172 virtual void getRenderStyleForStrike(
173 const char* family
, int sizeAndStyle
, WebKit::WebFontRenderStyle
* out
);
176 // WebKit likes to ask us for the correct font family to use for a set of
177 // unicode code points. It needs this information frequently so we cache it
178 // here. The key in this map is an array of 16-bit UTF16 values from WebKit.
179 // The value is a string containing the correct font family.
180 base::Lock unicode_font_families_mutex_
;
181 std::map
<string16
, WebKit::WebFontFamily
> unicode_font_families_
;
184 #endif // defined(OS_ANDROID)
186 //------------------------------------------------------------------------------
188 RendererWebKitPlatformSupportImpl::RendererWebKitPlatformSupportImpl()
189 : clipboard_client_(new RendererClipboardClient
),
190 clipboard_(new webkit_glue::WebClipboardImpl(clipboard_client_
.get())),
191 mime_registry_(new RendererWebKitPlatformSupportImpl::MimeRegistry
),
192 hyphenator_(new RendererWebKitPlatformSupportImpl::Hyphenator
),
193 sudden_termination_disables_(0),
194 plugin_refresh_allowed_(true),
195 shared_worker_repository_(new WebSharedWorkerRepositoryImpl
) {
196 if (g_sandbox_enabled
&& sandboxEnabled()) {
197 sandbox_support_
.reset(
198 new RendererWebKitPlatformSupportImpl::SandboxSupport
);
200 DVLOG(1) << "Disabling sandbox support for testing.";
203 // ChildThread may not exist in some tests.
204 if (ChildThread::current())
205 thread_safe_sender_
= ChildThread::current()->thread_safe_sender();
208 RendererWebKitPlatformSupportImpl::~RendererWebKitPlatformSupportImpl() {
211 //------------------------------------------------------------------------------
213 WebKit::WebClipboard
* RendererWebKitPlatformSupportImpl::clipboard() {
214 WebKit::WebClipboard
* clipboard
=
215 GetContentClient()->renderer()->OverrideWebClipboard();
218 return clipboard_
.get();
221 WebKit::WebMimeRegistry
* RendererWebKitPlatformSupportImpl::mimeRegistry() {
222 WebKit::WebMimeRegistry
* mime_registry
=
223 GetContentClient()->renderer()->OverrideWebMimeRegistry();
225 return mime_registry
;
226 return mime_registry_
.get();
229 WebKit::WebFileUtilities
*
230 RendererWebKitPlatformSupportImpl::fileUtilities() {
231 if (!file_utilities_
) {
232 file_utilities_
.reset(new FileUtilities(thread_safe_sender_
));
233 file_utilities_
->set_sandbox_enabled(sandboxEnabled());
235 return file_utilities_
.get();
238 WebKit::WebSandboxSupport
* RendererWebKitPlatformSupportImpl::sandboxSupport() {
239 #if defined(OS_ANDROID)
240 // WebKit doesn't use WebSandboxSupport on android.
243 return sandbox_support_
.get();
247 WebKit::WebCookieJar
* RendererWebKitPlatformSupportImpl::cookieJar() {
248 NOTREACHED() << "Use WebFrameClient::cookieJar() instead!";
252 WebKit::WebThemeEngine
* RendererWebKitPlatformSupportImpl::themeEngine() {
253 WebKit::WebThemeEngine
* theme_engine
=
254 GetContentClient()->renderer()->OverrideThemeEngine();
257 return WebKitPlatformSupportImpl::themeEngine();
260 bool RendererWebKitPlatformSupportImpl::sandboxEnabled() {
261 // As explained in Platform.h, this function is used to decide
262 // whether to allow file system operations to come out of WebKit or not.
263 // Even if the sandbox is disabled, there's no reason why the code should
264 // act any differently...unless we're in single process mode. In which
265 // case, we have no other choice. Platform.h discourages using
266 // this switch unless absolutely necessary, so hopefully we won't end up
267 // with too many code paths being different in single-process mode.
268 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
);
271 unsigned long long RendererWebKitPlatformSupportImpl::visitedLinkHash(
272 const char* canonical_url
,
274 return GetContentClient()->renderer()->VisitedLinkHash(canonical_url
, length
);
277 bool RendererWebKitPlatformSupportImpl::isLinkVisited(
278 unsigned long long link_hash
) {
279 return GetContentClient()->renderer()->IsLinkVisited(link_hash
);
282 WebKit::WebMessagePortChannel
*
283 RendererWebKitPlatformSupportImpl::createMessagePortChannel() {
284 return new WebMessagePortChannelImpl();
287 void RendererWebKitPlatformSupportImpl::prefetchHostName(
288 const WebString
& hostname
) {
289 if (hostname
.isEmpty())
292 std::string hostname_utf8
;
293 UTF16ToUTF8(hostname
.data(), hostname
.length(), &hostname_utf8
);
294 GetContentClient()->renderer()->PrefetchHostName(
295 hostname_utf8
.data(), hostname_utf8
.length());
299 RendererWebKitPlatformSupportImpl::CheckPreparsedJsCachingEnabled() const {
300 static bool checked
= false;
301 static bool result
= false;
303 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
304 result
= command_line
.HasSwitch(switches::kEnablePreparsedJsCaching
);
310 void RendererWebKitPlatformSupportImpl::cacheMetadata(
311 const WebKit::WebURL
& url
,
312 double response_time
,
315 if (!CheckPreparsedJsCachingEnabled())
318 // Let the browser know we generated cacheable metadata for this resource. The
319 // browser may cache it and return it on subsequent responses to speed
320 // the processing of this resource.
321 std::vector
<char> copy(data
, data
+ size
);
322 RenderThread::Get()->Send(
323 new ViewHostMsg_DidGenerateCacheableMetadata(url
, response_time
, copy
));
326 WebString
RendererWebKitPlatformSupportImpl::defaultLocale() {
327 return ASCIIToUTF16(RenderThread::Get()->GetLocale());
330 void RendererWebKitPlatformSupportImpl::suddenTerminationChanged(bool enabled
) {
332 // We should not get more enables than disables, but we want it to be a
333 // non-fatal error if it does happen.
334 DCHECK_GT(sudden_termination_disables_
, 0);
335 sudden_termination_disables_
= std::max(sudden_termination_disables_
- 1,
337 if (sudden_termination_disables_
!= 0)
340 sudden_termination_disables_
++;
341 if (sudden_termination_disables_
!= 1)
345 RenderThread
* thread
= RenderThread::Get();
346 if (thread
) // NULL in unittests.
347 thread
->Send(new ViewHostMsg_SuddenTerminationChanged(enabled
));
351 RendererWebKitPlatformSupportImpl::createLocalStorageNamespace(
352 const WebString
& path
, unsigned quota
) {
353 return new WebStorageNamespaceImpl();
357 //------------------------------------------------------------------------------
359 WebIDBFactory
* RendererWebKitPlatformSupportImpl::idbFactory() {
360 if (!web_idb_factory_
) {
361 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess
))
362 web_idb_factory_
.reset(WebIDBFactory::create());
364 web_idb_factory_
.reset(new RendererWebIDBFactoryImpl());
366 return web_idb_factory_
.get();
369 //------------------------------------------------------------------------------
371 WebFileSystem
* RendererWebKitPlatformSupportImpl::fileSystem() {
372 if (!web_file_system_
)
373 web_file_system_
.reset(new WebFileSystemImpl());
374 return web_file_system_
.get();
377 //------------------------------------------------------------------------------
380 RendererWebKitPlatformSupportImpl::MimeRegistry::mimeTypeForExtension(
381 const WebString
& file_extension
) {
382 if (IsPluginProcess())
383 return SimpleWebMimeRegistryImpl::mimeTypeForExtension(file_extension
);
385 // The sandbox restricts our access to the registry, so we need to proxy
386 // these calls over to the browser process.
387 std::string mime_type
;
388 RenderThread::Get()->Send(
389 new MimeRegistryMsg_GetMimeTypeFromExtension(
390 webkit_base::WebStringToFilePathString(file_extension
), &mime_type
));
391 return ASCIIToUTF16(mime_type
);
394 WebString
RendererWebKitPlatformSupportImpl::MimeRegistry::mimeTypeFromFile(
395 const WebString
& file_path
) {
396 if (IsPluginProcess())
397 return SimpleWebMimeRegistryImpl::mimeTypeFromFile(file_path
);
399 // The sandbox restricts our access to the registry, so we need to proxy
400 // these calls over to the browser process.
401 std::string mime_type
;
402 RenderThread::Get()->Send(new MimeRegistryMsg_GetMimeTypeFromFile(
403 base::FilePath(webkit_base::WebStringToFilePathString(file_path
)),
405 return ASCIIToUTF16(mime_type
);
409 RendererWebKitPlatformSupportImpl::MimeRegistry::preferredExtensionForMIMEType(
410 const WebString
& mime_type
) {
411 if (IsPluginProcess())
412 return SimpleWebMimeRegistryImpl::preferredExtensionForMIMEType(mime_type
);
414 // The sandbox restricts our access to the registry, so we need to proxy
415 // these calls over to the browser process.
416 base::FilePath::StringType file_extension
;
417 RenderThread::Get()->Send(
418 new MimeRegistryMsg_GetPreferredExtensionForMimeType(
419 UTF16ToASCII(mime_type
), &file_extension
));
420 return webkit_base::FilePathStringToWebString(file_extension
);
423 //------------------------------------------------------------------------------
425 bool RendererWebKitPlatformSupportImpl::FileUtilities::getFileInfo(
426 const WebString
& path
,
427 WebFileInfo
& web_file_info
) {
428 base::PlatformFileInfo file_info
;
429 base::PlatformFileError status
;
430 if (!SendSyncMessageFromAnyThread(new FileUtilitiesMsg_GetFileInfo(
431 webkit_base::WebStringToFilePath(path
), &file_info
, &status
)) ||
432 status
!= base::PLATFORM_FILE_OK
) {
435 webkit_glue::PlatformFileInfoToWebFileInfo(file_info
, &web_file_info
);
436 web_file_info
.platformPath
= path
;
440 base::PlatformFile
RendererWebKitPlatformSupportImpl::FileUtilities::openFile(
441 const WebString
& path
,
443 IPC::PlatformFileForTransit handle
= IPC::InvalidPlatformFileForTransit();
444 SendSyncMessageFromAnyThread(new FileUtilitiesMsg_OpenFile(
445 webkit_base::WebStringToFilePath(path
), mode
, &handle
));
446 return IPC::PlatformFileForTransitToPlatformFile(handle
);
449 bool RendererWebKitPlatformSupportImpl::FileUtilities::
450 SendSyncMessageFromAnyThread(IPC::SyncMessage
* msg
) const {
451 base::TimeTicks begin
= base::TimeTicks::Now();
452 const bool success
= thread_safe_sender_
->Send(msg
);
453 base::TimeDelta delta
= base::TimeTicks::Now() - begin
;
454 UMA_HISTOGRAM_TIMES("RendererSyncIPC.ElapsedTime", delta
);
458 //------------------------------------------------------------------------------
460 RendererWebKitPlatformSupportImpl::Hyphenator::Hyphenator() {}
462 RendererWebKitPlatformSupportImpl::Hyphenator::~Hyphenator() {}
464 bool RendererWebKitPlatformSupportImpl::Hyphenator::canHyphenate(
465 const WebKit::WebString
& locale
) {
466 // Return false unless WebKit asks for US English dictionaries because WebKit
467 // can currently hyphenate only English words.
468 if (!locale
.isEmpty() && !locale
.equals("en-US"))
471 // Create a hyphenator object and attach it to the render thread so it can
472 // receive a dictionary file opened by a browser.
474 hyphenator_
.reset(new content::Hyphenator(base::kInvalidPlatformFileValue
));
477 return hyphenator_
->Attach(RenderThreadImpl::current(), locale
);
479 return hyphenator_
->CanHyphenate(locale
);
482 size_t RendererWebKitPlatformSupportImpl::Hyphenator::computeLastHyphenLocation(
483 const char16
* characters
,
486 const WebKit::WebString
& locale
) {
487 // Crash if WebKit calls this function when canHyphenate returns false.
488 DCHECK(locale
.isEmpty() || locale
.equals("en-US"));
489 DCHECK(hyphenator_
.get());
490 return hyphenator_
->ComputeLastHyphenLocation(string16(characters
, length
),
494 //------------------------------------------------------------------------------
498 bool RendererWebKitPlatformSupportImpl::SandboxSupport::ensureFontLoaded(
501 GetObject(font
, sizeof(LOGFONT
), &logfont
);
502 RenderThread::Get()->PreCacheFont(logfont
);
506 #elif defined(OS_MACOSX)
508 bool RendererWebKitPlatformSupportImpl::SandboxSupport::loadFont(
509 NSFont
* src_font
, CGFontRef
* out
, uint32
* font_id
) {
510 uint32 font_data_size
;
511 FontDescriptor
src_font_descriptor(src_font
);
512 base::SharedMemoryHandle font_data
;
513 if (!RenderThread::Get()->Send(new ViewHostMsg_LoadFont(
514 src_font_descriptor
, &font_data_size
, &font_data
, font_id
))) {
520 if (font_data_size
== 0 || font_data
== base::SharedMemory::NULLHandle() ||
522 LOG(ERROR
) << "Bad response from ViewHostMsg_LoadFont() for " <<
523 src_font_descriptor
.font_name
;
529 // TODO(jeremy): Need to call back into WebKit to make sure that the font
530 // isn't already activated, based on the font id. If it's already
531 // activated, don't reactivate it here - crbug.com/72727 .
533 return FontLoader::CGFontRefFromBuffer(font_data
, font_data_size
, out
);
536 #elif defined(OS_ANDROID)
538 // WebKit doesn't use WebSandboxSupport on android so we don't need to
539 // implement anything here. This is cleaner to support than excluding the
540 // whole class for android.
542 #elif defined(OS_POSIX)
545 RendererWebKitPlatformSupportImpl::SandboxSupport::getFontFamilyForCharacters(
546 const WebKit::WebUChar
* characters
,
547 size_t num_characters
,
548 const char* preferred_locale
,
549 WebKit::WebFontFamily
* family
) {
550 base::AutoLock
lock(unicode_font_families_mutex_
);
551 const string16
key(characters
, num_characters
);
552 const std::map
<string16
, WebKit::WebFontFamily
>::const_iterator iter
=
553 unicode_font_families_
.find(key
);
554 if (iter
!= unicode_font_families_
.end()) {
555 family
->name
= iter
->second
.name
;
556 family
->isBold
= iter
->second
.isBold
;
557 family
->isItalic
= iter
->second
.isItalic
;
561 GetFontFamilyForCharacters(
566 unicode_font_families_
.insert(make_pair(key
, *family
));
570 RendererWebKitPlatformSupportImpl::SandboxSupport::getRenderStyleForStrike(
571 const char* family
, int sizeAndStyle
, WebKit::WebFontRenderStyle
* out
) {
572 GetRenderStyleForStrike(family
, sizeAndStyle
, out
);
577 //------------------------------------------------------------------------------
580 RendererWebKitPlatformSupportImpl::databaseOpenFile(
581 const WebString
& vfs_file_name
, int desired_flags
) {
582 return DatabaseUtil::DatabaseOpenFile(vfs_file_name
, desired_flags
);
585 int RendererWebKitPlatformSupportImpl::databaseDeleteFile(
586 const WebString
& vfs_file_name
, bool sync_dir
) {
587 return DatabaseUtil::DatabaseDeleteFile(vfs_file_name
, sync_dir
);
590 long RendererWebKitPlatformSupportImpl::databaseGetFileAttributes(
591 const WebString
& vfs_file_name
) {
592 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name
);
595 long long RendererWebKitPlatformSupportImpl::databaseGetFileSize(
596 const WebString
& vfs_file_name
) {
597 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name
);
600 long long RendererWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
601 const WebString
& origin_identifier
) {
602 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier
);
605 WebKit::WebSharedWorkerRepository
*
606 RendererWebKitPlatformSupportImpl::sharedWorkerRepository() {
607 if (!CommandLine::ForCurrentProcess()->HasSwitch(
608 switches::kDisableSharedWorkers
)) {
609 return shared_worker_repository_
.get();
615 bool RendererWebKitPlatformSupportImpl::canAccelerate2dCanvas() {
616 RenderThreadImpl
* thread
= RenderThreadImpl::current();
617 GpuChannelHost
* host
= thread
->EstablishGpuChannelSync(
618 CAUSE_FOR_GPU_LAUNCH_CANVAS_2D
);
622 const GPUInfo
& gpu_info
= host
->gpu_info();
623 if (gpu_info
.can_lose_context
|| gpu_info
.software_rendering
)
629 bool RendererWebKitPlatformSupportImpl::isThreadedCompositingEnabled() {
630 return !!RenderThreadImpl::current()->compositor_message_loop_proxy();
633 double RendererWebKitPlatformSupportImpl::audioHardwareSampleRate() {
634 RenderThreadImpl
* thread
= RenderThreadImpl::current();
635 return thread
->GetAudioHardwareConfig()->GetOutputSampleRate();
638 size_t RendererWebKitPlatformSupportImpl::audioHardwareBufferSize() {
639 RenderThreadImpl
* thread
= RenderThreadImpl::current();
640 return thread
->GetAudioHardwareConfig()->GetOutputBufferSize();
643 unsigned RendererWebKitPlatformSupportImpl::audioHardwareOutputChannels() {
644 RenderThreadImpl
* thread
= RenderThreadImpl::current();
645 return thread
->GetAudioHardwareConfig()->GetOutputChannels();
648 // TODO(crogers): remove deprecated API as soon as WebKit calls new API.
650 RendererWebKitPlatformSupportImpl::createAudioDevice(
654 WebAudioDevice::RenderCallback
* callback
) {
655 return createAudioDevice(
656 buffer_size
, 0, channels
, sample_rate
, callback
, "default");
659 // TODO(crogers): remove deprecated API as soon as WebKit calls new API.
661 RendererWebKitPlatformSupportImpl::createAudioDevice(
663 unsigned input_channels
,
666 WebAudioDevice::RenderCallback
* callback
) {
667 return createAudioDevice(
668 buffer_size
, input_channels
, channels
, sample_rate
, callback
, "default");
672 RendererWebKitPlatformSupportImpl::createAudioDevice(
674 unsigned input_channels
,
677 WebAudioDevice::RenderCallback
* callback
,
678 const WebKit::WebString
& input_device_id
) {
679 if (input_device_id
!= "default") {
680 // Only allow audio input if we know for sure that WebKit is giving us the
681 // "default" input device.
682 // TODO(crogers): add support for non-default audio input devices when
683 // using synchronized audio I/O in WebAudio.
684 if (input_channels
> 0)
685 DLOG(WARNING
) << "createAudioDevice(): request for audio input ignored";
689 // The |channels| does not exactly identify the channel layout of the
690 // device. The switch statement below assigns a best guess to the channel
691 // layout based on number of channels.
692 // TODO(crogers): WebKit should give the channel layout instead of the hard
694 media::ChannelLayout layout
= media::CHANNEL_LAYOUT_UNSUPPORTED
;
697 layout
= media::CHANNEL_LAYOUT_MONO
;
700 layout
= media::CHANNEL_LAYOUT_STEREO
;
703 layout
= media::CHANNEL_LAYOUT_2_1
;
706 layout
= media::CHANNEL_LAYOUT_4_0
;
709 layout
= media::CHANNEL_LAYOUT_5_0
;
712 layout
= media::CHANNEL_LAYOUT_5_1
;
715 layout
= media::CHANNEL_LAYOUT_7_0
;
718 layout
= media::CHANNEL_LAYOUT_7_1
;
721 layout
= media::CHANNEL_LAYOUT_STEREO
;
724 media::AudioParameters
params(
725 media::AudioParameters::AUDIO_PCM_LOW_LATENCY
,
726 layout
, input_channels
,
727 static_cast<int>(sample_rate
), 16, buffer_size
);
729 return new RendererWebAudioDeviceImpl(params
, callback
);
732 //------------------------------------------------------------------------------
735 RendererWebKitPlatformSupportImpl::signedPublicKeyAndChallengeString(
736 unsigned key_size_index
,
737 const WebKit::WebString
& challenge
,
738 const WebKit::WebURL
& url
) {
739 std::string signed_public_key
;
740 RenderThread::Get()->Send(new ViewHostMsg_Keygen(
741 static_cast<uint32
>(key_size_index
),
744 &signed_public_key
));
745 return WebString::fromUTF8(signed_public_key
);
748 //------------------------------------------------------------------------------
750 void RendererWebKitPlatformSupportImpl::screenColorProfile(
751 WebVector
<char>* to_profile
) {
752 std::vector
<char> profile
;
753 RenderThread::Get()->Send(
754 new ViewHostMsg_GetMonitorColorProfile(&profile
));
755 *to_profile
= profile
;
758 //------------------------------------------------------------------------------
760 WebBlobRegistry
* RendererWebKitPlatformSupportImpl::blobRegistry() {
761 // thread_safe_sender_ can be NULL when running some tests.
762 if (!blob_registry_
.get() && thread_safe_sender_
)
763 blob_registry_
.reset(new WebBlobRegistryImpl(thread_safe_sender_
));
764 return blob_registry_
.get();
767 //------------------------------------------------------------------------------
769 void RendererWebKitPlatformSupportImpl::sampleGamepads(WebGamepads
& gamepads
) {
770 if (g_test_gamepads
== 0) {
771 if (!gamepad_shared_memory_reader_
)
772 gamepad_shared_memory_reader_
.reset(new GamepadSharedMemoryReader
);
773 gamepad_shared_memory_reader_
->SampleGamepads(gamepads
);
775 gamepads
= g_test_gamepads
.Get();
780 WebKit::WebString
RendererWebKitPlatformSupportImpl::userAgent(
781 const WebKit::WebURL
& url
) {
782 return WebKitPlatformSupportImpl::userAgent(url
);
785 void RendererWebKitPlatformSupportImpl::GetPlugins(
786 bool refresh
, std::vector
<webkit::WebPluginInfo
>* plugins
) {
787 #if defined(ENABLE_PLUGINS)
788 if (!plugin_refresh_allowed_
)
790 RenderThread::Get()->Send(
791 new ViewHostMsg_GetPlugins(refresh
, plugins
));
795 //------------------------------------------------------------------------------
797 WebRTCPeerConnectionHandler
*
798 RendererWebKitPlatformSupportImpl::createRTCPeerConnectionHandler(
799 WebRTCPeerConnectionHandlerClient
* client
) {
800 RenderThreadImpl
* render_thread
= RenderThreadImpl::current();
801 DCHECK(render_thread
);
805 #if defined(ENABLE_WEBRTC)
806 WebRTCPeerConnectionHandler
* peer_connection_handler
=
807 GetContentClient()->renderer()->OverrideCreateWebRTCPeerConnectionHandler(
809 if (peer_connection_handler
)
810 return peer_connection_handler
;
812 MediaStreamDependencyFactory
* rtc_dependency_factory
=
813 render_thread
->GetMediaStreamDependencyFactory();
814 return rtc_dependency_factory
->CreateRTCPeerConnectionHandler(client
);
817 #endif // defined(ENABLE_WEBRTC)
820 //------------------------------------------------------------------------------
822 WebMediaStreamCenter
*
823 RendererWebKitPlatformSupportImpl::createMediaStreamCenter(
824 WebMediaStreamCenterClient
* client
) {
825 RenderThreadImpl
* render_thread
= RenderThreadImpl::current();
826 DCHECK(render_thread
);
829 return render_thread
->CreateMediaStreamCenter(client
);
833 bool RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(
835 bool was_enabled
= g_sandbox_enabled
;
836 g_sandbox_enabled
= enable
;
841 void RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(
842 const WebGamepads
& pads
) {
843 g_test_gamepads
.Get() = pads
;
846 //------------------------------------------------------------------------------
848 WebKit::WebHyphenator
* RendererWebKitPlatformSupportImpl::hyphenator() {
849 WebKit::WebHyphenator
* hyphenator
=
850 GetContentClient()->renderer()->OverrideWebHyphenator();
853 return hyphenator_
.get();
856 //------------------------------------------------------------------------------
858 bool RendererWebKitPlatformSupportImpl::processMemorySizesInBytes(
859 size_t* private_bytes
, size_t* shared_bytes
) {
860 content::RenderThread::Get()->Send(
861 new ViewHostMsg_GetProcessMemorySizes(private_bytes
, shared_bytes
));
865 //------------------------------------------------------------------------------
867 WebKit::WebGraphicsContext3D
*
868 RendererWebKitPlatformSupportImpl::createOffscreenGraphicsContext3D(
869 const WebKit::WebGraphicsContext3D::Attributes
& attributes
) {
870 return WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
871 RenderThreadImpl::current(),
873 GURL(attributes
.topDocumentURL
));
876 //------------------------------------------------------------------------------
878 WebKit::WebGraphicsContext3D
* RendererWebKitPlatformSupportImpl::
879 sharedOffscreenGraphicsContext3D() {
880 if (!shared_offscreen_context_
||
881 shared_offscreen_context_
->DestroyedOnMainThread()) {
882 shared_offscreen_context_
=
883 RenderThreadImpl::current()->OffscreenContextProviderForMainThread();
885 if (!shared_offscreen_context_
)
887 return shared_offscreen_context_
->Context3d();
890 //------------------------------------------------------------------------------
892 GrContext
* RendererWebKitPlatformSupportImpl::sharedOffscreenGrContext() {
893 if (!shared_offscreen_context_
)
895 return shared_offscreen_context_
->GrContext();
899 } // namespace content