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

Autoinstall extension in chrome #6442

Merged
merged 25 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e7de544
Initial extention autoinstall for chrome/chromium in linux
LyzardKing Apr 16, 2020
198819c
Add postinst/postrm chrome extension
LyzardKing Apr 28, 2020
4bbaba3
Add json files to macos build
LyzardKing May 7, 2020
384826a
Add macos resource files and link in the pkg release
LyzardKing May 8, 2020
02878cc
Use chrome store url to install extension
LyzardKing May 19, 2020
386e2d9
set chromium auto-install to world readable
LyzardKing May 20, 2020
fc0c8b6
Add permissions to macos autoinstall extension
LyzardKing May 20, 2020
ac8fd4f
Add inst/rm for chromium extension on rpm
LyzardKing May 20, 2020
53fda44
Rename postinstall to postinstall.test
tobiasdiez May 22, 2020
da01d55
Merge remote-tracking branch 'upstream/master' into autoinstall_ext
Siedlerchr May 26, 2020
6025b72
try again with postinstall
Siedlerchr May 26, 2020
15b8161
Add exec permission to mac postinstall
LyzardKing May 28, 2020
9f560b7
Fix shell indentations
LyzardKing May 28, 2020
1a54692
Fix mac postinst to use -d option
LyzardKing Jun 3, 2020
e9c0d55
Add custom host file for macos
LyzardKing Jun 15, 2020
9f1bc8b
Add path to macos native messaging host files
LyzardKing Jun 15, 2020
06f487c
Remove binary mode in python script
LyzardKing Jun 17, 2020
c7df1df
create the dir before copying file
LyzardKing Jun 17, 2020
ba6ae48
Add mac native-messaging json to postinst
LyzardKing Jun 17, 2020
a7eaf2d
Try mkdir/cp to add files to macos pkg
LyzardKing Jul 27, 2020
1b17049
Fix path spaces
LyzardKing Jul 28, 2020
ecec705
Add -p option to mkdir parents
LyzardKing Jul 28, 2020
0cb4ed2
Merge branch 'master' into autoinstall_ext
Siedlerchr Aug 1, 2020
378008a
postinstall macos force /bin/sh
LyzardKing Aug 4, 2020
42bef18
revert to using install on macos
LyzardKing Aug 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,17 @@ if (OperatingSystem.current().isLinux()) {
}
}

if (OperatingSystem.current().isMacOsX()) {
tasks.jpackageImage.doLast {
copy {
from("${projectDir}/buildres/mac") {
include "native-messaging-host/**", "jabrefHost.py"
}
into "$buildDir/distribution/JabRef.app/Contents/Resources"
}
}
}

