-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmanifest_windows.go
87 lines (72 loc) · 2.53 KB
/
manifest_windows.go
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
// manifest_windows.go - Install and Uninstall manifest file for Windows.
// Copyright (c) 2018 - 2020 Richard Huang <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package host
import (
"encoding/json"
"golang.org/x/sys/windows/registry"
"log"
"os"
"path/filepath"
)
// Install creates native-messaging manifest file on appropriate location and
// add an entry in windows registry. It will return error when it come across
// one.
//
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location
func (h *Host) Install() error {
manifest, _ := json.MarshalIndent(h, "", " ")
registryName := `Software\Google\Chrome\NativeMessagingHosts\` + h.AppName
targetName := filepath.Join(filepath.Dir(h.ExecName), h.AppName+".json")
if err := ioutilWriteFile(targetName, manifest, 0644); err != nil {
return err
}
// CreateKey creates a key named path under open key k. CreateKey returns the
// new key and a boolean flag that reports whether the key already existed.
key, _, err := registry.CreateKey(registry.CURRENT_USER, registryName, registry.SET_VALUE)
if err != nil {
return err
}
defer key.Close()
if err := key.SetStringValue("", targetName); err != nil {
return err
}
log.Printf(`Installed: HKCU\%s`, registryName)
return nil
}
// Uninstall removes entry from windows registry and removes native-messaging
// manifest file from installed location.
//
// See https://developer.chrome.com/extensions/nativeMessaging#native-messaging-host-location
func (h *Host) Uninstall() {
registryName := `Software\Google\Chrome\NativeMessagingHosts\` + h.AppName
targetName := filepath.Join(filepath.Dir(h.ExecName), h.AppName+".json")
key, err := registry.OpenKey(registry.CURRENT_USER, registryName, registry.SET_VALUE)
if err != nil {
// Unable to open windows registry.
log.Print(err)
}
defer key.Close()
if err := key.DeleteValue(""); err != nil {
// It might never have been installed.
log.Print(err)
}
if err := os.Remove(targetName); err != nil {
// It might never have been installed.
log.Print(err)
}
if err := os.Remove(h.ExecName); err != nil {
// It might be locked by current process.
log.Print(err)
}
if err := os.Remove(h.ExecName + ".chk"); err != nil {
// It might not exist.
log.Print(err)
}
log.Printf(`Uninstalled: HKCU\%s`, registryName)
// Exit gracefully.
runtimeGoexit()
}