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 "third_party/blink/renderer/modules/filesystem/directory_entry_sync.h"
32 
33 #include "third_party/blink/renderer/bindings/modules/v8/v8_file_system_flags.h"
34 #include "third_party/blink/renderer/modules/filesystem/directory_reader_sync.h"
35 #include "third_party/blink/renderer/modules/filesystem/entry.h"
36 #include "third_party/blink/renderer/modules/filesystem/file_entry_sync.h"
37 #include "third_party/blink/renderer/modules/filesystem/sync_callback_helper.h"
38 #include "third_party/blink/renderer/platform/bindings/exception_state.h"
39 #include "third_party/blink/renderer/platform/wtf/casting.h"
40 
41 namespace blink {
42 
DirectoryEntrySync(DOMFileSystemBase * file_system,const String & full_path)43 DirectoryEntrySync::DirectoryEntrySync(DOMFileSystemBase* file_system,
44                                        const String& full_path)
45     : EntrySync(file_system, full_path) {}
46 
createReader()47 DirectoryReaderSync* DirectoryEntrySync::createReader() {
48   return MakeGarbageCollected<DirectoryReaderSync>(file_system_, full_path_);
49 }
50 
getFile(const String & path,const FileSystemFlags * options,ExceptionState & exception_state)51 FileEntrySync* DirectoryEntrySync::getFile(const String& path,
52                                            const FileSystemFlags* options,
53                                            ExceptionState& exception_state) {
54   auto* sync_helper = MakeGarbageCollected<EntryCallbacksSyncHelper>();
55 
56   auto success_callback_wrapper =
57       WTF::Bind(&EntryCallbacksSyncHelper::OnSuccess,
58                 WrapPersistentIfNeeded(sync_helper));
59   auto error_callback_wrapper = WTF::Bind(&EntryCallbacksSyncHelper::OnError,
60                                           WrapPersistentIfNeeded(sync_helper));
61 
62   file_system_->GetFile(
63       this, path, options, std::move(success_callback_wrapper),
64       std::move(error_callback_wrapper), DOMFileSystemBase::kSynchronous);
65   Entry* entry = sync_helper->GetResultOrThrow(exception_state);
66   return entry ? To<FileEntrySync>(EntrySync::Create(entry)) : nullptr;
67 }
68 
getDirectory(const String & path,const FileSystemFlags * options,ExceptionState & exception_state)69 DirectoryEntrySync* DirectoryEntrySync::getDirectory(
70     const String& path,
71     const FileSystemFlags* options,
72     ExceptionState& exception_state) {
73   auto* sync_helper = MakeGarbageCollected<EntryCallbacksSyncHelper>();
74 
75   auto success_callback_wrapper =
76       WTF::Bind(&EntryCallbacksSyncHelper::OnSuccess,
77                 WrapPersistentIfNeeded(sync_helper));
78   auto error_callback_wrapper = WTF::Bind(&EntryCallbacksSyncHelper::OnError,
79                                           WrapPersistentIfNeeded(sync_helper));
80 
81   file_system_->GetDirectory(
82       this, path, options, std::move(success_callback_wrapper),
83       std::move(error_callback_wrapper), DOMFileSystemBase::kSynchronous);
84 
85   Entry* entry = sync_helper->GetResultOrThrow(exception_state);
86   return entry ? To<DirectoryEntrySync>(EntrySync::Create(entry)) : nullptr;
87 }
88 
removeRecursively(ExceptionState & exception_state)89 void DirectoryEntrySync::removeRecursively(ExceptionState& exception_state) {
90   auto* sync_helper = MakeGarbageCollected<VoidCallbacksSyncHelper>();
91 
92   auto error_callback_wrapper = WTF::Bind(&VoidCallbacksSyncHelper::OnError,
93                                           WrapPersistentIfNeeded(sync_helper));
94 
95   file_system_->RemoveRecursively(this, VoidCallbacks::SuccessCallback(),
96                                   std::move(error_callback_wrapper),
97                                   DOMFileSystemBase::kSynchronous);
98   sync_helper->GetResultOrThrow(exception_state);
99 }
100 
Trace(Visitor * visitor)101 void DirectoryEntrySync::Trace(Visitor* visitor) {
102   EntrySync::Trace(visitor);
103 }
104 
105 }  // namespace blink
106