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/stl_util.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "content/child/fling_curve_configuration.h"
12 #include "content/child/web_discardable_memory_impl.h"
13 #include "content/child/webthread_impl.h"
14 #include "content/child/worker_task_runner.h"
15 #include "third_party/WebKit/public/platform/WebWaitableEvent.h"
16 #include "third_party/WebKit/public/web/WebInputEvent.h"
18 #if defined(OS_ANDROID)
19 #include "content/child/fling_animator_impl_android.h"
22 using blink::WebFallbackThemeEngine
;
23 using blink::WebThemeEngine
;
29 class WebWaitableEventImpl
: public blink::WebWaitableEvent
{
31 WebWaitableEventImpl() : impl_(new base::WaitableEvent(false, false)) {}
32 virtual ~WebWaitableEventImpl() {}
34 virtual void wait() { impl_
->Wait(); }
35 virtual void signal() { impl_
->Signal(); }
37 base::WaitableEvent
* impl() {
42 scoped_ptr
<base::WaitableEvent
> impl_
;
43 DISALLOW_COPY_AND_ASSIGN(WebWaitableEventImpl
);
48 WebKitPlatformSupportChildImpl::WebKitPlatformSupportChildImpl()
49 : current_thread_slot_(&DestroyCurrentThread
),
50 fling_curve_configuration_(new FlingCurveConfiguration
) {}
52 WebKitPlatformSupportChildImpl::~WebKitPlatformSupportChildImpl() {}
54 WebThemeEngine
* WebKitPlatformSupportChildImpl::themeEngine() {
55 return &native_theme_engine_
;
58 WebFallbackThemeEngine
* WebKitPlatformSupportChildImpl::fallbackThemeEngine() {
59 return &fallback_theme_engine_
;
62 void WebKitPlatformSupportChildImpl::SetFlingCurveParameters(
63 const std::vector
<float>& new_touchpad
,
64 const std::vector
<float>& new_touchscreen
) {
65 fling_curve_configuration_
->SetCurveParameters(new_touchpad
, new_touchscreen
);
68 blink::WebGestureCurve
*
69 WebKitPlatformSupportChildImpl::createFlingAnimationCurve(
71 const blink::WebFloatPoint
& velocity
,
72 const blink::WebSize
& cumulative_scroll
) {
73 #if defined(OS_ANDROID)
74 return content::FlingAnimatorImpl::CreateAndroidGestureCurve(
79 if (device_source
== blink::WebGestureEvent::Touchscreen
)
80 return fling_curve_configuration_
->CreateForTouchScreen(velocity
,
83 return fling_curve_configuration_
->CreateForTouchPad(velocity
,
87 blink::WebThread
* WebKitPlatformSupportChildImpl::createThread(
89 return new WebThreadImpl(name
);
92 blink::WebThread
* WebKitPlatformSupportChildImpl::currentThread() {
93 WebThreadImplForMessageLoop
* thread
=
94 static_cast<WebThreadImplForMessageLoop
*>(current_thread_slot_
.Get());
98 scoped_refptr
<base::MessageLoopProxy
> message_loop
=
99 base::MessageLoopProxy::current();
100 if (!message_loop
.get())
103 thread
= new WebThreadImplForMessageLoop(message_loop
.get());
104 current_thread_slot_
.Set(thread
);
108 blink::WebWaitableEvent
* WebKitPlatformSupportChildImpl::createWaitableEvent() {
109 return new WebWaitableEventImpl();
112 blink::WebWaitableEvent
* WebKitPlatformSupportChildImpl::waitMultipleEvents(
113 const blink::WebVector
<blink::WebWaitableEvent
*>& web_events
) {
114 std::vector
<base::WaitableEvent
*> events
;
115 for (size_t i
= 0; i
< web_events
.size(); ++i
)
116 events
.push_back(static_cast<WebWaitableEventImpl
*>(web_events
[i
])->impl());
117 size_t idx
= base::WaitableEvent::WaitMany(
118 vector_as_array(&events
), events
.size());
119 DCHECK_LT(idx
, web_events
.size());
120 return web_events
[idx
];
123 void WebKitPlatformSupportChildImpl::didStartWorkerRunLoop(
124 const blink::WebWorkerRunLoop
& runLoop
) {
125 WorkerTaskRunner
* worker_task_runner
= WorkerTaskRunner::Instance();
126 worker_task_runner
->OnWorkerRunLoopStarted(runLoop
);
129 void WebKitPlatformSupportChildImpl::didStopWorkerRunLoop(
130 const blink::WebWorkerRunLoop
& runLoop
) {
131 WorkerTaskRunner
* worker_task_runner
= WorkerTaskRunner::Instance();
132 worker_task_runner
->OnWorkerRunLoopStopped(runLoop
);
135 blink::WebDiscardableMemory
*
136 WebKitPlatformSupportChildImpl::allocateAndLockDiscardableMemory(size_t bytes
) {
137 base::DiscardableMemoryType type
=
138 base::DiscardableMemory::GetPreferredType();
139 if (type
== base::DISCARDABLE_MEMORY_TYPE_EMULATED
)
141 return content::WebDiscardableMemoryImpl::CreateLockedMemory(bytes
).release();
145 void WebKitPlatformSupportChildImpl::DestroyCurrentThread(void* thread
) {
146 WebThreadImplForMessageLoop
* impl
=
147 static_cast<WebThreadImplForMessageLoop
*>(thread
);
151 } // namespace content