Skip to content

Commit

Permalink
Bug 1438026 - Part 3: Replace nsPresState with the new PresState type…
Browse files Browse the repository at this point in the history
…, r=baku
  • Loading branch information
mystor committed Apr 10, 2018
1 parent 9982630 commit 242c9ce
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 372 deletions.
11 changes: 6 additions & 5 deletions dom/html/HTMLButtonElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "mozilla/TextEvents.h"
#include "nsUnicharUtils.h"
#include "nsLayoutUtils.h"
#include "nsPresState.h"
#include "mozilla/PresState.h"
#include "nsError.h"
#include "nsFocusManager.h"
#include "mozilla/dom/HTMLFormElement.h"
Expand Down Expand Up @@ -448,20 +448,21 @@ HTMLButtonElement::SaveState()
return NS_OK;
}

nsPresState* state = GetPrimaryPresState();
PresState* state = GetPrimaryPresState();
if (state) {
// We do not want to save the real disabled state but the disabled
// attribute.
state->SetDisabled(HasAttr(kNameSpaceID_None, nsGkAtoms::disabled));
state->disabled() = HasAttr(kNameSpaceID_None, nsGkAtoms::disabled);
state->disabledSet() = true;
}

return NS_OK;
}

bool
HTMLButtonElement::RestoreState(nsPresState* aState)
HTMLButtonElement::RestoreState(PresState* aState)
{
if (aState && aState->IsDisabledSet() && !aState->GetDisabled()) {
if (aState && aState->disabledSet() && !aState->disabled()) {
SetDisabled(false, IgnoreErrors());
}

Expand Down
2 changes: 1 addition & 1 deletion dom/html/HTMLButtonElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HTMLButtonElement final : public nsGenericHTMLFormElementWithState,
NS_IMETHOD Reset() override;
NS_IMETHOD SubmitNamesValues(HTMLFormSubmission* aFormSubmission) override;
NS_IMETHOD SaveState() override;
bool RestoreState(nsPresState* aState) override;
bool RestoreState(PresState* aState) override;
virtual bool IsDisabledForEvents(EventMessage aMessage) override;

virtual void FieldSetDisabledChanged(bool aNotify) override;
Expand Down
258 changes: 93 additions & 165 deletions dom/html/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include "nsAttrValueOrString.h"
#include "nsDateTimeControlFrame.h"

#include "nsPresState.h"
#include "mozilla/PresState.h"
#include "nsIDOMEvent.h"
#include "nsIDOMNodeList.h"
#include "nsLinebreakConverter.h" //to strip out carriage returns
Expand Down Expand Up @@ -274,129 +274,6 @@ class DispatchChangeEventCallback final : public GetFilesCallback
RefPtr<HTMLInputElement> mInputElement;
};

class HTMLInputElementState final : public nsISupports
{
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_INPUT_ELEMENT_STATE_IID)
NS_DECL_ISUPPORTS

bool IsCheckedSet()
{
return mCheckedSet;
}

bool GetChecked()
{
return mChecked;
}

void SetChecked(bool aChecked)
{
mChecked = aChecked;
mCheckedSet = true;
}

const nsString& GetValue()
{
return mValue;
}

void SetValue(const nsAString& aValue)
{
mValue = aValue;
}

void
GetFilesOrDirectories(nsPIDOMWindowInner* aWindow,
nsTArray<OwningFileOrDirectory>& aResult) const
{
for (uint32_t i = 0; i < mBlobImplsOrDirectoryPaths.Length(); ++i) {
if (mBlobImplsOrDirectoryPaths[i].mType == BlobImplOrDirectoryPath::eBlobImpl) {
RefPtr<File> file =
File::Create(aWindow,
mBlobImplsOrDirectoryPaths[i].mBlobImpl);
MOZ_ASSERT(file);

OwningFileOrDirectory* element = aResult.AppendElement();
element->SetAsFile() = file;
} else {
MOZ_ASSERT(mBlobImplsOrDirectoryPaths[i].mType == BlobImplOrDirectoryPath::eDirectoryPath);

nsCOMPtr<nsIFile> file;
nsresult rv =
NS_NewLocalFile(mBlobImplsOrDirectoryPaths[i].mDirectoryPath,
true, getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}

RefPtr<Directory> directory = Directory::Create(aWindow, file);
MOZ_ASSERT(directory);

OwningFileOrDirectory* element = aResult.AppendElement();
element->SetAsDirectory() = directory;
}
}
}

void SetFilesOrDirectories(const nsTArray<OwningFileOrDirectory>& aArray)
{
mBlobImplsOrDirectoryPaths.Clear();
for (uint32_t i = 0; i < aArray.Length(); ++i) {
if (aArray[i].IsFile()) {
BlobImplOrDirectoryPath* data = mBlobImplsOrDirectoryPaths.AppendElement();

data->mBlobImpl = aArray[i].GetAsFile()->Impl();
data->mType = BlobImplOrDirectoryPath::eBlobImpl;
} else {
MOZ_ASSERT(aArray[i].IsDirectory());
nsAutoString fullPath;
nsresult rv = aArray[i].GetAsDirectory()->GetFullRealPath(fullPath);
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}

BlobImplOrDirectoryPath* data =
mBlobImplsOrDirectoryPaths.AppendElement();

data->mDirectoryPath = fullPath;
data->mType = BlobImplOrDirectoryPath::eDirectoryPath;
}
}
}

