WebContentView.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #define AK_DONT_REPLACE_STD
  8. #include "WebContentView.h"
  9. #include "ConsoleWidget.h"
  10. #include "InspectorWidget.h"
  11. #include "Utilities.h"
  12. #include <AK/Assertions.h>
  13. #include <AK/ByteBuffer.h>
  14. #include <AK/Format.h>
  15. #include <AK/HashTable.h>
  16. #include <AK/LexicalPath.h>
  17. #include <AK/NonnullOwnPtr.h>
  18. #include <AK/StringBuilder.h>
  19. #include <AK/Types.h>
  20. #include <Browser/CookieJar.h>
  21. #include <Kernel/API/KeyCode.h>
  22. #include <LibCore/ArgsParser.h>
  23. #include <LibCore/EventLoop.h>
  24. #include <LibCore/File.h>
  25. #include <LibCore/IODevice.h>
  26. #include <LibCore/MemoryStream.h>
  27. #include <LibCore/Stream.h>
  28. #include <LibCore/System.h>
  29. #include <LibCore/Timer.h>
  30. #include <LibGfx/Bitmap.h>
  31. #include <LibGfx/Font/FontDatabase.h>
  32. #include <LibGfx/PNGWriter.h>
  33. #include <LibGfx/Painter.h>
  34. #include <LibGfx/Rect.h>
  35. #include <LibGfx/SystemTheme.h>
  36. #include <LibJS/Runtime/ConsoleObject.h>
  37. #include <LibMain/Main.h>
  38. #include <LibWeb/Loader/ContentFilter.h>
  39. #include <LibWebView/WebContentClient.h>
  40. #include <QApplication>
  41. #include <QCursor>
  42. #include <QIcon>
  43. #include <QInputDialog>
  44. #include <QLineEdit>
  45. #include <QMessageBox>
  46. #include <QMouseEvent>
  47. #include <QPaintEvent>
  48. #include <QPainter>
  49. #include <QScrollBar>
  50. #include <QSocketNotifier>
  51. #include <QTextEdit>
  52. #include <QTimer>
  53. #include <QToolTip>
  54. WebContentView::WebContentView(StringView webdriver_content_ipc_path)
  55. : m_webdriver_content_ipc_path(webdriver_content_ipc_path)
  56. {
  57. setMouseTracking(true);
  58. setFocusPolicy(Qt::FocusPolicy::StrongFocus);
  59. m_device_pixel_ratio = devicePixelRatio();
  60. m_inverse_pixel_scaling_ratio = 1.0 / m_device_pixel_ratio;
  61. verticalScrollBar()->setSingleStep(24);
  62. horizontalScrollBar()->setSingleStep(24);
  63. QObject::connect(verticalScrollBar(), &QScrollBar::valueChanged, [this](int) {
  64. update_viewport_rect();
  65. });
  66. QObject::connect(horizontalScrollBar(), &QScrollBar::valueChanged, [this](int) {
  67. update_viewport_rect();
  68. });
  69. create_client();
  70. }
  71. WebContentView::~WebContentView()
  72. {
  73. close_sub_widgets();
  74. }
  75. void WebContentView::load(AK::URL const& url)
  76. {
  77. m_url = url;
  78. client().async_load_url(url);
  79. }
  80. void WebContentView::load_html(StringView html, AK::URL const& url)
  81. {
  82. m_url = url;
  83. client().async_load_html(html, url);
  84. }
  85. unsigned get_button_from_qt_event(QMouseEvent const& event)
  86. {
  87. if (event.button() == Qt::MouseButton::LeftButton)
  88. return 1;
  89. if (event.button() == Qt::MouseButton::RightButton)
  90. return 2;
  91. if (event.button() == Qt::MouseButton::MiddleButton)
  92. return 4;
  93. if (event.button() == Qt::MouseButton::BackButton)
  94. return 8;
  95. if (event.buttons() == Qt::MouseButton::ForwardButton)
  96. return 16;
  97. return 0;
  98. }
  99. unsigned get_buttons_from_qt_event(QMouseEvent const& event)
  100. {
  101. unsigned buttons = 0;
  102. if (event.buttons() & Qt::MouseButton::LeftButton)
  103. buttons |= 1;
  104. if (event.buttons() & Qt::MouseButton::RightButton)
  105. buttons |= 2;
  106. if (event.buttons() & Qt::MouseButton::MiddleButton)
  107. buttons |= 4;
  108. if (event.buttons() & Qt::MouseButton::BackButton)
  109. buttons |= 8;
  110. if (event.buttons() & Qt::MouseButton::ForwardButton)
  111. buttons |= 16;
  112. return buttons;
  113. }
  114. unsigned get_modifiers_from_qt_mouse_event(QMouseEvent const& event)
  115. {
  116. unsigned modifiers = 0;
  117. if (event.modifiers() & Qt::Modifier::ALT)
  118. modifiers |= 1;
  119. if (event.modifiers() & Qt::Modifier::CTRL)
  120. modifiers |= 2;
  121. if (event.modifiers() & Qt::Modifier::SHIFT)
  122. modifiers |= 4;
  123. return modifiers;
  124. }
  125. unsigned get_modifiers_from_qt_keyboard_event(QKeyEvent const& event)
  126. {
  127. auto modifiers = 0;
  128. if (event.modifiers().testFlag(Qt::AltModifier))
  129. modifiers |= KeyModifier::Mod_Alt;
  130. if (event.modifiers().testFlag(Qt::ControlModifier))
  131. modifiers |= KeyModifier::Mod_Ctrl;
  132. if (event.modifiers().testFlag(Qt::MetaModifier))
  133. modifiers |= KeyModifier::Mod_Super;
  134. if (event.modifiers().testFlag(Qt::ShiftModifier))
  135. modifiers |= KeyModifier::Mod_Shift;
  136. if (event.modifiers().testFlag(Qt::AltModifier))
  137. modifiers |= KeyModifier::Mod_AltGr;
  138. return modifiers;
  139. }
  140. KeyCode get_keycode_from_qt_keyboard_event(QKeyEvent const& event)
  141. {
  142. struct Mapping {
  143. constexpr Mapping(Qt::Key q, KeyCode s)
  144. : qt_key(q)
  145. , serenity_key(s)
  146. {
  147. }
  148. Qt::Key qt_key;
  149. KeyCode serenity_key;
  150. };
  151. constexpr Mapping mappings[] = {
  152. { Qt::Key_0, Key_0 },
  153. { Qt::Key_1, Key_1 },
  154. { Qt::Key_2, Key_2 },
  155. { Qt::Key_3, Key_3 },
  156. { Qt::Key_4, Key_4 },
  157. { Qt::Key_5, Key_5 },
  158. { Qt::Key_6, Key_6 },
  159. { Qt::Key_7, Key_7 },
  160. { Qt::Key_8, Key_8 },
  161. { Qt::Key_9, Key_9 },
  162. { Qt::Key_A, Key_A },
  163. { Qt::Key_Alt, Key_Alt },
  164. { Qt::Key_Ampersand, Key_Ampersand },
  165. { Qt::Key_Apostrophe, Key_Apostrophe },
  166. { Qt::Key_AsciiCircum, Key_Circumflex },
  167. { Qt::Key_AsciiTilde, Key_Tilde },
  168. { Qt::Key_Asterisk, Key_Asterisk },
  169. { Qt::Key_At, Key_AtSign },
  170. { Qt::Key_B, Key_B },
  171. { Qt::Key_Backslash, Key_Backslash },
  172. { Qt::Key_Backspace, Key_Backspace },
  173. { Qt::Key_Bar, Key_Pipe },
  174. { Qt::Key_BraceLeft, Key_LeftBrace },
  175. { Qt::Key_BraceRight, Key_RightBrace },
  176. { Qt::Key_BracketLeft, Key_LeftBracket },
  177. { Qt::Key_BracketRight, Key_RightBracket },
  178. { Qt::Key_C, Key_C },
  179. { Qt::Key_CapsLock, Key_CapsLock },
  180. { Qt::Key_Colon, Key_Colon },
  181. { Qt::Key_Comma, Key_Comma },
  182. { Qt::Key_Control, Key_Control },
  183. { Qt::Key_D, Key_D },
  184. { Qt::Key_Delete, Key_Delete },
  185. { Qt::Key_Dollar, Key_Dollar },
  186. { Qt::Key_Down, Key_Down },
  187. { Qt::Key_E, Key_E },
  188. { Qt::Key_End, Key_End },
  189. { Qt::Key_Equal, Key_Equal },
  190. { Qt::Key_Escape, Key_Escape },
  191. { Qt::Key_exclamdown, Key_ExclamationPoint },
  192. { Qt::Key_F, Key_F },
  193. { Qt::Key_F1, Key_F1 },
  194. { Qt::Key_F10, Key_F10 },
  195. { Qt::Key_F11, Key_F11 },
  196. { Qt::Key_F12, Key_F12 },
  197. { Qt::Key_F2, Key_F2 },
  198. { Qt::Key_F3, Key_F3 },
  199. { Qt::Key_F4, Key_F4 },
  200. { Qt::Key_F5, Key_F5 },
  201. { Qt::Key_F6, Key_F6 },
  202. { Qt::Key_F7, Key_F7 },
  203. { Qt::Key_F8, Key_F8 },
  204. { Qt::Key_F9, Key_F9 },
  205. { Qt::Key_G, Key_G },
  206. { Qt::Key_Greater, Key_GreaterThan },
  207. { Qt::Key_H, Key_H },
  208. { Qt::Key_Home, Key_Home },
  209. { Qt::Key_I, Key_I },
  210. { Qt::Key_Insert, Key_Insert },
  211. { Qt::Key_J, Key_J },
  212. { Qt::Key_K, Key_K },
  213. { Qt::Key_L, Key_L },
  214. { Qt::Key_Left, Key_Left },
  215. { Qt::Key_Less, Key_LessThan },
  216. { Qt::Key_M, Key_M },
  217. { Qt::Key_Menu, Key_Menu },
  218. { Qt::Key_Minus, Key_Minus },
  219. { Qt::Key_N, Key_N },
  220. { Qt::Key_NumLock, Key_NumLock },
  221. { Qt::Key_O, Key_O },
  222. { Qt::Key_P, Key_P },
  223. { Qt::Key_PageDown, Key_PageDown },
  224. { Qt::Key_PageUp, Key_PageUp },
  225. { Qt::Key_ParenLeft, Key_LeftParen },
  226. { Qt::Key_ParenRight, Key_RightParen },
  227. { Qt::Key_Percent, Key_Percent },
  228. { Qt::Key_Period, Key_Period },
  229. { Qt::Key_Plus, Key_Plus },
  230. { Qt::Key_Print, Key_PrintScreen },
  231. { Qt::Key_Q, Key_Q },
  232. { Qt::Key_Question, Key_QuestionMark },
  233. { Qt::Key_QuoteDbl, Key_DoubleQuote },
  234. { Qt::Key_R, Key_R },
  235. { Qt::Key_Return, Key_Return },
  236. { Qt::Key_Right, Key_Right },
  237. { Qt::Key_S, Key_S },
  238. { Qt::Key_ScrollLock, Key_ScrollLock },
  239. { Qt::Key_Semicolon, Key_Semicolon },
  240. { Qt::Key_Shift, Key_LeftShift },
  241. { Qt::Key_Slash, Key_Slash },
  242. { Qt::Key_Space, Key_Space },
  243. { Qt::Key_Super_L, Key_Super },
  244. { Qt::Key_SysReq, Key_SysRq },
  245. { Qt::Key_T, Key_T },
  246. { Qt::Key_Tab, Key_Tab },
  247. { Qt::Key_U, Key_U },
  248. { Qt::Key_Underscore, Key_Underscore },
  249. { Qt::Key_Up, Key_Up },
  250. { Qt::Key_V, Key_V },
  251. { Qt::Key_W, Key_W },
  252. { Qt::Key_X, Key_X },
  253. { Qt::Key_Y, Key_Y },
  254. { Qt::Key_Z, Key_Z },
  255. };
  256. for (auto const& mapping : mappings) {
  257. if (event.key() == mapping.qt_key)
  258. return mapping.serenity_key;
  259. }
  260. return Key_Invalid;
  261. }
  262. void WebContentView::mouseMoveEvent(QMouseEvent* event)
  263. {
  264. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  265. auto buttons = get_buttons_from_qt_event(*event);
  266. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  267. client().async_mouse_move(to_content(position), 0, buttons, modifiers);
  268. }
  269. void WebContentView::mousePressEvent(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 (button == 0) {
  274. // We could not convert Qt buttons to something that Lagom can
  275. // recognize - don't even bother propagating this to the web engine
  276. // as it will not handle it anyway, and it will (currently) assert
  277. return;
  278. }
  279. auto modifiers = get_modifiers_from_qt_mouse_event(*event);
  280. auto buttons = get_buttons_from_qt_event(*event);
  281. client().async_mouse_down(to_content(position), button, buttons, modifiers);
  282. }
  283. void WebContentView::mouseReleaseEvent(QMouseEvent* event)
  284. {
  285. Gfx::IntPoint position(event->position().x() / m_inverse_pixel_scaling_ratio, event->position().y() / m_inverse_pixel_scaling_ratio);
  286. auto button = get_button_from_qt_event(*event);
  287. if (event->button() & Qt::MouseButton::BackButton) {
  288. emit back_mouse_button();
  289. } else if (event->button() & Qt::MouseButton::ForwardButton) {
  290. emit forward_mouse_button();
  291. }
  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_mouse_up(to_content(position), button, buttons, modifiers);
  301. }
  302. void WebContentView::keyPressEvent(QKeyEvent* event)
  303. {
  304. switch (event->key()) {
  305. case Qt::Key_Left:
  306. case Qt::Key_Right:
  307. case Qt::Key_Up:
  308. case Qt::Key_Down:
  309. case Qt::Key_PageUp:
  310. case Qt::Key_PageDown:
  311. QAbstractScrollArea::keyPressEvent(event);
  312. break;
  313. default:
  314. break;
  315. }
  316. if (event->key() == Qt::Key_Backtab) {
  317. // NOTE: Qt transforms Shift+Tab into a "Backtab", so we undo that transformation here.
  318. client().async_key_down(KeyCode::Key_Tab, Mod_Shift, '\t');
  319. return;
  320. }
  321. auto text = event->text();
  322. if (text.isEmpty()) {
  323. return;
  324. }
  325. auto point = event->text()[0].unicode();
  326. auto keycode = get_keycode_from_qt_keyboard_event(*event);
  327. auto modifiers = get_modifiers_from_qt_keyboard_event(*event);
  328. client().async_key_down(keycode, modifiers, point);
  329. }
  330. void WebContentView::keyReleaseEvent(QKeyEvent* event)
  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_up(keycode, modifiers, point);
  340. }
  341. void WebContentView::focusInEvent(QFocusEvent*)
  342. {
  343. client().async_set_has_focus(true);
  344. }
  345. void WebContentView::focusOutEvent(QFocusEvent*)
  346. {
  347. client().async_set_has_focus(false);
  348. }
  349. Gfx::IntPoint WebContentView::to_content(Gfx::IntPoint viewport_position) const
  350. {
  351. return viewport_position.translated(horizontalScrollBar()->value(), verticalScrollBar()->value());
  352. }
  353. Gfx::IntPoint WebContentView::to_widget(Gfx::IntPoint content_position) const
  354. {
  355. return content_position.translated(-horizontalScrollBar()->value(), -verticalScrollBar()->value());
  356. }
  357. void WebContentView::paintEvent(QPaintEvent*)
  358. {
  359. QPainter painter(viewport());
  360. painter.scale(m_inverse_pixel_scaling_ratio, m_inverse_pixel_scaling_ratio);
  361. if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr()) {
  362. QImage q_image(bitmap->scanline_u8(0), bitmap->width(), bitmap->height(), QImage::Format_RGB32);
  363. painter.drawImage(QPoint(0, 0), q_image);
  364. return;
  365. }
  366. painter.fillRect(rect(), palette().base());
  367. }
  368. void WebContentView::resizeEvent(QResizeEvent* event)
  369. {
  370. QAbstractScrollArea::resizeEvent(event);
  371. handle_resize();
  372. }
  373. void WebContentView::handle_resize()
  374. {
  375. update_viewport_rect();
  376. if (m_client_state.has_usable_bitmap) {
  377. // NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.
  378. m_backup_bitmap = m_client_state.front_bitmap.bitmap;
  379. }
  380. if (m_client_state.front_bitmap.bitmap)
  381. client().async_remove_backing_store(m_client_state.front_bitmap.id);
  382. if (m_client_state.back_bitmap.bitmap)
  383. client().async_remove_backing_store(m_client_state.back_bitmap.id);
  384. m_client_state.front_bitmap = {};
  385. m_client_state.back_bitmap = {};
  386. m_client_state.has_usable_bitmap = false;
  387. auto available_size = m_viewport_rect.size();
  388. if (available_size.is_empty())
  389. return;
  390. if (auto new_bitmap_or_error = Gfx::Bitmap::try_create_shareable(Gfx::BitmapFormat::BGRx8888, available_size); !new_bitmap_or_error.is_error()) {
  391. m_client_state.front_bitmap.bitmap = new_bitmap_or_error.release_value();
  392. m_client_state.front_bitmap.id = m_client_state.next_bitmap_id++;
  393. client().async_add_backing_store(m_client_state.front_bitmap.id, m_client_state.front_bitmap.bitmap->to_shareable_bitmap());
  394. }
  395. if (auto new_bitmap_or_error = Gfx::Bitmap::try_create_shareable(Gfx::BitmapFormat::BGRx8888, available_size); !new_bitmap_or_error.is_error()) {
  396. m_client_state.back_bitmap.bitmap = new_bitmap_or_error.release_value();
  397. m_client_state.back_bitmap.id = m_client_state.next_bitmap_id++;
  398. client().async_add_backing_store(m_client_state.back_bitmap.id, m_client_state.back_bitmap.bitmap->to_shareable_bitmap());
  399. }
  400. request_repaint();
  401. }
  402. void WebContentView::update_viewport_rect()
  403. {
  404. auto scaled_width = int(viewport()->width() / m_inverse_pixel_scaling_ratio);
  405. auto scaled_height = int(viewport()->height() / m_inverse_pixel_scaling_ratio);
  406. Gfx::IntRect rect(horizontalScrollBar()->value(), verticalScrollBar()->value(), scaled_width, scaled_height);
  407. m_viewport_rect = rect;
  408. client().async_set_viewport_rect(rect);
  409. request_repaint();
  410. }
  411. void WebContentView::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
  412. {
  413. client().async_debug_request(request, argument);
  414. }
  415. void WebContentView::run_javascript(DeprecatedString const& js_source)
  416. {
  417. client().async_run_javascript(js_source);
  418. }
  419. void WebContentView::did_output_js_console_message(i32 message_index)
  420. {
  421. if (m_console_widget)
  422. m_console_widget->notify_about_new_console_message(message_index);
  423. }
  424. void WebContentView::did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> message_types, Vector<DeprecatedString> messages)
  425. {
  426. if (m_console_widget)
  427. m_console_widget->handle_console_messages(start_index, message_types, messages);
  428. }
  429. void WebContentView::ensure_js_console_widget()
  430. {
  431. if (!m_console_widget) {
  432. m_console_widget = new Ladybird::ConsoleWidget;
  433. m_console_widget->setWindowTitle("JS Console");
  434. m_console_widget->resize(640, 480);
  435. m_console_widget->on_js_input = [this](auto js_source) {
  436. client().async_js_console_input(js_source);
  437. };
  438. m_console_widget->on_request_messages = [this](i32 start_index) {
  439. client().async_js_console_request_messages(start_index);
  440. };
  441. }
  442. }
  443. void WebContentView::show_js_console()
  444. {
  445. ensure_js_console_widget();
  446. m_console_widget->show();
  447. }
  448. void WebContentView::ensure_inspector_widget()
  449. {
  450. if (m_inspector_widget)
  451. return;
  452. m_inspector_widget = new Ladybird::InspectorWidget;
  453. m_inspector_widget->setWindowTitle("Inspector");
  454. m_inspector_widget->resize(640, 480);
  455. m_inspector_widget->on_close = [this] {
  456. clear_inspected_dom_node();
  457. };
  458. m_inspector_widget->on_dom_node_inspected = [&](auto id, auto pseudo_element) {
  459. return inspect_dom_node(id, pseudo_element);
  460. };
  461. }
  462. void WebContentView::close_sub_widgets()
  463. {
  464. auto close_widget_window = [](auto* widget) {
  465. if (widget)
  466. widget->close();
  467. };
  468. close_widget_window(m_console_widget);
  469. close_widget_window(m_inspector_widget);
  470. }
  471. bool WebContentView::is_inspector_open() const
  472. {
  473. return m_inspector_widget && m_inspector_widget->isVisible();
  474. }
  475. void WebContentView::inspect_dom_tree()
  476. {
  477. client().async_inspect_dom_tree();
  478. }
  479. ErrorOr<Ladybird::DOMNodeProperties> WebContentView::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element)
  480. {
  481. auto response = client().inspect_dom_node(node_id, pseudo_element);
  482. if (!response.has_style())
  483. return Error::from_string_view("Inspected node returned no style"sv);
  484. return Ladybird::DOMNodeProperties {
  485. .computed_style_json = TRY(String::from_deprecated_string(response.take_computed_style())),
  486. .resolved_style_json = TRY(String::from_deprecated_string(response.take_resolved_style())),
  487. .custom_properties_json = TRY(String::from_deprecated_string(response.take_custom_properties())),
  488. };
  489. }
  490. void WebContentView::clear_inspected_dom_node()
  491. {
  492. (void)inspect_dom_node(0, {});
  493. }
  494. void WebContentView::show_inspector()
  495. {
  496. ensure_inspector_widget();
  497. m_inspector_widget->show();
  498. inspect_dom_tree();
  499. }
  500. void WebContentView::set_color_scheme(ColorScheme color_scheme)
  501. {
  502. switch (color_scheme) {
  503. case ColorScheme::Auto:
  504. client().async_set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Auto);
  505. break;
  506. case ColorScheme::Light:
  507. client().async_set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Light);
  508. break;
  509. case ColorScheme::Dark:
  510. client().async_set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Dark);
  511. break;
  512. }
  513. }
  514. void WebContentView::zoom_in()
  515. {
  516. if (m_zoom_level >= ZOOM_MAX_LEVEL)
  517. return;
  518. m_zoom_level += ZOOM_STEP;
  519. update_zoom();
  520. }
  521. void WebContentView::zoom_out()
  522. {
  523. if (m_zoom_level <= ZOOM_MIN_LEVEL)
  524. return;
  525. m_zoom_level -= ZOOM_STEP;
  526. update_zoom();
  527. }
  528. void WebContentView::reset_zoom()
  529. {
  530. m_zoom_level = 1.0f;
  531. update_zoom();
  532. }
  533. void WebContentView::update_zoom()
  534. {
  535. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio * m_zoom_level);
  536. update_viewport_rect();
  537. request_repaint();
  538. }
  539. void WebContentView::showEvent(QShowEvent* event)
  540. {
  541. QAbstractScrollArea::showEvent(event);
  542. client().async_set_system_visibility_state(true);
  543. }
  544. void WebContentView::hideEvent(QHideEvent* event)
  545. {
  546. QAbstractScrollArea::hideEvent(event);
  547. client().async_set_system_visibility_state(false);
  548. }
  549. WebContentClient& WebContentView::client()
  550. {
  551. VERIFY(m_client_state.client);
  552. return *m_client_state.client;
  553. }
  554. void WebContentView::create_client()
  555. {
  556. m_client_state = {};
  557. int socket_fds[2] {};
  558. MUST(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  559. int ui_fd = socket_fds[0];
  560. int wc_fd = socket_fds[1];
  561. int fd_passing_socket_fds[2] {};
  562. MUST(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  563. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  564. int wc_fd_passing_fd = fd_passing_socket_fds[1];
  565. auto child_pid = fork();
  566. if (!child_pid) {
  567. MUST(Core::System::close(ui_fd_passing_fd));
  568. MUST(Core::System::close(ui_fd));
  569. auto takeover_string = DeprecatedString::formatted("WebContent:{}", wc_fd);
  570. MUST(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
  571. auto webcontent_fd_passing_socket_string = DeprecatedString::number(wc_fd_passing_fd);
  572. Vector<StringView> arguments {
  573. "WebContent"sv,
  574. "--webcontent-fd-passing-socket"sv,
  575. webcontent_fd_passing_socket_string
  576. };
  577. if (!m_webdriver_content_ipc_path.is_empty()) {
  578. arguments.append("--webdriver-content-path"sv);
  579. arguments.append(m_webdriver_content_ipc_path);
  580. }
  581. auto result = Core::System::exec("./WebContent/WebContent"sv, arguments, Core::System::SearchInPath::Yes);
  582. if (result.is_error()) {
  583. auto web_content_path = ak_deprecated_string_from_qstring(QCoreApplication::applicationDirPath() + "/WebContent");
  584. result = Core::System::exec(web_content_path, arguments, Core::System::SearchInPath::Yes);
  585. }
  586. if (result.is_error())
  587. warnln("Could not launch WebContent: {}", result.error());
  588. VERIFY_NOT_REACHED();
  589. }
  590. MUST(Core::System::close(wc_fd_passing_fd));
  591. MUST(Core::System::close(wc_fd));
  592. auto socket = MUST(Core::Stream::LocalSocket::adopt_fd(ui_fd));
  593. MUST(socket->set_blocking(true));
  594. auto new_client = MUST(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(std::move(socket), *this)));
  595. new_client->set_fd_passing_socket(MUST(Core::Stream::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  596. auto* notifier = new QSocketNotifier(new_client->socket().fd().value(), QSocketNotifier::Type::Read);
  597. QObject::connect(notifier, &QSocketNotifier::activated, [new_client = new_client.ptr()] {
  598. if (auto notifier = new_client->socket().notifier())
  599. notifier->on_ready_to_read();
  600. });
  601. struct DeferredInvokerQt final : IPC::DeferredInvoker {
  602. virtual ~DeferredInvokerQt() = default;
  603. virtual void schedule(Function<void()> callback) override
  604. {
  605. QTimer::singleShot(0, std::move(callback));
  606. }
  607. };
  608. new_client->set_deferred_invoker(make<DeferredInvokerQt>());
  609. m_client_state.client = new_client;
  610. m_client_state.client->on_web_content_process_crash = [this] {
  611. QTimer::singleShot(0, [this] {
  612. handle_web_content_process_crash();
  613. });
  614. };
  615. client().async_set_device_pixels_per_css_pixel(m_device_pixel_ratio);
  616. client().async_update_system_theme(MUST(Gfx::load_system_theme(DeprecatedString::formatted("{}/res/themes/Default.ini", s_serenity_resource_root))));
  617. client().async_update_system_fonts(Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query());
  618. // FIXME: Get the screen rect.
  619. // client().async_update_screen_rects(GUI::Desktop::the().rects(), GUI::Desktop::the().main_screen_index());
  620. }
  621. void WebContentView::handle_web_content_process_crash()
  622. {
  623. dbgln("WebContent process crashed!");
  624. create_client();
  625. VERIFY(m_client_state.client);
  626. // Don't keep a stale backup bitmap around.
  627. m_backup_bitmap = nullptr;
  628. handle_resize();
  629. StringBuilder builder;
  630. builder.append("<html><head><title>Crashed: "sv);
  631. builder.append(escape_html_entities(m_url.to_deprecated_string()));
  632. builder.append("</title></head><body>"sv);
  633. builder.append("<h1>Web page crashed"sv);
  634. if (!m_url.host().is_empty()) {
  635. builder.appendff(" on {}", escape_html_entities(m_url.host()));
  636. }
  637. builder.append("</h1>"sv);
  638. auto escaped_url = escape_html_entities(m_url.to_deprecated_string());
  639. builder.appendff("The web page <a href=\"{}\">{}</a> has crashed.<br><br>You can reload the page to try again.", escaped_url, escaped_url);
  640. builder.append("</body></html>"sv);
  641. load_html(builder.to_deprecated_string(), m_url);
  642. }
  643. void WebContentView::notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id)
  644. {
  645. if (m_client_state.back_bitmap.id == bitmap_id) {
  646. m_client_state.has_usable_bitmap = true;
  647. m_client_state.back_bitmap.pending_paints--;
  648. swap(m_client_state.back_bitmap, m_client_state.front_bitmap);
  649. // We don't need the backup bitmap anymore, so drop it.
  650. m_backup_bitmap = nullptr;
  651. viewport()->update();
  652. if (m_client_state.got_repaint_requests_while_painting) {
  653. m_client_state.got_repaint_requests_while_painting = false;
  654. request_repaint();
  655. }
  656. }
  657. }
  658. void WebContentView::notify_server_did_invalidate_content_rect(Badge<WebContentClient>, [[maybe_unused]] Gfx::IntRect const& content_rect)
  659. {
  660. request_repaint();
  661. }
  662. void WebContentView::notify_server_did_change_selection(Badge<WebContentClient>)
  663. {
  664. request_repaint();
  665. }
  666. void WebContentView::notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor)
  667. {
  668. switch (cursor) {
  669. case Gfx::StandardCursor::Hand:
  670. setCursor(Qt::PointingHandCursor);
  671. break;
  672. case Gfx::StandardCursor::IBeam:
  673. setCursor(Qt::IBeamCursor);
  674. break;
  675. case Gfx::StandardCursor::Arrow:
  676. default:
  677. setCursor(Qt::ArrowCursor);
  678. break;
  679. }
  680. }
  681. void WebContentView::notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size)
  682. {
  683. verticalScrollBar()->setMinimum(0);
  684. verticalScrollBar()->setMaximum(content_size.height() - m_viewport_rect.height());
  685. verticalScrollBar()->setPageStep(m_viewport_rect.height());
  686. horizontalScrollBar()->setMinimum(0);
  687. horizontalScrollBar()->setMaximum(content_size.width() - m_viewport_rect.width());
  688. horizontalScrollBar()->setPageStep(m_viewport_rect.width());
  689. }
  690. void WebContentView::notify_server_did_change_title(Badge<WebContentClient>, DeprecatedString const& title)
  691. {
  692. emit title_changed(qstring_from_ak_deprecated_string(title));
  693. }
  694. void WebContentView::notify_server_did_request_scroll(Badge<WebContentClient>, i32 x_delta, i32 y_delta)
  695. {
  696. horizontalScrollBar()->setValue(horizontalScrollBar()->value() + x_delta);
  697. verticalScrollBar()->setValue(verticalScrollBar()->value() + y_delta);
  698. }
  699. void WebContentView::notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint scroll_position)
  700. {
  701. horizontalScrollBar()->setValue(scroll_position.x());
  702. verticalScrollBar()->setValue(scroll_position.y());
  703. }
  704. void WebContentView::notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const& rect)
  705. {
  706. if (m_viewport_rect.contains(rect))
  707. return;
  708. if (rect.top() < m_viewport_rect.top()) {
  709. verticalScrollBar()->setValue(rect.top());
  710. } else if (rect.top() > m_viewport_rect.top() && rect.bottom() > m_viewport_rect.bottom()) {
  711. verticalScrollBar()->setValue(rect.bottom() - m_viewport_rect.height() + 1);
  712. }
  713. }
  714. void WebContentView::notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint content_position, DeprecatedString const& tooltip)
  715. {
  716. auto widget_position = to_widget(content_position);
  717. QToolTip::showText(
  718. mapToGlobal(QPoint(widget_position.x(), widget_position.y())),
  719. qstring_from_ak_deprecated_string(tooltip),
  720. this);
  721. }
  722. void WebContentView::notify_server_did_leave_tooltip_area(Badge<WebContentClient>)
  723. {
  724. QToolTip::hideText();
  725. }
  726. void WebContentView::notify_server_did_hover_link(Badge<WebContentClient>, AK::URL const& url)
  727. {
  728. emit link_hovered(qstring_from_ak_deprecated_string(url.to_deprecated_string()));
  729. }
  730. void WebContentView::notify_server_did_unhover_link(Badge<WebContentClient>)
  731. {
  732. emit link_unhovered();
  733. }
  734. void WebContentView::notify_server_did_click_link(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& target, unsigned int modifiers)
  735. {
  736. // FIXME
  737. (void)url;
  738. (void)target;
  739. (void)modifiers;
  740. // if (on_link_click)
  741. // on_link_click(url, target, modifiers);
  742. }
  743. void WebContentView::notify_server_did_middle_click_link(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& target, unsigned int modifiers)
  744. {
  745. (void)url;
  746. (void)target;
  747. (void)modifiers;
  748. }
  749. void WebContentView::notify_server_did_start_loading(Badge<WebContentClient>, AK::URL const& url, bool is_redirect)
  750. {
  751. m_url = url;
  752. emit load_started(url, is_redirect);
  753. if (m_inspector_widget)
  754. m_inspector_widget->clear_dom_json();
  755. }
  756. void WebContentView::notify_server_did_finish_loading(Badge<WebContentClient>, AK::URL const& url)
  757. {
  758. m_url = url;
  759. if (is_inspector_open())
  760. inspect_dom_tree();
  761. }
  762. void WebContentView::notify_server_did_request_navigate_back(Badge<WebContentClient>)
  763. {
  764. emit navigate_back();
  765. }
  766. void WebContentView::notify_server_did_request_navigate_forward(Badge<WebContentClient>)
  767. {
  768. emit navigate_forward();
  769. }
  770. void WebContentView::notify_server_did_request_refresh(Badge<WebContentClient>)
  771. {
  772. emit refresh();
  773. }
  774. void WebContentView::notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position)
  775. {
  776. // FIXME
  777. (void)content_position;
  778. }
  779. void WebContentView::notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint content_position, AK::URL const& url, DeprecatedString const&, unsigned)
  780. {
  781. // FIXME
  782. (void)content_position;
  783. (void)url;
  784. }
  785. 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)
  786. {
  787. // FIXME
  788. (void)content_position;
  789. (void)url;
  790. (void)bitmap;
  791. }
  792. void WebContentView::notify_server_did_request_alert(Badge<WebContentClient>, DeprecatedString const& message)
  793. {
  794. m_dialog = new QMessageBox(QMessageBox::Icon::Warning, "Ladybird", qstring_from_ak_deprecated_string(message), QMessageBox::StandardButton::Ok, this);
  795. m_dialog->exec();
  796. client().async_alert_closed();
  797. m_dialog = nullptr;
  798. }
  799. void WebContentView::notify_server_did_request_confirm(Badge<WebContentClient>, DeprecatedString const& message)
  800. {
  801. m_dialog = new QMessageBox(QMessageBox::Icon::Question, "Ladybird", qstring_from_ak_deprecated_string(message), QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel, this);
  802. auto result = m_dialog->exec();
  803. client().async_confirm_closed(result == QMessageBox::StandardButton::Ok || result == QDialog::Accepted);
  804. m_dialog = nullptr;
  805. }
  806. void WebContentView::notify_server_did_request_prompt(Badge<WebContentClient>, DeprecatedString const& message, DeprecatedString const& default_)
  807. {
  808. m_dialog = new QInputDialog(this);
  809. auto& dialog = static_cast<QInputDialog&>(*m_dialog);
  810. dialog.setWindowTitle("Ladybird");
  811. dialog.setLabelText(qstring_from_ak_deprecated_string(message));
  812. dialog.setTextValue(qstring_from_ak_deprecated_string(default_));
  813. if (dialog.exec() == QDialog::Accepted)
  814. client().async_prompt_closed(ak_deprecated_string_from_qstring(dialog.textValue()));
  815. else
  816. client().async_prompt_closed({});
  817. m_dialog = nullptr;
  818. }
  819. void WebContentView::notify_server_did_request_set_prompt_text(Badge<WebContentClient>, DeprecatedString const& message)
  820. {
  821. if (m_dialog && is<QInputDialog>(*m_dialog))
  822. static_cast<QInputDialog&>(*m_dialog).setTextValue(qstring_from_ak_deprecated_string(message));
  823. }
  824. void WebContentView::notify_server_did_request_accept_dialog(Badge<WebContentClient>)
  825. {
  826. if (m_dialog)
  827. m_dialog->accept();
  828. }
  829. void WebContentView::notify_server_did_request_dismiss_dialog(Badge<WebContentClient>)
  830. {
  831. if (m_dialog)
  832. m_dialog->reject();
  833. }
  834. void WebContentView::get_source()
  835. {
  836. client().async_get_source();
  837. }
  838. void WebContentView::notify_server_did_get_source(AK::URL const& url, DeprecatedString const& source)
  839. {
  840. emit got_source(url, qstring_from_ak_deprecated_string(source));
  841. }
  842. void WebContentView::notify_server_did_get_dom_tree(DeprecatedString const& dom_tree)
  843. {
  844. if (on_get_dom_tree)
  845. on_get_dom_tree(dom_tree);
  846. if (m_inspector_widget)
  847. m_inspector_widget->set_dom_json(dom_tree);
  848. }
  849. 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)
  850. {
  851. if (on_get_dom_node_properties)
  852. on_get_dom_node_properties(node_id, specified_style, computed_style, custom_properties, node_box_sizing);
  853. }
  854. void WebContentView::notify_server_did_output_js_console_message(i32 message_index)
  855. {
  856. if (m_console_widget)
  857. m_console_widget->notify_about_new_console_message(message_index);
  858. }
  859. void WebContentView::notify_server_did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages)
  860. {
  861. if (m_console_widget)
  862. m_console_widget->handle_console_messages(start_index, message_types, messages);
  863. }
  864. void WebContentView::notify_server_did_change_favicon(Gfx::Bitmap const& bitmap)
  865. {
  866. auto qimage = QImage(bitmap.scanline_u8(0), bitmap.width(), bitmap.height(), QImage::Format_ARGB32);
  867. if (qimage.isNull())
  868. return;
  869. auto qpixmap = QPixmap::fromImage(qimage);
  870. if (qpixmap.isNull())
  871. return;
  872. emit favicon_changed(QIcon(qpixmap));
  873. }
  874. Vector<Web::Cookie::Cookie> WebContentView::notify_server_did_request_all_cookies(Badge<WebContentClient>, AK::URL const& url)
  875. {
  876. if (on_get_all_cookies)
  877. return on_get_all_cookies(url);
  878. return {};
  879. }
  880. Optional<Web::Cookie::Cookie> WebContentView::notify_server_did_request_named_cookie(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& name)
  881. {
  882. if (on_get_named_cookie)
  883. return on_get_named_cookie(url, name);
  884. return {};
  885. }
  886. DeprecatedString WebContentView::notify_server_did_request_cookie(Badge<WebContentClient>, AK::URL const& url, Web::Cookie::Source source)
  887. {
  888. if (on_get_cookie)
  889. return on_get_cookie(url, source);
  890. return {};
  891. }
  892. void WebContentView::notify_server_did_set_cookie(Badge<WebContentClient>, AK::URL const& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)
  893. {
  894. if (on_set_cookie)
  895. on_set_cookie(url, cookie, source);
  896. }
  897. void WebContentView::notify_server_did_update_cookie(Badge<WebContentClient>, Web::Cookie::Cookie const& cookie)
  898. {
  899. if (on_update_cookie)
  900. on_update_cookie(cookie);
  901. }
  902. void WebContentView::notify_server_did_update_resource_count(i32 count_waiting)
  903. {
  904. // FIXME
  905. (void)count_waiting;
  906. }
  907. void WebContentView::notify_server_did_request_restore_window()
  908. {
  909. emit restore_window();
  910. }
  911. Gfx::IntPoint WebContentView::notify_server_did_request_reposition_window(Gfx::IntPoint position)
  912. {
  913. return emit reposition_window(position);
  914. }
  915. Gfx::IntSize WebContentView::notify_server_did_request_resize_window(Gfx::IntSize size)
  916. {
  917. return emit resize_window(size);
  918. }
  919. Gfx::IntRect WebContentView::notify_server_did_request_maximize_window()
  920. {
  921. return emit maximize_window();
  922. }
  923. Gfx::IntRect WebContentView::notify_server_did_request_minimize_window()
  924. {
  925. return emit minimize_window();
  926. }
  927. Gfx::IntRect WebContentView::notify_server_did_request_fullscreen_window()
  928. {
  929. return emit fullscreen_window();
  930. }
  931. void WebContentView::notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32 request_id)
  932. {
  933. auto file = Core::Stream::File::open(path, Core::Stream::OpenMode::Read);
  934. if (file.is_error())
  935. client().async_handle_file_return(file.error().code(), {}, request_id);
  936. else
  937. client().async_handle_file_return(0, IPC::File(*file.value()), request_id);
  938. }
  939. void WebContentView::request_repaint()
  940. {
  941. // If this widget was instantiated but not yet added to a window,
  942. // it won't have a back bitmap yet, so we can just skip repaint requests.
  943. if (!m_client_state.back_bitmap.bitmap)
  944. return;
  945. // Don't request a repaint until pending paint requests have finished.
  946. if (m_client_state.back_bitmap.pending_paints) {
  947. m_client_state.got_repaint_requests_while_painting = true;
  948. return;
  949. }
  950. m_client_state.back_bitmap.pending_paints++;
  951. client().async_paint(m_client_state.back_bitmap.bitmap->rect().translated(horizontalScrollBar()->value(), verticalScrollBar()->value()), m_client_state.back_bitmap.id);
  952. }
  953. bool WebContentView::event(QEvent* event)
  954. {
  955. // NOTE: We have to implement event() manually as Qt's focus navigation mechanism
  956. // eats all the Tab key presses by default.
  957. if (event->type() == QEvent::KeyPress) {
  958. keyPressEvent(static_cast<QKeyEvent*>(event));
  959. return true;
  960. }
  961. if (event->type() == QEvent::KeyRelease) {
  962. keyReleaseEvent(static_cast<QKeyEvent*>(event));
  963. return true;
  964. }
  965. return QAbstractScrollArea::event(event);
  966. }
  967. void WebContentView::notify_server_did_finish_handling_input_event(bool event_was_accepted)
  968. {
  969. // FIXME: Currently Ladybird handles the keyboard shortcuts before passing the event to web content, so
  970. // we don't need to do anything here. But we'll need to once we start asking web content first.
  971. (void)event_was_accepted;
  972. }
  973. void WebContentView::notify_server_did_get_accessibility_tree(DeprecatedString const&)
  974. {
  975. dbgln("TODO: support accessibility tree in Ladybird");
  976. }