Bug 1401069 - Part 2. Implement ReleaseAllResource to make WindowsEMF::InitForDrawing reusable. draft
authorcku <cku@mozilla.com>
Mon, 18 Sep 2017 23:16:08 +0800
changeset 667472 2d8ccf84a002cb0d74ac1a9465adb75d7cc5db18
parent 667471 fa8b36fa1ebbc136c4cf490d80d297c12cf152c2
child 667473 749b4f86c6de51fca2652e3571bf8682ab65956a
push id80721
push usercku@mozilla.com
push dateWed, 20 Sep 2017 04:22:29 +0000
bugs1401069
milestone57.0a1
Bug 1401069 - Part 2. Implement ReleaseAllResource to make WindowsEMF::InitForDrawing reusable. Currently, WindowsEMF::InitForDrawing can be used once. With the change in this patch, we can call WindowsEMF::InitForDrawing and Playback/SaveToFile in pair as many times as we want. MozReview-Commit-ID: 4fbY4Q6i9v5
widget/windows/WindowsEMF.cpp
widget/windows/WindowsEMF.h
--- a/widget/windows/WindowsEMF.cpp
+++ b/widget/windows/WindowsEMF.cpp
@@ -12,36 +12,33 @@ namespace widget {
 WindowsEMF::WindowsEMF()
   : mDC(nullptr)
   , mEmf(nullptr)
 {
 }
 
 WindowsEMF::~WindowsEMF()
 {
-  FinishDocument();
-  ReleaseEMFHandle();
+  ReleaseAllResource();
 }
 
 bool
 WindowsEMF::InitForDrawing(const wchar_t* aMetafilePath /* = nullptr */)
 {
-  MOZ_ASSERT(!mDC && !mEmf, "InitForDrawing and InitFromFileContents is"
-                            " designed to be used either one at once.");
+  ReleaseAllResource();
 
   mDC = ::CreateEnhMetaFile(nullptr, aMetafilePath, nullptr, nullptr);
   return !!mDC;
 }
 
 bool
 WindowsEMF::InitFromFileContents(const wchar_t* aMetafilePath)
 {
   MOZ_ASSERT(aMetafilePath);
-  MOZ_ASSERT(!mDC && !mEmf, "InitForDrawing and InitFromFileContents is"
-                            " designed to be used either one at once.");
+  ReleaseAllResource();
 
   mEmf = ::GetEnhMetaFileW(aMetafilePath);
   return !!mEmf;
 }
 
 bool
 WindowsEMF::FinishDocument()
 {
@@ -56,16 +53,23 @@ void
 WindowsEMF::ReleaseEMFHandle()
 {
   if (mEmf) {
     ::DeleteEnhMetaFile(mEmf);
     mEmf = nullptr;
   }
 }
 
+void
+WindowsEMF::ReleaseAllResource()
+{
+  FinishDocument();
+  ReleaseEMFHandle();
+}
+
 bool
 WindowsEMF::Playback(HDC aDeviceContext, const RECT* aRect)
 {
   MOZ_ASSERT(aRect);
   if (!FinishDocument()) {
     return false;
   }
 
--- a/widget/windows/WindowsEMF.h
+++ b/widget/windows/WindowsEMF.h
@@ -64,16 +64,17 @@ public:
    */
   bool SaveToFile();
 
 private:
 
   WindowsEMF(const WindowsEMF& aEMF) = delete;
   bool FinishDocument();
   void ReleaseEMFHandle();
+  void ReleaseAllResource();
 
   /* Compiled EMF data handle. */
   HENHMETAFILE mEmf;
   HDC mDC;
 };
 
 } // namespace widget
 } // namespace mozilla