Use span-based file API in //components/metrics

No (intended) functionality changes.

Bug: 435317390
Change-Id: Ia3524652f14d053c02839ee447a011cf55191820
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7472416
Reviewed-by: Alexei Svitkine <asvitkine@chromium.org>
Commit-Queue: Alexei Svitkine <asvitkine@chromium.org>
Auto-Submit: Jan Keitel <jkeitel@google.com>
Cr-Commit-Position: refs/heads/main@{#1569728}
NOKEYCHECK=True
GitOrigin-RevId: ece2814135c638ecf123881d1a909a0e99ce4486
diff --git a/call_stacks/stack_sampling_recorder_unittest.cc b/call_stacks/stack_sampling_recorder_unittest.cc
index e9bbb54..0325f4b 100644
--- a/call_stacks/stack_sampling_recorder_unittest.cc
+++ b/call_stacks/stack_sampling_recorder_unittest.cc
@@ -16,6 +16,7 @@
 #include "base/files/file_util.h"
 #include "base/files/scoped_temp_dir.h"
 #include "base/memory/scoped_refptr.h"
+#include "base/strings/string_view_util.h"
 #include "base/synchronization/waitable_event.h"
 #include "base/threading/platform_thread.h"
 #include "base/threading/thread.h"
@@ -253,9 +254,8 @@
   base::File file(path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ |
                             base::File::FLAG_WRITE);
   CHECK(file.IsValid());
-  constexpr char kPattern[] = "Not a valid proto";
-  CHECK_EQ(UNSAFE_TODO(file.Write(0, kPattern, sizeof(kPattern))),
-           static_cast<int>(sizeof(kPattern)));
+  constexpr std::string_view kPattern = "Not a valid proto";
+  CHECK(file.WriteAndCheck(0, base::as_byte_span(kPattern)));
 
   // 2. Lock the file
   CHECK_EQ(HANDLE_EINTR(flock(file.GetPlatformFile(), LOCK_EX)), 0);
@@ -274,10 +274,9 @@
   base::PlatformThreadBase::Sleep(base::Seconds(5));
 
   // 5. CHECK that the file still contains the original pattern.
-  char buffer[sizeof(kPattern) + 1];
-  UNSAFE_TODO(CHECK_EQ(file.Read(0, buffer, sizeof(buffer)),
-                       static_cast<int>(sizeof(kPattern))));
-  CHECK_EQ(std::string(buffer), std::string(kPattern));
+  uint8_t buffer[kPattern.size()];
+  CHECK(file.ReadAndCheck(0, buffer));
+  CHECK_EQ(kPattern, base::as_string_view(buffer));
 }
 
 TEST_F(StackSamplingRecorderTest, DoesNotWriteToLockedFile) {
diff --git a/file_metrics_provider_unittest.cc b/file_metrics_provider_unittest.cc
index 32b225a..abd0ea7 100644
--- a/file_metrics_provider_unittest.cc
+++ b/file_metrics_provider_unittest.cc
@@ -260,9 +260,10 @@
     // Use DCHECK so the stack-trace will indicate where this was called.
     DCHECK(writer.IsValid()) << path;
     size_t file_size = create_large_files_ ? metrics->size() : metrics->used();
-    int written =
-        UNSAFE_TODO(writer.Write(0, (const char*)metrics->data(), file_size));
-    DCHECK_EQ(static_cast<int>(file_size), written);
+    bool success = writer.WriteAndCheck(
+        0, UNSAFE_BUFFERS(base::span(
+               static_cast<const uint8_t*>(metrics->data()), file_size)));
+    DCHECK(success);
   }
 
   void WriteMetricsFileAtTime(const base::FilePath& path,