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