Bug 1319423 - Part 2 - Switch away from std::ifstream to PRFileDesc in PrintTranslator; r?jrmuizel draft
authorAlex Gaynor <agaynor@mozilla.com>
Tue, 03 Oct 2017 13:23:57 -0400
changeset 679294 84aa60947af209e3112040b8d0675dc3483e3b2f
parent 679293 0fa1597c91ab04d8a9e1a45c085fef09b4677985
child 679295 ffa3cd0727ec2222e40ee57e20f541e5bc2db129
child 679383 717e9b69aacf00e15fde94db968b13deeb80f9da
push id84187
push userbmo:agaynor@mozilla.com
push dateThu, 12 Oct 2017 14:29:17 +0000
reviewersjrmuizel
bugs1319423
milestone58.0a1
Bug 1319423 - Part 2 - Switch away from std::ifstream to PRFileDesc in PrintTranslator; r?jrmuizel MozReview-Commit-ID: 6ucwbGkqOQ0
gfx/2d/RecordedEvent.cpp
gfx/2d/RecordedEvent.h
layout/printing/DrawEventRecorder.h
layout/printing/PrintTranslator.cpp
layout/printing/PrintTranslator.h
layout/printing/ipc/RemotePrintJobParent.cpp
--- a/gfx/2d/RecordedEvent.cpp
+++ b/gfx/2d/RecordedEvent.cpp
@@ -18,16 +18,21 @@ namespace gfx {
 
 using namespace std;
 
 RecordedEvent *
 RecordedEvent::LoadEventFromStream(std::istream &aStream, EventType aType) {
   return LoadEvent(aStream, aType);
 }
 
+RecordedEvent *
+RecordedEvent::LoadEventFromStream(EventStream& aStream, EventType aType) {
+  return LoadEvent(aStream, aType);
+}
+
 string
 RecordedEvent::GetEventName(EventType aType)
 {
   switch (aType) {
   case DRAWTARGETCREATION:
     return "DrawTarget Creation";
   case DRAWTARGETDESTRUCTION:
     return "DrawTarget Destruction";
--- a/gfx/2d/RecordedEvent.h
+++ b/gfx/2d/RecordedEvent.h
@@ -205,16 +205,17 @@ struct MemStream {
 
   MemStream() : mData(nullptr), mLength(0), mCapacity(0) {}
   ~MemStream() { free(mData); }
 };
 
 class EventStream {
 public:
   virtual void write(const char* aData, size_t aSize) = 0;
+  virtual void read(char* aOut, size_t aSize) = 0;
 };
 
 class RecordedEvent {
 public:
   enum EventType {
     DRAWTARGETCREATION = 0,
     DRAWTARGETDESTRUCTION,
     FILLRECT,
@@ -293,16 +294,17 @@ public:
 
   virtual ReferencePtr GetDestinedDT() { return nullptr; }
 
   void OutputSimplePatternInfo(const PatternStorage &aStorage, std::stringstream &aOutput) const;
 
   template<class S>
   static RecordedEvent *LoadEvent(S &aStream, EventType aType);
   static RecordedEvent *LoadEventFromStream(std::istream &aStream, EventType aType);
+  static RecordedEvent *LoadEventFromStream(EventStream& aStream, EventType aType);
 
   // An alternative to LoadEvent that avoids a heap allocation for the event.
   // This accepts a callable `f' that will take a RecordedEvent* as a single parameter
   template<class S, class F>
   static bool DoWithEvent(S &aStream, EventType aType, F f);
 
   EventType GetType() const { return (EventType)mType; }
 protected:
--- a/layout/printing/DrawEventRecorder.h
+++ b/layout/printing/DrawEventRecorder.h
@@ -10,21 +10,22 @@
 #include "mozilla/gfx/RecordingTypes.h"
 #include "prio.h"
 
 namespace mozilla {
 namespace layout {
 
 class PRFileDescStream : public mozilla::gfx::EventStream {
 public:
-  PRFileDescStream() : mFd(nullptr) {}
+  PRFileDescStream() : mFd(nullptr), mGood(true) {}
 
   void Open(const char* aFilename) {
     MOZ_ASSERT(!IsOpen());
     mFd = PR_Open(aFilename, PR_RDWR | PR_CREATE_FILE, PR_IRUSR | PR_IWUSR);
+    mGood = true;
   }
 
   void Close() {
     PR_Close(mFd);
     mFd = nullptr;
   }
 
   bool IsOpen() {
@@ -41,18 +42,28 @@ public:
 
   void write(const char* aData, size_t aSize) {
     // See comment in Flush().
     if (IsOpen()) {
       PR_Write(mFd, static_cast<const void*>(aData), aSize);
     }
   }
 
+  void read(char* aOut, size_t aSize) {
+    PRInt32 res = PR_Read(mFd, static_cast<void*>(aOut), aSize);
+    mGood = res >= 0 && ((size_t)res == aSize);
+  }
+
+  bool good() {
+    return mGood;
+  }
+
 private:
   PRFileDesc* mFd;
+  bool mGood;
 };
 
 class DrawEventRecorderPRFileDesc : public gfx::DrawEventRecorderPrivate
 {
 public:
   MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawEventRecorderPRFileDesc, override)
   explicit DrawEventRecorderPRFileDesc(const char* aFilename);
   ~DrawEventRecorderPRFileDesc();
@@ -80,11 +91,12 @@ public:
 
 private:
   void Flush() override;
 
   PRFileDescStream mOutputStream;
 };
 
 }
+
 }
 
 #endif /* mozilla_layout_printing_DrawEventRecorder_h */
--- a/layout/printing/PrintTranslator.cpp
+++ b/layout/printing/PrintTranslator.cpp
@@ -20,17 +20,17 @@ namespace layout {
 PrintTranslator::PrintTranslator(nsDeviceContext* aDeviceContext)
   : mDeviceContext(aDeviceContext)
 {
   RefPtr<gfxContext> context = mDeviceContext->CreateReferenceRenderingContext();
   mBaseDT = context->GetDrawTarget();
 }
 
 bool
-PrintTranslator::TranslateRecording(std::istream& aRecording)
+PrintTranslator::TranslateRecording(PRFileDescStream& aRecording)
 {
   uint32_t magicInt;
   ReadElement(aRecording, magicInt);
   if (magicInt != mozilla::gfx::kMagicInt) {
     return false;
   }
 
   uint16_t majorRevision;
--- a/layout/printing/PrintTranslator.h
+++ b/layout/printing/PrintTranslator.h
@@ -30,17 +30,17 @@ using gfx::ScaledFont;
 using gfx::UnscaledFont;
 using gfx::NativeFontResource;
 
 class PrintTranslator final : public Translator
 {
 public:
   explicit PrintTranslator(nsDeviceContext* aDeviceContext);
 
-  bool TranslateRecording(std::istream& aRecording);
+  bool TranslateRecording(PRFileDescStream& aRecording);
 
   DrawTarget* LookupDrawTarget(ReferencePtr aRefPtr) final
   {
     DrawTarget* result = mDrawTargets.GetWeak(aRefPtr);
     MOZ_ASSERT(result);
     return result;
   }
 
--- a/layout/printing/ipc/RemotePrintJobParent.cpp
+++ b/layout/printing/ipc/RemotePrintJobParent.cpp
@@ -119,27 +119,29 @@ RemotePrintJobParent::PrintPage(const ns
   }
 
   nsAutoCString recordingPath;
   rv = recordingFile->GetNativePath(recordingPath);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  std::ifstream recording(recordingPath.get(), std::ifstream::binary);
+  PRFileDescStream recording;
+  recording.Open(recordingPath.get());
+  MOZ_ASSERT(recording.IsOpen());
   if (!mPrintTranslator->TranslateRecording(recording)) {
     return NS_ERROR_FAILURE;
   }
 
   rv = mPrintDeviceContext->EndPage();
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
-  recording.close();
+  recording.Close();
   rv = recordingFile->Remove(/* recursive= */ false);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
@@ -235,10 +237,8 @@ RemotePrintJobParent::~RemotePrintJobPar
 
 void
 RemotePrintJobParent::ActorDestroy(ActorDestroyReason aWhy)
 {
 }
 
 } // namespace layout
 } // namespace mozilla
-
-