From 1cc41ea84f3ae72b784b5150d275b5ea45625217 Mon Sep 17 00:00:00 2001 From: Philipp Hancke Date: Wed, 24 May 2023 18:49:10 +0200 Subject: [PATCH] Remove unused Win32Window class BUG=None Change-Id: I1d6b4e64a01076166d841c7c72eb0e2b968dd812 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/306441 Reviewed-by: Harald Alvestrand Commit-Queue: Philipp Hancke Reviewed-by: Mirko Bonadei Cr-Commit-Position: refs/heads/main@{#40140} --- rtc_base/BUILD.gn | 7 +- rtc_base/win32_window.cc | 150 ------------------------------ rtc_base/win32_window.h | 70 -------------- rtc_base/win32_window_unittest.cc | 66 ------------- 4 files changed, 1 insertion(+), 292 deletions(-) delete mode 100644 rtc_base/win32_window.cc delete mode 100644 rtc_base/win32_window.h delete mode 100644 rtc_base/win32_window_unittest.cc diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index cbbf359ecb..561cb9ae7f 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -1065,8 +1065,6 @@ if (is_win) { sources = [ "win32.cc", "win32.h", - "win32_window.cc", - "win32_window.h", ] deps = [ @@ -2042,10 +2040,7 @@ if (rtc_include_tests) { deps += [ "synchronization:synchronization_unittests" ] } if (is_win) { - sources += [ - "win32_unittest.cc", - "win32_window_unittest.cc", - ] + sources += [ "win32_unittest.cc" ] deps += [ ":win32" ] } if (is_posix || is_fuchsia) { diff --git a/rtc_base/win32_window.cc b/rtc_base/win32_window.cc deleted file mode 100644 index 275237f4a6..0000000000 --- a/rtc_base/win32_window.cc +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/win32_window.h" - -#include "rtc_base/checks.h" -#include "rtc_base/logging.h" - -namespace rtc { - -/////////////////////////////////////////////////////////////////////////////// -// Win32Window -/////////////////////////////////////////////////////////////////////////////// - -static const wchar_t kWindowBaseClassName[] = L"RtcWindowBaseClass"; -HINSTANCE Win32Window::instance_ = nullptr; -ATOM Win32Window::window_class_ = 0; - -Win32Window::Win32Window() : wnd_(nullptr) {} - -Win32Window::~Win32Window() { - RTC_DCHECK(nullptr == wnd_); -} - -bool Win32Window::Create(HWND parent, - const wchar_t* title, - DWORD style, - DWORD exstyle, - int x, - int y, - int cx, - int cy) { - if (wnd_) { - // Window already exists. - return false; - } - - if (!window_class_) { - if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - reinterpret_cast(&Win32Window::WndProc), - &instance_)) { - RTC_LOG_GLE(LS_ERROR) << "GetModuleHandleEx failed"; - return false; - } - - // Register or reregister the class as necessary. window_class_ == nullptr - // is not an infallible indicator that the class is unregistered. - WNDCLASSEXW wcex; - memset(&wcex, 0, sizeof(wcex)); - wcex.cbSize = sizeof(wcex); - if (::GetClassInfoExW(instance_, kWindowBaseClassName, &wcex) && - !::UnregisterClassW(kWindowBaseClassName, instance_)) { - RTC_LOG_GLE(LS_ERROR) << "UnregisterClass failed."; - } - - memset(&wcex, 0, sizeof(wcex)); - wcex.cbSize = sizeof(wcex); - wcex.hInstance = instance_; - wcex.lpfnWndProc = &Win32Window::WndProc; - wcex.lpszClassName = kWindowBaseClassName; - window_class_ = ::RegisterClassExW(&wcex); - if (!window_class_) { - RTC_LOG_GLE(LS_ERROR) << "RegisterClassEx failed"; - return false; - } - } - wnd_ = ::CreateWindowExW(exstyle, kWindowBaseClassName, title, style, x, y, - cx, cy, parent, nullptr, instance_, this); - return (nullptr != wnd_); -} - -void Win32Window::Destroy() { - const bool success = ::DestroyWindow(wnd_); - RTC_DCHECK(success); -} - -void Win32Window::Shutdown() { - if (window_class_) { - if (!::UnregisterClass(MAKEINTATOM(window_class_), instance_)) { - RTC_LOG_GLE(LS_ERROR) << "UnregisterClass failed."; - } - window_class_ = 0; - } -} - -bool Win32Window::OnMessage(UINT uMsg, - WPARAM wParam, - LPARAM lParam, - LRESULT& result) { - switch (uMsg) { - case WM_CLOSE: - if (!OnClose()) { - result = 0; - return true; - } - break; - } - return false; -} - -bool Win32Window::OnClose() { - return true; -} - -void Win32Window::OnNcDestroy() { - // Do nothing. } -} - -LRESULT Win32Window::WndProc(HWND hwnd, - UINT uMsg, - WPARAM wParam, - LPARAM lParam) { - Win32Window* that = - reinterpret_cast(::GetWindowLongPtr(hwnd, GWLP_USERDATA)); - if (!that && (WM_CREATE == uMsg)) { - CREATESTRUCT* cs = reinterpret_cast(lParam); - that = static_cast(cs->lpCreateParams); - that->wnd_ = hwnd; - ::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast(that)); - } - if (that) { - LRESULT result; - bool handled = that->OnMessage(uMsg, wParam, lParam, result); - if (WM_DESTROY == uMsg) { - for (HWND child = ::GetWindow(hwnd, GW_CHILD); child; - child = ::GetWindow(child, GW_HWNDNEXT)) { - RTC_LOG(LS_INFO) << "Child window: " << static_cast(child); - } - } - if (WM_NCDESTROY == uMsg) { - ::SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL); - that->wnd_ = nullptr; - that->OnNcDestroy(); - } - if (handled) { - return result; - } - } - return ::DefWindowProc(hwnd, uMsg, wParam, lParam); -} - -} // namespace rtc diff --git a/rtc_base/win32_window.h b/rtc_base/win32_window.h deleted file mode 100644 index 5256a85d09..0000000000 --- a/rtc_base/win32_window.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2004 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#ifndef RTC_BASE_WIN32_WINDOW_H_ -#define RTC_BASE_WIN32_WINDOW_H_ - -#if defined(WEBRTC_WIN) - -#include "rtc_base/win32.h" - -namespace rtc { - -/////////////////////////////////////////////////////////////////////////////// -// Win32Window -/////////////////////////////////////////////////////////////////////////////// - -class Win32Window { - public: - Win32Window(); - virtual ~Win32Window(); - - HWND handle() const { return wnd_; } - - bool Create(HWND parent, - const wchar_t* title, - DWORD style, - DWORD exstyle, - int x, - int y, - int cx, - int cy); - void Destroy(); - - // Call this when your DLL unloads. - static void Shutdown(); - - protected: - virtual bool OnMessage(UINT uMsg, - WPARAM wParam, - LPARAM lParam, - LRESULT& result); - - virtual bool OnClose(); - virtual void OnNcDestroy(); - - private: - static LRESULT CALLBACK WndProc(HWND hwnd, - UINT uMsg, - WPARAM wParam, - LPARAM lParam); - - HWND wnd_; - static HINSTANCE instance_; - static ATOM window_class_; -}; - -/////////////////////////////////////////////////////////////////////////////// - -} // namespace rtc - -#endif // WEBRTC_WIN - -#endif // RTC_BASE_WIN32_WINDOW_H_ diff --git a/rtc_base/win32_window_unittest.cc b/rtc_base/win32_window_unittest.cc deleted file mode 100644 index 665b5c6b9a..0000000000 --- a/rtc_base/win32_window_unittest.cc +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2009 The WebRTC Project Authors. All rights reserved. - * - * Use of this source code is governed by a BSD-style license - * that can be found in the LICENSE file in the root of the source - * tree. An additional intellectual property rights grant can be found - * in the file PATENTS. All contributing project authors may - * be found in the AUTHORS file in the root of the source tree. - */ - -#include "rtc_base/win32_window.h" - -#include "rtc_base/gunit.h" -#include "rtc_base/logging.h" - -static LRESULT kDummyResult = 0x1234ABCD; - -class TestWindow : public rtc::Win32Window { - public: - TestWindow() : destroyed_(false) { memset(&msg_, 0, sizeof(msg_)); } - const MSG& msg() const { return msg_; } - bool destroyed() const { return destroyed_; } - - bool OnMessage(UINT uMsg, - WPARAM wParam, - LPARAM lParam, - LRESULT& result) override { - msg_.message = uMsg; - msg_.wParam = wParam; - msg_.lParam = lParam; - result = kDummyResult; - return true; - } - void OnNcDestroy() override { destroyed_ = true; } - - private: - MSG msg_; - bool destroyed_; -}; - -TEST(Win32WindowTest, Basics) { - TestWindow wnd; - EXPECT_TRUE(wnd.handle() == nullptr); - EXPECT_FALSE(wnd.destroyed()); - EXPECT_TRUE(wnd.Create(0, L"Test", 0, 0, 0, 0, 100, 100)); - EXPECT_TRUE(wnd.handle() != nullptr); - EXPECT_EQ(kDummyResult, ::SendMessage(wnd.handle(), WM_USER, 1, 2)); - EXPECT_EQ(static_cast(WM_USER), wnd.msg().message); - EXPECT_EQ(1u, wnd.msg().wParam); - EXPECT_EQ(2l, wnd.msg().lParam); - wnd.Destroy(); - EXPECT_TRUE(wnd.handle() == nullptr); - EXPECT_TRUE(wnd.destroyed()); -} - -TEST(Win32WindowTest, MultipleWindows) { - TestWindow wnd1, wnd2; - EXPECT_TRUE(wnd1.Create(0, L"Test", 0, 0, 0, 0, 100, 100)); - EXPECT_TRUE(wnd2.Create(0, L"Test", 0, 0, 0, 0, 100, 100)); - EXPECT_TRUE(wnd1.handle() != nullptr); - EXPECT_TRUE(wnd2.handle() != nullptr); - wnd1.Destroy(); - wnd2.Destroy(); - EXPECT_TRUE(wnd2.handle() == nullptr); - EXPECT_TRUE(wnd1.handle() == nullptr); -}