After Aura copied the original Linux scrollbar behvaior, it never got updated
[chromium-blink-merge.git] / content / renderer / renderer_webkitplatformsupport_impl.cc
blob3118a1297eb9f32ce5ea1e573a40680c803191d7
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/files/file_path.h"
9 #include "base/lazy_instance.h"
10 #include "base/memory/shared_memory.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/metrics/histogram.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "base/platform_file.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "content/child/blink_glue.h"
18 #include "content/child/database_util.h"
19 #include "content/child/fileapi/webfilesystem_impl.h"
20 #include "content/child/indexed_db/webidbfactory_impl.h"
21 #include "content/child/npapi/npobject_util.h"
22 #include "content/child/quota_dispatcher.h"
23 #include "content/child/quota_message_filter.h"
24 #include "content/child/simple_webmimeregistry_impl.h"
25 #include "content/child/thread_safe_sender.h"
26 #include "content/child/web_database_observer_impl.h"
27 #include "content/child/webblobregistry_impl.h"
28 #include "content/child/webfileutilities_impl.h"
29 #include "content/child/webmessageportchannel_impl.h"
30 #include "content/common/file_utilities_messages.h"
31 #include "content/common/gpu/client/context_provider_command_buffer.h"
32 #include "content/common/gpu/client/gpu_channel_host.h"
33 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
34 #include "content/common/gpu/gpu_process_launch_causes.h"
35 #include "content/common/mime_registry_messages.h"
36 #include "content/common/screen_orientation_messages.h"
37 #include "content/common/view_messages.h"
38 #include "content/public/common/content_switches.h"
39 #include "content/public/common/webplugininfo.h"
40 #include "content/public/renderer/content_renderer_client.h"
41 #include "content/renderer/device_orientation/device_motion_event_pump.h"
42 #include "content/renderer/device_orientation/device_orientation_event_pump.h"
43 #include "content/renderer/dom_storage/webstoragenamespace_impl.h"
44 #include "content/renderer/gamepad_shared_memory_reader.h"
45 #include "content/renderer/media/audio_decoder.h"
46 #include "content/renderer/media/crypto/key_systems.h"
47 #include "content/renderer/media/media_stream_dependency_factory.h"
48 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
49 #include "content/renderer/media/renderer_webmidiaccessor_impl.h"
50 #include "content/renderer/media/webcontentdecryptionmodule_impl.h"
51 #include "content/renderer/render_thread_impl.h"
52 #include "content/renderer/renderer_clipboard_client.h"
53 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h"
54 #include "content/renderer/webclipboard_impl.h"
55 #include "content/renderer/webcrypto/webcrypto_impl.h"
56 #include "content/renderer/webgraphicscontext3d_provider_impl.h"
57 #include "content/renderer/webpublicsuffixlist_impl.h"
58 #include "gpu/config/gpu_info.h"
59 #include "ipc/ipc_sync_message_filter.h"
60 #include "media/audio/audio_output_device.h"
61 #include "media/base/audio_hardware_config.h"
62 #include "media/filters/stream_parser_factory.h"
63 #include "net/base/mime_util.h"
64 #include "net/base/net_util.h"
65 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
66 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h"
67 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
68 #include "third_party/WebKit/public/platform/WebFileInfo.h"
69 #include "third_party/WebKit/public/platform/WebGamepads.h"
70 #include "third_party/WebKit/public/platform/WebMediaStreamCenter.h"
71 #include "third_party/WebKit/public/platform/WebMediaStreamCenterClient.h"
72 #include "third_party/WebKit/public/platform/WebPluginListBuilder.h"
73 #include "third_party/WebKit/public/platform/WebURL.h"
74 #include "third_party/WebKit/public/platform/WebVector.h"
75 #include "ui/gfx/color_profile.h"
76 #include "url/gurl.h"
77 #include "webkit/common/gpu/context_provider_web_context.h"
78 #include "webkit/common/quota/quota_types.h"
80 #if defined(OS_ANDROID)
81 #include "content/renderer/media/android/audio_decoder_android.h"
82 #endif
84 #if defined(OS_MACOSX)
85 #include "content/common/mac/font_descriptor.h"
86 #include "content/common/mac/font_loader.h"
87 #include "content/renderer/webscrollbarbehavior_impl_mac.h"
88 #include "third_party/WebKit/public/platform/mac/WebSandboxSupport.h"
89 #endif
91 #if defined(OS_POSIX)
92 #include "base/file_descriptor_posix.h"
93 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
94 #include <map>
95 #include <string>
97 #include "base/synchronization/lock.h"
98 #include "content/common/child_process_sandbox_support_impl_linux.h"
99 #include "third_party/WebKit/public/platform/linux/WebFontFamily.h"
100 #include "third_party/WebKit/public/platform/linux/WebSandboxSupport.h"
101 #include "third_party/icu/source/common/unicode/utf16.h"
102 #endif
103 #endif
105 #if defined(OS_WIN)
106 #include "content/common/child_process_messages.h"
107 #include "third_party/WebKit/public/platform/win/WebSandboxSupport.h"
108 #endif
110 #if defined(TOOLKIT_GTK) || defined(USE_AURA)
111 #include "content/renderer/webscrollbarbehavior_impl_gtkoraura.h"
112 #elif !defined(OS_MACOSX)
113 #include "third_party/WebKit/public/platform/WebScrollbarBehavior.h"
114 #define WebScrollbarBehaviorImpl blink::WebScrollbarBehavior
115 #endif
117 using blink::Platform;
118 using blink::WebAudioDevice;
119 using blink::WebBlobRegistry;
120 using blink::WebDatabaseObserver;
121 using blink::WebFileInfo;
122 using blink::WebFileSystem;
123 using blink::WebGamepads;
124 using blink::WebIDBFactory;
125 using blink::WebMIDIAccessor;
126 using blink::WebMediaStreamCenter;
127 using blink::WebMediaStreamCenterClient;
128 using blink::WebMimeRegistry;
129 using blink::WebRTCPeerConnectionHandler;
130 using blink::WebRTCPeerConnectionHandlerClient;
131 using blink::WebStorageNamespace;
132 using blink::WebString;
133 using blink::WebURL;
134 using blink::WebVector;
136 namespace content {
138 static bool g_sandbox_enabled = true;
139 base::LazyInstance<WebGamepads>::Leaky g_test_gamepads =
140 LAZY_INSTANCE_INITIALIZER;
141 base::LazyInstance<blink::WebDeviceMotionData>::Leaky
142 g_test_device_motion_data = LAZY_INSTANCE_INITIALIZER;
143 base::LazyInstance<blink::WebDeviceOrientationData>::Leaky
144 g_test_device_orientation_data = LAZY_INSTANCE_INITIALIZER;
146 //------------------------------------------------------------------------------
148 class RendererWebKitPlatformSupportImpl::MimeRegistry
149 : public SimpleWebMimeRegistryImpl {
150 public:
151 virtual blink::WebMimeRegistry::SupportsType supportsMediaMIMEType(
152 const blink::WebString& mime_type,
153 const blink::WebString& codecs,
154 const blink::WebString& key_system);
155 virtual bool supportsMediaSourceMIMEType(const blink::WebString& mime_type,
156 const blink::WebString& codecs);
157 virtual bool supportsEncryptedMediaMIMEType(const WebString& key_system,
158 const WebString& mime_type,
159 const WebString& codecs) OVERRIDE;
160 virtual blink::WebString mimeTypeForExtension(
161 const blink::WebString& file_extension);
162 virtual blink::WebString mimeTypeFromFile(
163 const blink::WebString& file_path);
166 class RendererWebKitPlatformSupportImpl::FileUtilities
167 : public WebFileUtilitiesImpl {
168 public:
169 explicit FileUtilities(ThreadSafeSender* sender)
170 : thread_safe_sender_(sender) {}
171 virtual bool getFileInfo(const WebString& path, WebFileInfo& result);
172 private:
173 bool SendSyncMessageFromAnyThread(IPC::SyncMessage* msg) const;
174 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
177 #if defined(OS_ANDROID)
178 // WebKit doesn't use WebSandboxSupport on android so we don't need to
179 // implement anything here.
180 class RendererWebKitPlatformSupportImpl::SandboxSupport {
182 #else
183 class RendererWebKitPlatformSupportImpl::SandboxSupport
184 : public blink::WebSandboxSupport {
185 public:
186 virtual ~SandboxSupport() {}
188 #if defined(OS_WIN)
189 virtual bool ensureFontLoaded(HFONT);
190 #elif defined(OS_MACOSX)
191 virtual bool loadFont(
192 NSFont* src_font,
193 CGFontRef* container,
194 uint32* font_id);
195 #elif defined(OS_POSIX)
196 virtual void getFontFamilyForCharacter(
197 blink::WebUChar32 character,
198 const char* preferred_locale,
199 blink::WebFontFamily* family);
200 virtual void getRenderStyleForStrike(
201 const char* family, int sizeAndStyle, blink::WebFontRenderStyle* out);
203 private:
204 // WebKit likes to ask us for the correct font family to use for a set of
205 // unicode code points. It needs this information frequently so we cache it
206 // here.
207 base::Lock unicode_font_families_mutex_;
208 std::map<int32_t, blink::WebFontFamily> unicode_font_families_;
209 #endif
211 #endif // defined(OS_ANDROID)
213 //------------------------------------------------------------------------------
215 RendererWebKitPlatformSupportImpl::RendererWebKitPlatformSupportImpl()
216 : clipboard_client_(new RendererClipboardClient),
217 clipboard_(new WebClipboardImpl(clipboard_client_.get())),
218 mime_registry_(new RendererWebKitPlatformSupportImpl::MimeRegistry),
219 sudden_termination_disables_(0),
220 plugin_refresh_allowed_(true),
221 child_thread_loop_(base::MessageLoopProxy::current()),
222 web_scrollbar_behavior_(new WebScrollbarBehaviorImpl) {
223 if (g_sandbox_enabled && sandboxEnabled()) {
224 sandbox_support_.reset(
225 new RendererWebKitPlatformSupportImpl::SandboxSupport);
226 } else {
227 DVLOG(1) << "Disabling sandbox support for testing.";
230 // ChildThread may not exist in some tests.
231 if (ChildThread::current()) {
232 sync_message_filter_ = ChildThread::current()->sync_message_filter();
233 thread_safe_sender_ = ChildThread::current()->thread_safe_sender();
234 quota_message_filter_ = ChildThread::current()->quota_message_filter();
235 blob_registry_.reset(new WebBlobRegistryImpl(thread_safe_sender_));
236 web_idb_factory_.reset(new WebIDBFactoryImpl(thread_safe_sender_));
237 web_database_observer_impl_.reset(
238 new WebDatabaseObserverImpl(sync_message_filter_));
242 RendererWebKitPlatformSupportImpl::~RendererWebKitPlatformSupportImpl() {
243 WebFileSystemImpl::DeleteThreadSpecificInstance();
246 //------------------------------------------------------------------------------
248 blink::WebClipboard* RendererWebKitPlatformSupportImpl::clipboard() {
249 blink::WebClipboard* clipboard =
250 GetContentClient()->renderer()->OverrideWebClipboard();
251 if (clipboard)
252 return clipboard;
253 return clipboard_.get();
256 blink::WebMimeRegistry* RendererWebKitPlatformSupportImpl::mimeRegistry() {
257 return mime_registry_.get();
260 blink::WebFileUtilities*
261 RendererWebKitPlatformSupportImpl::fileUtilities() {
262 if (!file_utilities_) {
263 file_utilities_.reset(new FileUtilities(thread_safe_sender_.get()));
264 file_utilities_->set_sandbox_enabled(sandboxEnabled());
266 return file_utilities_.get();
269 blink::WebSandboxSupport* RendererWebKitPlatformSupportImpl::sandboxSupport() {
270 #if defined(OS_ANDROID)
271 // WebKit doesn't use WebSandboxSupport on android.
272 return NULL;
273 #else
274 return sandbox_support_.get();
275 #endif
278 blink::WebCookieJar* RendererWebKitPlatformSupportImpl::cookieJar() {
279 NOTREACHED() << "Use WebFrameClient::cookieJar() instead!";
280 return NULL;
283 blink::WebThemeEngine* RendererWebKitPlatformSupportImpl::themeEngine() {
284 blink::WebThemeEngine* theme_engine =
285 GetContentClient()->renderer()->OverrideThemeEngine();
286 if (theme_engine)
287 return theme_engine;
288 return WebKitPlatformSupportImpl::themeEngine();
291 bool RendererWebKitPlatformSupportImpl::sandboxEnabled() {
292 // As explained in Platform.h, this function is used to decide
293 // whether to allow file system operations to come out of WebKit or not.
294 // Even if the sandbox is disabled, there's no reason why the code should
295 // act any differently...unless we're in single process mode. In which
296 // case, we have no other choice. Platform.h discourages using
297 // this switch unless absolutely necessary, so hopefully we won't end up
298 // with too many code paths being different in single-process mode.
299 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess);
302 unsigned long long RendererWebKitPlatformSupportImpl::visitedLinkHash(
303 const char* canonical_url,
304 size_t length) {
305 return GetContentClient()->renderer()->VisitedLinkHash(canonical_url, length);
308 bool RendererWebKitPlatformSupportImpl::isLinkVisited(
309 unsigned long long link_hash) {
310 return GetContentClient()->renderer()->IsLinkVisited(link_hash);
313 blink::WebMessagePortChannel*
314 RendererWebKitPlatformSupportImpl::createMessagePortChannel() {
315 return new WebMessagePortChannelImpl(child_thread_loop_.get());
318 blink::WebPrescientNetworking*
319 RendererWebKitPlatformSupportImpl::prescientNetworking() {
320 return GetContentClient()->renderer()->GetPrescientNetworking();
323 bool
324 RendererWebKitPlatformSupportImpl::CheckPreparsedJsCachingEnabled() const {
325 static bool checked = false;
326 static bool result = false;
327 if (!checked) {
328 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
329 result = command_line.HasSwitch(switches::kEnablePreparsedJsCaching);
330 checked = true;
332 return result;
335 void RendererWebKitPlatformSupportImpl::cacheMetadata(
336 const blink::WebURL& url,
337 double response_time,
338 const char* data,
339 size_t size) {
340 if (!CheckPreparsedJsCachingEnabled())
341 return;
343 // Let the browser know we generated cacheable metadata for this resource. The
344 // browser may cache it and return it on subsequent responses to speed
345 // the processing of this resource.
346 std::vector<char> copy(data, data + size);
347 RenderThread::Get()->Send(
348 new ViewHostMsg_DidGenerateCacheableMetadata(url, response_time, copy));
351 WebString RendererWebKitPlatformSupportImpl::defaultLocale() {
352 return base::ASCIIToUTF16(RenderThread::Get()->GetLocale());
355 void RendererWebKitPlatformSupportImpl::suddenTerminationChanged(bool enabled) {
356 if (enabled) {
357 // We should not get more enables than disables, but we want it to be a
358 // non-fatal error if it does happen.
359 DCHECK_GT(sudden_termination_disables_, 0);
360 sudden_termination_disables_ = std::max(sudden_termination_disables_ - 1,
362 if (sudden_termination_disables_ != 0)
363 return;
364 } else {
365 sudden_termination_disables_++;
366 if (sudden_termination_disables_ != 1)
367 return;
370 RenderThread* thread = RenderThread::Get();
371 if (thread) // NULL in unittests.
372 thread->Send(new ViewHostMsg_SuddenTerminationChanged(enabled));
375 WebStorageNamespace*
376 RendererWebKitPlatformSupportImpl::createLocalStorageNamespace() {
377 return new WebStorageNamespaceImpl();
381 //------------------------------------------------------------------------------
383 WebIDBFactory* RendererWebKitPlatformSupportImpl::idbFactory() {
384 return web_idb_factory_.get();
387 //------------------------------------------------------------------------------
389 WebFileSystem* RendererWebKitPlatformSupportImpl::fileSystem() {
390 return WebFileSystemImpl::ThreadSpecificInstance(child_thread_loop_.get());
393 //------------------------------------------------------------------------------
395 WebMimeRegistry::SupportsType
396 RendererWebKitPlatformSupportImpl::MimeRegistry::supportsMediaMIMEType(
397 const WebString& mime_type,
398 const WebString& codecs,
399 const WebString& key_system) {
400 const std::string mime_type_ascii = ToASCIIOrEmpty(mime_type);
401 // Not supporting the container is a flat-out no.
402 if (!net::IsSupportedMediaMimeType(mime_type_ascii))
403 return IsNotSupported;
405 if (!key_system.isEmpty()) {
406 // Check whether the key system is supported with the mime_type and codecs.
408 // Chromium only supports ASCII parameters.
409 if (!IsStringASCII(key_system))
410 return IsNotSupported;
412 std::string key_system_ascii =
413 GetUnprefixedKeySystemName(UTF16ToASCII(key_system));
414 std::vector<std::string> strict_codecs;
415 bool strip_suffix = !net::IsStrictMediaMimeType(mime_type_ascii);
416 net::ParseCodecString(ToASCIIOrEmpty(codecs), &strict_codecs, strip_suffix);
418 if (!IsSupportedKeySystemWithMediaMimeType(
419 mime_type_ascii, strict_codecs, key_system_ascii)) {
420 return IsNotSupported;
423 // Continue processing the mime_type and codecs.
426 // Check list of strict codecs to see if it is supported.
427 if (net::IsStrictMediaMimeType(mime_type_ascii)) {
428 // We support the container, but no codecs were specified.
429 if (codecs.isNull())
430 return MayBeSupported;
432 // Check if the codecs are a perfect match.
433 std::vector<std::string> strict_codecs;
434 net::ParseCodecString(ToASCIIOrEmpty(codecs), &strict_codecs, false);
435 if (!net::IsSupportedStrictMediaMimeType(mime_type_ascii, strict_codecs))
436 return IsNotSupported;
438 // Good to go!
439 return IsSupported;
442 // If we don't recognize the codec, it's possible we support it.
443 std::vector<std::string> parsed_codecs;
444 net::ParseCodecString(ToASCIIOrEmpty(codecs), &parsed_codecs, true);
445 if (!net::AreSupportedMediaCodecs(parsed_codecs))
446 return MayBeSupported;
448 // Otherwise we have a perfect match.
449 return IsSupported;
452 bool
453 RendererWebKitPlatformSupportImpl::MimeRegistry::supportsMediaSourceMIMEType(
454 const blink::WebString& mime_type,
455 const WebString& codecs) {
456 const std::string mime_type_ascii = ToASCIIOrEmpty(mime_type);
457 std::vector<std::string> parsed_codec_ids;
458 net::ParseCodecString(ToASCIIOrEmpty(codecs), &parsed_codec_ids, false);
459 if (mime_type_ascii.empty())
460 return false;
461 return media::StreamParserFactory::IsTypeSupported(
462 mime_type_ascii, parsed_codec_ids);
465 bool
466 RendererWebKitPlatformSupportImpl::MimeRegistry::supportsEncryptedMediaMIMEType(
467 const WebString& key_system,
468 const WebString& mime_type,
469 const WebString& codecs) {
470 // Chromium only supports ASCII parameters.
471 if (!IsStringASCII(key_system) || !IsStringASCII(mime_type) ||
472 !IsStringASCII(codecs)) {
473 return false;
476 if (key_system.isEmpty())
477 return false;
479 const std::string mime_type_ascii = UTF16ToASCII(mime_type);
481 std::vector<std::string> codec_vector;
482 bool strip_suffix = !net::IsStrictMediaMimeType(mime_type_ascii);
483 net::ParseCodecString(UTF16ToASCII(codecs), &codec_vector, strip_suffix);
485 return IsSupportedKeySystemWithMediaMimeType(
486 mime_type_ascii, codec_vector, UTF16ToASCII(key_system));
489 WebString
490 RendererWebKitPlatformSupportImpl::MimeRegistry::mimeTypeForExtension(
491 const WebString& file_extension) {
492 if (IsPluginProcess())
493 return SimpleWebMimeRegistryImpl::mimeTypeForExtension(file_extension);
495 // The sandbox restricts our access to the registry, so we need to proxy
496 // these calls over to the browser process.
497 std::string mime_type;
498 RenderThread::Get()->Send(
499 new MimeRegistryMsg_GetMimeTypeFromExtension(
500 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type));
501 return base::ASCIIToUTF16(mime_type);
504 WebString RendererWebKitPlatformSupportImpl::MimeRegistry::mimeTypeFromFile(
505 const WebString& file_path) {
506 if (IsPluginProcess())
507 return SimpleWebMimeRegistryImpl::mimeTypeFromFile(file_path);
509 // The sandbox restricts our access to the registry, so we need to proxy
510 // these calls over to the browser process.
511 std::string mime_type;
512 RenderThread::Get()->Send(new MimeRegistryMsg_GetMimeTypeFromFile(
513 base::FilePath::FromUTF16Unsafe(file_path),
514 &mime_type));
515 return base::ASCIIToUTF16(mime_type);
518 //------------------------------------------------------------------------------
520 bool RendererWebKitPlatformSupportImpl::FileUtilities::getFileInfo(
521 const WebString& path,
522 WebFileInfo& web_file_info) {
523 base::File::Info file_info;
524 base::File::Error status;
525 if (!SendSyncMessageFromAnyThread(new FileUtilitiesMsg_GetFileInfo(
526 base::FilePath::FromUTF16Unsafe(path), &file_info, &status)) ||
527 status != base::File::FILE_OK) {
528 return false;
530 FileInfoToWebFileInfo(file_info, &web_file_info);
531 web_file_info.platformPath = path;
532 return true;
535 bool RendererWebKitPlatformSupportImpl::FileUtilities::
536 SendSyncMessageFromAnyThread(IPC::SyncMessage* msg) const {
537 base::TimeTicks begin = base::TimeTicks::Now();
538 const bool success = thread_safe_sender_->Send(msg);
539 base::TimeDelta delta = base::TimeTicks::Now() - begin;
540 UMA_HISTOGRAM_TIMES("RendererSyncIPC.ElapsedTime", delta);
541 return success;
544 //------------------------------------------------------------------------------
546 #if defined(OS_WIN)
548 bool RendererWebKitPlatformSupportImpl::SandboxSupport::ensureFontLoaded(
549 HFONT font) {
550 LOGFONT logfont;
551 GetObject(font, sizeof(LOGFONT), &logfont);
552 RenderThread::Get()->PreCacheFont(logfont);
553 return true;
556 #elif defined(OS_MACOSX)
558 bool RendererWebKitPlatformSupportImpl::SandboxSupport::loadFont(
559 NSFont* src_font, CGFontRef* out, uint32* font_id) {
560 uint32 font_data_size;
561 FontDescriptor src_font_descriptor(src_font);
562 base::SharedMemoryHandle font_data;
563 if (!RenderThread::Get()->Send(new ViewHostMsg_LoadFont(
564 src_font_descriptor, &font_data_size, &font_data, font_id))) {
565 *out = NULL;
566 *font_id = 0;
567 return false;
570 if (font_data_size == 0 || font_data == base::SharedMemory::NULLHandle() ||
571 *font_id == 0) {
572 LOG(ERROR) << "Bad response from ViewHostMsg_LoadFont() for " <<
573 src_font_descriptor.font_name;
574 *out = NULL;
575 *font_id = 0;
576 return false;
579 // TODO(jeremy): Need to call back into WebKit to make sure that the font
580 // isn't already activated, based on the font id. If it's already
581 // activated, don't reactivate it here - crbug.com/72727 .
583 return FontLoader::CGFontRefFromBuffer(font_data, font_data_size, out);
586 #elif defined(OS_ANDROID)
588 // WebKit doesn't use WebSandboxSupport on android so we don't need to
589 // implement anything here. This is cleaner to support than excluding the
590 // whole class for android.
592 #elif defined(OS_POSIX)
594 void
595 RendererWebKitPlatformSupportImpl::SandboxSupport::getFontFamilyForCharacter(
596 blink::WebUChar32 character,
597 const char* preferred_locale,
598 blink::WebFontFamily* family) {
599 base::AutoLock lock(unicode_font_families_mutex_);
600 const std::map<int32_t, blink::WebFontFamily>::const_iterator iter =
601 unicode_font_families_.find(character);
602 if (iter != unicode_font_families_.end()) {
603 family->name = iter->second.name;
604 family->isBold = iter->second.isBold;
605 family->isItalic = iter->second.isItalic;
606 return;
609 GetFontFamilyForCharacter(character, preferred_locale, family);
610 unicode_font_families_.insert(std::make_pair(character, *family));
613 void
614 RendererWebKitPlatformSupportImpl::SandboxSupport::getRenderStyleForStrike(
615 const char* family, int sizeAndStyle, blink::WebFontRenderStyle* out) {
616 GetRenderStyleForStrike(family, sizeAndStyle, out);
619 #endif
621 //------------------------------------------------------------------------------
623 Platform::FileHandle
624 RendererWebKitPlatformSupportImpl::databaseOpenFile(
625 const WebString& vfs_file_name, int desired_flags) {
626 return DatabaseUtil::DatabaseOpenFile(
627 vfs_file_name, desired_flags, sync_message_filter_.get());
630 int RendererWebKitPlatformSupportImpl::databaseDeleteFile(
631 const WebString& vfs_file_name, bool sync_dir) {
632 return DatabaseUtil::DatabaseDeleteFile(
633 vfs_file_name, sync_dir, sync_message_filter_.get());
636 long RendererWebKitPlatformSupportImpl::databaseGetFileAttributes(
637 const WebString& vfs_file_name) {
638 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name,
639 sync_message_filter_.get());
642 long long RendererWebKitPlatformSupportImpl::databaseGetFileSize(
643 const WebString& vfs_file_name) {
644 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name,
645 sync_message_filter_.get());
648 long long RendererWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
649 const WebString& origin_identifier) {
650 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier,
651 sync_message_filter_.get());
654 bool RendererWebKitPlatformSupportImpl::canAccelerate2dCanvas() {
655 RenderThreadImpl* thread = RenderThreadImpl::current();
656 GpuChannelHost* host = thread->EstablishGpuChannelSync(
657 CAUSE_FOR_GPU_LAUNCH_CANVAS_2D);
658 if (!host)
659 return false;
661 return host->gpu_info().SupportsAccelerated2dCanvas();
664 bool RendererWebKitPlatformSupportImpl::isThreadedCompositingEnabled() {
665 return !!RenderThreadImpl::current()->compositor_message_loop_proxy().get();
668 double RendererWebKitPlatformSupportImpl::audioHardwareSampleRate() {
669 RenderThreadImpl* thread = RenderThreadImpl::current();
670 return thread->GetAudioHardwareConfig()->GetOutputSampleRate();
673 size_t RendererWebKitPlatformSupportImpl::audioHardwareBufferSize() {
674 RenderThreadImpl* thread = RenderThreadImpl::current();
675 return thread->GetAudioHardwareConfig()->GetOutputBufferSize();
678 unsigned RendererWebKitPlatformSupportImpl::audioHardwareOutputChannels() {
679 RenderThreadImpl* thread = RenderThreadImpl::current();
680 return thread->GetAudioHardwareConfig()->GetOutputChannels();
683 WebDatabaseObserver* RendererWebKitPlatformSupportImpl::databaseObserver() {
684 return web_database_observer_impl_.get();
687 // TODO(crogers): remove deprecated API as soon as WebKit calls new API.
688 WebAudioDevice*
689 RendererWebKitPlatformSupportImpl::createAudioDevice(
690 size_t buffer_size,
691 unsigned channels,
692 double sample_rate,
693 WebAudioDevice::RenderCallback* callback) {
694 return createAudioDevice(
695 buffer_size, 0, channels, sample_rate, callback, "default");
698 // TODO(crogers): remove deprecated API as soon as WebKit calls new API.
699 WebAudioDevice*
700 RendererWebKitPlatformSupportImpl::createAudioDevice(
701 size_t buffer_size,
702 unsigned input_channels,
703 unsigned channels,
704 double sample_rate,
705 WebAudioDevice::RenderCallback* callback) {
706 return createAudioDevice(
707 buffer_size, input_channels, channels, sample_rate, callback, "default");
710 WebAudioDevice*
711 RendererWebKitPlatformSupportImpl::createAudioDevice(
712 size_t buffer_size,
713 unsigned input_channels,
714 unsigned channels,
715 double sample_rate,
716 WebAudioDevice::RenderCallback* callback,
717 const blink::WebString& input_device_id) {
718 // Use a mock for testing.
719 blink::WebAudioDevice* mock_device =
720 GetContentClient()->renderer()->OverrideCreateAudioDevice(sample_rate);
721 if (mock_device)
722 return mock_device;
724 // The |channels| does not exactly identify the channel layout of the
725 // device. The switch statement below assigns a best guess to the channel
726 // layout based on number of channels.
727 // TODO(crogers): WebKit should give the channel layout instead of the hard
728 // channel count.
729 media::ChannelLayout layout = media::CHANNEL_LAYOUT_UNSUPPORTED;
730 switch (channels) {
731 case 1:
732 layout = media::CHANNEL_LAYOUT_MONO;
733 break;
734 case 2:
735 layout = media::CHANNEL_LAYOUT_STEREO;
736 break;
737 case 3:
738 layout = media::CHANNEL_LAYOUT_2_1;
739 break;
740 case 4:
741 layout = media::CHANNEL_LAYOUT_4_0;
742 break;
743 case 5:
744 layout = media::CHANNEL_LAYOUT_5_0;
745 break;
746 case 6:
747 layout = media::CHANNEL_LAYOUT_5_1;
748 break;
749 case 7:
750 layout = media::CHANNEL_LAYOUT_7_0;
751 break;
752 case 8:
753 layout = media::CHANNEL_LAYOUT_7_1;
754 break;
755 default:
756 layout = media::CHANNEL_LAYOUT_STEREO;
759 int session_id = 0;
760 if (input_device_id.isNull() ||
761 !base::StringToInt(base::UTF16ToUTF8(input_device_id), &session_id)) {
762 if (input_channels > 0)
763 DLOG(WARNING) << "createAudioDevice(): request for audio input ignored";
765 input_channels = 0;
768 media::AudioParameters params(
769 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
770 layout, input_channels,
771 static_cast<int>(sample_rate), 16, buffer_size,
772 media::AudioParameters::NO_EFFECTS);
774 return new RendererWebAudioDeviceImpl(params, callback, session_id);
777 #if defined(OS_ANDROID)
778 bool RendererWebKitPlatformSupportImpl::loadAudioResource(
779 blink::WebAudioBus* destination_bus, const char* audio_file_data,
780 size_t data_size, double sample_rate) {
781 return DecodeAudioFileData(destination_bus,
782 audio_file_data,
783 data_size,
784 sample_rate,
785 thread_safe_sender_);
787 #else
788 bool RendererWebKitPlatformSupportImpl::loadAudioResource(
789 blink::WebAudioBus* destination_bus, const char* audio_file_data,
790 size_t data_size, double sample_rate) {
791 return DecodeAudioFileData(
792 destination_bus, audio_file_data, data_size, sample_rate);
794 #endif // defined(OS_ANDROID)
796 //------------------------------------------------------------------------------
798 blink::WebContentDecryptionModule*
799 RendererWebKitPlatformSupportImpl::createContentDecryptionModule(
800 const blink::WebString& key_system) {
801 return WebContentDecryptionModuleImpl::Create(key_system);
804 //------------------------------------------------------------------------------
806 blink::WebMIDIAccessor*
807 RendererWebKitPlatformSupportImpl::createMIDIAccessor(
808 blink::WebMIDIAccessorClient* client) {
809 blink::WebMIDIAccessor* accessor =
810 GetContentClient()->renderer()->OverrideCreateMIDIAccessor(client);
811 if (accessor)
812 return accessor;
814 return new RendererWebMIDIAccessorImpl(client);
817 void RendererWebKitPlatformSupportImpl::getPluginList(
818 bool refresh,
819 blink::WebPluginListBuilder* builder) {
820 #if defined(ENABLE_PLUGINS)
821 std::vector<WebPluginInfo> plugins;
822 if (!plugin_refresh_allowed_)
823 refresh = false;
824 RenderThread::Get()->Send(
825 new ViewHostMsg_GetPlugins(refresh, &plugins));
826 for (size_t i = 0; i < plugins.size(); ++i) {
827 const WebPluginInfo& plugin = plugins[i];
829 builder->addPlugin(
830 plugin.name, plugin.desc,
831 plugin.path.BaseName().AsUTF16Unsafe());
833 for (size_t j = 0; j < plugin.mime_types.size(); ++j) {
834 const WebPluginMimeType& mime_type = plugin.mime_types[j];
836 builder->addMediaTypeToLastPlugin(
837 WebString::fromUTF8(mime_type.mime_type), mime_type.description);
839 for (size_t k = 0; k < mime_type.file_extensions.size(); ++k) {
840 builder->addFileExtensionToLastMediaType(
841 WebString::fromUTF8(mime_type.file_extensions[k]));
845 #endif
848 //------------------------------------------------------------------------------
850 blink::WebPublicSuffixList*
851 RendererWebKitPlatformSupportImpl::publicSuffixList() {
852 return &public_suffix_list_;
855 //------------------------------------------------------------------------------
857 blink::WebString
858 RendererWebKitPlatformSupportImpl::signedPublicKeyAndChallengeString(
859 unsigned key_size_index,
860 const blink::WebString& challenge,
861 const blink::WebURL& url) {
862 std::string signed_public_key;
863 RenderThread::Get()->Send(new ViewHostMsg_Keygen(
864 static_cast<uint32>(key_size_index),
865 challenge.utf8(),
866 GURL(url),
867 &signed_public_key));
868 return WebString::fromUTF8(signed_public_key);
871 //------------------------------------------------------------------------------
873 void RendererWebKitPlatformSupportImpl::screenColorProfile(
874 WebVector<char>* to_profile) {
875 #if defined(OS_WIN)
876 // On Windows screen color profile is only available in the browser.
877 std::vector<char> profile;
878 // This Send() can be called from any impl-side thread. Use a thread
879 // safe send to avoid crashing trying to access RenderThread::Get(),
880 // which is not accessible from arbitrary threads.
881 thread_safe_sender_->Send(
882 new ViewHostMsg_GetMonitorColorProfile(&profile));
883 *to_profile = profile;
884 #else
885 // On other platforms, the primary monitor color profile can be read
886 // directly.
887 gfx::ColorProfile profile;
888 *to_profile = profile.profile();
889 #endif
892 //------------------------------------------------------------------------------
894 blink::WebScrollbarBehavior*
895 RendererWebKitPlatformSupportImpl::scrollbarBehavior() {
896 return web_scrollbar_behavior_.get();
899 //------------------------------------------------------------------------------
901 WebBlobRegistry* RendererWebKitPlatformSupportImpl::blobRegistry() {
902 // blob_registry_ can be NULL when running some tests.
903 return blob_registry_.get();
906 //------------------------------------------------------------------------------
908 void RendererWebKitPlatformSupportImpl::sampleGamepads(WebGamepads& gamepads) {
909 if (g_test_gamepads == 0) {
910 RenderThreadImpl::current()->SampleGamepads(&gamepads);
911 } else {
912 gamepads = g_test_gamepads.Get();
916 blink::WebString RendererWebKitPlatformSupportImpl::userAgent(
917 const blink::WebURL& url) {
918 return WebKitPlatformSupportImpl::userAgent(url);
921 //------------------------------------------------------------------------------
923 WebRTCPeerConnectionHandler*
924 RendererWebKitPlatformSupportImpl::createRTCPeerConnectionHandler(
925 WebRTCPeerConnectionHandlerClient* client) {
926 RenderThreadImpl* render_thread = RenderThreadImpl::current();
927 DCHECK(render_thread);
928 if (!render_thread)
929 return NULL;
931 #if defined(ENABLE_WEBRTC)
932 WebRTCPeerConnectionHandler* peer_connection_handler =
933 GetContentClient()->renderer()->OverrideCreateWebRTCPeerConnectionHandler(
934 client);
935 if (peer_connection_handler)
936 return peer_connection_handler;
938 MediaStreamDependencyFactory* rtc_dependency_factory =
939 render_thread->GetMediaStreamDependencyFactory();
940 return rtc_dependency_factory->CreateRTCPeerConnectionHandler(client);
941 #else
942 return NULL;
943 #endif // defined(ENABLE_WEBRTC)
946 //------------------------------------------------------------------------------
948 WebMediaStreamCenter*
949 RendererWebKitPlatformSupportImpl::createMediaStreamCenter(
950 WebMediaStreamCenterClient* client) {
951 RenderThreadImpl* render_thread = RenderThreadImpl::current();
952 DCHECK(render_thread);
953 if (!render_thread)
954 return NULL;
955 return render_thread->CreateMediaStreamCenter(client);
958 // static
959 bool RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(
960 bool enable) {
961 bool was_enabled = g_sandbox_enabled;
962 g_sandbox_enabled = enable;
963 return was_enabled;
966 // static
967 void RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(
968 const WebGamepads& pads) {
969 g_test_gamepads.Get() = pads;
972 //------------------------------------------------------------------------------
974 blink::WebSpeechSynthesizer*
975 RendererWebKitPlatformSupportImpl::createSpeechSynthesizer(
976 blink::WebSpeechSynthesizerClient* client) {
977 return GetContentClient()->renderer()->OverrideSpeechSynthesizer(client);
980 //------------------------------------------------------------------------------
982 bool RendererWebKitPlatformSupportImpl::processMemorySizesInBytes(
983 size_t* private_bytes, size_t* shared_bytes) {
984 content::RenderThread::Get()->Send(
985 new ViewHostMsg_GetProcessMemorySizes(private_bytes, shared_bytes));
986 return true;
989 //------------------------------------------------------------------------------
992 blink::WebGraphicsContext3D*
993 RendererWebKitPlatformSupportImpl::createOffscreenGraphicsContext3D(
994 #ifdef ENABLE_EXPLICIT_GL_SHARE_GROUPS
995 const blink::WebGraphicsContext3D::Attributes& attributes,
996 blink::WebGraphicsContext3D* share_context) {
997 #else
998 const blink::WebGraphicsContext3D::Attributes& attributes) {
999 blink::WebGraphicsContext3D* share_context = NULL;
1000 #endif
1001 if (!RenderThreadImpl::current())
1002 return NULL;
1004 scoped_refptr<GpuChannelHost> gpu_channel_host(
1005 RenderThreadImpl::current()->EstablishGpuChannelSync(
1006 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE));
1008 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits limits;
1010 CommandLine* command_line = CommandLine::ForCurrentProcess();
1011 if (command_line->HasSwitch(switches::kWebGLCommandBufferSizeKb)) {
1012 std::string size_string = command_line->GetSwitchValueASCII(
1013 switches::kWebGLCommandBufferSizeKb);
1014 size_t buffer_size_kb;
1015 if (base::StringToSizeT(size_string, &buffer_size_kb)) {
1016 limits.command_buffer_size = buffer_size_kb * 1024;
1020 return WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
1021 gpu_channel_host.get(),
1022 attributes,
1023 GURL(attributes.topDocumentURL),
1024 limits,
1025 static_cast<WebGraphicsContext3DCommandBufferImpl*>(share_context));
1028 //------------------------------------------------------------------------------
1030 blink::WebGraphicsContext3DProvider* RendererWebKitPlatformSupportImpl::
1031 createSharedOffscreenGraphicsContext3DProvider() {
1032 scoped_refptr<webkit::gpu::ContextProviderWebContext> provider =
1033 RenderThreadImpl::current()->SharedMainThreadContextProvider();
1034 if (!provider)
1035 return NULL;
1036 return new WebGraphicsContext3DProviderImpl(provider);
1039 //------------------------------------------------------------------------------
1041 blink::WebCompositorSupport*
1042 RendererWebKitPlatformSupportImpl::compositorSupport() {
1043 return &compositor_support_;
1046 //------------------------------------------------------------------------------
1048 blink::WebString RendererWebKitPlatformSupportImpl::convertIDNToUnicode(
1049 const blink::WebString& host,
1050 const blink::WebString& languages) {
1051 return net::IDNToUnicode(host.utf8(), languages.utf8());
1054 //------------------------------------------------------------------------------
1056 void RendererWebKitPlatformSupportImpl::setDeviceMotionListener(
1057 blink::WebDeviceMotionListener* listener) {
1058 if (g_test_device_motion_data == 0) {
1059 if (!device_motion_event_pump_) {
1060 device_motion_event_pump_.reset(new DeviceMotionEventPump);
1061 device_motion_event_pump_->Attach(RenderThreadImpl::current());
1063 device_motion_event_pump_->SetListener(listener);
1064 } else if (listener) {
1065 // Testing mode: just echo the test data to the listener.
1066 base::MessageLoopProxy::current()->PostTask(
1067 FROM_HERE,
1068 base::Bind(&blink::WebDeviceMotionListener::didChangeDeviceMotion,
1069 base::Unretained(listener),
1070 g_test_device_motion_data.Get()));
1074 // static
1075 void RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(
1076 const blink::WebDeviceMotionData& data) {
1077 g_test_device_motion_data.Get() = data;
1080 //------------------------------------------------------------------------------
1082 void RendererWebKitPlatformSupportImpl::setDeviceOrientationListener(
1083 blink::WebDeviceOrientationListener* listener) {
1084 if (g_test_device_orientation_data == 0) {
1085 if (!device_orientation_event_pump_) {
1086 device_orientation_event_pump_.reset(new DeviceOrientationEventPump);
1087 device_orientation_event_pump_->Attach(RenderThreadImpl::current());
1089 device_orientation_event_pump_->SetListener(listener);
1090 } else if (listener) {
1091 // Testing mode: just echo the test data to the listener.
1092 base::MessageLoopProxy::current()->PostTask(
1093 FROM_HERE,
1094 base::Bind(
1095 &blink::WebDeviceOrientationListener::didChangeDeviceOrientation,
1096 base::Unretained(listener),
1097 g_test_device_orientation_data.Get()));
1101 // static
1102 void RendererWebKitPlatformSupportImpl::SetMockDeviceOrientationDataForTesting(
1103 const blink::WebDeviceOrientationData& data) {
1104 g_test_device_orientation_data.Get() = data;
1107 //------------------------------------------------------------------------------
1109 blink::WebCrypto* RendererWebKitPlatformSupportImpl::crypto() {
1110 if (!web_crypto_)
1111 web_crypto_.reset(new WebCryptoImpl());
1112 return web_crypto_.get();
1116 //------------------------------------------------------------------------------
1118 void RendererWebKitPlatformSupportImpl::vibrate(unsigned int milliseconds) {
1119 RenderThread::Get()->Send(
1120 new ViewHostMsg_Vibrate(base::checked_cast<int64>(milliseconds)));
1123 void RendererWebKitPlatformSupportImpl::cancelVibration() {
1124 RenderThread::Get()->Send(new ViewHostMsg_CancelVibration());
1127 //------------------------------------------------------------------------------
1129 void RendererWebKitPlatformSupportImpl::setScreenOrientationListener(
1130 blink::WebScreenOrientationListener* listener) {
1131 if (!screen_orientation_dispatcher_) {
1132 screen_orientation_dispatcher_.reset(
1133 new ScreenOrientationDispatcher(RenderThread::Get()));
1136 screen_orientation_dispatcher_->setListener(listener);
1139 void RendererWebKitPlatformSupportImpl::lockOrientation(
1140 blink::WebScreenOrientations orientations) {
1141 RenderThread::Get()->Send(new ScreenOrientationHostMsg_Lock(orientations));
1144 void RendererWebKitPlatformSupportImpl::unlockOrientation() {
1145 RenderThread::Get()->Send(new ScreenOrientationHostMsg_Unlock);
1148 //------------------------------------------------------------------------------
1150 void RendererWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
1151 const blink::WebURL& storage_partition,
1152 blink::WebStorageQuotaType type,
1153 blink::WebStorageQuotaCallbacks callbacks) {
1154 if (!thread_safe_sender_.get() || !quota_message_filter_.get())
1155 return;
1156 QuotaDispatcher::ThreadSpecificInstance(
1157 thread_safe_sender_.get(),
1158 quota_message_filter_.get())->QueryStorageUsageAndQuota(
1159 storage_partition,
1160 static_cast<quota::StorageType>(type),
1161 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
1164 } // namespace content