Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native WebKit on macOS #713

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
167 changes: 87 additions & 80 deletions src/modes/thread_view/page_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,87 @@
using namespace boost::filesystem;

namespace Astroid {

extern "C" void PageClient_init_web_extensions (WebKitWebContext *,
gpointer);

class PageClient::impl {
public:
void init_web_extensions (PageClient & self, WebKitWebContext * context) {

/* add path to Astroid web extension */
# ifdef DEBUG
if (exists (path (Resource::get_exe_dir ()) / path("libtvextension.so"))) {

LOG (warn) << "pc: DEBUG build - found local extension. adding: " << Resource::get_exe_dir().c_str () << " to web extensions search path.";
webkit_web_context_set_web_extensions_directory (
context,
Resource::get_exe_dir ().c_str());

} else {

LOG (warn) << "pc: DEBUG build. no local extension found. adding installed path.";

path wke = path (PREFIX) / path ("lib/astroid/web-extensions");
LOG (info) << "pc: adding " << wke.c_str () << " to web extension search path.";

webkit_web_context_set_web_extensions_directory (
context,
wke.c_str());

}

# else
path wke = path (PREFIX) / path ("lib/astroid/web-extensions");
LOG (info) << "pc: adding " << wke.c_str () << " to web extension search path.";

webkit_web_context_set_web_extensions_directory (
context,
wke.c_str());

# endif

/* set up unix socket */
LOG (warn) << "pc: id: " << id;

self.socket_addr = ustring::compose ("%1/astroid.%2.%3.%4",
astroid->standard_paths ().socket_dir.c_str(),
getpid(),
id,
UstringUtils::random_alphanumeric (30));
refptr<Gio::UnixSocketAddress> addr;
if(Gio::UnixSocketAddress::abstract_names_supported ()) {
addr = Gio::UnixSocketAddress::create (self.socket_addr,
Gio::UNIX_SOCKET_ADDRESS_ABSTRACT);
} else {
addr = Gio::UnixSocketAddress::create (self.socket_addr,
Gio::UNIX_SOCKET_ADDRESS_PATH);
}

refptr<Gio::SocketAddress> eaddr;

LOG (debug) << "pc: socket: " << addr->get_path ();

mode_t p = umask (0077);
self.srv = Gio::SocketListener::create ();
self.srv->add_address (addr, Gio::SocketType::SOCKET_TYPE_STREAM,
Gio::SocketProtocol::SOCKET_PROTOCOL_DEFAULT,
eaddr);

/* listen */
self.srv->accept_async (sigc::mem_fun (self, &PageClient::extension_connect));
umask (p);

/* send socket address (TODO: include key) */
GVariant * gaddr = g_variant_new_string (addr->get_path ().c_str ());

webkit_web_context_set_web_extensions_initialization_user_data (
context,
gaddr);
}

}; // class PageClient::impl

int PageClient::id = 0;

PageClient::PageClient (ThreadView * t) {
Expand All @@ -52,24 +133,22 @@ namespace Astroid {
ATTACHMENT_ICON_WIDTH,
Gtk::ICON_LOOKUP_USE_BUILTIN );

extension_connect_id = g_signal_connect (thread_view->context,
"initialize-web-extensions",
G_CALLBACK (PageClient_init_web_extensions),
(gpointer) this);
extension_connect_id = thread_view->connect_page_client("initialize-web-extensions",
G_CALLBACK (PageClient_init_web_extensions),
(gpointer) this);
}

extern "C" void PageClient_init_web_extensions (
WebKitWebContext * context,
gpointer user_data) {

((PageClient *) user_data)->init_web_extensions (context);
((PageClient *) user_data)->pImpl->init_web_extensions ( * ((PageClient *) user_data), context);
}

PageClient::~PageClient () {
LOG (debug) << "pc: destruct";
g_signal_handler_disconnect (thread_view->context,
extension_connect_id);

thread_view->disconnect_page_client(extension_connect_id);

LOG (debug) << "pc: closing";

istream.clear ();
Expand All @@ -79,78 +158,6 @@ namespace Astroid {
srv->close ();
}

void PageClient::init_web_extensions (WebKitWebContext * context) {

/* add path to Astroid web extension */
# ifdef DEBUG
if (exists (path (Resource::get_exe_dir ()) / path("libtvextension.so"))) {

LOG (warn) << "pc: DEBUG build - found local extension. adding: " << Resource::get_exe_dir().c_str () << " to web extensions search path.";
webkit_web_context_set_web_extensions_directory (
context,
Resource::get_exe_dir ().c_str());

} else {

LOG (warn) << "pc: DEBUG build. no local extension found. adding installed path.";

path wke = path (PREFIX) / path ("lib/astroid/web-extensions");
LOG (info) << "pc: adding " << wke.c_str () << " to web extension search path.";

webkit_web_context_set_web_extensions_directory (
context,
wke.c_str());

}

# else
path wke = path (PREFIX) / path ("lib/astroid/web-extensions");
LOG (info) << "pc: adding " << wke.c_str () << " to web extension search path.";

webkit_web_context_set_web_extensions_directory (
context,
wke.c_str());

# endif

/* set up unix socket */
LOG (warn) << "pc: id: " << id;

socket_addr = ustring::compose ("%1/astroid.%2.%3.%4",
astroid->standard_paths ().socket_dir.c_str(),
getpid(),
id,
UstringUtils::random_alphanumeric (30));
refptr<Gio::UnixSocketAddress> addr;
if(Gio::UnixSocketAddress::abstract_names_supported ()) {
addr = Gio::UnixSocketAddress::create (socket_addr,
Gio::UNIX_SOCKET_ADDRESS_ABSTRACT);
} else {
addr = Gio::UnixSocketAddress::create (socket_addr,
Gio::UNIX_SOCKET_ADDRESS_PATH);
}

refptr<Gio::SocketAddress> eaddr;

LOG (debug) << "pc: socket: " << addr->get_path ();

mode_t p = umask (0077);
srv = Gio::SocketListener::create ();
srv->add_address (addr, Gio::SocketType::SOCKET_TYPE_STREAM,
Gio::SocketProtocol::SOCKET_PROTOCOL_DEFAULT,
eaddr);

/* listen */
srv->accept_async (sigc::mem_fun (this, &PageClient::extension_connect));
umask (p);

/* send socket address (TODO: include key) */
GVariant * gaddr = g_variant_new_string (addr->get_path ().c_str ());

webkit_web_context_set_web_extensions_initialization_user_data (
context,
gaddr);
}

void PageClient::extension_connect (refptr<Gio::AsyncResult> &res) {
LOG (warn) << "pc: got extension connect";
Expand Down
11 changes: 5 additions & 6 deletions src/modes/thread_view/page_client.hh
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
# pragma once

# include <webkit2/webkit2.h>
# include <glib.h>
# include <glibmm.h>
# include <giomm.h>
# include <gtkmm.h>
# include <thread>
# include <atomic>
# include <experimental/propagate_const>

# include "astroid.hh"
# include "thread_view.hh"

# include "messages.pb.h"

namespace Astroid {
extern "C" void PageClient_init_web_extensions (
WebKitWebContext *,
gpointer);

class PageClient : public sigc::trackable {

public:
class impl;
std::experimental::propagate_const<std::unique_ptr<impl>> pImpl;

PageClient (ThreadView *);
~PageClient ();

ThreadView * thread_view;

void init_web_extensions (WebKitWebContext * context);

/* ThreadView interface */
void load ();
void add_message (refptr<Message> m);
Expand Down
Loading