From 18f517d8a6245135d0109d5f040114f09c84224d Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 25 Sep 2023 03:43:57 -0700 Subject: [PATCH] test: vendor node-is-valid-window (#39965) --- spec/api-browser-window-spec.ts | 4 +- spec/is-valid-window/.gitignore | 7 +++ spec/is-valid-window/README.md | 8 +++ spec/is-valid-window/binding.gyp | 41 +++++++++++++++ spec/is-valid-window/lib/is-valid-window.js | 1 + spec/is-valid-window/package.json | 9 ++++ spec/is-valid-window/src/impl.h | 12 +++++ spec/is-valid-window/src/impl_darwin.mm | 14 ++++++ spec/is-valid-window/src/impl_posix.cc | 9 ++++ spec/is-valid-window/src/impl_win.cc | 14 ++++++ spec/is-valid-window/src/main.cc | 56 +++++++++++++++++++++ spec/package.json | 2 +- spec/yarn.lock | 18 +++---- 13 files changed, 181 insertions(+), 14 deletions(-) create mode 100644 spec/is-valid-window/.gitignore create mode 100644 spec/is-valid-window/README.md create mode 100644 spec/is-valid-window/binding.gyp create mode 100644 spec/is-valid-window/lib/is-valid-window.js create mode 100644 spec/is-valid-window/package.json create mode 100644 spec/is-valid-window/src/impl.h create mode 100644 spec/is-valid-window/src/impl_darwin.mm create mode 100644 spec/is-valid-window/src/impl_posix.cc create mode 100644 spec/is-valid-window/src/impl_win.cc create mode 100644 spec/is-valid-window/src/main.cc diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts index 0c11ccf20400..d194e7c3854e 100644 --- a/spec/api-browser-window-spec.ts +++ b/spec/api-browser-window-spec.ts @@ -5891,9 +5891,7 @@ describe('BrowserWindow module', () => { afterEach(closeAllWindows); it('returns valid handle', () => { const w = new BrowserWindow({ show: false }); - // The module's source code is hosted at - // https://github.com/electron/node-is-valid-window - const isValidWindow = require('is-valid-window'); + const isValidWindow = require('@electron-ci/is-valid-window'); expect(isValidWindow(w.getNativeWindowHandle())).to.be.true('is valid window'); }); }); diff --git a/spec/is-valid-window/.gitignore b/spec/is-valid-window/.gitignore new file mode 100644 index 000000000000..41912cdec36b --- /dev/null +++ b/spec/is-valid-window/.gitignore @@ -0,0 +1,7 @@ +/node_modules +/build +*.swp +*.log +*~ +.node-version +package-lock.json diff --git a/spec/is-valid-window/README.md b/spec/is-valid-window/README.md new file mode 100644 index 000000000000..6727360b6adb --- /dev/null +++ b/spec/is-valid-window/README.md @@ -0,0 +1,8 @@ +# is-valid-window + +Validates if a pointer to window is valid. Used by Electron to validate the +result of `TopLevelWindow.getNativeWindowHandle`. + +## License + +Public domain. diff --git a/spec/is-valid-window/binding.gyp b/spec/is-valid-window/binding.gyp new file mode 100644 index 000000000000..0de7c1f9d171 --- /dev/null +++ b/spec/is-valid-window/binding.gyp @@ -0,0 +1,41 @@ +{ + 'target_defaults': { + 'conditions': [ + ['OS=="win"', { + 'msvs_disabled_warnings': [ + 4530, # C++ exception handler used, but unwind semantics are not enabled + 4506, # no definition for inline function + ], + }], + ], + }, + 'targets': [ + { + 'target_name': 'is_valid_window', + 'sources': [ + 'src/impl.h', + 'src/main.cc', + ], + 'conditions': [ + ['OS=="win"', { + 'sources': [ + 'src/impl_win.cc', + ], + }], + ['OS=="mac"', { + 'sources': [ + 'src/impl_darwin.mm', + ], + 'libraries': [ + '$(SDKROOT)/System/Library/Frameworks/AppKit.framework', + ], + }], + ['OS not in ["mac", "win"]', { + 'sources': [ + 'src/impl_posix.cc', + ], + }], + ], + } + ] +} diff --git a/spec/is-valid-window/lib/is-valid-window.js b/spec/is-valid-window/lib/is-valid-window.js new file mode 100644 index 000000000000..e3154cbc3015 --- /dev/null +++ b/spec/is-valid-window/lib/is-valid-window.js @@ -0,0 +1 @@ +module.exports = require('../build/Release/is_valid_window.node').isValidWindow; diff --git a/spec/is-valid-window/package.json b/spec/is-valid-window/package.json new file mode 100644 index 000000000000..40e5c921451e --- /dev/null +++ b/spec/is-valid-window/package.json @@ -0,0 +1,9 @@ +{ + "main": "./lib/is-valid-window.js", + "name": "is-valid-window", + "version": "0.0.5", + "licenses": "Public Domain", + "dependencies": { + "nan": "2.x" + } +} diff --git a/spec/is-valid-window/src/impl.h b/spec/is-valid-window/src/impl.h new file mode 100644 index 000000000000..b211ff05dc5d --- /dev/null +++ b/spec/is-valid-window/src/impl.h @@ -0,0 +1,12 @@ +#ifndef SRC_IMPL_H_ +#define SRC_IMPL_H_ + +#include + +namespace impl { + +bool IsValidWindow(char* handle, size_t size); + +} // namespace impl + +#endif // SRC_IMPL_H_ diff --git a/spec/is-valid-window/src/impl_darwin.mm b/spec/is-valid-window/src/impl_darwin.mm new file mode 100644 index 000000000000..ef71aed16258 --- /dev/null +++ b/spec/is-valid-window/src/impl_darwin.mm @@ -0,0 +1,14 @@ +#include "impl.h" + +#include + +namespace impl { + +bool IsValidWindow(char* handle, size_t size) { + if (size != sizeof(NSView*)) + return false; + NSView* view = *reinterpret_cast(handle); + return [view isKindOfClass:[NSView class]]; +} + +} // namespace impl diff --git a/spec/is-valid-window/src/impl_posix.cc b/spec/is-valid-window/src/impl_posix.cc new file mode 100644 index 000000000000..6c582b6ffb95 --- /dev/null +++ b/spec/is-valid-window/src/impl_posix.cc @@ -0,0 +1,9 @@ +#include "impl.h" + +namespace impl { + +bool IsValidWindow(char* handle, size_t size) { + return true; +} + +} // namespace impl diff --git a/spec/is-valid-window/src/impl_win.cc b/spec/is-valid-window/src/impl_win.cc new file mode 100644 index 000000000000..91d272bdc5db --- /dev/null +++ b/spec/is-valid-window/src/impl_win.cc @@ -0,0 +1,14 @@ +#include "impl.h" + +#include + +namespace impl { + +bool IsValidWindow(char* handle, size_t size) { + if (size != sizeof(HWND)) + return false; + HWND window = *reinterpret_cast(handle); + return ::IsWindow(window); +} + +} // namespace impl diff --git a/spec/is-valid-window/src/main.cc b/spec/is-valid-window/src/main.cc new file mode 100644 index 000000000000..b14e38b7d3d6 --- /dev/null +++ b/spec/is-valid-window/src/main.cc @@ -0,0 +1,56 @@ +#include +#include + +#include "impl.h" + +namespace { + +napi_value IsValidWindow(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1], result; + napi_status status; + + status = napi_get_cb_info(env, info, &argc, args, NULL, NULL); + if (status != napi_ok) + return NULL; + + bool is_buffer; + status = napi_is_buffer(env, args[0], &is_buffer); + if (status != napi_ok) + return NULL; + + if (!is_buffer) { + napi_throw_error(env, NULL, "First argument must be Buffer"); + return NULL; + } + + char* data; + size_t length; + status = napi_get_buffer_info(env, args[0], (void**)&data, &length); + if (status != napi_ok) + return NULL; + + status = napi_get_boolean(env, impl::IsValidWindow(data, length), &result); + if (status != napi_ok) + return NULL; + + return result; +} + +napi_value Init(napi_env env, napi_value exports) { + napi_status status; + napi_property_descriptor descriptors[] = {{"isValidWindow", NULL, + IsValidWindow, NULL, NULL, NULL, + napi_default, NULL}}; + + status = napi_define_properties( + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors); + if (status != napi_ok) + return NULL; + + return exports; +} + +} // namespace + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/spec/package.json b/spec/package.json index 34e7e5ee0e83..4bb3c0b6fe7f 100644 --- a/spec/package.json +++ b/spec/package.json @@ -8,6 +8,7 @@ }, "devDependencies": { "@electron-ci/echo": "file:./fixtures/native-addon/echo", + "@electron-ci/is-valid-window": "file:./is-valid-window", "@electron-ci/uv-dlopen": "file:./fixtures/native-addon/uv-dlopen/", "@marshallofsound/mocha-appveyor-reporter": "^0.4.3", "@types/sinon": "^9.0.4", @@ -20,7 +21,6 @@ "dbus-native": "github:nornagon/dbus-native#master", "dirty-chai": "^2.0.1", "graceful-fs": "^4.1.15", - "is-valid-window": "0.0.5", "mkdirp": "^0.5.1", "mocha": "^10.0.0", "mocha-junit-reporter": "^1.18.0", diff --git a/spec/yarn.lock b/spec/yarn.lock index b97e450d3810..86dbabf976e3 100644 --- a/spec/yarn.lock +++ b/spec/yarn.lock @@ -5,6 +5,11 @@ "@electron-ci/echo@file:./fixtures/native-addon/echo": version "0.0.1" +"@electron-ci/is-valid-window@file:./is-valid-window": + version "0.0.5" + dependencies: + nan "2.x" + "@electron-ci/uv-dlopen@file:./fixtures/native-addon/uv-dlopen": version "0.0.1" @@ -703,13 +708,6 @@ is-unicode-supported@^0.1.0: resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz#3f26c76a809593b52bfa2ecb5710ed2779b522a7" integrity sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw== -is-valid-window@0.0.5: - version "0.0.5" - resolved "https://registry.yarnpkg.com/is-valid-window/-/is-valid-window-0.0.5.tgz#57935b35b8c3f30f077b16d54fe5c0d0e4ba8a03" - integrity sha512-bs7tIvCJyJ9BOFZP+U3yGWH9mMQVBDxtWTokrpvpSzEQfMHX+hlhuKqltbYnVkEfj+YSgQJgAW/Klx0qQH7zbQ== - dependencies: - nan "2.x" - isarray@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf" @@ -897,9 +895,9 @@ ms@2.1.3: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== -nan@2.x, nan@^2.12.1, "nan@github:jkleinsc/nan#remove_accessor_signature": - version "2.16.0" - resolved "https://codeload.github.com/jkleinsc/nan/tar.gz/6a2f95a6a2209d8aa7542fb18099fd808a802059" +nan@2.x, nan@^2.12.1, nan@nodejs/nan#e14bdcd1f72d62bca1d541b66da43130384ec213: + version "2.18.0" + resolved "https://codeload.github.com/nodejs/nan/tar.gz/e14bdcd1f72d62bca1d541b66da43130384ec213" nanoid@3.3.3: version "3.3.3"