Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / renderer / renderer_webkitplatformsupport_impl.cc
blobb507a732a1eacb48b0ecdf3cac6f32fa3f736a22
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_sensors/device_motion_event_pump.h"
42 #include "content/renderer/device_sensors/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/webgraphicscontext3d_provider_impl.h"
56 #include "content/renderer/webpublicsuffixlist_impl.h"
57 #include "gpu/config/gpu_info.h"
58 #include "ipc/ipc_sync_message_filter.h"
59 #include "media/audio/audio_output_device.h"
60 #include "media/base/audio_hardware_config.h"
61 #include "media/filters/stream_parser_factory.h"
62 #include "net/base/mime_util.h"
63 #include "net/base/net_util.h"
64 #include "third_party/WebKit/public/platform/WebBlobRegistry.h"
65 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h"
66 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
67 #include "third_party/WebKit/public/platform/WebFileInfo.h"
68 #include "third_party/WebKit/public/platform/WebGamepads.h"
69 #include "third_party/WebKit/public/platform/WebMediaStreamCenter.h"
70 #include "third_party/WebKit/public/platform/WebMediaStreamCenterClient.h"
71 #include "third_party/WebKit/public/platform/WebPluginListBuilder.h"
72 #include "third_party/WebKit/public/platform/WebScreenOrientationListener.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/android/synchronous_compositor_factory.h"
82 #include "content/renderer/media/android/audio_decoder_android.h"
83 #endif
85 #if defined(OS_MACOSX)
86 #include "content/common/mac/font_descriptor.h"
87 #include "content/common/mac/font_loader.h"
88 #include "content/renderer/webscrollbarbehavior_impl_mac.h"
89 #include "third_party/WebKit/public/platform/mac/WebSandboxSupport.h"
90 #endif
92 #if defined(OS_POSIX)
93 #include "base/file_descriptor_posix.h"
94 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
95 #include <map>
96 #include <string>
98 #include "base/synchronization/lock.h"
99 #include "content/common/child_process_sandbox_support_impl_linux.h"
100 #include "third_party/WebKit/public/platform/linux/WebFontFamily.h"
101 #include "third_party/WebKit/public/platform/linux/WebSandboxSupport.h"
102 #include "third_party/icu/source/common/unicode/utf16.h"
103 #endif
104 #endif
106 #if defined(OS_WIN)
107 #include "content/common/child_process_messages.h"
108 #include "third_party/WebKit/public/platform/win/WebSandboxSupport.h"
109 #endif
111 #if defined(USE_AURA)
112 #include "content/renderer/webscrollbarbehavior_impl_gtkoraura.h"
113 #elif !defined(OS_MACOSX)
114 #include "third_party/WebKit/public/platform/WebScrollbarBehavior.h"
115 #define WebScrollbarBehaviorImpl blink::WebScrollbarBehavior
116 #endif
118 using blink::Platform;
119 using blink::WebAudioDevice;
120 using blink::WebBlobRegistry;
121 using blink::WebDatabaseObserver;
122 using blink::WebFileInfo;
123 using blink::WebFileSystem;
124 using blink::WebGamepad;
125 using blink::WebGamepads;
126 using blink::WebIDBFactory;
127 using blink::WebMIDIAccessor;
128 using blink::WebMediaStreamCenter;
129 using blink::WebMediaStreamCenterClient;
130 using blink::WebMimeRegistry;
131 using blink::WebRTCPeerConnectionHandler;
132 using blink::WebRTCPeerConnectionHandlerClient;
133 using blink::WebStorageNamespace;
134 using blink::WebString;
135 using blink::WebURL;
136 using blink::WebVector;
138 namespace content {
140 static bool g_sandbox_enabled = true;
141 static blink::WebGamepadListener* web_gamepad_listener = NULL;
142 base::LazyInstance<WebGamepads>::Leaky g_test_gamepads =
143 LAZY_INSTANCE_INITIALIZER;
144 base::LazyInstance<blink::WebDeviceMotionData>::Leaky
145 g_test_device_motion_data = LAZY_INSTANCE_INITIALIZER;
146 base::LazyInstance<blink::WebDeviceOrientationData>::Leaky
147 g_test_device_orientation_data = LAZY_INSTANCE_INITIALIZER;
148 static blink::WebScreenOrientationListener*
149 g_test_screen_orientation_listener = NULL;
151 //------------------------------------------------------------------------------
153 class RendererWebKitPlatformSupportImpl::MimeRegistry
154 : public SimpleWebMimeRegistryImpl {
155 public:
156 virtual blink::WebMimeRegistry::SupportsType supportsMediaMIMEType(
157 const blink::WebString& mime_type,
158 const blink::WebString& codecs,
159 const blink::WebString& key_system);
160 virtual bool supportsMediaSourceMIMEType(const blink::WebString& mime_type,
161 const blink::WebString& codecs);
162 virtual bool supportsEncryptedMediaMIMEType(const WebString& key_system,
163 const WebString& mime_type,
164 const WebString& codecs) OVERRIDE;
165 virtual blink::WebString mimeTypeForExtension(
166 const blink::WebString& file_extension);
167 virtual blink::WebString mimeTypeFromFile(
168 const blink::WebString& file_path);
171 class RendererWebKitPlatformSupportImpl::FileUtilities
172 : public WebFileUtilitiesImpl {
173 public:
174 explicit FileUtilities(ThreadSafeSender* sender)
175 : thread_safe_sender_(sender) {}
176 virtual bool getFileInfo(const WebString& path, WebFileInfo& result);
177 private:
178 bool SendSyncMessageFromAnyThread(IPC::SyncMessage* msg) const;
179 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
182 #if defined(OS_ANDROID)
183 // WebKit doesn't use WebSandboxSupport on android so we don't need to
184 // implement anything here.
185 class RendererWebKitPlatformSupportImpl::SandboxSupport {
187 #else
188 class RendererWebKitPlatformSupportImpl::SandboxSupport
189 : public blink::WebSandboxSupport {
190 public:
191 virtual ~SandboxSupport() {}
193 #if defined(OS_WIN)
194 virtual bool ensureFontLoaded(HFONT);
195 #elif defined(OS_MACOSX)
196 virtual bool loadFont(
197 NSFont* src_font,
198 CGFontRef* container,
199 uint32* font_id);
200 #elif defined(OS_POSIX)
201 virtual void getFontFamilyForCharacter(
202 blink::WebUChar32 character,
203 const char* preferred_locale,
204 blink::WebFontFamily* family);
205 virtual void getRenderStyleForStrike(
206 const char* family, int sizeAndStyle, blink::WebFontRenderStyle* out);
208 private:
209 // WebKit likes to ask us for the correct font family to use for a set of
210 // unicode code points. It needs this information frequently so we cache it
211 // here.
212 base::Lock unicode_font_families_mutex_;
213 std::map<int32_t, blink::WebFontFamily> unicode_font_families_;
214 #endif
216 #endif // defined(OS_ANDROID)
218 //------------------------------------------------------------------------------
220 RendererWebKitPlatformSupportImpl::RendererWebKitPlatformSupportImpl()
221 : clipboard_client_(new RendererClipboardClient),
222 clipboard_(new WebClipboardImpl(clipboard_client_.get())),
223 mime_registry_(new RendererWebKitPlatformSupportImpl::MimeRegistry),
224 sudden_termination_disables_(0),
225 plugin_refresh_allowed_(true),
226 child_thread_loop_(base::MessageLoopProxy::current()),
227 web_scrollbar_behavior_(new WebScrollbarBehaviorImpl) {
228 if (g_sandbox_enabled && sandboxEnabled()) {
229 sandbox_support_.reset(
230 new RendererWebKitPlatformSupportImpl::SandboxSupport);
231 } else {
232 DVLOG(1) << "Disabling sandbox support for testing.";
235 // ChildThread may not exist in some tests.
236 if (ChildThread::current()) {
237 sync_message_filter_ = ChildThread::current()->sync_message_filter();
238 thread_safe_sender_ = ChildThread::current()->thread_safe_sender();
239 quota_message_filter_ = ChildThread::current()->quota_message_filter();
240 blob_registry_.reset(new WebBlobRegistryImpl(thread_safe_sender_));
241 web_idb_factory_.reset(new WebIDBFactoryImpl(thread_safe_sender_));
242 web_database_observer_impl_.reset(
243 new WebDatabaseObserverImpl(sync_message_filter_));
247 RendererWebKitPlatformSupportImpl::~RendererWebKitPlatformSupportImpl() {
248 WebFileSystemImpl::DeleteThreadSpecificInstance();
251 //------------------------------------------------------------------------------
253 blink::WebClipboard* RendererWebKitPlatformSupportImpl::clipboard() {
254 blink::WebClipboard* clipboard =
255 GetContentClient()->renderer()->OverrideWebClipboard();
256 if (clipboard)
257 return clipboard;
258 return clipboard_.get();
261 blink::WebMimeRegistry* RendererWebKitPlatformSupportImpl::mimeRegistry() {
262 return mime_registry_.get();
265 blink::WebFileUtilities*
266 RendererWebKitPlatformSupportImpl::fileUtilities() {
267 if (!file_utilities_) {
268 file_utilities_.reset(new FileUtilities(thread_safe_sender_.get()));
269 file_utilities_->set_sandbox_enabled(sandboxEnabled());
271 return file_utilities_.get();
274 blink::WebSandboxSupport* RendererWebKitPlatformSupportImpl::sandboxSupport() {
275 #if defined(OS_ANDROID)
276 // WebKit doesn't use WebSandboxSupport on android.
277 return NULL;
278 #else
279 return sandbox_support_.get();
280 #endif
283 blink::WebCookieJar* RendererWebKitPlatformSupportImpl::cookieJar() {
284 NOTREACHED() << "Use WebFrameClient::cookieJar() instead!";
285 return NULL;
288 blink::WebThemeEngine* RendererWebKitPlatformSupportImpl::themeEngine() {
289 blink::WebThemeEngine* theme_engine =
290 GetContentClient()->renderer()->OverrideThemeEngine();
291 if (theme_engine)
292 return theme_engine;
293 return BlinkPlatformImpl::themeEngine();
296 bool RendererWebKitPlatformSupportImpl::sandboxEnabled() {
297 // As explained in Platform.h, this function is used to decide
298 // whether to allow file system operations to come out of WebKit or not.
299 // Even if the sandbox is disabled, there's no reason why the code should
300 // act any differently...unless we're in single process mode. In which
301 // case, we have no other choice. Platform.h discourages using
302 // this switch unless absolutely necessary, so hopefully we won't end up
303 // with too many code paths being different in single-process mode.
304 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess);
307 unsigned long long RendererWebKitPlatformSupportImpl::visitedLinkHash(
308 const char* canonical_url,
309 size_t length) {
310 return GetContentClient()->renderer()->VisitedLinkHash(canonical_url, length);
313 bool RendererWebKitPlatformSupportImpl::isLinkVisited(
314 unsigned long long link_hash) {
315 return GetContentClient()->renderer()->IsLinkVisited(link_hash);
318 void RendererWebKitPlatformSupportImpl::createMessageChannel(
319 blink::WebMessagePortChannel** channel1,
320 blink::WebMessagePortChannel** channel2) {
321 WebMessagePortChannelImpl::CreatePair(
322 child_thread_loop_.get(), channel1, channel2);
325 blink::WebPrescientNetworking*
326 RendererWebKitPlatformSupportImpl::prescientNetworking() {
327 return GetContentClient()->renderer()->GetPrescientNetworking();
330 bool
331 RendererWebKitPlatformSupportImpl::CheckPreparsedJsCachingEnabled() const {
332 static bool checked = false;
333 static bool result = false;
334 if (!checked) {
335 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
336 result = command_line.HasSwitch(switches::kEnablePreparsedJsCaching);
337 checked = true;
339 return result;
342 void RendererWebKitPlatformSupportImpl::cacheMetadata(
343 const blink::WebURL& url,
344 double response_time,
345 const char* data,
346 size_t size) {
347 if (!CheckPreparsedJsCachingEnabled())
348 return;
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(
355 new ViewHostMsg_DidGenerateCacheableMetadata(url, response_time, copy));
358 WebString RendererWebKitPlatformSupportImpl::defaultLocale() {
359 return base::ASCIIToUTF16(RenderThread::Get()->GetLocale());
362 void RendererWebKitPlatformSupportImpl::suddenTerminationChanged(bool enabled) {
363 if (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)
370 return;
371 } else {
372 sudden_termination_disables_++;
373 if (sudden_termination_disables_ != 1)
374 return;
377 RenderThread* thread = RenderThread::Get();
378 if (thread) // NULL in unittests.
379 thread->Send(new ViewHostMsg_SuddenTerminationChanged(enabled));
382 WebStorageNamespace*
383 RendererWebKitPlatformSupportImpl::createLocalStorageNamespace() {
384 return new WebStorageNamespaceImpl();
388 //------------------------------------------------------------------------------
390 WebIDBFactory* RendererWebKitPlatformSupportImpl::idbFactory() {
391 return web_idb_factory_.get();
394 //------------------------------------------------------------------------------
396 WebFileSystem* RendererWebKitPlatformSupportImpl::fileSystem() {
397 return WebFileSystemImpl::ThreadSpecificInstance(child_thread_loop_.get());
400 //------------------------------------------------------------------------------
402 WebMimeRegistry::SupportsType
403 RendererWebKitPlatformSupportImpl::MimeRegistry::supportsMediaMIMEType(
404 const WebString& mime_type,
405 const WebString& codecs,
406 const WebString& key_system) {
407 const std::string mime_type_ascii = ToASCIIOrEmpty(mime_type);
408 // Not supporting the container is a flat-out no.
409 if (!net::IsSupportedMediaMimeType(mime_type_ascii))
410 return IsNotSupported;
412 if (!key_system.isEmpty()) {
413 // Check whether the key system is supported with the mime_type and codecs.
415 // Chromium only supports ASCII parameters.
416 if (!IsStringASCII(key_system))
417 return IsNotSupported;
419 std::string key_system_ascii =
420 GetUnprefixedKeySystemName(base::UTF16ToASCII(key_system));
421 std::vector<std::string> strict_codecs;
422 bool strip_suffix = !net::IsStrictMediaMimeType(mime_type_ascii);
423 net::ParseCodecString(ToASCIIOrEmpty(codecs), &strict_codecs, strip_suffix);
425 if (!IsSupportedKeySystemWithMediaMimeType(
426 mime_type_ascii, strict_codecs, key_system_ascii)) {
427 return IsNotSupported;
430 // Continue processing the mime_type and codecs.
433 // Check list of strict codecs to see if it is supported.
434 if (net::IsStrictMediaMimeType(mime_type_ascii)) {
435 // Check if the codecs are a perfect match.
436 std::vector<std::string> strict_codecs;
437 net::ParseCodecString(ToASCIIOrEmpty(codecs), &strict_codecs, false);
438 if (net::IsSupportedStrictMediaMimeType(mime_type_ascii, strict_codecs))
439 return IsSupported;
441 // We support the container, but no codecs were specified.
442 if (codecs.isNull())
443 return MayBeSupported;
445 return IsNotSupported;
448 // If we don't recognize the codec, it's possible we support it.
449 std::vector<std::string> parsed_codecs;
450 net::ParseCodecString(ToASCIIOrEmpty(codecs), &parsed_codecs, true);
451 if (!net::AreSupportedMediaCodecs(parsed_codecs))
452 return MayBeSupported;
454 // Otherwise we have a perfect match.
455 return IsSupported;
458 bool
459 RendererWebKitPlatformSupportImpl::MimeRegistry::supportsMediaSourceMIMEType(
460 const blink::WebString& mime_type,
461 const WebString& codecs) {
462 const std::string mime_type_ascii = ToASCIIOrEmpty(mime_type);
463 std::vector<std::string> parsed_codec_ids;
464 net::ParseCodecString(ToASCIIOrEmpty(codecs), &parsed_codec_ids, false);
465 if (mime_type_ascii.empty())
466 return false;
467 return media::StreamParserFactory::IsTypeSupported(
468 mime_type_ascii, parsed_codec_ids);
471 bool
472 RendererWebKitPlatformSupportImpl::MimeRegistry::supportsEncryptedMediaMIMEType(
473 const WebString& key_system,
474 const WebString& mime_type,
475 const WebString& codecs) {
476 // Chromium only supports ASCII parameters.
477 if (!IsStringASCII(key_system) || !IsStringASCII(mime_type) ||
478 !IsStringASCII(codecs)) {
479 return false;
482 if (key_system.isEmpty())
483 return false;
485 const std::string mime_type_ascii = base::UTF16ToASCII(mime_type);
487 std::vector<std::string> codec_vector;
488 bool strip_suffix = !net::IsStrictMediaMimeType(mime_type_ascii);
489 net::ParseCodecString(base::UTF16ToASCII(codecs), &codec_vector,
490 strip_suffix);
492 return IsSupportedKeySystemWithMediaMimeType(
493 mime_type_ascii, codec_vector, base::UTF16ToASCII(key_system));
496 WebString
497 RendererWebKitPlatformSupportImpl::MimeRegistry::mimeTypeForExtension(
498 const WebString& file_extension) {
499 if (IsPluginProcess())
500 return SimpleWebMimeRegistryImpl::mimeTypeForExtension(file_extension);
502 // The sandbox restricts our access to the registry, so we need to proxy
503 // these calls over to the browser process.
504 std::string mime_type;
505 RenderThread::Get()->Send(
506 new MimeRegistryMsg_GetMimeTypeFromExtension(
507 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type));
508 return base::ASCIIToUTF16(mime_type);
511 WebString RendererWebKitPlatformSupportImpl::MimeRegistry::mimeTypeFromFile(
512 const WebString& file_path) {
513 if (IsPluginProcess())
514 return SimpleWebMimeRegistryImpl::mimeTypeFromFile(file_path);
516 // The sandbox restricts our access to the registry, so we need to proxy
517 // these calls over to the browser process.
518 std::string mime_type;
519 RenderThread::Get()->Send(new MimeRegistryMsg_GetMimeTypeFromFile(
520 base::FilePath::FromUTF16Unsafe(file_path),
521 &mime_type));
522 return base::ASCIIToUTF16(mime_type);
525 //------------------------------------------------------------------------------
527 bool RendererWebKitPlatformSupportImpl::FileUtilities::getFileInfo(
528 const WebString& path,
529 WebFileInfo& web_file_info) {
530 base::File::Info file_info;
531 base::File::Error status;
532 if (!SendSyncMessageFromAnyThread(new FileUtilitiesMsg_GetFileInfo(
533 base::FilePath::FromUTF16Unsafe(path), &file_info, &status)) ||
534 status != base::File::FILE_OK) {
535 return false;
537 FileInfoToWebFileInfo(file_info, &web_file_info);
538 web_file_info.platformPath = path;
539 return true;
542 bool RendererWebKitPlatformSupportImpl::FileUtilities::
543 SendSyncMessageFromAnyThread(IPC::SyncMessage* msg) const {
544 base::TimeTicks begin = base::TimeTicks::Now();
545 const bool success = thread_safe_sender_->Send(msg);
546 base::TimeDelta delta = base::TimeTicks::Now() - begin;
547 UMA_HISTOGRAM_TIMES("RendererSyncIPC.ElapsedTime", delta);
548 return success;
551 //------------------------------------------------------------------------------
553 #if defined(OS_WIN)
555 bool RendererWebKitPlatformSupportImpl::SandboxSupport::ensureFontLoaded(
556 HFONT font) {
557 LOGFONT logfont;
558 GetObject(font, sizeof(LOGFONT), &logfont);
559 RenderThread::Get()->PreCacheFont(logfont);
560 return true;
563 #elif defined(OS_MACOSX)
565 bool RendererWebKitPlatformSupportImpl::SandboxSupport::loadFont(
566 NSFont* src_font, CGFontRef* out, uint32* font_id) {
567 uint32 font_data_size;
568 FontDescriptor src_font_descriptor(src_font);
569 base::SharedMemoryHandle font_data;
570 if (!RenderThread::Get()->Send(new ViewHostMsg_LoadFont(
571 src_font_descriptor, &font_data_size, &font_data, font_id))) {
572 *out = NULL;
573 *font_id = 0;
574 return false;
577 if (font_data_size == 0 || font_data == base::SharedMemory::NULLHandle() ||
578 *font_id == 0) {
579 LOG(ERROR) << "Bad response from ViewHostMsg_LoadFont() for " <<
580 src_font_descriptor.font_name;
581 *out = NULL;
582 *font_id = 0;
583 return false;
586 // TODO(jeremy): Need to call back into WebKit to make sure that the font
587 // isn't already activated, based on the font id. If it's already
588 // activated, don't reactivate it here - crbug.com/72727 .
590 return FontLoader::CGFontRefFromBuffer(font_data, font_data_size, out);
593 #elif defined(OS_ANDROID)
595 // WebKit doesn't use WebSandboxSupport on android so we don't need to
596 // implement anything here. This is cleaner to support than excluding the
597 // whole class for android.
599 #elif defined(OS_POSIX)
601 void
602 RendererWebKitPlatformSupportImpl::SandboxSupport::getFontFamilyForCharacter(
603 blink::WebUChar32 character,
604 const char* preferred_locale,
605 blink::WebFontFamily* family) {
606 base::AutoLock lock(unicode_font_families_mutex_);
607 const std::map<int32_t, blink::WebFontFamily>::const_iterator iter =
608 unicode_font_families_.find(character);
609 if (iter != unicode_font_families_.end()) {
610 family->name = iter->second.name;
611 family->isBold = iter->second.isBold;
612 family->isItalic = iter->second.isItalic;
613 return;
616 GetFontFamilyForCharacter(character, preferred_locale, family);
617 unicode_font_families_.insert(std::make_pair(character, *family));
620 void
621 RendererWebKitPlatformSupportImpl::SandboxSupport::getRenderStyleForStrike(
622 const char* family, int sizeAndStyle, blink::WebFontRenderStyle* out) {
623 GetRenderStyleForStrike(family, sizeAndStyle, out);
626 #endif
628 //------------------------------------------------------------------------------
630 Platform::FileHandle
631 RendererWebKitPlatformSupportImpl::databaseOpenFile(
632 const WebString& vfs_file_name, int desired_flags) {
633 return DatabaseUtil::DatabaseOpenFile(
634 vfs_file_name, desired_flags, sync_message_filter_.get());
637 int RendererWebKitPlatformSupportImpl::databaseDeleteFile(
638 const WebString& vfs_file_name, bool sync_dir) {
639 return DatabaseUtil::DatabaseDeleteFile(
640 vfs_file_name, sync_dir, sync_message_filter_.get());
643 long RendererWebKitPlatformSupportImpl::databaseGetFileAttributes(
644 const WebString& vfs_file_name) {
645 return DatabaseUtil::DatabaseGetFileAttributes(vfs_file_name,
646 sync_message_filter_.get());
649 long long RendererWebKitPlatformSupportImpl::databaseGetFileSize(
650 const WebString& vfs_file_name) {
651 return DatabaseUtil::DatabaseGetFileSize(vfs_file_name,
652 sync_message_filter_.get());
655 long long RendererWebKitPlatformSupportImpl::databaseGetSpaceAvailableForOrigin(
656 const WebString& origin_identifier) {
657 return DatabaseUtil::DatabaseGetSpaceAvailable(origin_identifier,
658 sync_message_filter_.get());
661 bool RendererWebKitPlatformSupportImpl::canAccelerate2dCanvas() {
662 RenderThreadImpl* thread = RenderThreadImpl::current();
663 GpuChannelHost* host = thread->EstablishGpuChannelSync(
664 CAUSE_FOR_GPU_LAUNCH_CANVAS_2D);
665 if (!host)
666 return false;
668 return host->gpu_info().SupportsAccelerated2dCanvas();
671 bool RendererWebKitPlatformSupportImpl::isThreadedCompositingEnabled() {
672 RenderThreadImpl* thread = RenderThreadImpl::current();
673 // thread can be NULL in tests.
674 return thread && thread->compositor_message_loop_proxy().get();
677 double RendererWebKitPlatformSupportImpl::audioHardwareSampleRate() {
678 RenderThreadImpl* thread = RenderThreadImpl::current();
679 return thread->GetAudioHardwareConfig()->GetOutputSampleRate();
682 size_t RendererWebKitPlatformSupportImpl::audioHardwareBufferSize() {
683 RenderThreadImpl* thread = RenderThreadImpl::current();
684 return thread->GetAudioHardwareConfig()->GetOutputBufferSize();
687 unsigned RendererWebKitPlatformSupportImpl::audioHardwareOutputChannels() {
688 RenderThreadImpl* thread = RenderThreadImpl::current();
689 return thread->GetAudioHardwareConfig()->GetOutputChannels();
692 WebDatabaseObserver* RendererWebKitPlatformSupportImpl::databaseObserver() {
693 return web_database_observer_impl_.get();
696 WebAudioDevice*
697 RendererWebKitPlatformSupportImpl::createAudioDevice(
698 size_t buffer_size,
699 unsigned input_channels,
700 unsigned channels,
701 double sample_rate,
702 WebAudioDevice::RenderCallback* callback,
703 const blink::WebString& input_device_id) {
704 // Use a mock for testing.
705 blink::WebAudioDevice* mock_device =
706 GetContentClient()->renderer()->OverrideCreateAudioDevice(sample_rate);
707 if (mock_device)
708 return mock_device;
710 // The |channels| does not exactly identify the channel layout of the
711 // device. The switch statement below assigns a best guess to the channel
712 // layout based on number of channels.
713 // TODO(crogers): WebKit should give the channel layout instead of the hard
714 // channel count.
715 media::ChannelLayout layout = media::CHANNEL_LAYOUT_UNSUPPORTED;
716 switch (channels) {
717 case 1:
718 layout = media::CHANNEL_LAYOUT_MONO;
719 break;
720 case 2:
721 layout = media::CHANNEL_LAYOUT_STEREO;
722 break;
723 case 3:
724 layout = media::CHANNEL_LAYOUT_2_1;
725 break;
726 case 4:
727 layout = media::CHANNEL_LAYOUT_4_0;
728 break;
729 case 5:
730 layout = media::CHANNEL_LAYOUT_5_0;
731 break;
732 case 6:
733 layout = media::CHANNEL_LAYOUT_5_1;
734 break;
735 case 7:
736 layout = media::CHANNEL_LAYOUT_7_0;
737 break;
738 case 8:
739 layout = media::CHANNEL_LAYOUT_7_1;
740 break;
741 default:
742 layout = media::CHANNEL_LAYOUT_STEREO;
745 int session_id = 0;
746 if (input_device_id.isNull() ||
747 !base::StringToInt(base::UTF16ToUTF8(input_device_id), &session_id)) {
748 if (input_channels > 0)
749 DLOG(WARNING) << "createAudioDevice(): request for audio input ignored";
751 input_channels = 0;
754 media::AudioParameters params(
755 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
756 layout, input_channels,
757 static_cast<int>(sample_rate), 16, buffer_size,
758 media::AudioParameters::NO_EFFECTS);
760 return new RendererWebAudioDeviceImpl(params, callback, session_id);
763 #if defined(OS_ANDROID)
764 bool RendererWebKitPlatformSupportImpl::loadAudioResource(
765 blink::WebAudioBus* destination_bus, const char* audio_file_data,
766 size_t data_size) {
767 return DecodeAudioFileData(destination_bus,
768 audio_file_data,
769 data_size,
770 thread_safe_sender_);
772 #else
773 bool RendererWebKitPlatformSupportImpl::loadAudioResource(
774 blink::WebAudioBus* destination_bus, const char* audio_file_data,
775 size_t data_size) {
776 return DecodeAudioFileData(
777 destination_bus, audio_file_data, data_size);
779 #endif // defined(OS_ANDROID)
781 //------------------------------------------------------------------------------
783 blink::WebMIDIAccessor*
784 RendererWebKitPlatformSupportImpl::createMIDIAccessor(
785 blink::WebMIDIAccessorClient* client) {
786 blink::WebMIDIAccessor* accessor =
787 GetContentClient()->renderer()->OverrideCreateMIDIAccessor(client);
788 if (accessor)
789 return accessor;
791 return new RendererWebMIDIAccessorImpl(client);
794 void RendererWebKitPlatformSupportImpl::getPluginList(
795 bool refresh,
796 blink::WebPluginListBuilder* builder) {
797 #if defined(ENABLE_PLUGINS)
798 std::vector<WebPluginInfo> plugins;
799 if (!plugin_refresh_allowed_)
800 refresh = false;
801 RenderThread::Get()->Send(
802 new ViewHostMsg_GetPlugins(refresh, &plugins));
803 for (size_t i = 0; i < plugins.size(); ++i) {
804 const WebPluginInfo& plugin = plugins[i];
806 builder->addPlugin(
807 plugin.name, plugin.desc,
808 plugin.path.BaseName().AsUTF16Unsafe());
810 for (size_t j = 0; j < plugin.mime_types.size(); ++j) {
811 const WebPluginMimeType& mime_type = plugin.mime_types[j];
813 builder->addMediaTypeToLastPlugin(
814 WebString::fromUTF8(mime_type.mime_type), mime_type.description);
816 for (size_t k = 0; k < mime_type.file_extensions.size(); ++k) {
817 builder->addFileExtensionToLastMediaType(
818 WebString::fromUTF8(mime_type.file_extensions[k]));
822 #endif
825 //------------------------------------------------------------------------------
827 blink::WebPublicSuffixList*
828 RendererWebKitPlatformSupportImpl::publicSuffixList() {
829 return &public_suffix_list_;
832 //------------------------------------------------------------------------------
834 blink::WebString
835 RendererWebKitPlatformSupportImpl::signedPublicKeyAndChallengeString(
836 unsigned key_size_index,
837 const blink::WebString& challenge,
838 const blink::WebURL& url) {
839 std::string signed_public_key;
840 RenderThread::Get()->Send(new ViewHostMsg_Keygen(
841 static_cast<uint32>(key_size_index),
842 challenge.utf8(),
843 GURL(url),
844 &signed_public_key));
845 return WebString::fromUTF8(signed_public_key);
848 //------------------------------------------------------------------------------
850 void RendererWebKitPlatformSupportImpl::screenColorProfile(
851 WebVector<char>* to_profile) {
852 #if defined(OS_WIN)
853 // On Windows screen color profile is only available in the browser.
854 std::vector<char> profile;
855 // This Send() can be called from any impl-side thread. Use a thread
856 // safe send to avoid crashing trying to access RenderThread::Get(),
857 // which is not accessible from arbitrary threads.
858 thread_safe_sender_->Send(
859 new ViewHostMsg_GetMonitorColorProfile(&profile));
860 *to_profile = profile;
861 #else
862 // On other platforms, the primary monitor color profile can be read
863 // directly.
864 gfx::ColorProfile profile;
865 *to_profile = profile.profile();
866 #endif
869 //------------------------------------------------------------------------------
871 blink::WebScrollbarBehavior*
872 RendererWebKitPlatformSupportImpl::scrollbarBehavior() {
873 return web_scrollbar_behavior_.get();
876 //------------------------------------------------------------------------------
878 WebBlobRegistry* RendererWebKitPlatformSupportImpl::blobRegistry() {
879 // blob_registry_ can be NULL when running some tests.
880 return blob_registry_.get();
883 //------------------------------------------------------------------------------
885 void RendererWebKitPlatformSupportImpl::sampleGamepads(WebGamepads& gamepads) {
886 if (g_test_gamepads == 0) {
887 RenderThreadImpl::current()->SampleGamepads(&gamepads);
888 } else {
889 gamepads = g_test_gamepads.Get();
893 void RendererWebKitPlatformSupportImpl::setGamepadListener(
894 blink::WebGamepadListener* listener) {
895 web_gamepad_listener = listener;
898 //------------------------------------------------------------------------------
900 WebRTCPeerConnectionHandler*
901 RendererWebKitPlatformSupportImpl::createRTCPeerConnectionHandler(
902 WebRTCPeerConnectionHandlerClient* client) {
903 RenderThreadImpl* render_thread = RenderThreadImpl::current();
904 DCHECK(render_thread);
905 if (!render_thread)
906 return NULL;
908 #if defined(ENABLE_WEBRTC)
909 WebRTCPeerConnectionHandler* peer_connection_handler =
910 GetContentClient()->renderer()->OverrideCreateWebRTCPeerConnectionHandler(
911 client);
912 if (peer_connection_handler)
913 return peer_connection_handler;
915 MediaStreamDependencyFactory* rtc_dependency_factory =
916 render_thread->GetMediaStreamDependencyFactory();
917 return rtc_dependency_factory->CreateRTCPeerConnectionHandler(client);
918 #else
919 return NULL;
920 #endif // defined(ENABLE_WEBRTC)
923 //------------------------------------------------------------------------------
925 WebMediaStreamCenter*
926 RendererWebKitPlatformSupportImpl::createMediaStreamCenter(
927 WebMediaStreamCenterClient* client) {
928 RenderThreadImpl* render_thread = RenderThreadImpl::current();
929 DCHECK(render_thread);
930 if (!render_thread)
931 return NULL;
932 return render_thread->CreateMediaStreamCenter(client);
935 // static
936 bool RendererWebKitPlatformSupportImpl::SetSandboxEnabledForTesting(
937 bool enable) {
938 bool was_enabled = g_sandbox_enabled;
939 g_sandbox_enabled = enable;
940 return was_enabled;
943 // static
944 void RendererWebKitPlatformSupportImpl::SetMockGamepadsForTesting(
945 const WebGamepads& pads) {
946 g_test_gamepads.Get() = pads;
949 // static
950 void RendererWebKitPlatformSupportImpl::MockGamepadConnected(
951 int index,
952 const WebGamepad& pad) {
953 if (web_gamepad_listener)
954 web_gamepad_listener->didConnectGamepad(index, pad);
957 // static
958 void RendererWebKitPlatformSupportImpl::MockGamepadDisconnected(
959 int index,
960 const WebGamepad& pad) {
961 if (web_gamepad_listener)
962 web_gamepad_listener->didDisconnectGamepad(index, pad);
965 //------------------------------------------------------------------------------
967 blink::WebSpeechSynthesizer*
968 RendererWebKitPlatformSupportImpl::createSpeechSynthesizer(
969 blink::WebSpeechSynthesizerClient* client) {
970 return GetContentClient()->renderer()->OverrideSpeechSynthesizer(client);
973 //------------------------------------------------------------------------------
975 bool RendererWebKitPlatformSupportImpl::processMemorySizesInBytes(
976 size_t* private_bytes, size_t* shared_bytes) {
977 content::RenderThread::Get()->Send(
978 new ViewHostMsg_GetProcessMemorySizes(private_bytes, shared_bytes));
979 return true;
982 //------------------------------------------------------------------------------
984 blink::WebGraphicsContext3D*
985 RendererWebKitPlatformSupportImpl::createOffscreenGraphicsContext3D(
986 const blink::WebGraphicsContext3D::Attributes& attributes) {
987 return createOffscreenGraphicsContext3D(attributes, NULL);
990 blink::WebGraphicsContext3D*
991 RendererWebKitPlatformSupportImpl::createOffscreenGraphicsContext3D(
992 const blink::WebGraphicsContext3D::Attributes& attributes,
993 blink::WebGraphicsContext3D* share_context) {
994 if (!RenderThreadImpl::current())
995 return NULL;
997 #if defined(OS_ANDROID)
998 if (SynchronousCompositorFactory* factory =
999 SynchronousCompositorFactory::GetInstance()) {
1000 return factory->CreateOffscreenGraphicsContext3D(attributes);
1002 #endif
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;
1019 bool lose_context_when_out_of_memory = false;
1020 return WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
1021 gpu_channel_host.get(),
1022 attributes,
1023 lose_context_when_out_of_memory,
1024 GURL(attributes.topDocumentURL),
1025 limits,
1026 static_cast<WebGraphicsContext3DCommandBufferImpl*>(share_context));
1029 //------------------------------------------------------------------------------
1031 blink::WebGraphicsContext3DProvider* RendererWebKitPlatformSupportImpl::
1032 createSharedOffscreenGraphicsContext3DProvider() {
1033 scoped_refptr<webkit::gpu::ContextProviderWebContext> provider =
1034 RenderThreadImpl::current()->SharedMainThreadContextProvider();
1035 if (!provider)
1036 return NULL;
1037 return new WebGraphicsContext3DProviderImpl(provider);
1040 //------------------------------------------------------------------------------
1042 blink::WebCompositorSupport*
1043 RendererWebKitPlatformSupportImpl::compositorSupport() {
1044 return &compositor_support_;
1047 //------------------------------------------------------------------------------
1049 blink::WebString RendererWebKitPlatformSupportImpl::convertIDNToUnicode(
1050 const blink::WebString& host,
1051 const blink::WebString& languages) {
1052 return net::IDNToUnicode(host.utf8(), languages.utf8());
1055 //------------------------------------------------------------------------------
1057 void RendererWebKitPlatformSupportImpl::setDeviceMotionListener(
1058 blink::WebDeviceMotionListener* listener) {
1059 if (g_test_device_motion_data == 0) {
1060 if (!device_motion_event_pump_) {
1061 device_motion_event_pump_.reset(new DeviceMotionEventPump);
1062 device_motion_event_pump_->Attach(RenderThreadImpl::current());
1064 device_motion_event_pump_->SetListener(listener);
1065 } else if (listener) {
1066 // Testing mode: just echo the test data to the listener.
1067 base::MessageLoopProxy::current()->PostTask(
1068 FROM_HERE,
1069 base::Bind(&blink::WebDeviceMotionListener::didChangeDeviceMotion,
1070 base::Unretained(listener),
1071 g_test_device_motion_data.Get()));
1075 // static
1076 void RendererWebKitPlatformSupportImpl::SetMockDeviceMotionDataForTesting(
1077 const blink::WebDeviceMotionData& data) {
1078 g_test_device_motion_data.Get() = data;
1081 //------------------------------------------------------------------------------
1083 void RendererWebKitPlatformSupportImpl::setDeviceOrientationListener(
1084 blink::WebDeviceOrientationListener* listener) {
1085 if (g_test_device_orientation_data == 0) {
1086 if (!device_orientation_event_pump_) {
1087 device_orientation_event_pump_.reset(new DeviceOrientationEventPump);
1088 device_orientation_event_pump_->Attach(RenderThreadImpl::current());
1090 device_orientation_event_pump_->SetListener(listener);
1091 } else if (listener) {
1092 // Testing mode: just echo the test data to the listener.
1093 base::MessageLoopProxy::current()->PostTask(
1094 FROM_HERE,
1095 base::Bind(
1096 &blink::WebDeviceOrientationListener::didChangeDeviceOrientation,
1097 base::Unretained(listener),
1098 g_test_device_orientation_data.Get()));
1102 // static
1103 void RendererWebKitPlatformSupportImpl::SetMockDeviceOrientationDataForTesting(
1104 const blink::WebDeviceOrientationData& data) {
1105 g_test_device_orientation_data.Get() = data;
1108 //------------------------------------------------------------------------------
1110 void RendererWebKitPlatformSupportImpl::vibrate(unsigned int milliseconds) {
1111 RenderThread::Get()->Send(
1112 new ViewHostMsg_Vibrate(base::checked_cast<int64>(milliseconds)));
1115 void RendererWebKitPlatformSupportImpl::cancelVibration() {
1116 RenderThread::Get()->Send(new ViewHostMsg_CancelVibration());
1119 //------------------------------------------------------------------------------
1121 void RendererWebKitPlatformSupportImpl::setScreenOrientationListener(
1122 blink::WebScreenOrientationListener* listener) {
1123 if (RenderThreadImpl::current() &&
1124 RenderThreadImpl::current()->layout_test_mode()) {
1125 // If we are in test mode, we want to fully disable the screen orientation
1126 // backend in order to let Blink get tested properly, That means that screen
1127 // orientation updates have to be done manually instead of from signals sent
1128 // by the browser process.
1129 g_test_screen_orientation_listener = listener;
1130 return;
1133 if (!screen_orientation_dispatcher_) {
1134 screen_orientation_dispatcher_.reset(
1135 new ScreenOrientationDispatcher(RenderThread::Get()));
1138 screen_orientation_dispatcher_->setListener(listener);
1141 void RendererWebKitPlatformSupportImpl::lockOrientation(
1142 blink::WebScreenOrientationLockType orientation) {
1143 if (RenderThreadImpl::current() &&
1144 RenderThreadImpl::current()->layout_test_mode()) {
1145 return;
1147 RenderThread::Get()->Send(new ScreenOrientationHostMsg_Lock(orientation));
1150 void RendererWebKitPlatformSupportImpl::unlockOrientation() {
1151 if (RenderThreadImpl::current() &&
1152 RenderThreadImpl::current()->layout_test_mode()) {
1153 return;
1155 RenderThread::Get()->Send(new ScreenOrientationHostMsg_Unlock);
1158 // static
1159 void RendererWebKitPlatformSupportImpl::SetMockScreenOrientationForTesting(
1160 blink::WebScreenOrientationType orientation) {
1161 if (!g_test_screen_orientation_listener)
1162 return;
1163 g_test_screen_orientation_listener->didChangeScreenOrientation(orientation);
1166 //------------------------------------------------------------------------------
1168 void RendererWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
1169 const blink::WebURL& storage_partition,
1170 blink::WebStorageQuotaType type,
1171 blink::WebStorageQuotaCallbacks callbacks) {
1172 if (!thread_safe_sender_.get() || !quota_message_filter_.get())
1173 return;
1174 QuotaDispatcher::ThreadSpecificInstance(
1175 thread_safe_sender_.get(),
1176 quota_message_filter_.get())->QueryStorageUsageAndQuota(
1177 storage_partition,
1178 static_cast<quota::StorageType>(type),
1179 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
1182 } // namespace content