1/*
2 * Copyright (C) 2010 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
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#if ENABLE(FILE_SYSTEM)
34
35#include "JSDirectoryEntry.h"
36
37#include "ExceptionCode.h"
38#include "JSDOMBinding.h"
39#include "JSEntryCallback.h"
40#include "JSErrorCallback.h"
41#include "JSWebKitFlags.h"
42#include <wtf/Assertions.h>
43
44using namespace JSC;
45
46namespace WebCore {
47
48JSValue JSDirectoryEntry::getFile(ExecState* exec)
49{
50    DirectoryEntry* imp = static_cast<DirectoryEntry*>(impl());
51    const String& path = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0));
52    if (exec->hadException())
53        return jsUndefined();
54
55    int argsCount = exec->argumentCount();
56    if (argsCount <= 1) {
57        imp->getFile(path);
58        return jsUndefined();
59    }
60
61    RefPtr<WebKitFlags> flags;
62    if (!exec->argument(1).isNull() && !exec->argument(1).isUndefined() && exec->argument(1).isObject() && !exec->argument(1).inherits(&JSWebKitFlags::s_info)) {
63        JSObject* object = exec->argument(1).getObject();
64        flags = WebKitFlags::create();
65        JSValue jsCreate = object->get(exec, Identifier(exec, "create"));
66        flags->setCreate(jsCreate.toBoolean(exec));
67        JSValue jsExclusive = object->get(exec, Identifier(exec, "exclusive"));
68        flags->setExclusive(jsExclusive.toBoolean(exec));
69    } else
70        flags = toWebKitFlags(exec->argument(1));
71    if (exec->hadException())
72        return jsUndefined();
73    RefPtr<EntryCallback> successCallback;
74    if (exec->argumentCount() > 2 && !exec->argument(2).isNull() && !exec->argument(2).isUndefined()) {
75        if (!exec->argument(2).isObject()) {
76            setDOMException(exec, TYPE_MISMATCH_ERR);
77            return jsUndefined();
78        }
79        successCallback = JSEntryCallback::create(asObject(exec->argument(2)), globalObject());
80    }
81    RefPtr<ErrorCallback> errorCallback;
82    if (exec->argumentCount() > 3 && !exec->argument(3).isNull() && !exec->argument(3).isUndefined()) {
83        if (!exec->argument(3).isObject()) {
84            setDOMException(exec, TYPE_MISMATCH_ERR);
85            return jsUndefined();
86        }
87        errorCallback = JSErrorCallback::create(asObject(exec->argument(3)), globalObject());
88    }
89
90    imp->getFile(path, flags, successCallback, errorCallback);
91    return jsUndefined();
92}
93
94JSValue JSDirectoryEntry::getDirectory(ExecState* exec)
95{
96    DirectoryEntry* imp = static_cast<DirectoryEntry*>(impl());
97    const String& path = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0));
98    if (exec->hadException())
99        return jsUndefined();
100
101    int argsCount = exec->argumentCount();
102    if (argsCount <= 1) {
103        imp->getDirectory(path);
104        return jsUndefined();
105    }
106
107    RefPtr<WebKitFlags> flags;
108    if (!exec->argument(1).isNull() && !exec->argument(1).isUndefined() && exec->argument(1).isObject() && !exec->argument(1).inherits(&JSWebKitFlags::s_info)) {
109        JSObject* object = exec->argument(1).getObject();
110        flags = WebKitFlags::create();
111        JSValue jsCreate = object->get(exec, Identifier(exec, "create"));
112        flags->setCreate(jsCreate.toBoolean(exec));
113        JSValue jsExclusive = object->get(exec, Identifier(exec, "exclusive"));
114        flags->setExclusive(jsExclusive.toBoolean(exec));
115    } else
116        flags = toWebKitFlags(exec->argument(1));
117    if (exec->hadException())
118        return jsUndefined();
119    RefPtr<EntryCallback> successCallback;
120    if (exec->argumentCount() > 2 && !exec->argument(2).isNull() && !exec->argument(2).isUndefined()) {
121        if (!exec->argument(2).isObject()) {
122            setDOMException(exec, TYPE_MISMATCH_ERR);
123            return jsUndefined();
124        }
125        successCallback = JSEntryCallback::create(asObject(exec->argument(2)), globalObject());
126    }
127    RefPtr<ErrorCallback> errorCallback;
128    if (exec->argumentCount() > 3 && !exec->argument(3).isNull() && !exec->argument(3).isUndefined()) {
129        if (!exec->argument(3).isObject()) {
130            setDOMException(exec, TYPE_MISMATCH_ERR);
131            return jsUndefined();
132        }
133        errorCallback = JSErrorCallback::create(asObject(exec->argument(3)), globalObject());
134    }
135
136    imp->getDirectory(path, flags, successCallback, errorCallback);
137    return jsUndefined();
138}
139
140} // namespace WebCore
141
142#endif // ENABLE(FILE_SYSTEM)
143