d39eb77e8a42d146d3340c2f1c9436df86036df2
[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 #include <wtf/Environment.h>
40
41 using namespace WebKit;
42
43 namespace WKWPE {
44
45 View::View(struct wpe_view_backend* backend, const API::PageConfiguration& baseConfiguration)
46     : m_client(std::make_unique<API::ViewClient>())
47     , m_pageClient(std::make_unique<PageClientImpl>(*this))
48     , m_size { 800, 600 }
49 #if !defined(WPE_BACKEND_CHECK_VERSION) || !WPE_BACKEND_CHECK_VERSION(1, 1, 0)
50     , m_viewStateFlags { WebCore::ActivityState::WindowIsActive, WebCore::ActivityState::IsFocused, WebCore::ActivityState::IsVisible, WebCore::ActivityState::IsInWindow }
51 #endif
52     , m_backend(backend)
53 {
54     ASSERT(m_backend);
55
56     auto configuration = baseConfiguration.copy();
57     auto* preferences = configuration->preferences();
58     if (!preferences && configuration->pageGroup()) {
59         preferences = &configuration->pageGroup()->preferences();
60         configuration->setPreferences(preferences);
61     }
62     if (preferences) {
63         preferences->setAcceleratedCompositingEnabled(true);
64         preferences->setForceCompositingMode(true);
65         preferences->setAccelerated2dCanvasEnabled(true);
66         preferences->setWebGLEnabled(true);
67         preferences->setDeveloperExtrasEnabled(true);
68     }
69
70     auto* pool = configuration->processPool();
71     m_pageProxy = pool->createWebPage(*m_pageClient, WTFMove(configuration));
72
73 #if ENABLE(MEMORY_SAMPLER)
74     if (Environment::get("WEBKIT_SAMPLE_MEMORY"))
75         pool->startMemorySampler(0);
76 #endif
77
78     static struct wpe_view_backend_client s_backendClient = {
79         // set_size
80         [](void* data, uint32_t width, uint32_t height)
81         {
82             auto& view = *reinterpret_cast<View*>(data);
83             view.setSize(WebCore::IntSize(width, height));
84         },
85         // frame_displayed
86         [](void* data)
87         {
88             auto& view = *reinterpret_cast<View*>(data);
89             view.frameDisplayed();
90         },
91 #if defined(WPE_BACKEND_CHECK_VERSION) && WPE_BACKEND_CHECK_VERSION(1, 1, 0)
92         // activity_state_changed
93         [](void* data, uint32_t state)
94         {
95             auto& view = *reinterpret_cast<View*>(data);
96             OptionSet<WebCore::ActivityState::Flag> flags;
97             if (state & wpe_view_activity_state_visible)
98                 flags.add(WebCore::ActivityState::IsVisible);
99             if (state & wpe_view_activity_state_focused) {
100                 flags.add(WebCore::ActivityState::IsFocused);
101                 flags.add(WebCore::ActivityState::WindowIsActive);
102             }
103             if (state & wpe_view_activity_state_in_window)
104                 flags.add(WebCore::ActivityState::IsInWindow);
105             view.setViewState(flags);
106         },
107 #else
108         nullptr,
109 #endif
110         // padding
111         nullptr,
112         nullptr,
113         nullptr
114     };
115     wpe_view_backend_set_backend_client(m_backend, &s_backendClient, this);
116
117     static struct wpe_view_backend_input_client s_inputClient = {
118         // handle_keyboard_event
119         [](void* data, struct wpe_input_keyboard_event* event)
120         {
121             auto& view = *reinterpret_cast<View*>(data);
122             if (event->pressed
123                 && event->modifiers & wpe_input_keyboard_modifier_control
124                 && event->modifiers & wpe_input_keyboard_modifier_shift
125                 && event->key_code == WPE_KEY_G) {
126                 auto& preferences = view.page().preferences();
127                 preferences.setResourceUsageOverlayVisible(!preferences.resourceUsageOverlayVisible());
128                 return;
129             }
130             view.page().handleKeyboardEvent(WebKit::NativeWebKeyboardEvent(event));
131         },
132         // handle_pointer_event
133         [](void* data, struct wpe_input_pointer_event* event)
134         {
135             auto& page = reinterpret_cast<View*>(data)->page();
136             page.handleMouseEvent(WebKit::NativeWebMouseEvent(event, page.deviceScaleFactor()));
137         },
138         // handle_axis_event
139         [](void* data, struct wpe_input_axis_event* event)
140         {
141             auto& page = reinterpret_cast<View*>(data)->page();
142             page.handleWheelEvent(WebKit::NativeWebWheelEvent(event, page.deviceScaleFactor()));
143         },
144         // handle_touch_event
145         [](void* data, struct wpe_input_touch_event* event)
146         {
147             auto& page = reinterpret_cast<View*>(data)->page();
148             page.handleTouchEvent(WebKit::NativeWebTouchEvent(event, page.deviceScaleFactor()));
149         },
150         // padding
151         nullptr,
152         nullptr,
153         nullptr,
154         nullptr
155     };
156     wpe_view_backend_set_input_client(m_backend, &s_inputClient, this);
157
158     wpe_view_backend_initialize(m_backend);
159
160     m_pageProxy->initializeWebPage();
161 }
162
163 void View::setClient(std::unique_ptr<API::ViewClient>&& client)
164 {
165     if (!client)
166         m_client = std::make_unique<API::ViewClient>();
167     else
168         m_client = WTFMove(client);
169 }
170
171 void View::frameDisplayed()
172 {
173     m_client->frameDisplayed(*this);
174 }
175
176 void View::handleDownloadRequest(DownloadProxy& download)
177 {
178     m_client->handleDownloadRequest(*this, download);
179 }
180
181 void View::setSize(const WebCore::IntSize& size)
182 {
183     m_size = size;
184     if (m_pageProxy->drawingArea())
185         m_pageProxy->drawingArea()->setSize(size);
186 }
187
188 void View::setViewState(OptionSet<WebCore::ActivityState::Flag> flags)
189 {
190     auto changedFlags = m_viewStateFlags ^ flags;
191     m_viewStateFlags = flags;
192
193     if (changedFlags)
194         m_pageProxy->activityStateDidChange(changedFlags);
195 }
196
197 void View::close()
198 {
199     m_pageProxy->close();
200 }
201
202 } // namespace WKWPE