WebContentView.cpp 33 KB

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