Unreviewed, rolling out r241559 and r241566.
[WebKit-https.git] / Source / WebKit / UIProcess / API / wpe / WPEView.cpp
1 /*
2  * Copyright (C) 2014 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WPEView.h"
28
29 #include "APIPageConfiguration.h"
30 #include "APIViewClient.h"
31 #include "DrawingAreaProxy.h"
32 #include "NativeWebKeyboardEvent.h"
33 #include "NativeWebMouseEvent.h"
34 #include "NativeWebTouchEvent.h"
35 #include "NativeWebWheelEvent.h"
36 #include "WebPageGroup.h"
37 #include "WebProcessPool.h"
38 #include <wpe/wpe.h>
39
40 using namespace WebKit;
41
42 namespace WKWPE {
43
44 View::View(struct wpe_view_backend* backend, const API::PageConfiguration& baseConfiguration)
45     : m_client(std::make_unique<API::ViewClient>())
46     , m_pageClient(std::make_unique<PageClientImpl>(*this))
47     , m_size { 800, 600 }
48 #if !defined(WPE_BACKEND_CHECK_VERSION) || !WPE_BACKEND_CHECK_VERSION(1, 1, 0)
49     , m_viewStateFlags { WebCore::ActivityState::WindowIsActive, WebCore::ActivityState::IsFocused, WebCore::ActivityState::IsVisible, WebCore::ActivityState::IsInWindow }
50 #endif
51     , m_backend(backend)
52 {
53     ASSERT(m_backend);
54
55     auto configuration = baseConfiguration.copy();
56     auto* preferences = configuration->preferences();
57     if (!preferences && configuration->pageGroup()) {
58         preferences = &configuration->pageGroup()->preferences();
59         configuration->setPreferences(preferences);
60     }
61     if (preferences) {
62         preferences->setAcceleratedCompositingEnabled(true);
63         preferences->setForceCompositingMode(true);
64         preferences->setAccelerated2dCanvasEnabled(true);
65         preferences->setWebGLEnabled(true);
66         preferences->setDeveloperExtrasEnabled(true);
67     }
68
69     auto* pool = configuration->processPool();
70     m_pageProxy = pool->createWebPage(*m_pageClient, WTFMove(configuration));
71
72 #if ENABLE(MEMORY_SAMPLER)
73     if (getenv("WEBKIT_SAMPLE_MEMORY"))
74         pool->startMemorySampler(0);
75 #endif
76
77     static struct wpe_view_backend_client s_backendClient = {
78         // set_size
79         [](void* data, uint32_t width, uint32_t height)
80         {
81             auto& view = *reinterpret_cast<View*>(data);
82             view.setSize(WebCore::IntSize(width, height));
83         },
84         // frame_displayed
85         [](void* data)
86         {
87             auto& view = *reinterpret_cast<View*>(data);
88             view.frameDisplayed();
89         },
90 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 1, 0)
91         // activity_state_changed
92         [](void* data, uint32_t state)
93         {
94             auto& view = *reinterpret_cast<View*>(data);
95             OptionSet<WebCore::ActivityState::Flag> flags;
96             if (state & wpe_view_activity_state_visible)
97                 flags.add(WebCore::ActivityState::IsVisible);
98             if (state & wpe_view_activity_state_focused) {
99                 flags.add(WebCore::ActivityState::IsFocused);
100                 flags.add(WebCore::ActivityState::WindowIsActive);
101             }
102             if (state & wpe_view_activity_state_in_window)
103                 flags.add(WebCore::ActivityState::IsInWindow);
104             view.setViewState(flags);
105         },
106 #else
107         nullptr,
108 #endif
109         // padding
110         nullptr,
111         nullptr,
112         nullptr
113     };
114     wpe_view_backend_set_backend_client(m_backend, &s_backendClient, this);
115
116     static struct wpe_view_backend_input_client s_inputClient = {
117         // handle_keyboard_event
118         [](void* data, struct wpe_input_keyboard_event* event)
119         {
120             auto& view = *reinterpret_cast<View*>(data);
121             if (event->pressed
122                 && event->modifiers & wpe_input_keyboard_modifier_control
123                 && event->modifiers & wpe_input_keyboard_modifier_shift
124                 && event->key_code == WPE_KEY_G) {
125                 auto& preferences = view.page().preferences();
126                 preferences.setResourceUsageOverlayVisible(!preferences.resourceUsageOverlayVisible());
127                 return;
128             }
129             view.page().handleKeyboardEvent(WebKit::NativeWebKeyboardEvent(event));
130         },
131         // handle_pointer_event
132         [](void* data, struct wpe_input_pointer_event* event)
133         {
134             auto& page = reinterpret_cast<View*>(data)->page();
135             page.handleMouseEvent(WebKit::NativeWebMouseEvent(event, page.deviceScaleFactor()));
136         },
137         // handle_axis_event
138         [](void* data, struct wpe_input_axis_event* event)
139         {
140             auto& page = reinterpret_cast<View*>(data)->page();
141             page.handleWheelEvent(WebKit::NativeWebWheelEvent(event, page.deviceScaleFactor()));
142         },
143         // handle_touch_event
144         [](void* data, struct wpe_input_touch_event* event)
145         {
146             auto& page = reinterpret_cast<View*>(data)->page();
147             page.handleTouchEvent(WebKit::NativeWebTouchEvent(event, page.deviceScaleFactor()));
148         },
149         // padding
150         nullptr,
151         nullptr,
152         nullptr,
153         nullptr
154     };
155     wpe_view_backend_set_input_client(m_backend, &s_inputClient, this);
156
157     wpe_view_backend_initialize(m_backend);
158
159     m_pageProxy->initializeWebPage();
160 }
161
162 void View::setClient(std::unique_ptr<API::ViewClient>&& client)
163 {
164     if (!client)
165         m_client = std::make_unique<API::ViewClient>();
166     else
167         m_client = WTFMove(client);
168 }
169
170 void View::frameDisplayed()
171 {
172     m_client->frameDisplayed(*this);
173 }
174
175 void View::handleDownloadRequest(DownloadProxy& download)
176 {
177     m_client->handleDownloadRequest(*this, download);
178 }
179
180 void View::setSize(const WebCore::IntSize& size)
181 {
182     m_size = size;
183     if (m_pageProxy->drawingArea())
184         m_pageProxy->drawingArea()->setSize(size);
185 }
186
187 void View::setViewState(OptionSet<WebCore::ActivityState::Flag> flags)
188 {
189     auto changedFlags = m_viewStateFlags ^ flags;
190     m_viewStateFlags = flags;
191
192     if (changedFlags)
193         m_pageProxy->activityStateDidChange(changedFlags);
194 }
195
196 void View::close()
197 {
198     m_pageProxy->close();
199 }
200
201 } // namespace WKWPE