1/*
2 * Copyright (C) 2012, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE.
24 */
25
26#include "config.h"
27#include "modules/filesystem/DOMWindowFileSystem.h"
28
29#include "core/dom/Document.h"
30#include "core/fileapi/FileError.h"
31#include "core/frame/LocalDOMWindow.h"
32#include "modules/filesystem/DOMFileSystem.h"
33#include "modules/filesystem/EntryCallback.h"
34#include "modules/filesystem/ErrorCallback.h"
35#include "modules/filesystem/FileSystemCallback.h"
36#include "modules/filesystem/FileSystemCallbacks.h"
37#include "modules/filesystem/LocalFileSystem.h"
38#include "platform/FileSystemType.h"
39#include "platform/weborigin/SecurityOrigin.h"
40
41namespace blink {
42
43DOMWindowFileSystem::DOMWindowFileSystem()
44{
45}
46
47DOMWindowFileSystem::~DOMWindowFileSystem()
48{
49}
50
51void DOMWindowFileSystem::webkitRequestFileSystem(LocalDOMWindow& window, int type, long long size, FileSystemCallback* successCallback, ErrorCallback* errorCallback)
52{
53    if (!window.isCurrentlyDisplayedInFrame())
54        return;
55
56    Document* document = window.document();
57    if (!document)
58        return;
59
60    if (!document->securityOrigin()->canAccessFileSystem()) {
61        DOMFileSystem::scheduleCallback(document, errorCallback, FileError::create(FileError::SECURITY_ERR));
62        return;
63    }
64
65    FileSystemType fileSystemType = static_cast<FileSystemType>(type);
66    if (!DOMFileSystemBase::isValidType(fileSystemType)) {
67        DOMFileSystem::scheduleCallback(document, errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
68        return;
69    }
70
71    LocalFileSystem::from(*document)->requestFileSystem(document, fileSystemType, size, FileSystemCallbacks::create(successCallback, errorCallback, document, fileSystemType));
72}
73
74void DOMWindowFileSystem::webkitResolveLocalFileSystemURL(LocalDOMWindow& window, const String& url, EntryCallback* successCallback, ErrorCallback* errorCallback)
75{
76    if (!window.isCurrentlyDisplayedInFrame())
77        return;
78
79    Document* document = window.document();
80    if (!document)
81        return;
82
83    SecurityOrigin* securityOrigin = document->securityOrigin();
84    KURL completedURL = document->completeURL(url);
85    if (!securityOrigin->canAccessFileSystem() || !securityOrigin->canRequest(completedURL)) {
86        DOMFileSystem::scheduleCallback(document, errorCallback, FileError::create(FileError::SECURITY_ERR));
87        return;
88    }
89
90    if (!completedURL.isValid()) {
91        DOMFileSystem::scheduleCallback(document, errorCallback, FileError::create(FileError::ENCODING_ERR));
92        return;
93    }
94
95    LocalFileSystem::from(*document)->resolveURL(document, completedURL, ResolveURICallbacks::create(successCallback, errorCallback, document));
96}
97
98COMPILE_ASSERT(static_cast<int>(DOMWindowFileSystem::TEMPORARY) == static_cast<int>(FileSystemTypeTemporary), enum_mismatch);
99COMPILE_ASSERT(static_cast<int>(DOMWindowFileSystem::PERSISTENT) == static_cast<int>(FileSystemTypePersistent), enum_mismatch);
100
101} // namespace blink
102