register_local_nm_hosts.sh revision effb81e5f8246d0db0270817048dc992db66e9fb
1#!/bin/sh
2# Copyright 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# Script that can be used to register native messaging hosts in the output
7# directory.
8
9set -e
10
11SRC_DIR="$(readlink -f "$(dirname "$0")/../..")"
12ME2ME_HOST_NAME="com.google.chrome.remote_desktop"
13IT2ME_HOST_NAME="com.google.chrome.remote_assistance"
14
15install_manifest() {
16  local manifest_template="$1"
17  local host_path="$2"
18  local host_path_var_name="$3"
19  local target_dir="$4"
20
21  local template_name="$(basename ${manifest_template})"
22  local manifest_name="${template_name%.*}"
23  local target_manifest="${target_dir}/${manifest_name}"
24
25  echo Registering ${host_path} in ${target_manifest}
26  mkdir -p "${target_dir}"
27  sed -e "s#{{ ${host_path_var_name} }}#${host_path}#g" \
28    < "$manifest_template" > "$target_manifest"
29}
30
31register_hosts() {
32  local build_dir="$1"
33  local chrome_data_dir="$2"
34
35  install_manifest \
36     "${SRC_DIR}/remoting/host/setup/${ME2ME_HOST_NAME}.json.jinja2" \
37     "${build_dir}/native_messaging_host" \
38     ME2ME_HOST_PATH "${chrome_data_dir}"
39
40  install_manifest \
41     "${SRC_DIR}/remoting/host/it2me/${IT2ME_HOST_NAME}.json.jinja2" \
42     "${build_dir}/remote_assistance_host" \
43     IT2ME_HOST_PATH "${chrome_data_dir}"
44}
45
46register_hosts_for_all_channels() {
47  local build_dir="$1"
48
49  if [ $(uname -s) == "Darwin" ]; then
50    register_hosts "${build_dir}" \
51        "${HOME}/Library/Application Support/Google/Chrome/NativeMessagingHosts"
52    register_hosts "${build_dir}" \
53        "${HOME}/Library/Application Support/Chromium/NativeMessagingHosts"
54  else
55    register_hosts "${build_dir}" \
56        "${HOME}/.config/google-chrome/NativeMessagingHosts"
57    register_hosts "${build_dir}" \
58        "${HOME}/.config/google-chrome-beta/NativeMessagingHosts"
59    register_hosts "${build_dir}" \
60        "${HOME}/.config/google-chrome-unstable/NativeMessagingHosts"
61    register_hosts "${build_dir}" \
62        "${HOME}/.config/chromium/NativeMessagingHosts"
63  fi
64}
65
66unregister_hosts() {
67  local chrome_data_dir="$1"
68
69  rm -f "${chrome_data_dir}/${ME2ME_HOST_NAME}.json"
70  rm -f "${chrome_data_dir}/${IT2ME_HOST_NAME}.json"
71}
72
73unregister_hosts_for_all_channels() {
74  if [ $(uname -s) == "Darwin" ]; then
75    unregister_hosts \
76        "${HOME}/Library/Application Support/Google/Chrome/NativeMessagingHosts"
77    unregister_hosts \
78        "${HOME}/Library/Application Support/Chromium/NativeMessagingHosts"
79  else
80    unregister_hosts "${HOME}/.config/google-chrome/NativeMessagingHosts"
81    unregister_hosts "${HOME}/.config/google-chrome-beta/NativeMessagingHosts"
82    unregister_hosts \
83        "${HOME}/.config/google-chrome-unstable/NativeMessagingHosts"
84    unregister_hosts "${HOME}/.config/chromium/NativeMessagingHosts"
85  fi
86}
87
88print_usage() {
89  echo "Usage: $0 [-r|-u]" >&2
90  echo "   -r   Register Release build instead of Debug" >&2
91  echo "   -u   Unregister" >&2
92}
93
94build_dir="Debug"
95
96if [[ $# -gt 1 ]]; then
97  print_usage
98elif [[ $# -eq 1 ]]; then
99  case "$1" in
100    "-r")
101      build_dir="Release"
102      ;;
103    "-u")
104      unregister_hosts_for_all_channels
105      exit 0
106      ;;
107    *)
108      print_usage
109      exit 1
110      ;;
111  esac
112fi
113
114register_hosts_for_all_channels "${SRC_DIR}/out/${build_dir}"
115