version.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //go:build windows && !native_webview2loader
  2. package webviewloader
  3. import (
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. )
  9. // UsingGoWebview2Loader is set to true when the go webview2loader is used.
  10. var UsingGoWebview2Loader bool
  11. // CompareBrowserVersions will compare the 2 given versions and return:
  12. //
  13. // -1 = v1 < v2
  14. // 0 = v1 == v2
  15. // 1 = v1 > v2
  16. func CompareBrowserVersions(v1 string, v2 string) (int, error) {
  17. v, err := parseVersion(v1)
  18. if err != nil {
  19. return 0, fmt.Errorf("v1 invalid: %w", err)
  20. }
  21. w, err := parseVersion(v2)
  22. if err != nil {
  23. return 0, fmt.Errorf("v2 invalid: %w", err)
  24. }
  25. return v.compare(w), nil
  26. }
  27. // GetAvailableCoreWebView2BrowserVersionString get the browser version info including channel name
  28. // if it is the WebView2 Runtime.
  29. // Channel names are Beta, Dev, and Canary.
  30. func GetAvailableCoreWebView2BrowserVersionString(browserExecutableFolder string) (string, error) {
  31. if browserExecutableFolder != "" {
  32. clientPath, err := findEmbeddedClientDll(browserExecutableFolder)
  33. if errors.Is(err, errNoClientDLLFound) {
  34. // WebView2 is not found
  35. return "", nil
  36. } else if err != nil {
  37. return "", err
  38. }
  39. return findEmbeddedBrowserVersion(clientPath)
  40. }
  41. _, version, err := findInstalledClientDll(false)
  42. if errors.Is(err, errNoClientDLLFound) {
  43. return "", nil
  44. } else if err != nil {
  45. return "", err
  46. }
  47. return version.String(), nil
  48. }
  49. type version struct {
  50. major int
  51. minor int
  52. patch int
  53. build int
  54. channel string
  55. }
  56. func (v version) String() string {
  57. vv := fmt.Sprintf("%d.%d.%d.%d", v.major, v.minor, v.patch, v.build)
  58. if v.channel != "" {
  59. vv += " " + v.channel
  60. }
  61. return vv
  62. }
  63. func (v version) compare(o version) int {
  64. if c := compareInt(v.major, o.major); c != 0 {
  65. return c
  66. }
  67. if c := compareInt(v.minor, o.minor); c != 0 {
  68. return c
  69. }
  70. if c := compareInt(v.patch, o.patch); c != 0 {
  71. return c
  72. }
  73. return compareInt(v.build, o.build)
  74. }
  75. func parseVersion(v string) (version, error) {
  76. var p version
  77. // Split away channel information...
  78. if i := strings.Index(v, " "); i > 0 {
  79. p.channel = v[i+1:]
  80. v = v[:i]
  81. }
  82. vv := strings.Split(v, ".")
  83. if len(vv) > 4 {
  84. return p, fmt.Errorf("too many version parts")
  85. }
  86. var err error
  87. vv, p.major, err = parseInt(vv)
  88. if err != nil {
  89. return p, fmt.Errorf("bad major version: %w", err)
  90. }
  91. vv, p.minor, err = parseInt(vv)
  92. if err != nil {
  93. return p, fmt.Errorf("bad minor version: %w", err)
  94. }
  95. vv, p.patch, err = parseInt(vv)
  96. if err != nil {
  97. return p, fmt.Errorf("bad patch version: %w", err)
  98. }
  99. _, p.build, err = parseInt(vv)
  100. if err != nil {
  101. return p, fmt.Errorf("bad build version: %w", err)
  102. }
  103. return p, nil
  104. }
  105. func parseInt(v []string) ([]string, int, error) {
  106. if len(v) == 0 {
  107. return nil, 0, nil
  108. }
  109. p, err := strconv.ParseInt(v[0], 10, 32)
  110. if err != nil {
  111. return nil, 0, err
  112. }
  113. return v[1:], int(p), nil
  114. }
  115. func compareInt(v1, v2 int) int {
  116. if v1 == v2 {
  117. return 0
  118. }
  119. if v1 < v2 {
  120. return -1
  121. } else {
  122. return +1
  123. }
  124. }