1 // Copyright 2014 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/child/webkitplatformsupport_child_impl.h"
7 #include "base/memory/discardable_memory.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "content/child/fling_curve_configuration.h"
11 #include "content/child/web_discardable_memory_impl.h"
12 #include "content/child/webthread_impl.h"
13 #include "content/child/worker_task_runner.h"
14 #include "third_party/WebKit/public/platform/WebWaitableEvent.h"
15 #include "third_party/WebKit/public/web/WebInputEvent.h"
17 #if defined(OS_ANDROID)
18 #include "content/child/fling_animator_impl_android.h"
21 using blink::WebFallbackThemeEngine
;
22 using blink::WebThemeEngine
;
28 class WebWaitableEventImpl
: public blink::WebWaitableEvent
{
30 WebWaitableEventImpl() : impl_(new base::WaitableEvent(false, false)) {}
31 virtual ~WebWaitableEventImpl() {}
33 virtual void wait() { impl_
->Wait(); }
34 virtual void signal() { impl_
->Signal(); }
36 base::WaitableEvent
* impl() {
41 scoped_ptr
<base::WaitableEvent
> impl_
;
42 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl
);
47 WebKitPlatformSupportChildImpl::WebKitPlatformSupportChildImpl()
48 : current_thread_slot_(&DestroyCurrentThread
),
49 fling_curve_configuration_(new FlingCurveConfiguration
) {}
51 WebKitPlatformSupportChildImpl::~WebKitPlatformSupportChildImpl() {}
53 WebThemeEngine
* WebKitPlatformSupportChildImpl::themeEngine() {
54 return &native_theme_engine_
;
57 WebFallbackThemeEngine
* WebKitPlatformSupportChildImpl::fallbackThemeEngine() {
58 return &fallback_theme_engine_
;
61 void WebKitPlatformSupportChildImpl::SetFlingCurveParameters(
62 const std::vector
<float>& new_touchpad
,
63 const std::vector
<float>& new_touchscreen
) {
64 fling_curve_configuration_
->SetCurveParameters(new_touchpad
, new_touchscreen
);
67 blink::WebGestureCurve
*
68 WebKitPlatformSupportChildImpl::createFlingAnimationCurve(
70 const blink::WebFloatPoint
& velocity
,
71 const blink::WebSize
& cumulative_scroll
) {
72 #if defined(OS_ANDROID)
73 return content::FlingAnimatorImpl::CreateAndroidGestureCurve(
78 if (device_source
== blink::WebGestureEvent::Touchscreen
)
79 return fling_curve_configuration_
->CreateForTouchScreen(velocity
,
82 return fling_curve_configuration_
->CreateForTouchPad(velocity
,
86 blink::WebThread
* WebKitPlatformSupportChildImpl::createThread(
88 return new WebThreadImpl(name
);
91 blink::WebThread
* WebKitPlatformSupportChildImpl::currentThread() {
92 WebThreadImplForMessageLoop
* thread
=
93 static_cast<WebThreadImplForMessageLoop
*>(current_thread_slot_
.Get());
97 scoped_refptr
<base::MessageLoopProxy
> message_loop
=
98 base::MessageLoopProxy::current();
99 if (!message_loop
.get())
102 thread
= new WebThreadImplForMessageLoop(message_loop
.get());
103 current_thread_slot_
.Set(thread
);
107 blink::WebWaitableEvent
* WebKitPlatformSupportChildImpl::createWaitableEvent() {
108 return new WebWaitableEventImpl();
111 blink::WebWaitableEvent
* WebKitPlatformSupportChildImpl::waitMultipleEvents(
112 const blink::WebVector
<blink::WebWaitableEvent
*>& web_events
) {
113 base::WaitableEvent
** events
= new base::WaitableEvent
*[web_events
.size()];
114 for (size_t i
= 0; i
< web_events
.size(); ++i
)
115 events
[i
] = static_cast<WebWaitableEventImpl
*>(web_events
[i
])->impl();
116 size_t idx
= base::WaitableEvent::WaitMany(events
, web_events
.size());
117 DCHECK_LT(idx
, web_events
.size());
118 return web_events
[idx
];
121 void WebKitPlatformSupportChildImpl::didStartWorkerRunLoop(
122 const blink::WebWorkerRunLoop
& runLoop
) {
123 WorkerTaskRunner
* worker_task_runner
= WorkerTaskRunner::Instance();
124 worker_task_runner
->OnWorkerRunLoopStarted(runLoop
);
127 void WebKitPlatformSupportChildImpl::didStopWorkerRunLoop(
128 const blink::WebWorkerRunLoop
& runLoop
) {
129 WorkerTaskRunner
* worker_task_runner
= WorkerTaskRunner::Instance();
130 worker_task_runner
->OnWorkerRunLoopStopped(runLoop
);
133 blink::WebDiscardableMemory
*
134 WebKitPlatformSupportChildImpl::allocateAndLockDiscardableMemory(size_t bytes
) {
135 base::DiscardableMemoryType type
=
136 base::DiscardableMemory::GetPreferredType();
137 if (type
== base::DISCARDABLE_MEMORY_TYPE_EMULATED
)
139 return content::WebDiscardableMemoryImpl::CreateLockedMemory(bytes
).release();
143 void WebKitPlatformSupportChildImpl::DestroyCurrentThread(void* thread
) {
144 WebThreadImplForMessageLoop
* impl
=
145 static_cast<WebThreadImplForMessageLoop
*>(thread
);
149 } // namespace content