浏览代码

LibWeb+WebContent: Hook document.cookie to the backend cookie storage

Timothy Flynn 4 年之前
父节点
当前提交
1ef48d50ff

+ 6 - 4
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -825,15 +825,17 @@ void Document::completely_finish_loading()
     dispatch_event(DOM::Event::create(HTML::EventNames::load));
 }
 
-String Document::cookie() const
+String Document::cookie()
 {
-    // FIXME: Support cookies!
+    if (auto* page = this->page())
+        return page->client().page_did_request_cookie(m_url);
     return {};
 }
 
-void Document::set_cookie(String)
+void Document::set_cookie(String cookie)
 {
-    // FIXME: Support cookies!
+    if (auto* page = this->page())
+        page->client().page_did_set_cookie(m_url, cookie);
 }
 
 }

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Document.h

@@ -73,7 +73,7 @@ public:
 
     virtual ~Document() override;
 
-    String cookie() const;
+    String cookie();
     void set_cookie(String);
 
     bool should_invalidate_styles_on_attribute_changes() const { return m_should_invalidate_styles_on_attribute_changes; }

+ 13 - 0
Userland/Libraries/LibWeb/InProcessWebView.cpp

@@ -433,4 +433,17 @@ String InProcessWebView::page_did_request_prompt(const String& message, const St
     return {};
 }
 
+String InProcessWebView::page_did_request_cookie(const URL& url)
+{
+    if (on_get_cookie)
+        return on_get_cookie(url);
+    return {};
+}
+
+void InProcessWebView::page_did_set_cookie(const URL& url, const String& cookie)
+{
+    if (on_set_cookie)
+        on_set_cookie(url, cookie);
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/InProcessWebView.h

@@ -111,6 +111,8 @@ private:
     virtual void page_did_request_alert(const String&) override;
     virtual bool page_did_request_confirm(const String&) override;
     virtual String page_did_request_prompt(const String&, const String&) override;
+    virtual String page_did_request_cookie(const URL&) override;
+    virtual void page_did_set_cookie(const URL&, const String&) override;
 
     void layout_and_sync_size();
 

+ 13 - 0
Userland/Libraries/LibWeb/OutOfProcessWebView.cpp

@@ -365,6 +365,19 @@ void OutOfProcessWebView::notify_server_did_change_favicon(const Gfx::Bitmap& fa
         on_favicon_change(favicon);
 }
 
+String OutOfProcessWebView::notify_server_did_request_cookie(Badge<WebContentClient>, const URL& url)
+{
+    if (on_get_cookie)
+        return on_get_cookie(url);
+    return {};
+}
+
+void OutOfProcessWebView::notify_server_did_set_cookie(Badge<WebContentClient>, const URL& url, const String& cookie)
+{
+    if (on_set_cookie)
+        on_set_cookie(url, cookie);
+}
+
 void OutOfProcessWebView::did_scroll()
 {
     client().post_message(Messages::WebContentServer::SetViewportRect(visible_content_rect()));

+ 2 - 0
Userland/Libraries/LibWeb/OutOfProcessWebView.h

@@ -79,6 +79,8 @@ public:
     void notify_server_did_get_source(const URL& url, const String& source);
     void notify_server_did_js_console_output(const String& method, const String& line);
     void notify_server_did_change_favicon(const Gfx::Bitmap& favicon);
+    String notify_server_did_request_cookie(Badge<WebContentClient>, const URL& url);
+    void notify_server_did_set_cookie(Badge<WebContentClient>, const URL& url, const String& cookie);
 
 private:
     OutOfProcessWebView();

+ 2 - 0
Userland/Libraries/LibWeb/Page/Page.h

@@ -111,6 +111,8 @@ public:
     virtual void page_did_request_alert(const String&) { }
     virtual bool page_did_request_confirm(const String&) { return false; }
     virtual String page_did_request_prompt(const String&, const String&) { return {}; }
+    virtual String page_did_request_cookie(const URL&) { return {}; }
+    virtual void page_did_set_cookie(const URL&, const String&) { }
 };
 
 }

+ 11 - 0
Userland/Libraries/LibWeb/WebContentClient.cpp

@@ -197,4 +197,15 @@ void WebContentClient::handle(const Messages::WebContentClient::DidChangeFavicon
     m_view.notify_server_did_change_favicon(*message.favicon().bitmap());
 }
 
+OwnPtr<Messages::WebContentClient::DidRequestCookieResponse> WebContentClient::handle(const Messages::WebContentClient::DidRequestCookie& message)
+{
+    auto result = m_view.notify_server_did_request_cookie({}, message.url());
+    return make<Messages::WebContentClient::DidRequestCookieResponse>(result);
+}
+
+void WebContentClient::handle(const Messages::WebContentClient::DidSetCookie& message)
+{
+    m_view.notify_server_did_set_cookie({}, message.url(), message.cookie());
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/WebContentClient.h

@@ -75,6 +75,8 @@ private:
     virtual OwnPtr<Messages::WebContentClient::DidRequestAlertResponse> handle(const Messages::WebContentClient::DidRequestAlert&) override;
     virtual OwnPtr<Messages::WebContentClient::DidRequestConfirmResponse> handle(const Messages::WebContentClient::DidRequestConfirm&) override;
     virtual OwnPtr<Messages::WebContentClient::DidRequestPromptResponse> handle(const Messages::WebContentClient::DidRequestPrompt&) override;
+    virtual OwnPtr<Messages::WebContentClient::DidRequestCookieResponse> handle(const Messages::WebContentClient::DidRequestCookie&) override;
+    virtual void handle(const Messages::WebContentClient::DidSetCookie&) override;
 
     OutOfProcessWebView& m_view;
 };

+ 10 - 0
Userland/Services/WebContent/PageHost.cpp

@@ -208,4 +208,14 @@ void PageHost::page_did_request_image_context_menu(const Gfx::IntPoint& content_
     m_client.post_message(Messages::WebContentClient::DidRequestImageContextMenu(content_position, url, target, modifiers, bitmap->to_shareable_bitmap()));
 }
 
+String PageHost::page_did_request_cookie(const URL& url)
+{
+    return m_client.send_sync<Messages::WebContentClient::DidRequestCookie>(url)->cookie();
+}
+
+void PageHost::page_did_set_cookie(const URL& url, const String& cookie)
+{
+    m_client.post_message(Messages::WebContentClient::DidSetCookie(url, cookie));
+}
+
 }

+ 2 - 0
Userland/Services/WebContent/PageHost.h

@@ -79,6 +79,8 @@ private:
     virtual String page_did_request_prompt(const String&, const String&) override;
     virtual void page_did_change_favicon(const Gfx::Bitmap&) override;
     virtual void page_did_request_image_context_menu(const Gfx::IntPoint&, const URL&, const String& target, unsigned modifiers, const Gfx::Bitmap*) override;
+    virtual String page_did_request_cookie(const URL&) override;
+    virtual void page_did_set_cookie(const URL&, const String&) override;
 
     explicit PageHost(ClientConnection&);
 

+ 2 - 0
Userland/Services/WebContent/WebContentClient.ipc

@@ -25,4 +25,6 @@ endpoint WebContentClient = 90
     DidGetSource(URL url, String source) =|
     DidJSConsoleOutput(String method, String line) =|
     DidChangeFavicon(Gfx::ShareableBitmap favicon) =|
+    DidRequestCookie(URL url) => (String cookie)
+    DidSetCookie(URL url, String cookie) =|
 }