Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/FormData.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_FormData_h
8
#define mozilla_dom_FormData_h
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/ErrorResult.h"
12
#include "mozilla/dom/BindingDeclarations.h"
13
#include "mozilla/dom/HTMLFormSubmission.h"
14
#include "mozilla/dom/File.h"
15
#include "mozilla/dom/FormDataBinding.h"
16
#include "nsTArray.h"
17
#include "nsWrapperCache.h"
18
19
namespace mozilla {
20
namespace dom {
21
22
class HTMLFormElement;
23
class GlobalObject;
24
25
class FormData final : public nsISupports,
26
                       public HTMLFormSubmission,
27
                       public nsWrapperCache
28
{
29
private:
30
0
  ~FormData() {}
31
32
  struct FormDataTuple
33
  {
34
    nsString name;
35
    bool wasNullBlob;
36
    OwningBlobOrDirectoryOrUSVString value;
37
  };
38
39
  // Returns the FormDataTuple to modify. This may be null, in which case
40
  // no element with aName was found.
41
  FormDataTuple*
42
  RemoveAllOthersAndGetFirstFormDataTuple(const nsAString& aName);
43
44
  void SetNameValuePair(FormDataTuple* aData,
45
                        const nsAString& aName,
46
                        const nsAString& aValue,
47
                        bool aWasNullBlob = false);
48
49
  void SetNameFilePair(FormDataTuple* aData,
50
                       const nsAString& aName,
51
                       File* aFile);
52
53
  void SetNameDirectoryPair(FormDataTuple* aData,
54
                            const nsAString& aName,
55
                            Directory* aDirectory);
56
57
public:
58
  explicit FormData(nsISupports* aOwner = nullptr);
59
60
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
61
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FormData)
62
63
  // nsWrapperCache
64
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
65
66
  // WebIDL
67
  nsISupports*
68
  GetParentObject() const
69
  {
70
    return mOwner;
71
  }
72
73
  static already_AddRefed<FormData>
74
  Constructor(const GlobalObject& aGlobal,
75
              const Optional<NonNull<HTMLFormElement> >& aFormElement,
76
              ErrorResult& aRv);
77
78
  void Append(const nsAString& aName, const nsAString& aValue,
79
              ErrorResult& aRv);
80
81
  void Append(const nsAString& aName, Blob& aBlob,
82
              const Optional<nsAString>& aFilename,
83
              ErrorResult& aRv);
84
85
  void Append(const nsAString& aName, Directory* aDirectory);
86
87
  void Delete(const nsAString& aName);
88
89
  void Get(const nsAString& aName, Nullable<OwningBlobOrDirectoryOrUSVString>& aOutValue);
90
91
  void GetAll(const nsAString& aName, nsTArray<OwningBlobOrDirectoryOrUSVString>& aValues);
92
93
  bool Has(const nsAString& aName);
94
95
  void Set(const nsAString& aName, Blob& aBlob,
96
           const Optional<nsAString>& aFilename,
97
           ErrorResult& aRv);
98
  void Set(const nsAString& aName, const nsAString& aValue,
99
           ErrorResult& aRv);
100
101
  uint32_t GetIterableLength() const;
102
103
  const nsAString& GetKeyAtIndex(uint32_t aIndex) const;
104
105
  const OwningBlobOrDirectoryOrUSVString& GetValueAtIndex(uint32_t aIndex) const;
106
107
  // HTMLFormSubmission
108
  virtual nsresult
109
  GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream,
110
                       nsCOMPtr<nsIURI>& aOutURI) override;
111
112
  virtual nsresult AddNameValuePair(const nsAString& aName,
113
                                    const nsAString& aValue) override
114
0
  {
115
0
    FormDataTuple* data = mFormData.AppendElement();
116
0
    SetNameValuePair(data, aName, aValue);
117
0
    return NS_OK;
118
0
  }
119
120
  virtual nsresult AddNameBlobOrNullPair(const nsAString& aName,
121
                                         Blob* aBlob) override;
122
123
  virtual nsresult AddNameDirectoryPair(const nsAString& aName,
124
                                        Directory* aDirectory) override;
125
126
  typedef bool (*FormDataEntryCallback)(const nsString& aName,
127
                                        const OwningBlobOrDirectoryOrUSVString& aValue,
128
                                        void* aClosure);
129
130
  uint32_t
131
  Length() const
132
  {
133
    return mFormData.Length();
134
  }
135
136
  // Stops iteration and returns false if any invocation of callback returns
137
  // false. Returns true otherwise.
138
  bool
139
  ForEach(FormDataEntryCallback aFunc, void* aClosure)
140
  {
141
    for (uint32_t i = 0; i < mFormData.Length(); ++i) {
142
      FormDataTuple& tuple = mFormData[i];
143
      if (!aFunc(tuple.name, tuple.value, aClosure)) {
144
        return false;
145
      }
146
    }
147
148
    return true;
149
  }
150
151
  nsresult
152
  GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
153
              nsACString& aContentTypeWithCharset, nsACString& aCharset) const;
154
155
private:
156
  nsCOMPtr<nsISupports> mOwner;
157
158
  nsTArray<FormDataTuple> mFormData;
159
};
160
161
} // dom namespace
162
} // mozilla namepsace
163
164
#endif // mozilla_dom_FormData_h