content/base/src/nsGenConImageContent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsGenConImageContent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/**
    1.10 + * A fake content node class so that the image frame for
    1.11 + *   content: url(foo);
    1.12 + * in CSS can have an nsIImageLoadingContent but use an
    1.13 + * imgIRequest that's already been loaded from the style system.
    1.14 + */
    1.15 +
    1.16 +#include "nsContentCreatorFunctions.h"
    1.17 +#include "nsXMLElement.h"
    1.18 +#include "nsImageLoadingContent.h"
    1.19 +#include "imgIRequest.h"
    1.20 +#include "mozilla/BasicEvents.h"
    1.21 +#include "mozilla/EventDispatcher.h"
    1.22 +#include "mozilla/EventStates.h"
    1.23 +
    1.24 +using namespace mozilla;
    1.25 +
    1.26 +class nsGenConImageContent MOZ_FINAL : public nsXMLElement,
    1.27 +                                       public nsImageLoadingContent
    1.28 +{
    1.29 +public:
    1.30 +  nsGenConImageContent(already_AddRefed<nsINodeInfo>& aNodeInfo)
    1.31 +    : nsXMLElement(aNodeInfo)
    1.32 +  {
    1.33 +    // nsImageLoadingContent starts out broken, so we start out
    1.34 +    // suppressed to match it.
    1.35 +    AddStatesSilently(NS_EVENT_STATE_SUPPRESSED);
    1.36 +  }
    1.37 +
    1.38 +  nsresult Init(imgRequestProxy* aImageRequest)
    1.39 +  {
    1.40 +    // No need to notify, since we have no frame.
    1.41 +    return UseAsPrimaryRequest(aImageRequest, false);
    1.42 +  }
    1.43 +
    1.44 +  // nsIContent overrides
    1.45 +  virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
    1.46 +                              nsIContent* aBindingParent,
    1.47 +                              bool aCompileEventHandlers);
    1.48 +  virtual void UnbindFromTree(bool aDeep, bool aNullParent);
    1.49 +  virtual EventStates IntrinsicState() const;
    1.50 +
    1.51 +  virtual nsresult PreHandleEvent(EventChainPreVisitor& aVisitor)
    1.52 +  {
    1.53 +    MOZ_ASSERT(IsInNativeAnonymousSubtree());
    1.54 +    if (aVisitor.mEvent->message == NS_LOAD ||
    1.55 +        aVisitor.mEvent->message == NS_LOAD_ERROR) {
    1.56 +      // Don't propagate the events to the parent.
    1.57 +      return NS_OK;
    1.58 +    }
    1.59 +    return nsXMLElement::PreHandleEvent(aVisitor);
    1.60 +  }
    1.61 +  
    1.62 +private:
    1.63 +  virtual ~nsGenConImageContent();
    1.64 +
    1.65 +public:
    1.66 +  NS_DECL_ISUPPORTS_INHERITED
    1.67 +};
    1.68 +
    1.69 +NS_IMPL_ISUPPORTS_INHERITED(nsGenConImageContent,
    1.70 +                            nsXMLElement,
    1.71 +                            nsIImageLoadingContent,
    1.72 +                            imgINotificationObserver,
    1.73 +                            imgIOnloadBlocker)
    1.74 +
    1.75 +nsresult
    1.76 +NS_NewGenConImageContent(nsIContent** aResult, already_AddRefed<nsINodeInfo>&& aNodeInfo,
    1.77 +                         imgRequestProxy* aImageRequest)
    1.78 +{
    1.79 +  NS_PRECONDITION(aImageRequest, "Must have request!");
    1.80 +  nsGenConImageContent *it = new nsGenConImageContent(aNodeInfo);
    1.81 +  if (!it)
    1.82 +    return NS_ERROR_OUT_OF_MEMORY;
    1.83 +  NS_ADDREF(*aResult = it);
    1.84 +  nsresult rv = it->Init(aImageRequest);
    1.85 +  if (NS_FAILED(rv))
    1.86 +    NS_RELEASE(*aResult);
    1.87 +  return rv;
    1.88 +}
    1.89 +
    1.90 +nsGenConImageContent::~nsGenConImageContent()
    1.91 +{
    1.92 +  DestroyImageLoadingContent();
    1.93 +}
    1.94 +
    1.95 +nsresult
    1.96 +nsGenConImageContent::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
    1.97 +                                 nsIContent* aBindingParent,
    1.98 +                                 bool aCompileEventHandlers)
    1.99 +{
   1.100 +  nsresult rv;
   1.101 +  rv = nsXMLElement::BindToTree(aDocument, aParent, aBindingParent,
   1.102 +                                aCompileEventHandlers);
   1.103 +  NS_ENSURE_SUCCESS(rv, rv);
   1.104 +
   1.105 +  nsImageLoadingContent::BindToTree(aDocument, aParent, aBindingParent,
   1.106 +                                    aCompileEventHandlers);
   1.107 +  return NS_OK;
   1.108 +}
   1.109 +
   1.110 +void
   1.111 +nsGenConImageContent::UnbindFromTree(bool aDeep, bool aNullParent)
   1.112 +{
   1.113 +  nsImageLoadingContent::UnbindFromTree(aDeep, aNullParent);
   1.114 +  nsXMLElement::UnbindFromTree(aDeep, aNullParent);
   1.115 +}
   1.116 +
   1.117 +EventStates
   1.118 +nsGenConImageContent::IntrinsicState() const
   1.119 +{
   1.120 +  EventStates state = nsXMLElement::IntrinsicState();
   1.121 +
   1.122 +  EventStates imageState = nsImageLoadingContent::ImageState();
   1.123 +  if (imageState.HasAtLeastOneOfStates(NS_EVENT_STATE_BROKEN | NS_EVENT_STATE_USERDISABLED)) {
   1.124 +    // We should never be in an error state; if the image fails to load, we
   1.125 +    // just go to the suppressed state.
   1.126 +    imageState |= NS_EVENT_STATE_SUPPRESSED;
   1.127 +    imageState &= ~NS_EVENT_STATE_BROKEN;
   1.128 +  }
   1.129 +  imageState &= ~NS_EVENT_STATE_LOADING;
   1.130 +  return state | imageState;
   1.131 +}

mercurial