Fix nits in some Windows-only base/ code.

- Make the control flow easier to read in IsKeyboardPresentOnSlate(),
  LoadSystemLibrary(), and PinSystemLibrary().
- Change GetSystemLibraryName() to use an empty FilePath to indicate
  failure.
- Flip the parameter ordering for IsKeyboardPresentOnSlate().
- Don't bother writing base:: inside namespace base.

Change-Id: Ic3e016aec00fdfd3f4e66e7fc69b2f7c588f029b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1824023
Reviewed-by: Yuichiro Hanada <[email protected]>
Reviewed-by: Greg Thompson <[email protected]>
Commit-Queue: Lei Zhang <[email protected]>
Cr-Commit-Position: refs/heads/master@{#700549}
diff --git a/base/native_library_win.cc b/base/native_library_win.cc
index 08d520d0..74413b6 100644
--- a/base/native_library_win.cc
+++ b/base/native_library_win.cc
@@ -8,7 +8,6 @@
 
 #include "base/files/file_util.h"
 #include "base/metrics/histogram_macros.h"
-#include "base/optional.h"
 #include "base/path_service.h"
 #include "base/scoped_native_library.h"
 #include "base/strings/string_util.h"
@@ -170,12 +169,12 @@
   return module;
 }
 
-Optional<FilePath> GetSystemLibraryName(FilePath::StringPieceType name) {
+FilePath GetSystemLibraryName(FilePath::StringPieceType name) {
   FilePath library_path;
   // Use an absolute path to load the DLL to avoid DLL preloading attacks.
-  if (!base::PathService::Get(base::DIR_SYSTEM, &library_path))
-    return base::nullopt;
-  return make_optional(library_path.Append(name));
+  if (PathService::Get(DIR_SYSTEM, &library_path))
+    library_path = library_path.Append(name);
+  return library_path;
 }
 
 }  // namespace
@@ -210,18 +209,19 @@
 
 NativeLibrary LoadSystemLibrary(FilePath::StringPieceType name,
                                 NativeLibraryLoadError* error) {
-  Optional<FilePath> library_path = GetSystemLibraryName(name);
-  if (library_path)
-    return LoadSystemLibraryHelper(library_path.value(), error);
-  if (error)
-    error->code = ERROR_NOT_FOUND;
-  return nullptr;
+  FilePath library_path = GetSystemLibraryName(name);
+  if (library_path.empty()) {
+    if (error)
+      error->code = ERROR_NOT_FOUND;
+    return nullptr;
+  }
+  return LoadSystemLibraryHelper(library_path, error);
 }
 
 NativeLibrary PinSystemLibrary(FilePath::StringPieceType name,
                                NativeLibraryLoadError* error) {
-  Optional<FilePath> library_path = GetSystemLibraryName(name);
-  if (!library_path) {
+  FilePath library_path = GetSystemLibraryName(name);
+  if (library_path.empty()) {
     if (error)
       error->code = ERROR_NOT_FOUND;
     return nullptr;
@@ -231,25 +231,28 @@
   // Dllmain.
   ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
   ScopedNativeLibrary module;
-  if (!::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN,
-                            as_wcstr(library_path.value().value()),
-                            ScopedNativeLibrary::Receiver(module).get())) {
-    // Load and pin the library since it wasn't already loaded.
-    module = ScopedNativeLibrary(
-        LoadSystemLibraryHelper(library_path.value(), error));
-    if (module.is_valid()) {
-      ScopedNativeLibrary temp;
-      if (!::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN,
-                                as_wcstr(library_path.value().value()),
-                                ScopedNativeLibrary::Receiver(temp).get())) {
-        if (error)
-          error->code = ::GetLastError();
-        // Return nullptr since we failed to pin the module.
-        return nullptr;
-      }
-    }
+  if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN,
+                           as_wcstr(library_path.value()),
+                           ScopedNativeLibrary::Receiver(module).get())) {
+    return module.release();
   }
-  return module.release();
+
+  // Load and pin the library since it wasn't already loaded.
+  module = ScopedNativeLibrary(LoadSystemLibraryHelper(library_path, error));
+  if (!module.is_valid())
+    return nullptr;
+
+  ScopedNativeLibrary temp;
+  if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN,
+                           as_wcstr(library_path.value()),
+                           ScopedNativeLibrary::Receiver(temp).get())) {
+    return module.release();
+  }
+
+  if (error)
+    error->code = ::GetLastError();
+  // Return nullptr since we failed to pin the module.
+  return nullptr;
 }
 
 }  // namespace base