1/*
2 * Copyright (C) 2013 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 "public/web/WebDOMFileSystem.h"
33
34#include "bindings/core/v8/WrapperTypeInfo.h"
35#include "bindings/modules/v8/V8DOMFileSystem.h"
36#include "bindings/modules/v8/V8DirectoryEntry.h"
37#include "bindings/modules/v8/V8FileEntry.h"
38#include "core/dom/Document.h"
39#include "modules/filesystem/DOMFileSystem.h"
40#include "modules/filesystem/DirectoryEntry.h"
41#include "modules/filesystem/FileEntry.h"
42#include "web/WebLocalFrameImpl.h"
43#include <v8.h>
44
45namespace blink {
46
47WebDOMFileSystem WebDOMFileSystem::fromV8Value(v8::Handle<v8::Value> value)
48{
49    if (!V8DOMFileSystem::hasInstance(value, v8::Isolate::GetCurrent()))
50        return WebDOMFileSystem();
51    v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(value);
52    DOMFileSystem* domFileSystem = V8DOMFileSystem::toImpl(object);
53    ASSERT(domFileSystem);
54    return WebDOMFileSystem(domFileSystem);
55}
56
57WebURL WebDOMFileSystem::createFileSystemURL(v8::Handle<v8::Value> value)
58{
59    const FileEntry* const entry = V8FileEntry::toImplWithTypeCheck(v8::Isolate::GetCurrent(), value);
60    if (entry)
61        return entry->filesystem()->createFileSystemURL(entry);
62    return WebURL();
63}
64
65WebDOMFileSystem WebDOMFileSystem::create(
66    WebLocalFrame* frame,
67    WebFileSystemType type,
68    const WebString& name,
69    const WebURL& rootURL,
70    SerializableType serializableType)
71{
72    ASSERT(frame && toWebLocalFrameImpl(frame)->frame());
73    DOMFileSystem* domFileSystem = DOMFileSystem::create(toWebLocalFrameImpl(frame)->frame()->document(), name, static_cast<FileSystemType>(type), rootURL);
74    if (serializableType == SerializableTypeSerializable)
75        domFileSystem->makeClonable();
76    return WebDOMFileSystem(domFileSystem);
77}
78
79void WebDOMFileSystem::reset()
80{
81    m_private.reset();
82}
83
84void WebDOMFileSystem::assign(const WebDOMFileSystem& other)
85{
86    m_private = other.m_private;
87}
88
89WebString WebDOMFileSystem::name() const
90{
91    ASSERT(m_private.get());
92    return m_private->name();
93}
94
95WebFileSystem::Type WebDOMFileSystem::type() const
96{
97    ASSERT(m_private.get());
98    switch (m_private->type()) {
99    case FileSystemTypeTemporary:
100        return WebFileSystem::TypeTemporary;
101    case FileSystemTypePersistent:
102        return WebFileSystem::TypePersistent;
103    case FileSystemTypeIsolated:
104        return WebFileSystem::TypeIsolated;
105    case FileSystemTypeExternal:
106        return WebFileSystem::TypeExternal;
107    default:
108        ASSERT_NOT_REACHED();
109        return WebFileSystem::TypeTemporary;
110    }
111}
112
113WebURL WebDOMFileSystem::rootURL() const
114{
115    ASSERT(m_private.get());
116    return m_private->rootURL();
117}
118
119v8::Handle<v8::Value> WebDOMFileSystem::toV8Value(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
120{
121    if (!m_private.get())
122        return v8::Handle<v8::Value>();
123    return toV8(m_private.get(), creationContext, isolate);
124}
125
126v8::Handle<v8::Value> WebDOMFileSystem::createV8Entry(
127    const WebString& path,
128    EntryType entryType,
129    v8::Handle<v8::Object> creationContext,
130    v8::Isolate* isolate)
131{
132    if (!m_private.get())
133        return v8::Handle<v8::Value>();
134    if (entryType == EntryTypeDirectory)
135        return toV8(DirectoryEntry::create(m_private.get(), path), creationContext, isolate);
136    ASSERT(entryType == EntryTypeFile);
137    return toV8(FileEntry::create(m_private.get(), path), creationContext, isolate);
138}
139
140WebDOMFileSystem::WebDOMFileSystem(DOMFileSystem* domFileSystem)
141    : m_private(domFileSystem)
142{
143}
144
145WebDOMFileSystem& WebDOMFileSystem::operator=(DOMFileSystem* domFileSystem)
146{
147    m_private = domFileSystem;
148    return *this;
149}
150
151} // namespace blink
152