WebContentView.cpp 35 KB

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