Commit be99bdb9 authored by Marijn Kruisselbrink's avatar Marijn Kruisselbrink Committed by Commit Bot

[NativeFS] Add kind enum to replace isFile/isDirectory.

Following changes in https://github.com/WICG/native-file-system/pull/198

Bug: 1103841
Change-Id: I4c67d6eb6cf605bb10d4ace83594900bfe1157ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2290535
Commit-Queue: Marijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#787046}
parent ed6924e2
......@@ -2,6 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// https://wicg.github.io/native-file-system/#enumdef-filesystemhandlekind
enum FileSystemHandleKind {
"file",
"directory",
};
// https://wicg.github.io/native-file-system/#filesystemhandle
[
Exposed=(Window,Worker),
......@@ -13,8 +19,9 @@
// Brand checking APIs because javascript makes it otherwise really hard to
// figure out what type an object is when you don't know in which global
// (i.e. iframe) the object was created.
readonly attribute boolean isFile;
readonly attribute boolean isDirectory;
[RuntimeEnabled=LegacyNativeFileSystem] readonly attribute boolean isFile;
[RuntimeEnabled=LegacyNativeFileSystem] readonly attribute boolean isDirectory;
readonly attribute FileSystemHandleKind kind;
readonly attribute USVString name;
......
......@@ -35,6 +35,7 @@ class NativeFileSystemHandle : public ScriptWrappable,
virtual bool isFile() const { return false; }
virtual bool isDirectory() const { return false; }
const char* kind() const { return isFile() ? "file" : "directory"; }
const String& name() const { return name_; }
ScriptPromise queryPermission(ScriptState*,
......
......@@ -15,16 +15,15 @@ async function serialize_handles(handle_array) {
// Serializes either a FileSystemFileHandle or FileSystemDirectoryHandle.
async function serialize_handle(handle) {
let serialized;
if (handle.isDirectory) {
serialized = await serialize_file_system_directory_handle(handle);
} else if (handle.isFile) {
serialized = await serialize_file_system_file_handle(handle);
} else {
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${handle}`;
switch (handle.kind) {
case 'directory':
return await serialize_file_system_directory_handle(handle);
case 'file':
return await serialize_file_system_file_handle(handle);
default:
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${handle}`;
}
return serialized;
}
// Creates a dictionary for a FileSystemHandle base, which contains
......@@ -38,8 +37,7 @@ async function serialize_file_system_handle(handle) {
await handle.queryPermission({ writable: true })
return {
is_file: handle.isFile,
is_directory: handle.isDirectory,
kind: handle.kind,
name: handle.name,
read_permission,
write_permission
......@@ -50,8 +48,7 @@ async function serialize_file_system_handle(handle) {
// Also, reads the contents of the file to include with the returned
// dictionary. Example output:
// {
// is_file: true,
// is_directory: false,
// kind: "file",
// name: "example-file-name"
// read_permission: "granted",
// write_permission: "granted",
......@@ -69,8 +66,7 @@ async function serialize_file_system_file_handle(file_handle) {
// Create a dictionary with each property value in FileSystemDirectoryHandle.
// Example output:
// {
// is_file: false,
// is_directory: true,
// kind: "directory",
// name: "example-directory-name"
// read_permission: "granted",
// write_permission: "granted",
......@@ -83,7 +79,7 @@ async function serialize_file_system_directory_handle(directory_handle) {
const serialized_directories = [];
for await (const child_handle of directory_handle.getEntries()) {
const serialized_child_handle = await serialize_handle(child_handle);
if (child_handle.isDirectory) {
if (child_handle.kind === "directory") {
serialized_directories.push(serialized_child_handle);
} else {
serialized_files.push(serialized_child_handle);
......@@ -138,24 +134,24 @@ function assert_equals_serialized_handles(left_array, right_array) {
// Verifies each property of a serialized FileSystemFileHandle or
// FileSystemDirectoryHandle.
function assert_equals_serialized_handle(left, right) {
if (left.is_directory) {
assert_equals_serialized_file_system_directory_handle(left, right);
} else if (left.is_file) {
assert_equals_serialized_file_system_file_handle(left, right);
} else {
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${left}`;
switch (left.kind) {
case 'directory':
assert_equals_serialized_file_system_directory_handle(left, right);
break;
case 'file':
assert_equals_serialized_file_system_file_handle(left, right);
break;
default:
throw 'Object is not a FileSystemFileHandle or ' +
`FileSystemDirectoryHandle ${left}`;
}
}
// Compares the output of serialize_file_system_handle() for
// two FileSystemHandles.
function assert_equals_serialized_file_system_handle(left, right) {
assert_equals(left.is_file, right.is_file,
'Each FileSystemHandle instance must use the expected "isFile".');
assert_equals(left.is_directory, right.is_directory,
'Each FileSystemHandle instance must use the expected "isDirectory".');
assert_equals(left.kind, right.kind,
'Each FileSystemHandle instance must use the expected "kind".');
assert_equals(left.name, right.name,
'Each FileSystemHandle instance must use the expected "name" ' +
......
......@@ -33,7 +33,8 @@ function directory_test(func, description) {
// To be resilient against tests not cleaning up properly, cleanup before
// every test.
for await (let entry of directory.getEntries()) {
await directory.removeEntry(entry.name, {recursive: entry.isDirectory});
await directory.removeEntry(
entry.name, {recursive: entry.kind === 'directory'});
}
await func(t, directory);
}, description);
......
......@@ -10,7 +10,7 @@ async function cleanupSandboxedFileSystem() {
const dir =
await self.getOriginPrivateDirectory();
for await (let entry of dir.getEntries())
await dir.removeEntry(entry.name, {recursive: entry.isDirectory});
await dir.removeEntry(entry.name, {recursive: entry.kind === 'directory'});
}
function directory_test(func, description) {
......
......@@ -34,7 +34,7 @@ async function getDirectoryEntryCount(handle) {
async function getSortedDirectoryEntries(handle) {
let result = [];
for await (let entry of handle.getEntries()) {
if (entry.isDirectory)
if (entry.kind === 'directory')
result.push(entry.name + '/');
else
result.push(entry.name);
......
......@@ -8,8 +8,7 @@ directory_test(async (t, root) => {
await root.getDirectoryHandle('non-existing-dir', {create: true});
t.add_cleanup(() => root.removeEntry('non-existing-dir', {recursive: true}));
assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.kind, 'directory');
assert_equals(handle.name, 'non-existing-dir');
assert_equals(await getDirectoryEntryCount(handle), 0);
assert_array_equals(
......@@ -25,8 +24,7 @@ directory_test(async (t, root) => {
const handle =
await root.getDirectoryHandle('dir-with-contents', {create: false});
assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.kind, 'directory');
assert_equals(handle.name, 'dir-with-contents');
assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']);
}, 'getDirectoryHandle(create=false) returns existing directories');
......@@ -41,8 +39,7 @@ directory_test(async (t, root) => {
const handle =
await root.getDirectoryHandle('dir-with-contents', {create: true});
assert_false(handle.isFile);
assert_true(handle.isDirectory);
assert_equals(handle.kind, 'directory');
assert_equals(handle.name, 'dir-with-contents');
assert_array_equals(await getSortedDirectoryEntries(handle), ['test-file']);
}, 'getDirectoryHandle(create=true) returns existing directories without erasing');
......
......@@ -7,8 +7,7 @@ directory_test(async (t, dir) => {
const handle = await dir.getFileHandle('non-existing-file', {create: true});
t.add_cleanup(() => dir.removeEntry('non-existing-file'));
assert_true(handle.isFile);
assert_false(handle.isDirectory);
assert_equals(handle.kind, 'file');
assert_equals(handle.name, 'non-existing-file');
assert_equals(await getFileSize(handle), 0);
assert_equals(await getFileContents(handle), '');
......@@ -19,8 +18,7 @@ directory_test(async (t, dir) => {
t, 'existing-file', '1234567890', /*parent=*/ dir);
const handle = await dir.getFileHandle('existing-file');
assert_true(handle.isFile);
assert_false(handle.isDirectory);
assert_equals(handle.kind, 'file');
assert_equals(handle.name, 'existing-file');
assert_equals(await getFileSize(handle), 10);
assert_equals(await getFileContents(handle), '1234567890');
......@@ -31,8 +29,7 @@ directory_test(async (t, dir) => {
t, 'file-with-contents', '1234567890', /*parent=*/ dir);
const handle = await dir.getFileHandle('file-with-contents', {create: true});
assert_true(handle.isFile);
assert_false(handle.isDirectory);
assert_equals(handle.kind, 'file');
assert_equals(handle.name, 'file-with-contents');
assert_equals(await getFileSize(handle), 10);
assert_equals(await getFileContents(handle), '1234567890');
......
......@@ -22,8 +22,7 @@
const dir = await self.showDirectoryPicker();
assert_true(dir instanceof FileSystemHandle);
assert_true(dir instanceof FileSystemDirectoryHandle);
assert_false(dir.isFile);
assert_true(dir.isDirectory);
assert_equals(dir.kind, "directory");
assert_equals(dir.name, 'data');
assert_array_equals(await getSortedDirectoryEntries(dir), ['testfile.txt']);
......
......@@ -29,8 +29,7 @@
assert_equals(files.length, 1);
assert_true(files[0] instanceof FileSystemHandle);
assert_true(files[0] instanceof FileSystemFileHandle);
assert_true(files[0].isFile);
assert_false(files[0].isDirectory);
assert_equals(files[0].kind, "file");
assert_equals(files[0].name, 'testfile.txt');
assert_equals(await (await files[0].getFile()).text(), 'Hello World!\n');
......
......@@ -19,8 +19,7 @@
});
assert_true(file instanceof FileSystemHandle);
assert_true(file instanceof FileSystemFileHandle);
assert_true(file.isFile);
assert_false(file.isDirectory);
assert_equals(file.kind, "file");
assert_equals(file.name, 'testfile.txt');
assert_equals(await (await file.getFile()).text(), '',
'showSaveFilePicker should clear contents of file');
......
......@@ -577,6 +577,7 @@ interface FileSystemHandle
attribute @@toStringTag
getter isDirectory
getter isFile
getter kind
getter name
method constructor
method isSameEntry
......
......@@ -512,6 +512,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] attribute @@toStringTag
[Worker] getter isDirectory
[Worker] getter isFile
[Worker] getter kind
[Worker] getter name
[Worker] method constructor
[Worker] method isSameEntry
......
......@@ -2458,6 +2458,7 @@ interface FileSystemHandle
attribute @@toStringTag
getter isDirectory
getter isFile
getter kind
getter name
method constructor
method isSameEntry
......
......@@ -507,6 +507,7 @@ Starting worker: resources/global-interface-listing-worker.js
[Worker] attribute @@toStringTag
[Worker] getter isDirectory
[Worker] getter isFile
[Worker] getter kind
[Worker] getter name
[Worker] method constructor
[Worker] method isSameEntry
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment