Home | History | Annotate | Download | only in filesystem
      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/v8/ExceptionState.h"
     32 #include "core/dom/ExceptionCode.h"
     33 #include "core/fileapi/FileError.h"
     34 #include "core/platform/AsyncFileSystem.h"
     35 #include "core/workers/WorkerGlobalScope.h"
     36 #include "modules/filesystem/DOMFileSystemBase.h"
     37 #include "modules/filesystem/DOMFileSystemSync.h"
     38 #include "modules/filesystem/DirectoryEntrySync.h"
     39 #include "modules/filesystem/ErrorCallback.h"
     40 #include "modules/filesystem/FileEntrySync.h"
     41 #include "modules/filesystem/FileSystemCallback.h"
     42 #include "modules/filesystem/FileSystemCallbacks.h"
     43 #include "modules/filesystem/FileSystemType.h"
     44 #include "modules/filesystem/SyncCallbackHelper.h"
     45 #include "modules/filesystem/WorkerLocalFileSystem.h"
     46 #include "weborigin/SecurityOrigin.h"
     47 
     48 namespace WebCore {
     49 
     50 void WorkerGlobalScopeFileSystem::webkitRequestFileSystem(WorkerGlobalScope* worker, int type, long long size, PassRefPtr<FileSystemCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
     51 {
     52     ScriptExecutionContext* secureContext = worker->scriptExecutionContext();
     53     if (!secureContext->securityOrigin()->canAccessFileSystem()) {
     54         DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::SECURITY_ERR));
     55         return;
     56     }
     57 
     58     FileSystemType fileSystemType = static_cast<FileSystemType>(type);
     59     if (!DOMFileSystemBase::isValidType(fileSystemType)) {
     60         DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::INVALID_MODIFICATION_ERR));
     61         return;
     62     }
     63 
     64     WorkerLocalFileSystem::from(worker)->requestFileSystem(worker, fileSystemType, size, FileSystemCallbacks::create(successCallback, errorCallback, worker, fileSystemType), AsynchronousFileSystem);
     65 }
     66 
     67 PassRefPtr<DOMFileSystemSync> WorkerGlobalScopeFileSystem::webkitRequestFileSystemSync(WorkerGlobalScope* worker, int type, long long size, ExceptionState& es)
     68 {
     69     ScriptExecutionContext* secureContext = worker->scriptExecutionContext();
     70     if (!secureContext->securityOrigin()->canAccessFileSystem()) {
     71         es.throwDOMException(SecurityError, FileError::securityErrorMessage);
     72         return 0;
     73     }
     74 
     75     FileSystemType fileSystemType = static_cast<FileSystemType>(type);
     76     if (!DOMFileSystemBase::isValidType(fileSystemType)) {
     77         es.throwDOMException(InvalidModificationError);
     78         return 0;
     79     }
     80 
     81     FileSystemSyncCallbackHelper helper;
     82     OwnPtr<FileSystemCallbacks> callbacks = FileSystemCallbacks::create(helper.successCallback(), helper.errorCallback(), worker, fileSystemType);
     83     callbacks->setShouldBlockUntilCompletion(true);
     84 
     85     WorkerLocalFileSystem::from(worker)->requestFileSystem(worker, fileSystemType, size, callbacks.release(), SynchronousFileSystem);
     86     return helper.getResult(es);
     87 }
     88 
     89 void WorkerGlobalScopeFileSystem::webkitResolveLocalFileSystemURL(WorkerGlobalScope* worker, const String& url, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
     90 {
     91     KURL completedURL = worker->completeURL(url);
     92     ScriptExecutionContext* secureContext = worker->scriptExecutionContext();
     93     if (!secureContext->securityOrigin()->canAccessFileSystem() || !secureContext->securityOrigin()->canRequest(completedURL)) {
     94         DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::SECURITY_ERR));
     95         return;
     96     }
     97 
     98     FileSystemType type;
     99     String filePath;
    100     if (!completedURL.isValid() || !DOMFileSystemBase::crackFileSystemURL(completedURL, type, filePath)) {
    101         DOMFileSystem::scheduleCallback(worker, errorCallback, FileError::create(FileError::ENCODING_ERR));
    102         return;
    103     }
    104 
    105     WorkerLocalFileSystem::from(worker)->readFileSystem(worker, type, ResolveURICallbacks::create(successCallback, errorCallback, worker, type, filePath));
    106 }
    107 
    108 PassRefPtr<EntrySync> WorkerGlobalScopeFileSystem::webkitResolveLocalFileSystemSyncURL(WorkerGlobalScope* worker, const String& url, ExceptionState& es)
    109 {
    110     KURL completedURL = worker->completeURL(url);
    111     ScriptExecutionContext* secureContext = worker->scriptExecutionContext();
    112     if (!secureContext->securityOrigin()->canAccessFileSystem() || !secureContext->securityOrigin()->canRequest(completedURL)) {
    113         es.throwDOMException(SecurityError, FileError::securityErrorMessage);
    114         return 0;
    115     }
    116 
    117     FileSystemType type;
    118     String filePath;
    119     if (!completedURL.isValid() || !DOMFileSystemBase::crackFileSystemURL(completedURL, type, filePath)) {
    120         es.throwDOMException(EncodingError);
    121         return 0;
    122     }
    123 
    124     FileSystemSyncCallbackHelper readFileSystemHelper;
    125     WorkerLocalFileSystem::from(worker)->readFileSystem(worker, type, FileSystemCallbacks::create(readFileSystemHelper.successCallback(), readFileSystemHelper.errorCallback(), worker, type), SynchronousFileSystem);
    126     RefPtr<DOMFileSystemSync> fileSystem = readFileSystemHelper.getResult(es);
    127     if (!fileSystem)
    128         return 0;
    129 
    130     RefPtr<EntrySync> entry = fileSystem->root()->getDirectory(filePath, Dictionary(), es);
    131     if (es.code() == TypeMismatchError) {
    132         es.clearException();
    133         return fileSystem->root()->getFile(filePath, Dictionary(), es);
    134     }
    135 
    136     return entry.release();
    137 }
    138 
    139 COMPILE_ASSERT(static_cast<int>(WorkerGlobalScopeFileSystem::TEMPORARY) == static_cast<int>(FileSystemTypeTemporary), enum_mismatch);
    140 COMPILE_ASSERT(static_cast<int>(WorkerGlobalScopeFileSystem::PERSISTENT) == static_cast<int>(FileSystemTypePersistent), enum_mismatch);
    141 
    142 } // namespace WebCore
    143