2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
23 #include "FileInputType.h"
29 #include "FileSystem.h"
30 #include "FormDataList.h"
32 #include "HTMLInputElement.h"
33 #include "HTMLNames.h"
35 #include "LocalizedStrings.h"
37 #include "RenderFileUploadControl.h"
38 #include "ScriptController.h"
39 #include "ShadowRoot.h"
40 #include <wtf/PassOwnPtr.h>
41 #include <wtf/text/WTFString.h>
45 using namespace HTMLNames;
47 class UploadButtonElement : public HTMLInputElement {
49 static PassRefPtr<UploadButtonElement> create(Document*);
50 static PassRefPtr<UploadButtonElement> createForMultiple(Document*);
53 UploadButtonElement(Document*);
55 virtual const AtomicString& shadowPseudoId() const;
58 PassRefPtr<UploadButtonElement> UploadButtonElement::create(Document* document)
60 RefPtr<UploadButtonElement> button = adoptRef(new UploadButtonElement(document));
61 button->setType("button");
62 button->setValue(fileButtonChooseFileLabel());
63 return button.release();
66 PassRefPtr<UploadButtonElement> UploadButtonElement::createForMultiple(Document* document)
68 RefPtr<UploadButtonElement> button = adoptRef(new UploadButtonElement(document));
69 button->setType("button");
70 button->setValue(fileButtonChooseMultipleFilesLabel());
71 return button.release();
74 UploadButtonElement::UploadButtonElement(Document* document)
75 : HTMLInputElement(inputTag, document, 0, false)
79 const AtomicString& UploadButtonElement::shadowPseudoId() const
81 DEFINE_STATIC_LOCAL(AtomicString, pseudoId, ("-webkit-file-upload-button"));
85 inline FileInputType::FileInputType(HTMLInputElement* element)
86 : BaseButtonInputType(element)
87 , m_fileList(FileList::create())
91 PassOwnPtr<InputType> FileInputType::create(HTMLInputElement* element)
93 return adoptPtr(new FileInputType(element));
96 const AtomicString& FileInputType::formControlType() const
98 return InputTypeNames::file();
101 bool FileInputType::appendFormData(FormDataList& encoding, bool multipart) const
103 FileList* fileList = element()->files();
104 unsigned numFiles = fileList->length();
106 // Send only the basenames.
107 // 4.10.16.4 and 4.10.16.6 sections in HTML5.
109 // Unlike the multipart case, we have no special handling for the empty
110 // fileList because Netscape doesn't support for non-multipart
111 // submission of file inputs, and Firefox doesn't add "name=" query
113 for (unsigned i = 0; i < numFiles; ++i)
114 encoding.appendData(element()->name(), fileList->item(i)->fileName());
118 // If no filename at all is entered, return successful but empty.
119 // Null would be more logical, but Netscape posts an empty file. Argh.
121 encoding.appendBlob(element()->name(), File::create(""));
125 for (unsigned i = 0; i < numFiles; ++i)
126 encoding.appendBlob(element()->name(), fileList->item(i));
130 bool FileInputType::valueMissing(const String& value) const
132 return value.isEmpty();
135 String FileInputType::valueMissingText() const
137 return element()->multiple() ? validationMessageValueMissingForMultipleFileText() : validationMessageValueMissingForFileText();
140 void FileInputType::handleDOMActivateEvent(Event* event)
142 if (element()->disabled() || !element()->renderer())
145 if (!ScriptController::processingUserGesture())
148 if (Chrome* chrome = this->chrome()) {
149 FileChooserSettings settings;
150 HTMLInputElement* input = element();
151 #if ENABLE(DIRECTORY_UPLOAD)
152 settings.allowsDirectoryUpload = input->fastHasAttribute(webkitdirectoryAttr);
153 settings.allowsMultipleFiles = settings.allowsDirectoryUpload || input->fastHasAttribute(multipleAttr);
155 settings.allowsMultipleFiles = input->fastHasAttribute(multipleAttr);
157 settings.acceptTypes = input->accept();
158 settings.selectedFiles = m_fileList->paths();
159 chrome->runOpenPanel(input->document()->frame(), newFileChooser(settings));
161 event->setDefaultHandled();
164 RenderObject* FileInputType::createRenderer(RenderArena* arena, RenderStyle*) const
166 return new (arena) RenderFileUploadControl(element());
169 bool FileInputType::canSetStringValue() const
174 bool FileInputType::canChangeFromAnotherType() const
176 // Don't allow the type to be changed to file after the first type change.
177 // In other engines this might mean a JavaScript programmer could set a text
178 // field's value to something like /etc/passwd and then change it to a file input.
179 // I don't think this would actually occur in WebKit, but this rule still may be
180 // important for compatibility.
184 FileList* FileInputType::files()
186 return m_fileList.get();
189 bool FileInputType::canSetValue(const String& value)
191 // For security reasons, we don't allow setting the filename, but we do allow clearing it.
192 // The HTML5 spec (as of the 10/24/08 working draft) says that the value attribute isn't
193 // applicable to the file upload control at all, but for now we are keeping this behavior
194 // to avoid breaking existing websites that may be relying on this.
195 return value.isEmpty();
198 bool FileInputType::getTypeSpecificValue(String& value)
200 if (m_fileList->isEmpty()) {
205 // HTML5 tells us that we're supposed to use this goofy value for
206 // file input controls. Historically, browsers revealed the real
207 // file path, but that's a privacy problem. Code on the web
208 // decided to try to parse the value by looking for backslashes
209 // (because that's what Windows file paths use). To be compatible
210 // with that code, we make up a fake path for the file.
211 value = "C:\\fakepath\\" + m_fileList->item(0)->fileName();
215 bool FileInputType::storesValueSeparateFromAttribute()
220 void FileInputType::setFileList(const Vector<String>& paths)
223 size_t size = paths.size();
225 #if ENABLE(DIRECTORY_UPLOAD)
226 // If a directory is being selected, the UI allows a directory to be chosen
227 // and the paths provided here share a root directory somewhere up the tree;
228 // we want to store only the relative paths from that point.
229 if (size && element()->fastHasAttribute(webkitdirectoryAttr)) {
230 // Find the common root path.
231 String rootPath = directoryName(paths[0]);
232 for (size_t i = 1; i < size; i++) {
233 while (!paths[i].startsWith(rootPath))
234 rootPath = directoryName(rootPath);
236 rootPath = directoryName(rootPath);
237 ASSERT(rootPath.length());
238 for (size_t i = 0; i < size; i++) {
239 // Normalize backslashes to slashes before exposing the relative path to script.
240 String relativePath = paths[i].substring(1 + rootPath.length()).replace('\\', '/');
241 m_fileList->append(File::createWithRelativePath(paths[i], relativePath));
247 for (size_t i = 0; i < size; i++)
248 m_fileList->append(File::create(paths[i]));
251 bool FileInputType::isFileUpload() const
256 void FileInputType::createShadowSubtree()
258 ExceptionCode ec = 0;
259 element()->ensureShadowRoot()->appendChild(element()->multiple() ? UploadButtonElement::createForMultiple(element()->document()): UploadButtonElement::create(element()->document()), ec);
262 void FileInputType::multipleAttributeChanged()
264 UploadButtonElement* button = static_cast<UploadButtonElement*>(element()->ensureShadowRoot()->firstChild());
266 button->setValue(element()->multiple() ? fileButtonChooseMultipleFilesLabel() : fileButtonChooseFileLabel());
269 void FileInputType::requestIcon(const Vector<String>& paths)
274 if (Chrome* chrome = this->chrome())
275 chrome->loadIconForFiles(paths, newFileIconLoader());
278 void FileInputType::filesChosen(const Vector<String>& paths)
280 HTMLInputElement* input = element();
283 input->setFormControlValueMatchesRenderer(true);
284 input->notifyFormStateChanged();
285 input->setNeedsValidityCheck();
289 if (input->renderer())
290 input->renderer()->repaint();
291 // This call may cause destruction of this instance and thus must always be last in the function.
292 input->dispatchFormControlChangeEvent();
295 #if ENABLE(DIRECTORY_UPLOAD)
296 void FileInputType::receiveDropForDirectoryUpload(const Vector<String>& paths)
298 if (Chrome* chrome = this->chrome()) {
299 FileChooserSettings settings;
300 settings.allowsDirectoryUpload = true;
301 settings.allowsMultipleFiles = true;
302 settings.selectedFiles.append(paths[0]);
303 chrome->enumerateChosenDirectory(newFileChooser(settings));
308 void FileInputType::updateRendering(PassRefPtr<Icon> icon)
314 if (element()->renderer())
315 element()->renderer()->repaint();
318 Chrome* FileInputType::chrome() const
320 if (Page* page = element()->document()->page())
321 return page->chrome();
325 void FileInputType::receiveDroppedFiles(const Vector<String>& paths)
327 HTMLInputElement* input = element();
328 #if ENABLE(DIRECTORY_UPLOAD)
329 if (input->fastHasAttribute(webkitdirectoryAttr)) {
330 receiveDropForDirectoryUpload(paths);
335 if (input->fastHasAttribute(multipleAttr))
338 Vector<String> firstPathOnly;
339 firstPathOnly.append(paths[0]);
340 filesChosen(firstPathOnly);
344 Icon* FileInputType::icon() const
349 } // namespace WebCore