WebContentView.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Copyright (c) 2022-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "WebContentView.h"
  8. #include "HelperProcess.h"
  9. #include "Utilities.h"
  10. #include <AK/Assertions.h>
  11. #include <AK/ByteBuffer.h>
  12. #include <AK/Format.h>
  13. #include <AK/HashTable.h>
  14. #include <AK/LexicalPath.h>
  15. #include <AK/NonnullOwnPtr.h>
  16. #include <AK/StringBuilder.h>
  17. #include <AK/Types.h>
  18. #include <Browser/CookieJar.h>
  19. #include <Kernel/API/KeyCode.h>
  20. #include <LibCore/ArgsParser.h>
  21. #include <LibCore/EventLoop.h>
  22. #include <LibCore/IODevice.h>
  23. #include <LibCore/System.h>
  24. #include <LibCore/Timer.h>
  25. #include <LibGfx/Bitmap.h>
  26. #include <LibGfx/Font/FontDatabase.h>
  27. #include <LibGfx/ImageFormats/PNGWriter.h>
  28. #include <LibGfx/Painter.h>
  29. #include <LibGfx/Palette.h>
  30. #include <LibGfx/Rect.h>
  31. #include <LibGfx/SystemTheme.h>
  32. #include <LibMain/Main.h>
  33. #include <LibWeb/Crypto/Crypto.h>
  34. #include <LibWeb/Loader/ContentFilter.h>
  35. #include <LibWebView/WebContentClient.h>
  36. #include <QApplication>
  37. #include <QCursor>
  38. #include <QIcon>
  39. #include <QInputDialog>
  40. #include <QLineEdit>
  41. #include <QMessageBox>
  42. #include <QMimeData>
  43. #include <QMouseEvent>
  44. #include <QPaintEvent>
  45. #include <QPainter>
  46. #include <QPalette>
  47. #include <QScrollBar>
  48. #include <QTextEdit>
  49. #include <QTimer>
  50. #include <QToolTip>
  51. bool is_using_dark_system_theme(QWidget&);
  52. WebContentView::WebContentView(StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling)
  53. : m_webdriver_content_ipc_path(webdriver_content_ipc_path)
  54. {
  55. setMouseTracking(true);
  56. setAcceptDrops(true);
  57. setFocusPolicy(Qt::FocusPolicy::StrongFocus);
  58. m_device_pixel_ratio = devicePixelRatio();
  59. m_inverse_pixel_scaling_ratio = 1.0 / m_device_pixel_ratio;
  60. verticalScrollBar()->setSingleStep(24);
  61. horizontalScrollBar()->setSingleStep(24);
  62. QObject::connect(verticalScrollBar(), &QScrollBar::valueChanged, [this](int) {
  63. update_viewport_rect();
  64. });
  65. QObject::connect(horizontalScrollBar(), &QScrollBar::valueChanged, [this](int) {
  66. update_viewport_rect();
  67. });
  68. create_client(enable_callgrind_profiling);
  69. }
  70. WebContentView::~WebContentView() = default;
  71. unsigned get_button_from_qt_event(QMouseEvent const& event)
  72. {
  73. if (event.button() == Qt::MouseButton::LeftButton)
  74. return 1;
  75. if (event.button() == Qt::MouseButton::RightButton)
  76. return 2;
  77. if (event.button() == Qt::MouseButton::MiddleButton)
  78. return 4;
  79. if (event.button() == Qt::MouseButton::BackButton)
  80. return 8;
  81. if (event.buttons() == Qt::MouseButton::ForwardButton)
  82. return 16;
  83. return 0;
  84. }
  85. unsigned get_buttons_from_qt_event(QMouseEvent const& event)
  86. {
  87. unsigned buttons = 0;
  88. if (event.buttons() & Qt::MouseButton::LeftButton)
  89. buttons |= 1;
  90. if (event.buttons() & Qt::MouseButton::RightButton)
  91. buttons |= 2;
  92. if (event.buttons() & Qt::MouseButton::MiddleButton)
  93. buttons |= 4;
  94. if (event.buttons() & Qt::MouseButton::BackButton)
  95. buttons |= 8;
  96. if (event.buttons() & Qt::MouseButton::ForwardButton)
  97. buttons |= 16;
  98. return buttons;
  99. }
  100. unsigned get_modifiers_from_qt_mouse_event(QMouseEvent const& event)
  101. {
  102. unsigned modifiers = 0;
  103. if (event.modifiers() & Qt::Modifier::ALT)
  104. modifiers |= 1;
  105. if (event.modifiers() & Qt::Modifier::CTRL)
  106. modifiers |= 2;
  107. if (event.modifiers() & Qt::Modifier::SHIFT)
  108. modifiers |= 4;
  109. return modifiers;
  110. }
  111. unsigned get_modifiers_from_qt_keyboard_event(QKeyEvent const& event)
  112. {
  113. auto modifiers = 0;
  114. if (event.modifiers().testFlag(Qt::AltModifier))
  115. modifiers |= KeyModifier::Mod_Alt;
  116. if (event.modifiers().testFlag(Qt::ControlModifier))
  117. modifiers |= KeyModifier::Mod_Ctrl;
  118. if (event.modifiers().testFlag(Qt::MetaModifier))
  119. modifiers |= KeyModifier::Mod_Super;
  120. if (event.modifiers().testFlag(Qt::ShiftModifier))
  121. modifiers |= KeyModifier::Mod_Shift;
  122. if (event.modifiers().testFlag(Qt::AltModifier))
  123. modifiers |= KeyModifier::Mod_AltGr;
  124. return modifiers;
  125. }
  126. KeyCode get_keycode_from_qt_keyboard_event(QKeyEvent const& event)
  127. {
  128. struct Mapping {
  129. constexpr Mapping(Qt::Key q, KeyCode s)
  130. : qt_key(q)
  131. , serenity_key(s)
  132. {
  133. }
  134. Qt::Key qt_key;
  135. KeyCode serenity_key;
  136. };
  137. constexpr Mapping mappings[] = {
  138. { Qt::Key_0, Key_0 },
  139. { Qt::Key_1, Key_1 },
  140. { Qt::Key_2, Key_2 },
  141. { Qt::Key_3, Key_3 },
  142. { Qt::Key_4, Key_4 },
  143. { Qt::Key_5, Key_5 },
  144. { Qt::Key_6, Key_6 },
  145. { Qt::Key_7, Key_7 },
  146. { Qt::Key_8, Key_8 },
  147. { Qt::Key_9, Key_9 },
  148. { Qt::Key_A, Key_A },
  149. { Qt::Key_Alt, Key_Alt },
  150. { Qt::Key_Ampersand, Key_Ampersand },
  151. { Qt::Key_Apostrophe, Key_Apostrophe },
  152. { Qt::Key_AsciiCircum, Key_Circumflex },
  153. { Qt::Key_AsciiTilde, Key_Tilde },
  154. { Qt::Key_Asterisk, Key_Asterisk },
  155. { Qt::Key_At, Key_AtSign },
  156. { Qt::Key_B, Key_B },
  157. { Qt::Key_Backslash, Key_Backslash },
  158. { Qt::Key_Backspace, Key_Backspace },
  159. { Qt::Key_Bar, Key_Pipe },
  160. { Qt::Key_BraceLeft, Key_LeftBrace },
  161. { Qt::Key_BraceRight, Key_RightBrace },
  162. { Qt::Key_BracketLeft, Key_LeftBracket },
  163. { Qt::Key_BracketRight, Key_RightBracket },
  164. { Qt::Key_C, Key_C },
  165. { Qt::Key_CapsLock, Key_CapsLock },
  166. { Qt::Key_Colon, Key_Colon },
  167. { Qt::Key_Comma, Key_Comma },
  168. { Qt::Key_Control, Key_Control },
  169. { Qt::Key_D, Key_D },
  170. { Qt::Key_Delete, Key_Delete },
  171. { Qt::Key_Dollar, Key_Dollar },
  172. { Qt::Key_Down, Key_Down },
  173. { Qt::Key_E, Key_E },
  174. { Qt::Key_End, Key_End },
  175. { Qt::Key_Equal, Key_Equal },
  176. { Qt::Key_Escape, Key_Escape },
  177. { Qt::Key_exclamdown, Key_ExclamationPoint },
  178. { Qt::Key_F, Key_F },
  179. { Qt::Key_F1, Key_F1 },
  180. { Qt::Key_F10, Key_F10 },
  181. { Qt::Key_F11, Key_F11 },
  182. { Qt::Key_F12, Key_F12 },
  183. { Qt::Key_F2, Key_F2 },
  184. { Qt::Key_F3, Key_F3 },
  185. { Qt::Key_F4, Key_F4 },
  186. { Qt::Key_F5, Key_F5 },
  187. { Qt::Key_F6, Key_F6 },
  188. { Qt::Key_F7, Key_F7 },
  189. { Qt::Key_F8, Key_F8 },
  190. { Qt::Key_F9, Key_F9 },
  191. { Qt::Key_G, Key_G },
  192. { Qt::Key_Greater, Key_GreaterThan },
  193. { Qt::Key_H, Key_H },
  194. { Qt::Key_Home, Key_Home },
  195. { Qt::Key_I, Key_I },
  196. { Qt::Key_Insert, Key_Insert },
  197. { Qt::Key_J, Key_J },
  198. { Qt::Key_K, Key_K },
  199. { Qt::Key_L, Key_L },
  200. { Qt::Key_Left, Key_Left },
  201. { Qt::Key_Less, Key_LessThan },
  202. { Qt::Key_M, Key_M },
  203. { Qt::Key_Menu, Key_Menu },
  204. { Qt::Key_Minus, Key_Minus },
  205. { Qt::Key_N, Key_N },
  206. { Qt::Key_NumLock, Key_NumLock },
  207. { Qt::Key_O, Key_O },
  208. { Qt::Key_P, Key_P },
  209. { Qt::Key_PageDown, Key_PageDown },
  210. { Qt::Key_PageUp, Key_PageUp },
  211. { Qt::Key_ParenLeft, Key_LeftParen },
  212. { Qt::Key_ParenRight, Key_RightParen },
  213. { Qt::Key_Percent, Key_Percent },
  214. { Qt::Key_Period, Key_Period },
  215. { Qt::Key_Plus, Key_Plus },
  216. { Qt::Key_Print, Key_PrintScreen },
  217. { Qt::Key_Q, Key_Q },
  218. { Qt::Key_Question, Key_QuestionMark },
  219. { Qt::Key_QuoteDbl, Key_DoubleQuote },
  220. { Qt::Key_R, Key_R },
  221. { Qt::Key_Return, Key_Return },
  222. { Qt::Key_Right, Key_Right },
  223. { Qt::Key_S, Key_S },
  224. { Qt::Key_ScrollLock, Key_ScrollLock },
  225. { Qt::Key_Semicolon, Key_Semicolon },
  226. { Qt::Key_Shift, Key_LeftShift },
  227. { Qt::Key_Slash, Key_Slash },
  228. { Qt::Key_Space, Key_Space },
  229. { Qt::Key_Super_L, Key_Super },
  230. { Qt::Key_SysReq, Key_SysRq },
  231. { Qt::Key_T, Key_T },
  232. { Qt::Key_Tab, Key_Tab },
  233. { Qt::Key_U, Key_U },
  234. { Qt::Key_Underscore, Key_Underscore },
  235. { Qt::Key_Up, Key_Up },
  236. { Qt::Key_V, Key_V },
  237. { Qt::Key_W, Key_W },
  238. { Qt::Key_X, Key_X },
  239. { Qt::Key_Y, Key_Y },
  240. { Qt::Key_Z, Key_Z },
  241. };
  242. for (auto const& mapping : mappings) {
  243. if (event.key() == mapping.qt_key)
  244. return mapping.serenity_key;
  245. }
  246. return Key_Invalid;
  247. }
  248. void WebContentView::mouseMoveEvent(QMouseEvent* event)
  249. {
  250. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  251. auto buttons = get_buttons_from_qt_event(*event);
  252. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  253. client().async_mouse_move(to_content_position(position), 0, buttons, modifiers);
  254. }
  255. void WebContentView::mousePressEvent(QMouseEvent* event)
  256. {
  257. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  258. auto button = get_button_from_qt_event(*event);
  259. if (button == 0) {
  260. // We could not convert Qt buttons to something that Lagom can
  261. // recognize - don't even bother propagating this to the web engine
  262. // as it will not handle it anyway, and it will (currently) assert
  263. return;
  264. }
  265. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  266. auto buttons = get_buttons_from_qt_event(*event);
  267. client().async_mouse_down(to_content_position(position), button, buttons, modifiers);
  268. }
  269. void WebContentView::mouseReleaseEvent(QMouseEvent* event)
  270. {
  271. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  272. auto button = get_button_from_qt_event(*event);
  273. if (event->button() & Qt::MouseButton::BackButton) {
  274. emit back_mouse_button();
  275. } else if (event->button() & Qt::MouseButton::ForwardButton) {
  276. emit forward_mouse_button();
  277. }
  278. if (button == 0) {
  279. // We could not convert Qt buttons to something that Lagom can
  280. // recognize - don't even bother propagating this to the web engine
  281. // as it will not handle it anyway, and it will (currently) assert
  282. return;
  283. }
  284. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  285. auto buttons = get_buttons_from_qt_event(*event);
  286. client().async_mouse_up(to_content_position(position), button, buttons, modifiers);
  287. }
  288. void WebContentView::mouseDoubleClickEvent(QMouseEvent* event)
  289. {
  290. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  291. auto button = get_button_from_qt_event(*event);
  292. if (button == 0) {
  293. // We could not convert Qt buttons to something that Lagom can
  294. // recognize - don't even bother propagating this to the web engine
  295. // as it will not handle it anyway, and it will (currently) assert
  296. return;
  297. }
  298. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  299. auto buttons = get_buttons_from_qt_event(*event);
  300. client().async_doubleclick(to_content_position(position), button, buttons, modifiers);
  301. }
  302. void WebContentView::dragEnterEvent(QDragEnterEvent* event)
  303. {
  304. if (event->mimeData()->hasUrls())
  305. event->acceptProposedAction();
  306. }
  307. void WebContentView::dropEvent(QDropEvent* event)
  308. {
  309. VERIFY(event->mimeData()->hasUrls());
  310. emit urls_dropped(event->mimeData()->urls());
  311. event->acceptProposedAction();
  312. }
  313. void WebContentView::keyPressEvent(QKeyEvent* event)
  314. {
  315. switch (event->key()) {
  316. case Qt::Key_Left:
  317. case Qt::Key_Right:
  318. case Qt::Key_Up:
  319. case Qt::Key_Down:
  320. case Qt::Key_PageUp:
  321. case Qt::Key_PageDown:
  322. QAbstractScrollArea::keyPressEvent(event);
  323. break;
  324. default:
  325. break;
  326. }
  327. if (event->key() == Qt::Key_Backtab) {
  328. // NOTE: Qt transforms Shift+Tab into a "Backtab", so we undo that transformation here.
  329. client().async_key_down(KeyCode::Key_Tab, Mod_Shift, '\t');
  330. return;
  331. }
  332. auto text = event->text();
  333. if (text.isEmpty()) {
  334. return;
  335. }
  336. auto point = event->text()[0].unicode();
  337. auto keycode = get_keycode_from_qt_keyboard_event(*event);
  338. auto modifiers = get_modifiers_from_qt_keyboard_event(*event);
  339. client().async_key_down(keycode, modifiers, point);
  340. }
  341. void WebContentView::keyReleaseEvent(QKeyEvent* event)
  342. {
  343. auto text = event->text();
  344. if (text.isEmpty()) {
  345. return;
  346. }
  347. auto point = event->text()[0].unicode();
  348. auto keycode = get_keycode_from_qt_keyboard_event(*event);
  349. auto modifiers = get_modifiers_from_qt_keyboard_event(*event);
  350. client().async_key_up(keycode, modifiers, point);
  351. }
  352. void WebContentView::focusInEvent(QFocusEvent*)
  353. {
  354. client().async_set_has_focus(true);
  355. }
  356. void WebContentView::focusOutEvent(QFocusEvent*)
  357. {
  358. client().async_set_has_focus(false);
  359. }
  360. void WebContentView::paintEvent(QPaintEvent*)
  361. {
  362. QPainter painter(viewport());
  363. painter.scale(m_inverse_pixel_scaling_ratio, m_inverse_pixel_scaling_ratio);
  364. Gfx::Bitmap const* bitmap = nullptr;
  365. Gfx::IntSize bitmap_size;
  366. if (m_client_state.has_usable_bitmap) {
  367. bitmap = m_client_state.front_bitmap.bitmap.ptr();
  368. bitmap_size = m_client_state.front_bitmap.last_painted_size;
  369. } else {
  370. bitmap = m_backup_bitmap.ptr();
  371. bitmap_size = m_backup_bitmap_size;
  372. }
  373. if (bitmap) {
  374. QImage q_image(bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), QImage::Format_RGB32);
  375. painter.drawImage(QPoint(0, 0), q_image, QRect(0, 0, bitmap_size.width(), bitmap_size.height()));
  376. if (bitmap_size.width() < width()) {
  377. painter.fillRect(bitmap_size.width(), 0, width() - bitmap_size.width(), bitmap->height(), palette().base());
  378. }
  379. if (bitmap_size.height() < height()) {
  380. painter.fillRect(0, bitmap_size.height(), width(), height() - bitmap_size.height(), palette().base());
  381. }
  382. return;
  383. }
  384. painter.fillRect(rect(), palette().base());
  385. }
  386. void WebContentView::resizeEvent(QResizeEvent* event)
  387. {
  388. QAbstractScrollArea::resizeEvent(event);
  389. update_viewport_rect();
  390. handle_resize();
  391. }
  392. void WebContentView::set_viewport_rect(Gfx::IntRect rect)
  393. {
  394. m_viewport_rect = rect;
  395. client().async_set_viewport_rect(rect);
  396. }
  397. void WebContentView::set_window_size(Gfx::IntSize size)
  398. {
  399. client().async_set_window_size(size);
  400. }
  401. void WebContentView::set_window_position(Gfx::IntPoint position)
  402. {
  403. client().async_set_window_position(position);
  404. }
  405. void WebContentView::update_viewport_rect()
  406. {
  407. auto scaled_width = int(viewport()->width() / m_inverse_pixel_scaling_ratio);
  408. auto scaled_height = int(viewport()->height() / m_inverse_pixel_scaling_ratio);
  409. Gfx::IntRect rect(max(0, horizontalScrollBar()->value()), max(0, verticalScrollBar()->value()), scaled_width, scaled_height);
  410. set_viewport_rect(rect);
  411. request_repaint();
  412. }
  413. void WebContentView::update_zoom()
  414. {
  415. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  416. update_viewport_rect();
  417. request_repaint();
  418. }
  419. void WebContentView::showEvent(QShowEvent* event)
  420. {
  421. QAbstractScrollArea::showEvent(event);
  422. client().async_set_system_visibility_state(true);
  423. }
  424. void WebContentView::hideEvent(QHideEvent* event)
  425. {
  426. QAbstractScrollArea::hideEvent(event);
  427. client().async_set_system_visibility_state(false);
  428. }
  429. static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget, WebContentView::PaletteMode mode)
  430. {
  431. auto qt_palette = widget.palette();
  432. auto theme_file = mode == WebContentView::PaletteMode::Default ? "Default"sv : "Dark"sv;
  433. auto theme = Gfx::load_system_theme(DeprecatedString::formatted("{}/res/themes/{}.ini", s_serenity_resource_root, theme_file)).release_value_but_fixme_should_propagate_errors();
  434. auto palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);
  435. auto palette = Gfx::Palette(move(palette_impl));
  436. auto translate = [&](Gfx::ColorRole gfx_color_role, QPalette::ColorRole qt_color_role) {
  437. auto new_color = Gfx::Color::from_argb(qt_palette.color(qt_color_role).rgba());
  438. palette.set_color(gfx_color_role, new_color);
  439. };
  440. translate(Gfx::ColorRole::ThreedHighlight, QPalette::ColorRole::Light);
  441. translate(Gfx::ColorRole::ThreedShadow1, QPalette::ColorRole::Mid);
  442. translate(Gfx::ColorRole::ThreedShadow2, QPalette::ColorRole::Dark);
  443. translate(Gfx::ColorRole::HoverHighlight, QPalette::ColorRole::Light);
  444. translate(Gfx::ColorRole::Link, QPalette::ColorRole::Link);
  445. translate(Gfx::ColorRole::VisitedLink, QPalette::ColorRole::LinkVisited);
  446. translate(Gfx::ColorRole::Button, QPalette::ColorRole::Button);
  447. translate(Gfx::ColorRole::ButtonText, QPalette::ColorRole::ButtonText);
  448. translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
  449. translate(Gfx::ColorRole::SelectionText, QPalette::ColorRole::HighlightedText);
  450. palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget));
  451. return theme;
  452. }
  453. void WebContentView::update_palette(PaletteMode mode)
  454. {
  455. client().async_update_system_theme(make_system_theme_from_qt_palette(*this, mode));
  456. }
  457. void WebContentView::create_client(WebView::EnableCallgrindProfiling enable_callgrind_profiling)
  458. {
  459. m_client_state = {};
  460. auto candidate_web_content_paths = get_paths_for_helper_process("WebContent"sv).release_value_but_fixme_should_propagate_errors();
  461. auto new_client = launch_web_content_process(candidate_web_content_paths, enable_callgrind_profiling).release_value_but_fixme_should_propagate_errors();
  462. m_client_state.client = new_client;
  463. m_client_state.client->on_web_content_process_crash = [this] {
  464. Core::deferred_invoke([this] {
  465. handle_web_content_process_crash();
  466. });
  467. };
  468. m_client_state.client_handle = Web::Crypto::generate_random_uuid().release_value_but_fixme_should_propagate_errors();
  469. client().async_set_window_handle(m_client_state.client_handle);
  470. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio);
  471. update_palette();
  472. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  473. // FIXME: Get the screen rect.
  474. // client().async_update_screen_rects(GUI::Desktop::the().rects(), GUI::Desktop::the().main_screen_index());
  475. if (!m_webdriver_content_ipc_path.is_empty())
  476. client().async_connect_to_webdriver(m_webdriver_content_ipc_path);
  477. }
  478. void WebContentView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id, Gfx::IntSize size)
  479. {
  480. if (m_client_state.back_bitmap.id == bitmap_id) {
  481. m_client_state.has_usable_bitmap = true;
  482. m_client_state.back_bitmap.pending_paints--;
  483. m_client_state.back_bitmap.last_painted_size = size;
  484. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  485. // We don't need the backup bitmap anymore, so drop it.
  486. m_backup_bitmap = nullptr;
  487. viewport()->update();
  488. if (m_client_state.got_repaint_requests_while_painting) {
  489. m_client_state.got_repaint_requests_while_painting = false;
  490. request_repaint();
  491. }
  492. }
  493. }
  494. void WebContentView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] Gfx::IntRect const& content_rect)
  495. {
  496. request_repaint();
  497. }
  498. void WebContentView::notify_server_did_change_selection(Badge<WebContentClient>)
  499. {
  500. request_repaint();
  501. }
  502. void WebContentView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  503. {
  504. switch (cursor) {
  505. case Gfx::StandardCursor::Hidden:
  506. setCursor(Qt::BlankCursor);
  507. break;
  508. case Gfx::StandardCursor::Arrow:
  509. setCursor(Qt::ArrowCursor);
  510. break;
  511. case Gfx::StandardCursor::Crosshair:
  512. setCursor(Qt::CrossCursor);
  513. break;
  514. case Gfx::StandardCursor::IBeam:
  515. setCursor(Qt::IBeamCursor);
  516. break;
  517. case Gfx::StandardCursor::ResizeHorizontal:
  518. setCursor(Qt::SizeHorCursor);
  519. break;
  520. case Gfx::StandardCursor::ResizeVertical:
  521. setCursor(Qt::SizeVerCursor);
  522. break;
  523. case Gfx::StandardCursor::ResizeDiagonalTLBR:
  524. setCursor(Qt::SizeFDiagCursor);
  525. break;
  526. case Gfx::StandardCursor::ResizeDiagonalBLTR:
  527. setCursor(Qt::SizeBDiagCursor);
  528. break;
  529. case Gfx::StandardCursor::ResizeColumn:
  530. setCursor(Qt::SplitHCursor);
  531. break;
  532. case Gfx::StandardCursor::ResizeRow:
  533. setCursor(Qt::SplitVCursor);
  534. break;
  535. case Gfx::StandardCursor::Hand:
  536. setCursor(Qt::PointingHandCursor);
  537. break;
  538. case Gfx::StandardCursor::Help:
  539. setCursor(Qt::WhatsThisCursor);
  540. break;
  541. case Gfx::StandardCursor::Drag:
  542. setCursor(Qt::ClosedHandCursor);
  543. break;
  544. case Gfx::StandardCursor::DragCopy:
  545. setCursor(Qt::DragCopyCursor);
  546. break;
  547. case Gfx::StandardCursor::Move:
  548. setCursor(Qt::DragMoveCursor);
  549. break;
  550. case Gfx::StandardCursor::Wait:
  551. setCursor(Qt::BusyCursor);
  552. break;
  553. case Gfx::StandardCursor::Disallowed:
  554. setCursor(Qt::ForbiddenCursor);
  555. break;
  556. case Gfx::StandardCursor::Eyedropper:
  557. case Gfx::StandardCursor::Zoom:
  558. // FIXME: No corresponding Qt cursors, default to Arrow
  559. default:
  560. setCursor(Qt::ArrowCursor);
  561. break;
  562. }
  563. }
  564. void WebContentView::notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size)
  565. {
  566. verticalScrollBar()->setMinimum(0);
  567. verticalScrollBar()->setMaximum(content_size.height() - m_viewport_rect.height());
  568. verticalScrollBar()->setPageStep(m_viewport_rect.height());
  569. horizontalScrollBar()->setMinimum(0);
  570. horizontalScrollBar()->setMaximum(content_size.width() - m_viewport_rect.width());
  571. horizontalScrollBar()->setPageStep(m_viewport_rect.width());
  572. }
  573. void WebContentView::notify_server_did_change_title(Badge<WebContentClient>, DeprecatedString const& title)
  574. {
  575. emit title_changed(qstring_from_ak_deprecated_string(title));
  576. }
  577. void WebContentView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
  578. {
  579. horizontalScrollBar()->setValue(max(0, horizontalScrollBar()->value() + x_delta));
  580. verticalScrollBar()->setValue(max(0, verticalScrollBar()->value() + y_delta));
  581. }
  582. void WebContentView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint scroll_position)
  583. {
  584. horizontalScrollBar()->setValue(scroll_position.x());
  585. verticalScrollBar()->setValue(scroll_position.y());
  586. }
  587. void WebContentView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const& rect)
  588. {
  589. if (m_viewport_rect.contains(rect))
  590. return;
  591. if (rect.top() < m_viewport_rect.top()) {
  592. verticalScrollBar()->setValue(rect.top());
  593. } else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom()) {
  594. verticalScrollBar()->setValue(rect.bottom() - m_viewport_rect.height() + 1);
  595. }
  596. }
  597. void WebContentView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint content_position, DeprecatedString const& tooltip)
  598. {
  599. auto widget_position = to_widget_position(content_position);
  600. QToolTip::showText(
  601. mapToGlobal(QPoint(widget_position.x(), widget_position.y())),
  602. qstring_from_ak_deprecated_string(tooltip),
  603. this);
  604. }
  605. void WebContentView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  606. {
  607. QToolTip::hideText();
  608. }
  609. void WebContentView::notify_server_did_hover_link(Badge<WebContentClient>, AK::URL const& url)
  610. {
  611. emit link_hovered(qstring_from_ak_deprecated_string(url.to_deprecated_string()));
  612. }
  613. void WebContentView::notify_server_did_unhover_link(Badge<WebContentClient>)
  614. {
  615. emit link_unhovered();
  616. }
  617. void WebContentView::notify_server_did_click_link(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& target, unsigned int modifiers)
  618. {
  619. if (on_link_click) {
  620. on_link_click(url, target, modifiers);
  621. }
  622. }
  623. void WebContentView::notify_server_did_middle_click_link(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& target, unsigned int modifiers)
  624. {
  625. if (on_link_middle_click) {
  626. on_link_middle_click(url, target, modifiers);
  627. }
  628. }
  629. void WebContentView::notify_server_did_start_loading(Badge<WebContentClient>, AK::URL const& url, bool is_redirect)
  630. {
  631. m_url = url;
  632. emit load_started(url, is_redirect);
  633. }
  634. void WebContentView::notify_server_did_finish_loading(Badge<WebContentClient>, AK::URL const& url)
  635. {
  636. m_url = url;
  637. if (on_load_finish)
  638. on_load_finish(url);
  639. }
  640. void WebContentView::notify_server_did_request_navigate_back(Badge<WebContentClient>)
  641. {
  642. emit navigate_back();
  643. }
  644. void WebContentView::notify_server_did_request_navigate_forward(Badge<WebContentClient>)
  645. {
  646. emit navigate_forward();
  647. }
  648. void WebContentView::notify_server_did_request_refresh(Badge<WebContentClient>)
  649. {
  650. emit refresh();
  651. }
  652. void WebContentView::notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position)
  653. {
  654. if (on_context_menu_request)
  655. on_context_menu_request(to_widget_position(content_position));
  656. }
  657. void WebContentView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, AK::URL const& url, DeprecatedString const&, unsigned)
  658. {
  659. if (on_link_context_menu_request)
  660. on_link_context_menu_request(url, to_widget_position(content_position));
  661. }
  662. void WebContentView::notify_server_did_request_image_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, AK::URL const& url, DeprecatedString const&, unsigned, Gfx::ShareableBitmap const& bitmap)
  663. {
  664. if (on_image_context_menu_request)
  665. on_image_context_menu_request(url, to_widget_position(content_position), bitmap);
  666. }
  667. void WebContentView::notify_server_did_request_video_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, AK::URL const& url, DeprecatedString const&, unsigned, bool is_playing, bool has_user_agent_controls, bool is_looping)
  668. {
  669. if (on_video_context_menu_request)
  670. on_video_context_menu_request(url, to_widget_position(content_position), is_playing, has_user_agent_controls, is_looping);
  671. }
  672. void WebContentView::notify_server_did_request_alert(Badge<WebContentClient>, String const& message)
  673. {
  674. m_dialog = new QMessageBox(QMessageBox::Icon::Warning, "Ladybird", qstring_from_ak_string(message), QMessageBox::StandardButton::Ok, this);
  675. m_dialog->exec();
  676. client().async_alert_closed();
  677. m_dialog = nullptr;
  678. }
  679. void WebContentView::notify_server_did_request_confirm(Badge<WebContentClient>, String const& message)
  680. {
  681. m_dialog = new QMessageBox(QMessageBox::Icon::Question, "Ladybird", qstring_from_ak_string(message), QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel, this);
  682. auto result = m_dialog->exec();
  683. client().async_confirm_closed(result == QMessageBox::StandardButton::Ok || result == QDialog::Accepted);
  684. m_dialog = nullptr;
  685. }
  686. void WebContentView::notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_)
  687. {
  688. m_dialog = new QInputDialog(this);
  689. auto& dialog = static_cast<QInputDialog&>(*m_dialog);
  690. dialog.setWindowTitle("Ladybird");
  691. dialog.setLabelText(qstring_from_ak_string(message));
  692. dialog.setTextValue(qstring_from_ak_string(default_));
  693. if (dialog.exec() == QDialog::Accepted)
  694. client().async_prompt_closed(ak_string_from_qstring(dialog.textValue()).release_value_but_fixme_should_propagate_errors());
  695. else
  696. client().async_prompt_closed({});
  697. m_dialog = nullptr;
  698. }
  699. void WebContentView::notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message)
  700. {
  701. if (m_dialog && is<QInputDialog>(*m_dialog))
  702. static_cast<QInputDialog&>(*m_dialog).setTextValue(qstring_from_ak_string(message));
  703. }
  704. void WebContentView::notify_server_did_request_accept_dialog(Badge<WebContentClient>)
  705. {
  706. if (m_dialog)
  707. m_dialog->accept();
  708. }
  709. void WebContentView::notify_server_did_request_dismiss_dialog(Badge<WebContentClient>)
  710. {
  711. if (m_dialog)
  712. m_dialog->reject();
  713. }
  714. void WebContentView::notify_server_did_get_source(AK::URL const& url, DeprecatedString const& source)
  715. {
  716. emit got_source(url, qstring_from_ak_deprecated_string(source));
  717. }
  718. void WebContentView::notify_server_did_get_dom_tree(DeprecatedString const& dom_tree)
  719. {
  720. if (on_get_dom_tree)
  721. on_get_dom_tree(dom_tree);
  722. }
  723. void WebContentView::notify_server_did_get_dom_node_properties(i32 node_id, DeprecatedString const& specified_style, DeprecatedString const& computed_style, DeprecatedString const& custom_properties, DeprecatedString const& node_box_sizing)
  724. {
  725. if (on_get_dom_node_properties)
  726. on_get_dom_node_properties(node_id, specified_style, computed_style, custom_properties, node_box_sizing);
  727. }
  728. void WebContentView::notify_server_did_output_js_console_message(i32 message_index)
  729. {
  730. if (on_js_console_new_message)
  731. on_js_console_new_message(message_index);
  732. }
  733. void WebContentView::notify_server_did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages)
  734. {
  735. if (on_get_js_console_messages)
  736. on_get_js_console_messages(start_index, message_types, messages);
  737. }
  738. void WebContentView::notify_server_did_change_favicon(Gfx::Bitmap const& bitmap)
  739. {
  740. auto qimage = QImage(bitmap.scanline_u8(0), bitmap.width(), bitmap.height(), QImage::Format_ARGB32);
  741. if (qimage.isNull())
  742. return;
  743. auto qpixmap = QPixmap::fromImage(qimage);
  744. if (qpixmap.isNull())
  745. return;
  746. emit favicon_changed(QIcon(qpixmap));
  747. }
  748. Vector<Web::Cookie::Cookie> WebContentView::notify_server_did_request_all_cookies(Badge<WebContentClient>, AK::URL const& url)
  749. {
  750. if (on_get_all_cookies)
  751. return on_get_all_cookies(url);
  752. return {};
  753. }
  754. Optional<Web::Cookie::Cookie> WebContentView::notify_server_did_request_named_cookie(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& name)
  755. {
  756. if (on_get_named_cookie)
  757. return on_get_named_cookie(url, name);
  758. return {};
  759. }
  760. DeprecatedString WebContentView::notify_server_did_request_cookie(Badge<WebContentClient>, AK::URL const& url, Web::Cookie::Source source)
  761. {
  762. if (on_get_cookie)
  763. return on_get_cookie(url, source);
  764. return {};
  765. }
  766. void WebContentView::notify_server_did_set_cookie(Badge<WebContentClient>, AK::URL const& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)
  767. {
  768. if (on_set_cookie)
  769. on_set_cookie(url, cookie, source);
  770. }
  771. void WebContentView::notify_server_did_update_cookie(Badge<WebContentClient>, Web::Cookie::Cookie const& cookie)
  772. {
  773. if (on_update_cookie)
  774. on_update_cookie(cookie);
  775. }
  776. void WebContentView::notify_server_did_close_browsing_context(Badge<WebContentClient>)
  777. {
  778. emit close();
  779. }
  780. String WebContentView::notify_server_did_request_new_tab(Badge<WebContentClient>, Web::HTML::ActivateTab activate_tab)
  781. {
  782. if (on_new_tab)
  783. return on_new_tab(activate_tab);
  784. return {};
  785. }
  786. void WebContentView::notify_server_did_request_activate_tab(Badge<WebContentClient>)
  787. {
  788. emit activate_tab();
  789. }
  790. void WebContentView::notify_server_did_update_resource_count(i32 count_waiting)
  791. {
  792. // FIXME
  793. (void)count_waiting;
  794. }
  795. void WebContentView::notify_server_did_request_restore_window()
  796. {
  797. emit restore_window();
  798. }
  799. Gfx::IntPoint WebContentView::notify_server_did_request_reposition_window(Gfx::IntPoint position)
  800. {
  801. return emit reposition_window(position);
  802. }
  803. Gfx::IntSize WebContentView::notify_server_did_request_resize_window(Gfx::IntSize size)
  804. {
  805. return emit resize_window(size);
  806. }
  807. Gfx::IntRect WebContentView::notify_server_did_request_maximize_window()
  808. {
  809. return emit maximize_window();
  810. }
  811. Gfx::IntRect WebContentView::notify_server_did_request_minimize_window()
  812. {
  813. return emit minimize_window();
  814. }
  815. Gfx::IntRect WebContentView::notify_server_did_request_fullscreen_window()
  816. {
  817. return emit fullscreen_window();
  818. }
  819. void WebContentView::notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32 request_id)
  820. {
  821. auto file = Core::File::open(path, Core::File::OpenMode::Read);
  822. if (file.is_error())
  823. client().async_handle_file_return(file.error().code(), {}, request_id);
  824. else
  825. client().async_handle_file_return(0, IPC::File(*file.value()), request_id);
  826. }
  827. Gfx::IntRect WebContentView::viewport_rect() const
  828. {
  829. return m_viewport_rect;
  830. }
  831. Gfx::IntPoint WebContentView::to_content_position(Gfx::IntPoint widget_position) const
  832. {
  833. return widget_position.translated(max(0, horizontalScrollBar()->value()), max(0, verticalScrollBar()->value()));
  834. }
  835. Gfx::IntPoint WebContentView::to_widget_position(Gfx::IntPoint content_position) const
  836. {
  837. return content_position.translated(-(max(0, horizontalScrollBar()->value())), -(max(0, verticalScrollBar()->value())));
  838. }
  839. bool WebContentView::event(QEvent* event)
  840. {
  841. // NOTE: We have to implement event() manually as Qt's focus navigation mechanism
  842. // eats all the Tab key presses by default.
  843. if (event->type() == QEvent::KeyPress) {
  844. keyPressEvent(static_cast<QKeyEvent*>(event));
  845. return true;
  846. }
  847. if (event->type() == QEvent::KeyRelease) {
  848. keyReleaseEvent(static_cast<QKeyEvent*>(event));
  849. return true;
  850. }
  851. if (event->type() == QEvent::PaletteChange) {
  852. update_palette();
  853. request_repaint();
  854. return QAbstractScrollArea::event(event);
  855. }
  856. return QAbstractScrollArea::event(event);
  857. }
  858. void WebContentView::notify_server_did_finish_handling_input_event(bool event_was_accepted)
  859. {
  860. // FIXME: Currently Ladybird handles the keyboard shortcuts before passing the event to web content, so
  861. // we don't need to do anything here. But we'll need to once we start asking web content first.
  862. (void)event_was_accepted;
  863. }
  864. void WebContentView::notify_server_did_get_accessibility_tree(DeprecatedString const& accessibility_tree)
  865. {
  866. if (on_get_accessibility_tree)
  867. on_get_accessibility_tree(accessibility_tree);
  868. }
  869. ErrorOr<String> WebContentView::dump_layout_tree()
  870. {
  871. return String::from_deprecated_string(client().dump_layout_tree());
  872. }