// Delete unnecessary folder in application image
// TODO: Remove this workaround as soon as https://github.com/beryx/badass-jlink-plugin/issues/51 is fixed
task deleteModsFolder(type: Delete) {
Expand Down
14 changes: 11 additions & 3 deletions buildres/linux/jabref.spec
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,28 @@ cp -r %{_sourcedir}APPLICATION_DIRECTORY/* %{buildroot}APPLICATION_DIRECTORY
%(echo APPLICATION_DIRECTORY | sed -e "s|\(^/[^/]\{1,\}\).*$|\1|")

%post
# Install the native-messaging host script for firefox/chrome/chromium
install -D -m0755 /opt/jabref/lib/native-messaging-host/firefox/org.jabref.jabref.json /usr/lib/mozilla/native-messaging-hosts/org.jabref.jabref.json
install -D -m0755 /opt/jabref/lib/native-messaging-host/chromium/org.jabref.jabref.json /etc/chromium/native-messaging-hosts/org.jabref.jabref.json
install -D -m0755 /opt/jabref/lib/native-messaging-host/chromium/org.jabref.jabref.json /etc/opt/chrome/native-messaging-hosts/org.jabref.jabref.json
# Trigger an auto-install of the browser addon for chrome/chromium browsers
install -D -m0644 /opt/jabref/lib/native-messaging-host/chromium/bifehkofibaamoeaopjglfkddgkijdlh.json /opt/google/chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
install -D -m0644 /opt/jabref/lib/native-messaging-host/chromium/bifehkofibaamoeaopjglfkddgkijdlh.json /usr/share/google-chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
DESKTOP_COMMANDS_INSTALL

%preun
# Remove the native-messaging hosts script only if relative to the deb package
for NATIVE_MESSAGING_JSON in "/usr/lib/mozilla/native-messaging-hosts/org.jabref.jabref.json"\
"/etc/chromium/native-messaging-hosts/org.jabref.jabref.json"\
"/etc/opt/chrome/native-messaging-hosts/org.jabref.jabref.json"; do
"/etc/chromium/native-messaging-hosts/org.jabref.jabref.json"\
"/etc/opt/chrome/native-messaging-hosts/org.jabref.jabref.json"; do
if [ -e $NATIVE_MESSAGING_JSON ] && grep --quiet '"path": "/opt' $NATIVE_MESSAGING_JSON; then
rm $NATIVE_MESSAGING_JSON
fi
done
# Remove the auto-install triggers of the browser addon for chrom/chromium
rm /opt/google/chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
rm /usr/share/google-chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
UTILITY_SCRIPTS
DESKTOP_COMMANDS_UNINSTALL

%clean
%clean
13 changes: 9 additions & 4 deletions buildres/linux/jabrefHost.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
# than text, mode.

import json
import sys
import struct
import subprocess
import shlex
import logging
import platform
import shlex
import shutil
import struct
import subprocess
import sys
from pathlib import Path

# We assume that this python script is located in "jabref/lib" while the executable is "jabref/bin/JabRef"
Expand All @@ -19,6 +20,10 @@
if not JABREF_PATH.exists():
JABREF_PATH = shutil.which("jabref")

if not JABREF_PATH.exists():
logging.error("Could not determine JABREF_PATH")
sys.exit(-1)

logging_dir = Path.home() / ".mozilla/native-messaging-hosts/"
if not logging_dir.exists():
logging_dir.mkdir(parents=True)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"external_update_url": "https://clients2.google.com/service/update2/crx"
}
4 changes: 4 additions & 0 deletions buildres/linux/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ set -e

case "$1" in
configure)
# Install the native-messaging host script for firefox/chrome/chromium
install -D -m0755 /opt/jabref/lib/native-messaging-host/firefox/org.jabref.jabref.json /usr/lib/mozilla/native-messaging-hosts/org.jabref.jabref.json
install -D -m0755 /opt/jabref/lib/native-messaging-host/chromium/org.jabref.jabref.json /etc/chromium/native-messaging-hosts/org.jabref.jabref.json
install -D -m0755 /opt/jabref/lib/native-messaging-host/chromium/org.jabref.jabref.json /etc/opt/chrome/native-messaging-hosts/org.jabref.jabref.json
# Trigger an auto-install of the browser addon for chrome/chromium browsers
install -D -m0644 /opt/jabref/lib/native-messaging-host/chromium/bifehkofibaamoeaopjglfkddgkijdlh.json /opt/google/chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
install -D -m0644 /opt/jabref/lib/native-messaging-host/chromium/bifehkofibaamoeaopjglfkddgkijdlh.json /usr/share/google-chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
DESKTOP_COMMANDS_INSTALL
;;

Expand Down
14 changes: 9 additions & 5 deletions buildres/linux/postrm
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ set -e

case "$1" in
purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
for NATIVE_MESSAGING_JSON in "/usr/lib/mozilla/native-messaging-hosts/org.jabref.jabref.json"\
# Remove the native-messaging hosts script only if relative to the deb package
for NATIVE_MESSAGING_JSON in "/usr/lib/mozilla/native-messaging-hosts/org.jabref.jabref.json"\
"/etc/chromium/native-messaging-hosts/org.jabref.jabref.json"\
"/etc/opt/chrome/native-messaging-hosts/org.jabref.jabref.json"; do
if [ -e $NATIVE_MESSAGING_JSON ] && grep --quiet '"path": "/opt' $NATIVE_MESSAGING_JSON; then
rm $NATIVE_MESSAGING_JSON
fi
done
if [ -e $NATIVE_MESSAGING_JSON ] && grep --quiet '"path": "/opt' $NATIVE_MESSAGING_JSON; then
rm $NATIVE_MESSAGING_JSON
fi
done
# Remove the auto-install triggers of the browser addon for chrom/chromium
rm /opt/google/chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
rm /usr/share/google-chrome/extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
;;

*)
Expand Down
96 changes: 96 additions & 0 deletions buildres/mac/jabrefHost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/python3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not possible to reuse the linux version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is basically the same file. The issue is that with the --resource-dir option in jpackage I cannot add a single file from another directory.
So I copied it and changed the path settings.


import json
import logging
import platform
import shlex
import shutil
import struct
import subprocess
import sys
from pathlib import Path

# We assume that this python script is located in "jabref/lib" while the executable is "jabref/bin/JabRef"
# Note that the `which` command does not work as intended on MacOs, so the path must be hardcoded.
script_dir = Path(__file__).resolve().parent.parent
JABREF_PATH = script_dir / "bin/JabRef"
if not JABREF_PATH.exists():
JABREF_PATH = Path( "/Applications/JabRef.app/Contents/MacOS/JabRef")

if not JABREF_PATH.exists():
logging.error("Could not determine JABREF_PATH")
sys.exit(-1)

logging_dir = Path.home() / ".mozilla/native-messaging-hosts/"
if not logging_dir.exists():
logging_dir.mkdir(parents=True)
logging.basicConfig(filename=str(logging_dir / "jabref_browser_extension.log"))

# Read a message from stdin and decode it.
def get_message():
raw_length = sys.stdin.buffer.read(4)
if not raw_length:
logging.error("Raw_length null")
sys.exit(0)
message_length = struct.unpack("=I", raw_length)[0]
logging.info("Got length: {} bytes to be read".format(message_length))
message = sys.stdin.buffer.read(message_length).decode("utf-8")
logging.info("Got message of {} chars".format(len(message)))
data = json.loads(message)
logging.info("Successfully retrieved JSON")
return data


# Encode a message for transmission, given its content.
def encode_message(message_content):
encoded_content = json.dumps(message_content).encode("utf-8")
encoded_length = struct.pack("=I", len(encoded_content))
return {
"length": encoded_length,
"content": struct.pack(str(len(encoded_content)) + "s", encoded_content),
}


# Send an encoded message to stdout.
def send_message(message):
encoded_message = encode_message(message)
sys.stdout.buffer.write(encoded_message["length"])
sys.stdout.buffer.write(encoded_message["content"])
sys.stdout.buffer.flush()


def add_jabref_entry(data):
"""Send string via cli as literal to preserve special characters"""
cmd = [str(JABREF_PATH), "--importBibtex", r"{}".format(data)]
logging.info("Try to execute command {}".format(cmd))
try:
response = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
logging.error("Failed to call JabRef: {} {}".format(exc.returncode, exc.output))
else:
logging.info("Called JabRef and got: {}".format(response))
return response


logging.info("Starting JabRef backend")

try:
message = get_message()
except Exception as e:
message = str(e)
logging.info(str(message))

if "status" in message and message["status"] == "validate":
cmd = [JABREF_PATH, "--version"]
try:
response = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
logging.error("Failed to call JabRef: {} {}".format(exc.returncode, exc.output))
send_message({"message": "jarNotFound", "path": JABREF_PATH})
else:
logging.info("Response: {}".format(response))
send_message({"message": "jarFound"})
else:
entry = message["text"]
output = add_jabref_entry(entry)
send_message({"message": "ok", "output": str(output)})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"external_update_url": "https://clients2.google.com/service/update2/crx"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "org.jabref.jabref",
"description": "JabRef",
"path": "/Applications/JabRef.app/Contents/Resources/jabrefHost.py",
"type": "stdio",
"allowed_origins": [
"chrome-extension://bifehkofibaamoeaopjglfkddgkijdlh/"
]
}
10 changes: 10 additions & 0 deletions buildres/mac/native-messaging-host/firefox/org.jabref.jabref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "org.jabref.jabref",
"description": "JabRef",
"path": "/Applications/JabRef.app/Contents/Resources/jabrefHost.py",
"type": "stdio",
"allowed_extensions": [
"browserextension@jabref.org",
"@jabfox"
]
}
18 changes: 18 additions & 0 deletions buildres/mac/postinstall
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env sh

chown root:wheel "INSTALL_LOCATION"
chmod a+rX "INSTALL_LOCATION"
chmod +r "APP_LOCATION/"*.jar
# Trigger an auto-install of the browser addon for chrome/chromium browsers
# First create the necessary path, then copy the autoinstall file.
install -d /Library/Application\ Support/Google/Chrome/External\ Extensions/
install -m0644 /Applications/JabRef.app/Contents/Resources/native-messaging-host/chromium/bifehkofibaamoeaopjglfkddgkijdlh.json /Library/Application\ Support/Google/Chrome/External\ Extensions/bifehkofibaamoeaopjglfkddgkijdlh.json
# Install the native-messaging host script for firefox/chrome/chromium
install -d /Library/Application Support/Mozilla/NativeMessagingHosts/
install -m0755 /Applications/JabRef.app/Contents/Resources/native-messaging-host/firefox/org.jabref.jabref.json /Library/Application Support/Mozilla/NativeMessagingHosts/org.jabref.jabref.json
install -d /Library/Application Support/Chromium/NativeMessagingHosts/
install -m0755 /Applications/JabRef.app/Contents/Resources/native-messaging-host/chromium/org.jabref.jabref.json /Library/Application Support/Chromium/NativeMessagingHosts/org.jabref.jabref.json
install -d /Library/Google/Chrome/NativeMessagingHosts/
install -m0755 /Applications/JabRef.app/Contents/Resources/native-messaging-host/chromium/org.jabref.jabref.json /Library/Google/Chrome/NativeMessagingHosts/org.jabref.jabref.json

exit 0