Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/filesystem/compat/FileSystemRootDirectoryEntry.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "FileSystemRootDirectoryEntry.h"
8
#include "FileSystemRootDirectoryReader.h"
9
#include "mozilla/dom/FileSystemUtils.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
NS_IMPL_CYCLE_COLLECTION_INHERITED(FileSystemRootDirectoryEntry,
15
                                   FileSystemDirectoryEntry, mEntries)
16
17
NS_IMPL_ADDREF_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
18
NS_IMPL_RELEASE_INHERITED(FileSystemRootDirectoryEntry, FileSystemDirectoryEntry)
19
20
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(FileSystemRootDirectoryEntry)
21
0
NS_INTERFACE_MAP_END_INHERITING(FileSystemDirectoryEntry)
22
23
FileSystemRootDirectoryEntry::FileSystemRootDirectoryEntry(nsIGlobalObject* aGlobal,
24
                                                           const Sequence<RefPtr<FileSystemEntry>>& aEntries,
25
                                                           FileSystem* aFileSystem)
26
  : FileSystemDirectoryEntry(aGlobal, nullptr, nullptr, aFileSystem)
27
  , mEntries(aEntries)
28
0
{
29
0
  MOZ_ASSERT(aGlobal);
30
0
}
31
32
FileSystemRootDirectoryEntry::~FileSystemRootDirectoryEntry()
33
0
{}
34
35
void
36
FileSystemRootDirectoryEntry::GetName(nsAString& aName, ErrorResult& aRv) const
37
0
{
38
0
  aName.Truncate();
39
0
}
40
41
void
42
FileSystemRootDirectoryEntry::GetFullPath(nsAString& aPath, ErrorResult& aRv) const
43
0
{
44
0
  aPath.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
45
0
}
46
47
already_AddRefed<FileSystemDirectoryReader>
48
FileSystemRootDirectoryEntry::CreateReader()
49
0
{
50
0
  RefPtr<FileSystemDirectoryReader> reader =
51
0
    new FileSystemRootDirectoryReader(this, Filesystem(), mEntries);
52
0
  return reader.forget();
53
0
}
54
55
void
56
FileSystemRootDirectoryEntry::GetInternal(const nsAString& aPath,
57
                                          const FileSystemFlags& aFlag,
58
                                          const Optional<OwningNonNull<FileSystemEntryCallback>>& aSuccessCallback,
59
                                          const Optional<OwningNonNull<ErrorCallback>>& aErrorCallback,
60
                                          GetInternalType aType)
61
0
{
62
0
  if (!aSuccessCallback.WasPassed() && !aErrorCallback.WasPassed()) {
63
0
    return;
64
0
  }
65
0
66
0
  if (aFlag.mCreate) {
67
0
    ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
68
0
                              NS_ERROR_DOM_SECURITY_ERR);
69
0
    return;
70
0
  }
71
0
72
0
  nsTArray<nsString> parts;
73
0
  if (!FileSystemUtils::IsValidRelativeDOMPath(aPath, parts)) {
74
0
    ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
75
0
                              NS_ERROR_DOM_NOT_FOUND_ERR);
76
0
    return;
77
0
  }
78
0
79
0
  MOZ_ASSERT(!parts.IsEmpty());
80
0
81
0
  RefPtr<FileSystemEntry> entry;
82
0
  for (uint32_t i = 0; i < mEntries.Length(); ++i) {
83
0
    ErrorResult rv;
84
0
    nsAutoString name;
85
0
    mEntries[i]->GetName(name, rv);
86
0
87
0
    if (NS_WARN_IF(rv.Failed())) {
88
0
      ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
89
0
                                rv.StealNSResult());
90
0
      return;
91
0
    }
92
0
93
0
    if (name == parts[0]) {
94
0
      entry = mEntries[i];
95
0
      break;
96
0
    }
97
0
  }
98
0
99
0
  // Not found.
100
0
  if (!entry) {
101
0
    ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
102
0
                              NS_ERROR_DOM_NOT_FOUND_ERR);
103
0
    return;
104
0
  }
105
0
106
0
  // No subdirectory in the path.
107
0
  if (parts.Length() == 1) {
108
0
    if ((entry->IsFile() && aType == eGetDirectory) ||
109
0
        (entry->IsDirectory() && aType == eGetFile)) {
110
0
      ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
111
0
                                NS_ERROR_DOM_TYPE_MISMATCH_ERR);
112
0
      return;
113
0
    }
114
0
115
0
    if (aSuccessCallback.WasPassed()) {
116
0
      RefPtr<EntryCallbackRunnable> runnable =
117
0
        new EntryCallbackRunnable(&aSuccessCallback.Value(), entry);
118
0
119
0
      FileSystemUtils::DispatchRunnable(GetParentObject(), runnable.forget());
120
0
121
0
    }
122
0
    return;
123
0
  }
124
0
125
0
  // Subdirectories, but this is a file.
126
0
  if (entry->IsFile()) {
127
0
    ErrorCallbackHelper::Call(GetParentObject(), aErrorCallback,
128
0
                              NS_ERROR_DOM_NOT_FOUND_ERR);
129
0
    return;
130
0
  }
131
0
132
0
  // Let's recreate a path without the first directory.
133
0
  nsAutoString path;
134
0
  for (uint32_t i = 1, len = parts.Length(); i < len; ++i) {
135
0
    path.Append(parts[i]);
136
0
    if (i < len - 1) {
137
0
      path.AppendLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
138
0
    }
139
0
  }
140
0
141
0
  auto* directoryEntry = static_cast<FileSystemDirectoryEntry*>(entry.get());
142
0
  directoryEntry->GetInternal(path, aFlag, aSuccessCallback, aErrorCallback,
143
0
                              aType);
144
0
}
145
146
} // dom namespace
147
} // mozilla namespace