Commit 9c078ea7 authored by Shimi Zhang's avatar Shimi Zhang Committed by Commit Bot

[Printing] Write file to file descriptor directly on Android

Android's policy doesn't allow to exchange file ownership. It should be
caught by our test but we don't have up-to-date bots setup in Chromium
yet.

Bug: 1050064
Change-Id: I26c82e538817e68fd49f40c0c7c414c5a76078b8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2048271
Commit-Queue: Shimi Zhang <ctzsm@chromium.org>
Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#740491}
parent 1e895df0
......@@ -45,4 +45,17 @@ bool Metafile::SaveTo(base::File* file) const {
return true;
}
#if defined(OS_ANDROID)
bool Metafile::SaveToFileDescriptor(int fd) const {
if (fd == base::kInvalidFd)
return false;
std::vector<char> buffer;
if (!GetDataAsVector(&buffer))
return false;
return base::WriteFileDescriptor(fd, buffer.data(), buffer.size());
}
#endif // defined(OS_ANDROID)
} // namespace printing
......@@ -20,6 +20,9 @@
#include <ApplicationServices/ApplicationServices.h>
#include <CoreFoundation/CoreFoundation.h>
#include "base/mac/scoped_cftyperef.h"
#elif defined(OS_ANDROID)
#include "base/file_descriptor_posix.h"
#include "base/files/file_util.h"
#endif
namespace base {
......@@ -93,6 +96,14 @@ class PRINTING_EXPORT MetafilePlayer {
// called after the metafile is closed. Returns true if writing succeeded.
virtual bool SaveTo(base::File* file) const = 0;
#if defined(OS_ANDROID)
// Similar to bool SaveTo(base::File* file) const, but write the data to the
// file descriptor directly. This is because Android doesn't allow file
// ownership exchange. This function should ONLY be called after the metafile
// is closed. Returns true if writing succeeded.
virtual bool SaveToFileDescriptor(int fd) const = 0;
#endif // defined(OS_ANDROID)
private:
DISALLOW_COPY_AND_ASSIGN(MetafilePlayer);
};
......@@ -160,6 +171,9 @@ class PRINTING_EXPORT Metafile : public MetafilePlayer {
// MetfilePlayer
bool GetDataAsVector(std::vector<char>* buffer) const override;
bool SaveTo(base::File* file) const override;
#if defined(OS_ANDROID)
bool SaveToFileDescriptor(int fd) const override;
#endif // defined(OS_ANDROID)
private:
DISALLOW_COPY_AND_ASSIGN(Metafile);
......
......@@ -13,7 +13,6 @@
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/files/file.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
......@@ -159,9 +158,7 @@ void PrintingContextAndroid::ShowSystemDialogDone(
void PrintingContextAndroid::PrintDocument(const MetafilePlayer& metafile) {
DCHECK(is_file_descriptor_valid());
base::File file(fd_);
metafile.SaveTo(&file);
file.TakePlatformFile();
metafile.SaveToFileDescriptor(fd_);
}
PrintingContext::Result PrintingContextAndroid::UseDefaultSettings() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment