Ticket #15640: 0001-fixup-Bug-6253-Add-canvas-image-extraction-prompt.patch

File 0001-fixup-Bug-6253-Add-canvas-image-extraction-prompt.patch, 6.2 KB (added by mcs, 3 years ago)

proposed fix

  • dom/html/HTMLCanvasElement.cpp

    From 7c30528bca2a9893d4d7f63beaa01044ef171079 Mon Sep 17 00:00:00 2001
    From: Kathy Brade <brade@pearlcrescent.com>
    Date: Mon, 28 Mar 2016 17:04:07 -0400
    Subject: [PATCH] fixup! Bug #6253: Add canvas image extraction prompt.
    
    Place Canvas MediaStream behind site permission prompt (#15640).
    ---
     dom/html/HTMLCanvasElement.cpp | 46 ++++++++++++++++++++++++++++++++----------
     dom/html/HTMLCanvasElement.h   |  5 ++++-
     2 files changed, 39 insertions(+), 12 deletions(-)
    
    diff --git a/dom/html/HTMLCanvasElement.cpp b/dom/html/HTMLCanvasElement.cpp
    index 314bf48..dd74d9f 100644
    a b class RequestedFrameRefreshObserver : public nsARefreshObserver 
    5959
    6060public:
    6161  RequestedFrameRefreshObserver(HTMLCanvasElement* const aOwningElement,
    62                                 nsRefreshDriver* aRefreshDriver)
     62                                nsRefreshDriver* aRefreshDriver,
     63                                bool aReturnPlaceholderData)
    6364    : mRegistered(false),
     65      mReturnPlaceholderData(aReturnPlaceholderData),
    6466      mOwningElement(aOwningElement),
    6567      mRefreshDriver(aRefreshDriver)
    6668  {
    public: 
    6870  }
    6971
    7072  static already_AddRefed<DataSourceSurface>
    71   CopySurface(const RefPtr<SourceSurface>& aSurface)
     73  CopySurface(const RefPtr<SourceSurface>& aSurface,
     74              bool aReturnPlaceholderData)
    7275  {
    7376    RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
    7477    if (!data) {
    public: 
    97100    MOZ_ASSERT(data->GetSize() == copy->GetSize());
    98101    MOZ_ASSERT(data->GetFormat() == copy->GetFormat());
    99102
    100     memcpy(write.GetData(), read.GetData(),
    101            write.GetStride() * copy->GetSize().height);
     103    if (aReturnPlaceholderData) {
     104      // If returning placeholder data, fill the frame copy with white pixels.
     105      memset(write.GetData(), 0xFF,
     106             write.GetStride() * copy->GetSize().height);
     107    } else {
     108      memcpy(write.GetData(), read.GetData(),
     109             write.GetStride() * copy->GetSize().height);
     110    }
    102111
    103112    return copy.forget();
    104113  }
    105114
     115  void SetReturnPlaceholderData(bool aReturnPlaceholderData)
     116  {
     117    mReturnPlaceholderData = aReturnPlaceholderData;
     118  }
     119
    106120  void WillRefresh(TimeStamp aTime) override
    107121  {
    108122    MOZ_ASSERT(NS_IsMainThread());
    public: 
    128142      return;
    129143    }
    130144
    131     RefPtr<DataSourceSurface> copy = CopySurface(snapshot);
    132 
     145    RefPtr<DataSourceSurface> copy = CopySurface(snapshot,
     146                                                 mReturnPlaceholderData);
    133147    mOwningElement->SetFrameCapture(copy.forget());
    134148    mOwningElement->MarkContextCleanForFrameCapture();
    135149  }
    private: 
    177191  }
    178192
    179193  bool mRegistered;
     194  bool mReturnPlaceholderData;
    180195  HTMLCanvasElement* const mOwningElement;
    181196  RefPtr<nsRefreshDriver> mRefreshDriver;
    182197};
    HTMLCanvasElement::CaptureStream(const Optional<double>& aFrameRate, 
    682697    return nullptr;
    683698  }
    684699
     700  // Check site-specific permission and display prompt if appropriate.
     701  // If no permission, arrange for the frame capture listener to return
     702  // all-white, opaque image data.
     703  bool usePlaceholder = !CanvasUtils::IsImageExtractionAllowed(OwnerDoc(),
     704                                       nsContentUtils::GetCurrentJSContext());
    685705  stream->CreateOwnDOMTrack(videoTrackId, MediaSegment::VIDEO);
    686   RegisterFrameCaptureListener(stream->FrameCaptureListener());
     706  RegisterFrameCaptureListener(stream->FrameCaptureListener(), usePlaceholder);
    687707  return stream.forget();
    688708}
    689709
    HTMLCanvasElement::ExtractData(JSContext* aCx, 
    693713                               const nsAString& aOptions,
    694714                               nsIInputStream** aStream)
    695715{
    696   // Check site-speciifc permission and display prompt if appropriate.
     716  // Check site-specific permission and display prompt if appropriate.
    697717  // If no permission, return all-white, opaque image data.
    698718  bool usePlaceholder = !CanvasUtils::IsImageExtractionAllowed(OwnerDoc(), aCx);
    699719  return ImageEncoder::ExtractData(aType,
    HTMLCanvasElement::ToBlob(JSContext* aCx, 
    766786  nsCOMPtr<nsIGlobalObject> global = OwnerDoc()->GetScopeObject();
    767787  MOZ_ASSERT(global);
    768788
    769   // Check site-speciifc permission and display prompt if appropriate.
     789  // Check site-specific permission and display prompt if appropriate.
    770790  // If no permission, return all-white, opaque image data.
    771791  bool usePlaceholder = !CanvasUtils::IsImageExtractionAllowed(OwnerDoc(), aCx);
    772792  CanvasRenderingContextHelper::ToBlob(aCx, global, aCallback, aType,
    HTMLCanvasElement::IsContextCleanForFrameCapture() 
    11311151}
    11321152
    11331153void
    1134 HTMLCanvasElement::RegisterFrameCaptureListener(FrameCaptureListener* aListener)
     1154HTMLCanvasElement::RegisterFrameCaptureListener(FrameCaptureListener* aListener,
     1155                                                bool aReturnPlaceholderData)
    11351156{
    11361157  WeakPtr<FrameCaptureListener> listener = aListener;
    11371158
    HTMLCanvasElement::RegisterFrameCaptureListener(FrameCaptureListener* aListener) 
    11581179    MOZ_RELEASE_ASSERT(driver);
    11591180
    11601181    mRequestedFrameRefreshObserver =
    1161       new RequestedFrameRefreshObserver(this, driver);
     1182      new RequestedFrameRefreshObserver(this, driver, aReturnPlaceholderData);
     1183  } else {
     1184    mRequestedFrameRefreshObserver->SetReturnPlaceholderData(
     1185                                                      aReturnPlaceholderData);
    11621186  }
    11631187
    11641188  mRequestedFrameRefreshObserver->Register();
  • dom/html/HTMLCanvasElement.h

    diff --git a/dom/html/HTMLCanvasElement.h b/dom/html/HTMLCanvasElement.h
    index 542f11b..6ed7bbd 100644
    a b public: 
    263263   * The registered FrameCaptureListeners are stored as WeakPtrs, thus it's the
    264264   * caller's responsibility to keep them alive. Once a registered
    265265   * FrameCaptureListener is destroyed it will be automatically deregistered.
     266   * If aReturnPlaceholderData is true, white data is captured instead of the
     267   * actual canvas contents.
    266268   */
    267   void RegisterFrameCaptureListener(FrameCaptureListener* aListener);
     269  void RegisterFrameCaptureListener(FrameCaptureListener* aListener,
     270                                    bool aReturnPlaceholderData);
    268271
    269272  /*
    270273   * Returns true when there is at least one registered FrameCaptureListener