1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009, 2011 Google Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28#include "config.h"
29#include "modules/filesystem/WorkerGlobalScopeFileSystem.h"
30
31#include "bindings/core/v8/ExceptionState.h"
32#include "core/dom/ExceptionCode.h"
33#include "core/fileapi/FileError.h"
34#include "core/workers/WorkerGlobalScope.h"
35#include "modules/filesystem/DOMFileSystemBase.h"
36#include "modules/filesystem/DirectoryEntrySync.h"
37#include "modules/filesystem/ErrorCallback.h"
38#include "modules/filesystem/FileEntrySync.h"
39#include "modules/filesystem/FileSystemCallback.h"
40#include "modules/filesystem/FileSystemCallbacks.h"
41#include "modules/filesystem/LocalFileSystem.h"
42#include "modules/filesystem/SyncCallbackHelper.h"
43#include "platform/FileSystemType.h"
44#include "platform/weborigin/SecurityOrigin.h"
45
46namespace blink {
47
48void WorkerGlobalScopeFileSystem::webkitRequestFileSystem(WorkerGlobalScope& worker, int type, long long size, FileSystemCallback* successCallback, ErrorCallback* errorCallback)
49{
50    ExecutionContext* secureContext = worker.executionContext();
51    if (!secureContext->securityOrigin()->canAccessFileSystem()) {
52        DOMFileSystem::scheduleCallback(&worker, errorCallback, FileError::create(FileError::SECURITY_ERR));
53        return;
54    }
55
56    FileSystemType fileSystemType = static_cast<FileSystemType>(type);
57    if (!DOMFileSystemBase::isValidType(fileSystemType)) {
58        DOMFileSystem::scheduleCallback(&worker, errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
59        return;
60    }
61
62    LocalFileSystem::from(worker)->requestFileSystem(&worker, fileSystemType, size, FileSystemCallbacks::create(successCallback, errorCallback, &worker, fileSystemType));
63}
64
65DOMFileSystemSync* WorkerGlobalScopeFileSystem::webkitRequestFileSystemSync(WorkerGlobalScope& worker, int type, long long size, ExceptionState& exceptionState)
66{
67    ExecutionContext* secureContext = worker.executionContext();
68    if (!secureContext->securityOrigin()->canAccessFileSystem()) {
69        exceptionState.throwSecurityError(FileError::securityErrorMessage);
70        return 0;
71    }
72
73    FileSystemType fileSystemType = static_cast<FileSystemType>(type);
74    if (!DOMFileSystemBase::isValidType(fileSystemType)) {
75        exceptionState.throwDOMException(InvalidModificationError, "the type must be TEMPORARY or PERSISTENT.");
76        return 0;
77    }
78
79    FileSystemSyncCallbackHelper* helper = FileSystemSyncCallbackHelper::create();
80    OwnPtr<AsyncFileSystemCallbacks> callbacks = FileSystemCallbacks::create(helper->successCallback(), helper->errorCallback(), &worker, fileSystemType);
81    callbacks->setShouldBlockUntilCompletion(true);
82
83    LocalFileSystem::from(worker)->requestFileSystem(&worker, fileSystemType, size, callbacks.release());
84    return helper->getResult(exceptionState);
85}
86
87void WorkerGlobalScopeFileSystem::webkitResolveLocalFileSystemURL(WorkerGlobalScope& worker, const String& url, EntryCallback* successCallback, ErrorCallback* errorCallback)
88{
89    KURL completedURL = worker.completeURL(url);
90    ExecutionContext* secureContext = worker.executionContext();
91    if (!secureContext->securityOrigin()->canAccessFileSystem() || !secureContext->securityOrigin()->canRequest(completedURL)) {
92        DOMFileSystem::scheduleCallback(&worker, errorCallback, FileError::create(FileError::SECURITY_ERR));
93        return;
94    }
95
96    if (!completedURL.isValid()) {
97        DOMFileSystem::scheduleCallback(&worker, errorCallback, FileError::create(FileError::ENCODING_ERR));
98        return;
99    }
100
101    LocalFileSystem::from(worker)->resolveURL(&worker, completedURL, ResolveURICallbacks::create(successCallback, errorCallback, &worker));
102}
103
104EntrySync* WorkerGlobalScopeFileSystem::webkitResolveLocalFileSystemSyncURL(WorkerGlobalScope& worker, const String& url, ExceptionState& exceptionState)
105{
106    KURL completedURL = worker.completeURL(url);
107    ExecutionContext* secureContext = worker.executionContext();
108    if (!secureContext->securityOrigin()->canAccessFileSystem() || !secureContext->securityOrigin()->canRequest(completedURL)) {
109        exceptionState.throwSecurityError(FileError::securityErrorMessage);
110        return 0;
111    }
112
113    if (!completedURL.isValid()) {
114        exceptionState.throwDOMException(EncodingError, "the URL '" + url + "' is invalid.");
115        return 0;
116    }
117
118    EntrySyncCallbackHelper* resolveURLHelper = EntrySyncCallbackHelper::create();
119    OwnPtr<AsyncFileSystemCallbacks> callbacks = ResolveURICallbacks::create(resolveURLHelper->successCallback(), resolveURLHelper->errorCallback(), &worker);
120    callbacks->setShouldBlockUntilCompletion(true);
121
122    LocalFileSystem::from(worker)->resolveURL(&worker, completedURL, callbacks.release());
123
124    return resolveURLHelper->getResult(exceptionState);
125}
126
127COMPILE_ASSERT(static_cast<int>(WorkerGlobalScopeFileSystem::TEMPORARY) == static_cast<int>(FileSystemTypeTemporary), enum_mismatch);
128COMPILE_ASSERT(static_cast<int>(WorkerGlobalScopeFileSystem::PERSISTENT) == static_cast<int>(FileSystemTypePersistent), enum_mismatch);
129
130} // namespace blink
131