1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2010
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPL"), or
25 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "nsFormData.h"
38 : #include "nsIVariant.h"
39 : #include "nsIInputStream.h"
40 : #include "nsIDOMFile.h"
41 : #include "nsContentUtils.h"
42 : #include "nsHTMLFormElement.h"
43 :
44 0 : nsFormData::nsFormData()
45 0 : : nsFormSubmission(NS_LITERAL_CSTRING("UTF-8"), nsnull)
46 : {
47 0 : }
48 :
49 : // -------------------------------------------------------------------------
50 : // nsISupports
51 :
52 : DOMCI_DATA(FormData, nsFormData)
53 :
54 0 : NS_IMPL_ADDREF(nsFormData)
55 0 : NS_IMPL_RELEASE(nsFormData)
56 0 : NS_INTERFACE_MAP_BEGIN(nsFormData)
57 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMFormData)
58 0 : NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
59 0 : NS_INTERFACE_MAP_ENTRY(nsIJSNativeInitializer)
60 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(FormData)
61 0 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMFormData)
62 0 : NS_INTERFACE_MAP_END
63 :
64 : // -------------------------------------------------------------------------
65 : // nsFormSubmission
66 : nsresult
67 0 : nsFormData::GetEncodedSubmission(nsIURI* aURI,
68 : nsIInputStream** aPostDataStream)
69 : {
70 0 : NS_NOTREACHED("Shouldn't call nsFormData::GetEncodedSubmission");
71 0 : return NS_OK;
72 : }
73 :
74 : nsresult
75 0 : nsFormData::AddNameValuePair(const nsAString& aName,
76 : const nsAString& aValue)
77 : {
78 0 : FormDataTuple* data = mFormData.AppendElement();
79 0 : data->name = aName;
80 0 : data->stringValue = aValue;
81 0 : data->valueIsFile = false;
82 :
83 0 : return NS_OK;
84 : }
85 :
86 : nsresult
87 0 : nsFormData::AddNameFilePair(const nsAString& aName,
88 : nsIDOMBlob* aBlob)
89 : {
90 0 : FormDataTuple* data = mFormData.AppendElement();
91 0 : data->name = aName;
92 0 : data->fileValue = aBlob;
93 0 : data->valueIsFile = true;
94 :
95 0 : return NS_OK;
96 : }
97 :
98 : // -------------------------------------------------------------------------
99 : // nsIDOMFormData
100 :
101 : NS_IMETHODIMP
102 0 : nsFormData::Append(const nsAString& aName, nsIVariant* aValue)
103 : {
104 : PRUint16 dataType;
105 0 : nsresult rv = aValue->GetDataType(&dataType);
106 0 : NS_ENSURE_SUCCESS(rv, rv);
107 :
108 0 : if (dataType == nsIDataType::VTYPE_INTERFACE ||
109 : dataType == nsIDataType::VTYPE_INTERFACE_IS) {
110 0 : nsCOMPtr<nsISupports> supports;
111 : nsID *iid;
112 0 : rv = aValue->GetAsInterface(&iid, getter_AddRefs(supports));
113 0 : NS_ENSURE_SUCCESS(rv, rv);
114 :
115 0 : nsMemory::Free(iid);
116 :
117 0 : nsCOMPtr<nsIDOMBlob> domBlob = do_QueryInterface(supports);
118 0 : if (domBlob) {
119 0 : return AddNameFilePair(aName, domBlob);
120 : }
121 : }
122 :
123 0 : PRUnichar* stringData = nsnull;
124 0 : PRUint32 stringLen = 0;
125 0 : rv = aValue->GetAsWStringWithSize(&stringLen, &stringData);
126 0 : NS_ENSURE_SUCCESS(rv, rv);
127 :
128 0 : nsString valAsString;
129 0 : valAsString.Adopt(stringData, stringLen);
130 :
131 0 : return AddNameValuePair(aName, valAsString);
132 : }
133 :
134 : // -------------------------------------------------------------------------
135 : // nsIXHRSendable
136 :
137 : NS_IMETHODIMP
138 0 : nsFormData::GetSendInfo(nsIInputStream** aBody, nsACString& aContentType,
139 : nsACString& aCharset)
140 : {
141 0 : nsFSMultipartFormData fs(NS_LITERAL_CSTRING("UTF-8"), nsnull);
142 :
143 0 : for (PRUint32 i = 0; i < mFormData.Length(); ++i) {
144 0 : if (mFormData[i].valueIsFile) {
145 0 : fs.AddNameFilePair(mFormData[i].name, mFormData[i].fileValue);
146 : }
147 : else {
148 0 : fs.AddNameValuePair(mFormData[i].name, mFormData[i].stringValue);
149 : }
150 : }
151 :
152 0 : fs.GetContentType(aContentType);
153 0 : aCharset.Truncate();
154 0 : NS_ADDREF(*aBody = fs.GetSubmissionBody());
155 :
156 0 : return NS_OK;
157 : }
158 :
159 :
160 : // -------------------------------------------------------------------------
161 : // nsIJSNativeInitializer
162 :
163 : NS_IMETHODIMP
164 0 : nsFormData::Initialize(nsISupports* aOwner,
165 : JSContext* aCx,
166 : JSObject* aObj,
167 : PRUint32 aArgc,
168 : jsval* aArgv)
169 : {
170 0 : if (aArgc > 0) {
171 0 : if (JSVAL_IS_PRIMITIVE(aArgv[0])) {
172 0 : return NS_ERROR_UNEXPECTED;
173 : }
174 : nsCOMPtr<nsIContent> formCont = do_QueryInterface(
175 0 : nsContentUtils::XPConnect()->
176 0 : GetNativeOfWrapper(aCx, JSVAL_TO_OBJECT(aArgv[0])));
177 :
178 0 : if (!formCont || !formCont->IsHTML(nsGkAtoms::form)) {
179 0 : return NS_ERROR_UNEXPECTED;
180 : }
181 :
182 0 : nsresult rv = static_cast<nsHTMLFormElement*>(formCont.get())->
183 0 : WalkFormElements(this);
184 0 : NS_ENSURE_SUCCESS(rv, rv);
185 : }
186 :
187 :
188 0 : return NS_OK;
189 : }
|