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#include "V8DirectoryEntrySync.h"
33
34#if ENABLE(FILE_SYSTEM)
35
36#include "DirectoryEntrySync.h"
37#include "ExceptionCode.h"
38#include "V8Binding.h"
39#include "V8BindingMacros.h"
40#include "V8EntryCallback.h"
41#include "V8ErrorCallback.h"
42#include "V8FileEntrySync.h"
43#include "V8WebKitFlags.h"
44#include "V8Proxy.h"
45#include <wtf/RefCounted.h>
46#include <wtf/RefPtr.h>
47
48namespace WebCore {
49
50static bool extractBooleanValue(const v8::Handle<v8::Object>& object, const char* name, ExceptionCode& ec) {
51    ec = 0;
52    v8::Local<v8::Value> value = object->Get(v8::String::New(name));
53    if (!value.IsEmpty() && !isUndefinedOrNull(value)) {
54        v8::TryCatch block;
55        bool nativeValue = value->BooleanValue();
56        if (block.HasCaught()) {
57            ec = TYPE_MISMATCH_ERR;
58            return false;
59        }
60        return nativeValue;
61    }
62    return false;
63}
64
65static PassRefPtr<WebKitFlags> getFlags(const v8::Local<v8::Value>& arg, ExceptionCode& ec)
66{
67    ec = 0;
68    if (isUndefinedOrNull(arg) || !arg->IsObject())
69        return 0;
70    if (V8WebKitFlags::HasInstance(arg))
71        return V8WebKitFlags::toNative(v8::Handle<v8::Object>::Cast(arg));
72
73    v8::Handle<v8::Object> object;
74    {
75        v8::TryCatch block;
76        object = v8::Handle<v8::Object>::Cast(arg);
77        if (block.HasCaught()) {
78            ec = TYPE_MISMATCH_ERR;
79            return 0;
80        }
81    }
82
83    bool isCreate = extractBooleanValue(object, "create", ec);
84    if (ec)
85        return 0;
86    bool isExclusive = extractBooleanValue(object, "exclusive", ec);
87    if (ec)
88        return 0;
89
90    RefPtr<WebKitFlags> flags = WebKitFlags::create();
91    flags->setCreate(isCreate);
92    flags->setExclusive(isExclusive);
93
94    return flags;
95}
96
97v8::Handle<v8::Value> V8DirectoryEntrySync::getDirectoryCallback(const v8::Arguments& args)
98{
99    INC_STATS("DOM.DirectoryEntrySync.getDirectory");
100    DirectoryEntrySync* imp = V8DirectoryEntrySync::toNative(args.Holder());
101    ExceptionCode ec = 0;
102    STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, path, args[0]);
103    RefPtr<WebKitFlags> flags = getFlags(args[1], ec);
104    if (UNLIKELY(ec)) {
105        V8Proxy::setDOMException(ec);
106        return v8::Handle<v8::Value>();
107    }
108    RefPtr<DirectoryEntrySync> result = imp->getDirectory(path, flags, ec);
109    if (UNLIKELY(ec)) {
110        V8Proxy::setDOMException(ec);
111        return v8::Handle<v8::Value>();
112    }
113    return toV8(result.release());
114}
115
116v8::Handle<v8::Value> V8DirectoryEntrySync::getFileCallback(const v8::Arguments& args)
117{
118    INC_STATS("DOM.DirectoryEntrySync.getFile");
119    DirectoryEntrySync* imp = V8DirectoryEntrySync::toNative(args.Holder());
120    ExceptionCode ec = 0;
121    STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, path, args[0]);
122    RefPtr<WebKitFlags> flags = getFlags(args[1], ec);
123    if (UNLIKELY(ec)) {
124        V8Proxy::setDOMException(ec);
125        return v8::Handle<v8::Value>();
126    }
127    RefPtr<FileEntrySync> result = imp->getFile(path, flags, ec);
128    if (UNLIKELY(ec)) {
129        V8Proxy::setDOMException(ec);
130        return v8::Handle<v8::Value>();
131    }
132    return toV8(result.release());
133}
134
135
136
137} // namespace WebCore
138
139#endif // ENABLE(FILE_SYSTEM)
140