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