HTMLInputElementState()
: mValue()
, mChecked(false)
, mCheckedSet(false)
{}

protected:
~HTMLInputElementState() {}

nsString mValue;

struct BlobImplOrDirectoryPath
{
RefPtr<BlobImpl> mBlobImpl;
nsString mDirectoryPath;

enum {
eBlobImpl,
eDirectoryPath
} mType;
};

nsTArray<BlobImplOrDirectoryPath> mBlobImplsOrDirectoryPaths;

bool mChecked;
bool mCheckedSet;
};

NS_DEFINE_STATIC_IID_ACCESSOR(HTMLInputElementState, NS_INPUT_ELEMENT_STATE_IID)

NS_IMPL_ISUPPORTS(HTMLInputElementState, HTMLInputElementState)

struct HTMLInputElement::FileData
{
/**
Expand Down Expand Up @@ -6366,11 +6243,31 @@ HTMLInputElement::SubmitNamesValues(HTMLFormSubmission* aFormSubmission)
return aFormSubmission->AddNameValuePair(name, value);
}

static nsTArray<FileContentData>
SaveFileContentData(const nsTArray<OwningFileOrDirectory>& aArray)
{
nsTArray<FileContentData> res(aArray.Length());
for (auto& it : aArray) {
if (it.IsFile()) {
RefPtr<BlobImpl> impl = it.GetAsFile()->Impl();
res.AppendElement(Move(impl));
} else {
MOZ_ASSERT(it.IsDirectory());
nsString fullPath;
nsresult rv = it.GetAsDirectory()->GetFullRealPath(fullPath);
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}
res.AppendElement(Move(fullPath));
}
}
return res;
}

NS_IMETHODIMP
HTMLInputElement::SaveState()
{
nsPresState* state = nullptr;
PresState* state = nullptr;
switch (GetValueMode()) {
case VALUE_MODE_DEFAULT_ON:
if (mCheckedChanged) {
Expand All @@ -6379,9 +6276,7 @@ HTMLInputElement::SaveState()
return NS_OK;
}

RefPtr<HTMLInputElementState> inputState = new HTMLInputElementState();
inputState->SetChecked(mChecked);
state->SetStateProperty(inputState);
state->contentData() = CheckedContentData(mChecked);
}
break;
case VALUE_MODE_FILENAME:
Expand All @@ -6391,9 +6286,8 @@ HTMLInputElement::SaveState()
return NS_OK;
}

RefPtr<HTMLInputElementState> inputState = new HTMLInputElementState();
inputState->SetFilesOrDirectories(mFileData->mFilesOrDirectories);
state->SetStateProperty(inputState);
state->contentData() =
SaveFileContentData(mFileData->mFilesOrDirectories);
}
break;
case VALUE_MODE_VALUE:
Expand All @@ -6411,7 +6305,6 @@ HTMLInputElement::SaveState()
return NS_OK;
}

RefPtr<HTMLInputElementState> inputState = new HTMLInputElementState();
nsAutoString value;
GetValue(value, CallerType::System);

Expand All @@ -6427,8 +6320,7 @@ HTMLInputElement::SaveState()
}
}

inputState->SetValue(value);
state->SetStateProperty(inputState);
state->contentData() = Move(value);
break;
}

Expand All @@ -6439,7 +6331,8 @@ HTMLInputElement::SaveState()
if (state) {
// We do not want to save the real disabled state but the disabled
// attribute.
state->SetDisabled(HasAttr(kNameSpaceID_None, nsGkAtoms::disabled));
state->disabled() = HasAttr(kNameSpaceID_None, nsGkAtoms::disabled);
state->disabledSet() = true;
}
}

Expand Down Expand Up @@ -6615,50 +6508,85 @@ HTMLInputElement::RemoveStates(EventStates aStates)
nsGenericHTMLFormElementWithState::RemoveStates(aStates);
}

static nsTArray<OwningFileOrDirectory>
RestoreFileContentData(nsPIDOMWindowInner* aWindow,
const nsTArray<FileContentData>& aData)
{
nsTArray<OwningFileOrDirectory> res(aData.Length());
for (auto& it : aData) {
if (it.type() == FileContentData::TBlobImplPtr) {
if (!it.get_BlobImplPtr()) {
// Serialization failed, skip this file.
continue;
}

RefPtr<File> file = File::Create(aWindow, it.get_BlobImplPtr());
MOZ_ASSERT(file);

OwningFileOrDirectory* element = res.AppendElement();
element->SetAsFile() = file;
} else {
MOZ_ASSERT(it.type() == FileContentData::TnsString);
nsCOMPtr<nsIFile> file;
nsresult rv = NS_NewLocalFile(it.get_nsString(), true,
getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}

RefPtr<Directory> directory = Directory::Create(aWindow, file);
MOZ_ASSERT(directory);

OwningFileOrDirectory* element = res.AppendElement();
element->SetAsDirectory() = directory;
}
}
return res;
}

bool
HTMLInputElement::RestoreState(nsPresState* aState)
HTMLInputElement::RestoreState(PresState* aState)
{
bool restoredCheckedState = false;

nsCOMPtr<HTMLInputElementState> inputState
(do_QueryInterface(aState->GetStateProperty()));

if (inputState) {
switch (GetValueMode()) {
case VALUE_MODE_DEFAULT_ON:
if (inputState->IsCheckedSet()) {
restoredCheckedState = true;
DoSetChecked(inputState->GetChecked(), true, true);
}
break;
case VALUE_MODE_FILENAME:
{
nsPIDOMWindowInner* window = OwnerDoc()->GetInnerWindow();
if (window) {
nsTArray<OwningFileOrDirectory> array;
inputState->GetFilesOrDirectories(window, array);
const PresContentData& inputState = aState->contentData();

SetFilesOrDirectories(array, true);
}
switch (GetValueMode()) {
case VALUE_MODE_DEFAULT_ON:
if (inputState.type() == PresContentData::TCheckedContentData) {
restoredCheckedState = true;
bool checked = inputState.get_CheckedContentData().checked();
DoSetChecked(checked, true, true);
}
break;
case VALUE_MODE_FILENAME:
if (inputState.type() == PresContentData::TArrayOfFileContentData) {
nsPIDOMWindowInner* window = OwnerDoc()->GetInnerWindow();
if (window) {
nsTArray<OwningFileOrDirectory> array =
RestoreFileContentData(window, inputState);
SetFilesOrDirectories(array, true);
}
}
break;
case VALUE_MODE_VALUE:
case VALUE_MODE_DEFAULT:
if (GetValueMode() == VALUE_MODE_DEFAULT &&
mType != NS_FORM_INPUT_HIDDEN) {
break;
case VALUE_MODE_VALUE:
case VALUE_MODE_DEFAULT:
if (GetValueMode() == VALUE_MODE_DEFAULT &&
mType != NS_FORM_INPUT_HIDDEN) {
break;
}
}

if (inputState.type() == PresContentData::TnsString) {
// TODO: What should we do if SetValueInternal fails? (The allocation
// may potentially be big, but most likely we've failed to allocate
// before the type change.)
SetValueInternal(inputState->GetValue(),
SetValueInternal(inputState.get_nsString(),
nsTextEditorState::eSetValue_Notify);
break;
}
}
break;
}

if (aState->IsDisabledSet() && !aState->GetDisabled()) {
if (aState->disabledSet() && !aState->disabled()) {
SetDisabled(false, IgnoreErrors());
}

Expand Down
2 changes: 1 addition & 1 deletion dom/html/HTMLInputElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class HTMLInputElement final : public nsGenericHTMLFormElementWithState,
NS_IMETHOD Reset() override;
NS_IMETHOD SubmitNamesValues(HTMLFormSubmission* aFormSubmission) override;
NS_IMETHOD SaveState() override;
virtual bool RestoreState(nsPresState* aState) override;
virtual bool RestoreState(PresState* aState) override;
virtual bool AllowDrop() override;
virtual bool IsDisabledForEvents(EventMessage aMessage) override;

Expand Down
Loading

0 comments on commit 242c9ce

Please sign in to comment.