|
1 |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /** |
|
7 * A fake content node class so that the image frame for |
|
8 * content: url(foo); |
|
9 * in CSS can have an nsIImageLoadingContent but use an |
|
10 * imgIRequest that's already been loaded from the style system. |
|
11 */ |
|
12 |
|
13 #include "nsContentCreatorFunctions.h" |
|
14 #include "nsXMLElement.h" |
|
15 #include "nsImageLoadingContent.h" |
|
16 #include "imgIRequest.h" |
|
17 #include "mozilla/BasicEvents.h" |
|
18 #include "mozilla/EventDispatcher.h" |
|
19 #include "mozilla/EventStates.h" |
|
20 |
|
21 using namespace mozilla; |
|
22 |
|
23 class nsGenConImageContent MOZ_FINAL : public nsXMLElement, |
|
24 public nsImageLoadingContent |
|
25 { |
|
26 public: |
|
27 nsGenConImageContent(already_AddRefed<nsINodeInfo>& aNodeInfo) |
|
28 : nsXMLElement(aNodeInfo) |
|
29 { |
|
30 // nsImageLoadingContent starts out broken, so we start out |
|
31 // suppressed to match it. |
|
32 AddStatesSilently(NS_EVENT_STATE_SUPPRESSED); |
|
33 } |
|
34 |
|
35 nsresult Init(imgRequestProxy* aImageRequest) |
|
36 { |
|
37 // No need to notify, since we have no frame. |
|
38 return UseAsPrimaryRequest(aImageRequest, false); |
|
39 } |
|
40 |
|
41 // nsIContent overrides |
|
42 virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
|
43 nsIContent* aBindingParent, |
|
44 bool aCompileEventHandlers); |
|
45 virtual void UnbindFromTree(bool aDeep, bool aNullParent); |
|
46 virtual EventStates IntrinsicState() const; |
|
47 |
|
48 virtual nsresult PreHandleEvent(EventChainPreVisitor& aVisitor) |
|
49 { |
|
50 MOZ_ASSERT(IsInNativeAnonymousSubtree()); |
|
51 if (aVisitor.mEvent->message == NS_LOAD || |
|
52 aVisitor.mEvent->message == NS_LOAD_ERROR) { |
|
53 // Don't propagate the events to the parent. |
|
54 return NS_OK; |
|
55 } |
|
56 return nsXMLElement::PreHandleEvent(aVisitor); |
|
57 } |
|
58 |
|
59 private: |
|
60 virtual ~nsGenConImageContent(); |
|
61 |
|
62 public: |
|
63 NS_DECL_ISUPPORTS_INHERITED |
|
64 }; |
|
65 |
|
66 NS_IMPL_ISUPPORTS_INHERITED(nsGenConImageContent, |
|
67 nsXMLElement, |
|
68 nsIImageLoadingContent, |
|
69 imgINotificationObserver, |
|
70 imgIOnloadBlocker) |
|
71 |
|
72 nsresult |
|
73 NS_NewGenConImageContent(nsIContent** aResult, already_AddRefed<nsINodeInfo>&& aNodeInfo, |
|
74 imgRequestProxy* aImageRequest) |
|
75 { |
|
76 NS_PRECONDITION(aImageRequest, "Must have request!"); |
|
77 nsGenConImageContent *it = new nsGenConImageContent(aNodeInfo); |
|
78 if (!it) |
|
79 return NS_ERROR_OUT_OF_MEMORY; |
|
80 NS_ADDREF(*aResult = it); |
|
81 nsresult rv = it->Init(aImageRequest); |
|
82 if (NS_FAILED(rv)) |
|
83 NS_RELEASE(*aResult); |
|
84 return rv; |
|
85 } |
|
86 |
|
87 nsGenConImageContent::~nsGenConImageContent() |
|
88 { |
|
89 DestroyImageLoadingContent(); |
|
90 } |
|
91 |
|
92 nsresult |
|
93 nsGenConImageContent::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
|
94 nsIContent* aBindingParent, |
|
95 bool aCompileEventHandlers) |
|
96 { |
|
97 nsresult rv; |
|
98 rv = nsXMLElement::BindToTree(aDocument, aParent, aBindingParent, |
|
99 aCompileEventHandlers); |
|
100 NS_ENSURE_SUCCESS(rv, rv); |
|
101 |
|
102 nsImageLoadingContent::BindToTree(aDocument, aParent, aBindingParent, |
|
103 aCompileEventHandlers); |
|
104 return NS_OK; |
|
105 } |
|
106 |
|
107 void |
|
108 nsGenConImageContent::UnbindFromTree(bool aDeep, bool aNullParent) |
|
109 { |
|
110 nsImageLoadingContent::UnbindFromTree(aDeep, aNullParent); |
|
111 nsXMLElement::UnbindFromTree(aDeep, aNullParent); |
|
112 } |
|
113 |
|
114 EventStates |
|
115 nsGenConImageContent::IntrinsicState() const |
|
116 { |
|
117 EventStates state = nsXMLElement::IntrinsicState(); |
|
118 |
|
119 EventStates imageState = nsImageLoadingContent::ImageState(); |
|
120 if (imageState.HasAtLeastOneOfStates(NS_EVENT_STATE_BROKEN | NS_EVENT_STATE_USERDISABLED)) { |
|
121 // We should never be in an error state; if the image fails to load, we |
|
122 // just go to the suppressed state. |
|
123 imageState |= NS_EVENT_STATE_SUPPRESSED; |
|
124 imageState &= ~NS_EVENT_STATE_BROKEN; |
|
125 } |
|
126 imageState &= ~NS_EVENT_STATE_LOADING; |
|
127 return state | imageState; |
|
128 } |