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 "JSDirectoryEntrySync.h"
36
37#include "JSDOMBinding.h"
38#include "JSEntryCallback.h"
39#include "JSErrorCallback.h"
40#include "JSFileEntrySync.h"
41#include "JSWebKitFlags.h"
42#include <wtf/Assertions.h>
43
44using namespace JSC;
45
46namespace WebCore {
47
48static PassRefPtr<WebKitFlags> getFlags(ExecState* exec, const JSValue& argument)
49{
50    if (argument.isNull() || argument.isUndefined() || !argument.isObject())
51        return 0;
52    if (argument.inherits(&JSWebKitFlags::s_info))
53        return toFlags(argument);
54
55    RefPtr<WebKitFlags> flags;
56    JSObject* object = argument.getObject();
57    flags = WebKitFlags::create();
58    JSValue jsCreate = object->get(exec, Identifier(exec, "create"));
59    flags->setCreate(jsCreate.toBoolean(exec));
60    JSValue jsExclusive = object->get(exec, Identifier(exec, "exclusive"));
61    flags->setExclusive(jsExclusive.toBoolean(exec));
62    return flags;
63}
64
65JSValue JSDirectoryEntrySync::getFile(ExecState* exec)
66{
67    DirectoryEntrySync* imp = static_cast<DirectoryEntrySync*>(impl());
68    const String& path = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0));
69    if (exec->hadException())
70        return jsUndefined();
71
72    RefPtr<WebKitFlags> flags = getFlags(exec, exec->argument(1));
73    if (exec->hadException())
74        return jsUndefined();
75
76    ExceptionCode ec = 0;
77    JSC::JSValue result = toJS(exec, this->globalObject(), WTF::getPtr(imp->getFile(path, flags, ec)));
78    setDOMException(exec, ec);
79    return result;
80}
81
82JSValue JSDirectoryEntrySync::getDirectory(ExecState* exec)
83{
84    DirectoryEntrySync* imp = static_cast<DirectoryEntrySync*>(impl());
85    const String& path = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0));
86    if (exec->hadException())
87        return jsUndefined();
88
89    RefPtr<WebKitFlags> flags = getFlags(exec, exec->argument(1));
90    if (exec->hadException())
91        return jsUndefined();
92
93    ExceptionCode ec = 0;
94    JSC::JSValue result = toJS(exec, this->globalObject(), WTF::getPtr(imp->getDirectory(path, flags, ec)));
95    setDOMException(exec, ec);
96    return result;
97}
98
99} // namespace WebCore
100
101#endif // ENABLE(FILE_SYSTEM)
102