Move third_party/base/{debug,win} to core/fxcrt

Move these 2 directories and remove nested namespaces. Also stop using
ScopedSelectObject in samples/pdfium_test.cc, to make the sample code
look more like what real embedders would write.

Bug: pdfium:2127
Change-Id: I6b941edc4bafb8f5ed73dfa98195b9291ff9808b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/116652
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
diff --git a/core/fxcrt/BUILD.gn b/core/fxcrt/BUILD.gn
index d3f68ca..88d74f3 100644
--- a/core/fxcrt/BUILD.gn
+++ b/core/fxcrt/BUILD.gn
@@ -48,6 +48,8 @@
     "containers/adapters.h",
     "containers/contains.h",
     "data_vector.h",
+    "debug/alias.cc",
+    "debug/alias.h",
     "fileaccess_iface.h",
     "fixed_size_data_vector.h",
     "fx_2d_size.h",
@@ -159,6 +161,9 @@
       "cfx_fileaccess_windows.cpp",
       "cfx_fileaccess_windows.h",
       "fx_folder_windows.cpp",
+      "win/scoped_select_object.h",
+      "win/win_util.cc",
+      "win/win_util.h",
     ]
   }
   if (pdf_enable_xfa) {
diff --git a/third_party/base/debug/alias.cc b/core/fxcrt/debug/alias.cc
similarity index 70%
rename from third_party/base/debug/alias.cc
rename to core/fxcrt/debug/alias.cc
index 6ee2ee9..213b6f9 100644
--- a/third_party/base/debug/alias.cc
+++ b/core/fxcrt/debug/alias.cc
@@ -1,14 +1,12 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2024 The PDFium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "third_party/base/debug/alias.h"
+#include "core/fxcrt/debug/alias.h"
 
 #include "build/build_config.h"
 
 namespace pdfium {
-namespace base {
-namespace debug {
 
 #if defined(COMPILER_MSVC)
 #pragma optimize("", off)
@@ -25,6 +23,4 @@
 #pragma clang optimize on
 #endif
 
-}  // namespace debug
-}  // namespace base
 }  // namespace pdfium
diff --git a/third_party/base/debug/alias.h b/core/fxcrt/debug/alias.h
similarity index 73%
rename from third_party/base/debug/alias.h
rename to core/fxcrt/debug/alias.h
index 8228a6f..de9c415 100644
--- a/third_party/base/debug/alias.h
+++ b/core/fxcrt/debug/alias.h
@@ -1,18 +1,16 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright 2024 The PDFium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef THIRD_PARTY_BASE_DEBUG_ALIAS_H_
-#define THIRD_PARTY_BASE_DEBUG_ALIAS_H_
+#ifndef CORE_FXCRT_DEBUG_ALIAS_H_
+#define CORE_FXCRT_DEBUG_ALIAS_H_
 
 namespace pdfium {
-namespace base {
-namespace debug {
 
 // Make the optimizer think that var is aliased. This is to prevent it from
 // optimizing out local variables that would not otherwise be live at the point
 // of a potential crash.
-// base::debug::Alias should only be used for local variables, not globals,
+// pdfium::Alias should only be used for local variables, not globals,
 // object members, or function return values - these must be copied to locals if
 // you want to ensure they are recorded in crash dumps.
 // Note that if the local variable is a pointer then its value will be retained
@@ -22,13 +20,11 @@
 // a pointer to an object and you want to retain the object's state you need to
 // copy the object or its fields to local variables. Example usage:
 //   int last_error = err_;
-//   base::debug::Alias(&last_error);
+//   pdfium::Alias(&last_error);
 //   DEBUG_ALIAS_FOR_CSTR(name_copy, p->name, 16);
 //   CHECK(false);
 void Alias(const void* var);
 
-}  // namespace debug
-}  // namespace base
 }  // namespace pdfium
 
-#endif  // THIRD_PARTY_BASE_DEBUG_ALIAS_H_
+#endif  // CORE_FXCRT_DEBUG_ALIAS_H_
diff --git a/core/fxcrt/fx_memory.cpp b/core/fxcrt/fx_memory.cpp
index 0d978b6..9d84540 100644
--- a/core/fxcrt/fx_memory.cpp
+++ b/core/fxcrt/fx_memory.cpp
@@ -12,7 +12,7 @@
 #include <limits>
 
 #include "build/build_config.h"
-#include "third_party/base/debug/alias.h"
+#include "core/fxcrt/debug/alias.h"
 
 #if BUILDFLAG(IS_WIN)
 #include <windows.h>
@@ -38,7 +38,7 @@
   // Convince the linker this should not be folded with similar functions using
   // Identical Code Folding.
   static int make_this_function_aliased = 0xbd;
-  pdfium::base::debug::Alias(&make_this_function_aliased);
+  pdfium::Alias(&make_this_function_aliased);
 
 #if BUILDFLAG(IS_WIN)
   // The same custom Windows exception code used in Chromium and Breakpad.
diff --git a/third_party/base/win/scoped_select_object.h b/core/fxcrt/win/scoped_select_object.h
similarity index 75%
rename from third_party/base/win/scoped_select_object.h
rename to core/fxcrt/win/scoped_select_object.h
index 89a5f1b..96c97f0 100644
--- a/third_party/base/win/scoped_select_object.h
+++ b/core/fxcrt/win/scoped_select_object.h
@@ -1,17 +1,15 @@
-// Copyright 2011 The Chromium Authors. All rights reserved.
+// Copyright 2024 The PDFium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef THIRD_PARTY_BASE_WIN_SCOPED_SELECT_OBJECT_H_
-#define THIRD_PARTY_BASE_WIN_SCOPED_SELECT_OBJECT_H_
+#ifndef CORE_FXCRT_WIN_SCOPED_SELECT_OBJECT_H_
+#define CORE_FXCRT_WIN_SCOPED_SELECT_OBJECT_H_
 
 #include <windows.h>
 
 #include "third_party/base/check.h"
 
 namespace pdfium {
-namespace base {
-namespace win {
 
 // Helper class for deselecting object from DC.
 class ScopedSelectObject {
@@ -38,8 +36,6 @@
   const HGDIOBJ oldobj_;
 };
 
-}  // namespace win
-}  // namespace base
 }  // namespace pdfium
 
-#endif  // THIRD_PARTY_BASE_WIN_SCOPED_SELECT_OBJECT_H_
+#endif  // CORE_FXCRT_WIN_SCOPED_SELECT_OBJECT_H_
diff --git a/third_party/base/win/win_util.cc b/core/fxcrt/win/win_util.cc
similarity index 86%
rename from third_party/base/win/win_util.cc
rename to core/fxcrt/win/win_util.cc
index ae2dba8..43e9151 100644
--- a/third_party/base/win/win_util.cc
+++ b/core/fxcrt/win/win_util.cc
@@ -1,15 +1,13 @@
-// Copyright 2019 The Chromium Authors. All rights reserved.
+// Copyright 2024 The PDFium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "third_party/base/win/win_util.h"
+#include "core/fxcrt/win/win_util.h"
 
 #include <windows.h>
 #include <processthreadsapi.h>
 
 namespace pdfium {
-namespace base {
-namespace win {
 
 bool IsUser32AndGdi32Available() {
   static auto is_user32_and_gdi32_available = []() {
@@ -36,6 +34,4 @@
   return is_user32_and_gdi32_available;
 }
 
-}  // namespace win
-}  // namespace base
 }  // namespace pdfium
diff --git a/third_party/base/win/win_util.h b/core/fxcrt/win/win_util.h
similarity index 67%
rename from third_party/base/win/win_util.h
rename to core/fxcrt/win/win_util.h
index b27f2f1..01548a6 100644
--- a/third_party/base/win/win_util.h
+++ b/core/fxcrt/win/win_util.h
@@ -1,13 +1,11 @@
-// Copyright 2019 The Chromium Authors. All rights reserved.
+// Copyright 2024 The PDFium Authors
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#ifndef THIRD_PARTY_BASE_WIN_WIN_UTIL_H_
-#define THIRD_PARTY_BASE_WIN_WIN_UTIL_H_
+#ifndef CORE_FXCRT_WIN_WIN_UTIL_H_
+#define CORE_FXCRT_WIN_WIN_UTIL_H_
 
 namespace pdfium {
-namespace base {
-namespace win {
 
 // Returns true if the current process can make USER32 or GDI32 calls such as
 // CreateWindow and CreateDC. Windows 8 and above allow the kernel component
@@ -16,8 +14,6 @@
 // and provide a fallback path if necessary.
 bool IsUser32AndGdi32Available();
 
-}  // namespace win
-}  // namespace base
 }  // namespace pdfium
 
-#endif  // THIRD_PARTY_BASE_WIN_WIN_UTIL_H_
+#endif  // CORE_FXCRT_WIN_WIN_UTIL_H_
diff --git a/core/fxge/win32/cwin32_platform.cpp b/core/fxge/win32/cwin32_platform.cpp
index 8368243..40774fd 100644
--- a/core/fxge/win32/cwin32_platform.cpp
+++ b/core/fxge/win32/cwin32_platform.cpp
@@ -15,16 +15,14 @@
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/numerics/safe_conversions.h"
 #include "core/fxcrt/span.h"
+#include "core/fxcrt/win/scoped_select_object.h"
+#include "core/fxcrt/win/win_util.h"
 #include "core/fxge/cfx_folderfontinfo.h"
 #include "core/fxge/cfx_gemodule.h"
 #include "third_party/base/check.h"
-#include "third_party/base/win/scoped_select_object.h"
-#include "third_party/base/win/win_util.h"
 
 namespace {
 
-using ScopedSelectObject = pdfium::base::win::ScopedSelectObject;
-
 struct Variant {
   const char* m_pFaceName;
   pdfium::span<const char> m_pVariantName;
@@ -420,7 +418,7 @@
 size_t CFX_Win32FontInfo::GetFontData(void* hFont,
                                       uint32_t table,
                                       pdfium::span<uint8_t> buffer) {
-  ScopedSelectObject select_object(m_hDC, static_cast<HFONT>(hFont));
+  pdfium::ScopedSelectObject select_object(m_hDC, static_cast<HFONT>(hFont));
   table = fxcrt::FromBE32(table);
   size_t size = ::GetFontData(m_hDC, table, 0, buffer.data(),
                               pdfium::checked_cast<DWORD>(buffer.size()));
@@ -428,7 +426,7 @@
 }
 
 bool CFX_Win32FontInfo::GetFaceName(void* hFont, ByteString* name) {
-  ScopedSelectObject select_object(m_hDC, static_cast<HFONT>(hFont));
+  pdfium::ScopedSelectObject select_object(m_hDC, static_cast<HFONT>(hFont));
   char facebuf[100];
   if (::GetTextFaceA(m_hDC, std::size(facebuf), facebuf) == 0)
     return false;
@@ -438,7 +436,7 @@
 }
 
 bool CFX_Win32FontInfo::GetFontCharset(void* hFont, FX_Charset* charset) {
-  ScopedSelectObject select_object(m_hDC, static_cast<HFONT>(hFont));
+  pdfium::ScopedSelectObject select_object(m_hDC, static_cast<HFONT>(hFont));
   TEXTMETRIC tm;
   ::GetTextMetrics(m_hDC, &tm);
   *charset = FX_GetCharsetFromInt(tm.tmCharSet);
@@ -452,8 +450,9 @@
 CWin32Platform::~CWin32Platform() = default;
 
 void CWin32Platform::Init() {
-  if (pdfium::base::win::IsUser32AndGdi32Available())
+  if (pdfium::IsUser32AndGdi32Available()) {
     m_GdiplusExt.Load();
+  }
 }
 
 std::unique_ptr<SystemFontInfoIface>
@@ -466,8 +465,9 @@
     return std::move(font_info);
   }
 
-  if (pdfium::base::win::IsUser32AndGdi32Available())
+  if (pdfium::IsUser32AndGdi32Available()) {
     return std::make_unique<CFX_Win32FontInfo>();
+  }
 
   // Select the fallback font information class if GDI is disabled.
   auto fallback_info = std::make_unique<CFX_Win32FallbackFontInfo>();
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index deebd73..94fd6a8 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -54,7 +54,6 @@
 #include <wingdi.h>
 
 #include "samples/helpers/win32/com_factory.h"
-#include "third_party/base/win/scoped_select_object.h"
 #else
 #include <unistd.h>
 #endif  // _WIN32
@@ -1181,14 +1180,21 @@
     if (!dib_.Get() || !InitializeBitmap(dib_pixels)) {
       return false;
     }
-    pdfium::base::win::ScopedSelectObject select_dib(dc_.Get(), dib_.Get());
+
+    HGDIOBJ old_obj = SelectObject(dc_.Get(), dib_.Get());
+    CHECK(old_obj);
+    CHECK_NE(old_obj, HGDI_ERROR);
 
     // Render into the in-memory DC.
     FPDF_RenderPage(dc_.Get(), page(), /*start_x=*/0, /*start_y=*/0,
                     /*size_x=*/width(), /*size_y=*/height(), /*rotate=*/0,
                     /*flags=*/flags());
 
-    return !!GdiFlush();
+    bool result = !!GdiFlush();
+    HGDIOBJ dib_obj = SelectObject(dc_.Get(), old_obj);
+    CHECK((GetObjectType(old_obj) != OBJ_REGION && dib_obj) ||
+          (GetObjectType(old_obj) == OBJ_REGION && dib_obj != HGDI_ERROR));
+    return result;
   }
 
   void Finish(FPDF_FORMHANDLE /*form*/) override {
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index babc0d6..d0e6bb3 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -556,8 +556,6 @@
     "base/check.h",
     "base/check_op.h",
     "base/component_export.h",
-    "base/debug/alias.cc",
-    "base/debug/alias.h",
     "base/immediate_crash.h",
     "base/memory/aligned_memory.cc",
     "base/memory/aligned_memory.h",
@@ -570,13 +568,6 @@
     ":pdfium_compiler_specific",
     "//third_party/abseil-cpp:absl",
   ]
-  if (is_win) {
-    sources += [
-      "base/win/scoped_select_object.h",
-      "base/win/win_util.cc",
-      "base/win/win_util.h",
-    ]
-  }
 }
 
 source_set("pdfium_base_test_support") {