summaryrefslogtreecommitdiffstats
path: root/remoting/tools/register_local_nm_hosts.sh
blob: aba87ef04b9578b62050e4960c896b7ae4647774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/sh
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Script that can be used to register native messaging hosts in the output
# directory.

set -e

SRC_DIR="$(readlink -f "$(dirname "$0")/../..")"
ME2ME_HOST_NAME="com.google.chrome.remote_desktop"
IT2ME_HOST_NAME="com.google.chrome.remote_assistance"

install_manifest() {
  local manifest_template="$1"
  local host_path="$2"
  local host_path_var_name="$3"
  local target_dir="$4"

  local template_name="$(basename ${manifest_template})"
  local manifest_name="${template_name%.*}"
  local target_manifest="${target_dir}/${manifest_name}"

  echo Registering ${host_path} in ${target_manifest}
  mkdir -p "${target_dir}"
  sed -e "s#{{ ${host_path_var_name} }}#${host_path}#g" \
    < "$manifest_template" > "$target_manifest"
}

register_hosts() {
  local build_dir="$1"
  local chrome_data_dir="$2"

  install_manifest \
     "${SRC_DIR}/remoting/host/setup/${ME2ME_HOST_NAME}.json.jinja2" \
     "${build_dir}/native_messaging_host" \
     ME2ME_HOST_PATH "${chrome_data_dir}"

  install_manifest \
     "${SRC_DIR}/remoting/host/it2me/${IT2ME_HOST_NAME}.json.jinja2" \
     "${build_dir}/remote_assistance_host" \
     IT2ME_HOST_PATH "${chrome_data_dir}"
}

register_hosts_for_all_channels() {
  local build_dir="$1"

  if [ -n "$CHROME_USER_DATA_DIR" ]; then
    register_hosts "${build_dir}" \
        "${CHROME_USER_DATA_DIR}/NativeMessagingHosts"
  elif [ $(uname -s) == "Darwin" ]; then
    register_hosts "${build_dir}" \
        "${HOME}/Library/Application Support/Google/Chrome/NativeMessagingHosts"
    register_hosts "${build_dir}" \
        "${HOME}/Library/Application Support/Chromium/NativeMessagingHosts"
  else
    register_hosts "${build_dir}" \
        "${HOME}/.config/google-chrome/NativeMessagingHosts"
    register_hosts "${build_dir}" \
        "${HOME}/.config/google-chrome-beta/NativeMessagingHosts"
    register_hosts "${build_dir}" \
        "${HOME}/.config/google-chrome-unstable/NativeMessagingHosts"
    register_hosts "${build_dir}" \
        "${HOME}/.config/chromium/NativeMessagingHosts"
  fi
}

unregister_hosts() {
  local chrome_data_dir="$1"

  rm -f "${chrome_data_dir}/${ME2ME_HOST_NAME}.json"
  rm -f "${chrome_data_dir}/${IT2ME_HOST_NAME}.json"
}

unregister_hosts_for_all_channels() {
  if [ -n "$CHROME_USER_DATA_DIR" ]; then
    unregister_hosts \
        "${CHROME_USER_DATA_DIR}/NativeMessagingHosts"
  elif [ $(uname -s) == "Darwin" ]; then
    unregister_hosts \
        "${HOME}/Library/Application Support/Google/Chrome/NativeMessagingHosts"
    unregister_hosts \
        "${HOME}/Library/Application Support/Chromium/NativeMessagingHosts"
  else
    unregister_hosts "${HOME}/.config/google-chrome/NativeMessagingHosts"
    unregister_hosts "${HOME}/.config/google-chrome-beta/NativeMessagingHosts"
    unregister_hosts \
        "${HOME}/.config/google-chrome-unstable/NativeMessagingHosts"
    unregister_hosts "${HOME}/.config/chromium/NativeMessagingHosts"
  fi
}

print_usage() {
  echo "Usage: $0 [-r|-u]" >&2
  echo "   -r   Register Release build instead of Debug" >&2
  echo "   -u   Unregister" >&2
}

build_dir="Debug"

if [[ $# -gt 1 ]]; then
  print_usage
elif [[ $# -eq 1 ]]; then
  case "$1" in
    "-r")
      build_dir="Release"
      ;;
    "-u")
      unregister_hosts_for_all_channels
      exit 0
      ;;
    *)
      print_usage
      exit 1
      ;;
  esac
fi

register_hosts_for_all_channels "${SRC_DIR}/out/${build_dir}"