layout/forms/nsFileControlFrame.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsFileControlFrame.h"
michael@0 7
michael@0 8 #include "nsGkAtoms.h"
michael@0 9 #include "nsCOMPtr.h"
michael@0 10 #include "nsIDocument.h"
michael@0 11 #include "nsINodeInfo.h"
michael@0 12 #include "mozilla/dom/Element.h"
michael@0 13 #include "mozilla/dom/DataTransfer.h"
michael@0 14 #include "mozilla/dom/HTMLButtonElement.h"
michael@0 15 #include "mozilla/dom/HTMLInputElement.h"
michael@0 16 #include "nsNodeInfoManager.h"
michael@0 17 #include "nsContentCreatorFunctions.h"
michael@0 18 #include "nsContentUtils.h"
michael@0 19 #include "mozilla/EventStates.h"
michael@0 20 #include "mozilla/dom/DOMStringList.h"
michael@0 21 #include "nsIDOMDragEvent.h"
michael@0 22 #include "nsIDOMFileList.h"
michael@0 23 #include "nsContentList.h"
michael@0 24 #include "nsIDOMMutationEvent.h"
michael@0 25 #include "nsTextNode.h"
michael@0 26
michael@0 27 using namespace mozilla;
michael@0 28 using namespace mozilla::dom;
michael@0 29
michael@0 30 nsIFrame*
michael@0 31 NS_NewFileControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
michael@0 32 {
michael@0 33 return new (aPresShell) nsFileControlFrame(aContext);
michael@0 34 }
michael@0 35
michael@0 36 NS_IMPL_FRAMEARENA_HELPERS(nsFileControlFrame)
michael@0 37
michael@0 38 nsFileControlFrame::nsFileControlFrame(nsStyleContext* aContext)
michael@0 39 : nsBlockFrame(aContext)
michael@0 40 {
michael@0 41 AddStateBits(NS_BLOCK_FLOAT_MGR);
michael@0 42 }
michael@0 43
michael@0 44
michael@0 45 void
michael@0 46 nsFileControlFrame::Init(nsIContent* aContent,
michael@0 47 nsIFrame* aParent,
michael@0 48 nsIFrame* aPrevInFlow)
michael@0 49 {
michael@0 50 nsBlockFrame::Init(aContent, aParent, aPrevInFlow);
michael@0 51
michael@0 52 mMouseListener = new DnDListener(this);
michael@0 53 }
michael@0 54
michael@0 55 void
michael@0 56 nsFileControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
michael@0 57 {
michael@0 58 ENSURE_TRUE(mContent);
michael@0 59
michael@0 60 // Remove the events.
michael@0 61 if (mContent) {
michael@0 62 mContent->RemoveSystemEventListener(NS_LITERAL_STRING("drop"),
michael@0 63 mMouseListener, false);
michael@0 64 mContent->RemoveSystemEventListener(NS_LITERAL_STRING("dragover"),
michael@0 65 mMouseListener, false);
michael@0 66 }
michael@0 67
michael@0 68 nsContentUtils::DestroyAnonymousContent(&mTextContent);
michael@0 69 nsContentUtils::DestroyAnonymousContent(&mBrowse);
michael@0 70
michael@0 71 mMouseListener->ForgetFrame();
michael@0 72 nsBlockFrame::DestroyFrom(aDestructRoot);
michael@0 73 }
michael@0 74
michael@0 75 nsresult
michael@0 76 nsFileControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
michael@0 77 {
michael@0 78 nsCOMPtr<nsIDocument> doc = mContent->GetDocument();
michael@0 79
michael@0 80 // Create and setup the file picking button.
michael@0 81 mBrowse = doc->CreateHTMLElement(nsGkAtoms::button);
michael@0 82 // NOTE: SetIsNativeAnonymousRoot() has to be called before setting any
michael@0 83 // attribute.
michael@0 84 mBrowse->SetIsNativeAnonymousRoot();
michael@0 85 mBrowse->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
michael@0 86 NS_LITERAL_STRING("button"), false);
michael@0 87
michael@0 88 // Set the file picking button text depending on the current locale.
michael@0 89 nsXPIDLString buttonTxt;
michael@0 90 nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
michael@0 91 "Browse", buttonTxt);
michael@0 92
michael@0 93 // Set the browse button text. It's a bit of a pain to do because we want to
michael@0 94 // make sure we are not notifying.
michael@0 95 nsRefPtr<nsTextNode> textContent =
michael@0 96 new nsTextNode(mBrowse->NodeInfo()->NodeInfoManager());
michael@0 97
michael@0 98 textContent->SetText(buttonTxt, false);
michael@0 99
michael@0 100 nsresult rv = mBrowse->AppendChildTo(textContent, false);
michael@0 101 NS_ENSURE_SUCCESS(rv, rv);
michael@0 102
michael@0 103 // Make sure access key and tab order for the element actually redirect to the
michael@0 104 // file picking button.
michael@0 105 nsRefPtr<HTMLInputElement> fileContent = HTMLInputElement::FromContentOrNull(mContent);
michael@0 106 nsRefPtr<HTMLButtonElement> browseControl = HTMLButtonElement::FromContentOrNull(mBrowse);
michael@0 107
michael@0 108 nsAutoString accessKey;
michael@0 109 fileContent->GetAccessKey(accessKey);
michael@0 110 browseControl->SetAccessKey(accessKey);
michael@0 111
michael@0 112 int32_t tabIndex;
michael@0 113 fileContent->GetTabIndex(&tabIndex);
michael@0 114 browseControl->SetTabIndex(tabIndex);
michael@0 115
michael@0 116 if (!aElements.AppendElement(mBrowse)) {
michael@0 117 return NS_ERROR_OUT_OF_MEMORY;
michael@0 118 }
michael@0 119
michael@0 120 // Create and setup the text showing the selected files.
michael@0 121 nsCOMPtr<nsINodeInfo> nodeInfo;
michael@0 122 nodeInfo = doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::label, nullptr,
michael@0 123 kNameSpaceID_XUL,
michael@0 124 nsIDOMNode::ELEMENT_NODE);
michael@0 125 NS_TrustedNewXULElement(getter_AddRefs(mTextContent), nodeInfo.forget());
michael@0 126 // NOTE: SetIsNativeAnonymousRoot() has to be called before setting any
michael@0 127 // attribute.
michael@0 128 mTextContent->SetIsNativeAnonymousRoot();
michael@0 129 mTextContent->SetAttr(kNameSpaceID_None, nsGkAtoms::crop,
michael@0 130 NS_LITERAL_STRING("center"), false);
michael@0 131
michael@0 132 // Update the displayed text to reflect the current element's value.
michael@0 133 nsAutoString value;
michael@0 134 HTMLInputElement::FromContent(mContent)->GetDisplayFileName(value);
michael@0 135 UpdateDisplayedValue(value, false);
michael@0 136
michael@0 137 if (!aElements.AppendElement(mTextContent)) {
michael@0 138 return NS_ERROR_OUT_OF_MEMORY;
michael@0 139 }
michael@0 140
michael@0 141 // We should be able to interact with the element by doing drag and drop.
michael@0 142 mContent->AddSystemEventListener(NS_LITERAL_STRING("drop"),
michael@0 143 mMouseListener, false);
michael@0 144 mContent->AddSystemEventListener(NS_LITERAL_STRING("dragover"),
michael@0 145 mMouseListener, false);
michael@0 146
michael@0 147 SyncDisabledState();
michael@0 148
michael@0 149 return NS_OK;
michael@0 150 }
michael@0 151
michael@0 152 void
michael@0 153 nsFileControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
michael@0 154 uint32_t aFilter)
michael@0 155 {
michael@0 156 aElements.MaybeAppendElement(mBrowse);
michael@0 157 aElements.MaybeAppendElement(mTextContent);
michael@0 158 }
michael@0 159
michael@0 160 NS_QUERYFRAME_HEAD(nsFileControlFrame)
michael@0 161 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
michael@0 162 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
michael@0 163 NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
michael@0 164
michael@0 165 void
michael@0 166 nsFileControlFrame::SetFocus(bool aOn, bool aRepaint)
michael@0 167 {
michael@0 168 }
michael@0 169
michael@0 170 /**
michael@0 171 * This is called when we receive a drop or a dragover.
michael@0 172 */
michael@0 173 NS_IMETHODIMP
michael@0 174 nsFileControlFrame::DnDListener::HandleEvent(nsIDOMEvent* aEvent)
michael@0 175 {
michael@0 176 NS_ASSERTION(mFrame, "We should have been unregistered");
michael@0 177
michael@0 178 bool defaultPrevented = false;
michael@0 179 aEvent->GetDefaultPrevented(&defaultPrevented);
michael@0 180 if (defaultPrevented) {
michael@0 181 return NS_OK;
michael@0 182 }
michael@0 183
michael@0 184 nsCOMPtr<nsIDOMDragEvent> dragEvent = do_QueryInterface(aEvent);
michael@0 185 if (!dragEvent || !IsValidDropData(dragEvent)) {
michael@0 186 return NS_OK;
michael@0 187 }
michael@0 188
michael@0 189 nsAutoString eventType;
michael@0 190 aEvent->GetType(eventType);
michael@0 191 if (eventType.EqualsLiteral("dragover")) {
michael@0 192 // Prevent default if we can accept this drag data
michael@0 193 aEvent->PreventDefault();
michael@0 194 return NS_OK;
michael@0 195 }
michael@0 196
michael@0 197 if (eventType.EqualsLiteral("drop")) {
michael@0 198 aEvent->StopPropagation();
michael@0 199 aEvent->PreventDefault();
michael@0 200
michael@0 201 nsIContent* content = mFrame->GetContent();
michael@0 202 NS_ASSERTION(content, "The frame has no content???");
michael@0 203
michael@0 204 HTMLInputElement* inputElement = HTMLInputElement::FromContent(content);
michael@0 205 NS_ASSERTION(inputElement, "No input element for this file upload control frame!");
michael@0 206
michael@0 207 nsCOMPtr<nsIDOMDataTransfer> dataTransfer;
michael@0 208 dragEvent->GetDataTransfer(getter_AddRefs(dataTransfer));
michael@0 209
michael@0 210 nsCOMPtr<nsIDOMFileList> fileList;
michael@0 211 dataTransfer->GetFiles(getter_AddRefs(fileList));
michael@0 212
michael@0 213 inputElement->SetFiles(fileList, true);
michael@0 214 nsContentUtils::DispatchTrustedEvent(content->OwnerDoc(), content,
michael@0 215 NS_LITERAL_STRING("change"), true,
michael@0 216 false);
michael@0 217 }
michael@0 218
michael@0 219 return NS_OK;
michael@0 220 }
michael@0 221
michael@0 222 /* static */ bool
michael@0 223 nsFileControlFrame::DnDListener::IsValidDropData(nsIDOMDragEvent* aEvent)
michael@0 224 {
michael@0 225 nsCOMPtr<nsIDOMDataTransfer> domDataTransfer;
michael@0 226 aEvent->GetDataTransfer(getter_AddRefs(domDataTransfer));
michael@0 227 nsCOMPtr<DataTransfer> dataTransfer = do_QueryInterface(domDataTransfer);
michael@0 228 NS_ENSURE_TRUE(dataTransfer, false);
michael@0 229
michael@0 230 // We only support dropping files onto a file upload control
michael@0 231 nsRefPtr<DOMStringList> types = dataTransfer->Types();
michael@0 232 return types->Contains(NS_LITERAL_STRING("Files"));
michael@0 233 }
michael@0 234
michael@0 235 nscoord
michael@0 236 nsFileControlFrame::GetMinWidth(nsRenderingContext *aRenderingContext)
michael@0 237 {
michael@0 238 nscoord result;
michael@0 239 DISPLAY_MIN_WIDTH(this, result);
michael@0 240
michael@0 241 // Our min width is our pref width
michael@0 242 result = GetPrefWidth(aRenderingContext);
michael@0 243 return result;
michael@0 244 }
michael@0 245
michael@0 246 void
michael@0 247 nsFileControlFrame::SyncDisabledState()
michael@0 248 {
michael@0 249 EventStates eventStates = mContent->AsElement()->State();
michael@0 250 if (eventStates.HasState(NS_EVENT_STATE_DISABLED)) {
michael@0 251 mBrowse->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled, EmptyString(),
michael@0 252 true);
michael@0 253 } else {
michael@0 254 mBrowse->UnsetAttr(kNameSpaceID_None, nsGkAtoms::disabled, true);
michael@0 255 }
michael@0 256 }
michael@0 257
michael@0 258 nsresult
michael@0 259 nsFileControlFrame::AttributeChanged(int32_t aNameSpaceID,
michael@0 260 nsIAtom* aAttribute,
michael@0 261 int32_t aModType)
michael@0 262 {
michael@0 263 if (aNameSpaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::tabindex) {
michael@0 264 if (aModType == nsIDOMMutationEvent::REMOVAL) {
michael@0 265 mBrowse->UnsetAttr(aNameSpaceID, aAttribute, true);
michael@0 266 } else {
michael@0 267 nsAutoString value;
michael@0 268 mContent->GetAttr(aNameSpaceID, aAttribute, value);
michael@0 269 mBrowse->SetAttr(aNameSpaceID, aAttribute, value, true);
michael@0 270 }
michael@0 271 }
michael@0 272
michael@0 273 return nsBlockFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
michael@0 274 }
michael@0 275
michael@0 276 void
michael@0 277 nsFileControlFrame::ContentStatesChanged(EventStates aStates)
michael@0 278 {
michael@0 279 if (aStates.HasState(NS_EVENT_STATE_DISABLED)) {
michael@0 280 nsContentUtils::AddScriptRunner(new SyncDisabledStateEvent(this));
michael@0 281 }
michael@0 282 }
michael@0 283
michael@0 284 #ifdef DEBUG_FRAME_DUMP
michael@0 285 nsresult
michael@0 286 nsFileControlFrame::GetFrameName(nsAString& aResult) const
michael@0 287 {
michael@0 288 return MakeFrameName(NS_LITERAL_STRING("FileControl"), aResult);
michael@0 289 }
michael@0 290 #endif
michael@0 291
michael@0 292 void
michael@0 293 nsFileControlFrame::UpdateDisplayedValue(const nsAString& aValue, bool aNotify)
michael@0 294 {
michael@0 295 mTextContent->SetAttr(kNameSpaceID_None, nsGkAtoms::value, aValue, aNotify);
michael@0 296 }
michael@0 297
michael@0 298 nsresult
michael@0 299 nsFileControlFrame::SetFormProperty(nsIAtom* aName,
michael@0 300 const nsAString& aValue)
michael@0 301 {
michael@0 302 if (nsGkAtoms::value == aName) {
michael@0 303 UpdateDisplayedValue(aValue, true);
michael@0 304 }
michael@0 305 return NS_OK;
michael@0 306 }
michael@0 307
michael@0 308 void
michael@0 309 nsFileControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
michael@0 310 const nsRect& aDirtyRect,
michael@0 311 const nsDisplayListSet& aLists)
michael@0 312 {
michael@0 313 BuildDisplayListForInline(aBuilder, aDirtyRect, aLists);
michael@0 314 }
michael@0 315
michael@0 316 #ifdef ACCESSIBILITY
michael@0 317 a11y::AccType
michael@0 318 nsFileControlFrame::AccessibleType()
michael@0 319 {
michael@0 320 return a11y::eHTMLFileInputType;
michael@0 321 }
michael@0 322 #endif
michael@0 323
michael@0 324 ////////////////////////////////////////////////////////////
michael@0 325 // Mouse listener implementation
michael@0 326
michael@0 327 NS_IMPL_ISUPPORTS(nsFileControlFrame::MouseListener,
michael@0 328 nsIDOMEventListener)

mercurial