[GTK][WPE] JSC bindings not introspectable
[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/view-backend.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     , m_viewStateFlags(WebCore::ActivityState::WindowIsActive | WebCore::ActivityState::IsFocused | WebCore::ActivityState::IsVisible | WebCore::ActivityState::IsInWindow)
49     , m_compositingManagerProxy(*this)
50     , m_backend(backend)
51 {
52     ASSERT(m_backend);
53
54     auto configuration = baseConfiguration.copy();
55     auto* preferences = configuration->preferences();
56     if (!preferences && configuration->pageGroup()) {
57         preferences = &configuration->pageGroup()->preferences();
58         configuration->setPreferences(preferences);
59     }
60     if (preferences) {
61         preferences->setAcceleratedCompositingEnabled(true);
62         preferences->setForceCompositingMode(true);
63         preferences->setAccelerated2dCanvasEnabled(true);
64         preferences->setWebGLEnabled(true);
65         preferences->setDeveloperExtrasEnabled(true);
66     }
67
68     auto* pool = configuration->processPool();
69     m_pageProxy = pool->createWebPage(*m_pageClient, WTFMove(configuration));
70
71 #if ENABLE(MEMORY_SAMPLER)
72     if (getenv("WEBKIT_SAMPLE_MEMORY"))
73         pool->startMemorySampler(0);
74 #endif
75
76     m_compositingManagerProxy.initialize();
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     };
92     wpe_view_backend_set_backend_client(m_backend, &s_backendClient, this);
93
94     static struct wpe_view_backend_input_client s_inputClient = {
95         // handle_keyboard_event
96         [](void* data, struct wpe_input_keyboard_event* event)
97         {
98             auto& view = *reinterpret_cast<View*>(data);
99             if (event->pressed
100                 && event->modifiers & wpe_input_keyboard_modifier_control
101                 && event->modifiers & wpe_input_keyboard_modifier_shift
102                 && event->keyCode == 'G') {
103                 auto& preferences = view.page().preferences();
104                 preferences.setResourceUsageOverlayVisible(!preferences.resourceUsageOverlayVisible());
105                 return;
106             }
107             view.page().handleKeyboardEvent(WebKit::NativeWebKeyboardEvent(event));
108         },
109         // handle_pointer_event
110         [](void* data, struct wpe_input_pointer_event* event)
111         {
112             auto& page = reinterpret_cast<View*>(data)->page();
113             page.handleMouseEvent(WebKit::NativeWebMouseEvent(event, page.deviceScaleFactor()));
114         },
115         // handle_axis_event
116         [](void* data, struct wpe_input_axis_event* event)
117         {
118             auto& page = reinterpret_cast<View*>(data)->page();
119             page.handleWheelEvent(WebKit::NativeWebWheelEvent(event, page.deviceScaleFactor()));
120         },
121         // handle_touch_event
122         [](void* data, struct wpe_input_touch_event* event)
123         {
124             auto& page = reinterpret_cast<View*>(data)->page();
125             page.handleTouchEvent(WebKit::NativeWebTouchEvent(event, page.deviceScaleFactor()));
126         },
127     };
128     wpe_view_backend_set_input_client(m_backend, &s_inputClient, this);
129
130     wpe_view_backend_initialize(m_backend);
131
132     m_pageProxy->initializeWebPage();
133 }
134
135 View::~View()
136 {
137     m_compositingManagerProxy.finalize();
138 }
139
140 void View::setClient(std::unique_ptr<API::ViewClient>&& client)
141 {
142     if (!client)
143         m_client = std::make_unique<API::ViewClient>();
144     else
145         m_client = WTFMove(client);
146 }
147
148 void View::frameDisplayed()
149 {
150     m_client->frameDisplayed(*this);
151 }
152
153 void View::handleDownloadRequest(DownloadProxy& download)
154 {
155     m_client->handleDownloadRequest(*this, download);
156 }
157
158 void View::setSize(const WebCore::IntSize& size)
159 {
160     m_size = size;
161     if (m_pageProxy->drawingArea())
162         m_pageProxy->drawingArea()->setSize(size);
163 }
164
165 void View::setViewState(WebCore::ActivityState::Flags flags)
166 {
167     static const WebCore::ActivityState::Flags defaultFlags = WebCore::ActivityState::WindowIsActive | WebCore::ActivityState::IsFocused;
168
169     WebCore::ActivityState::Flags changedFlags = m_viewStateFlags ^ (defaultFlags | flags);
170     m_viewStateFlags = defaultFlags | flags;
171
172     if (changedFlags)
173         m_pageProxy->activityStateDidChange(changedFlags);
174 }
175
176 void View::close()
177 {
178     m_pageProxy->close();
179 }
180
181 } // namespace WKWPE