Commit 510bd2d0 authored by Hazim Mohamed's avatar Hazim Mohamed Committed by Commit Bot

[NativeFS] Create IDL to retrieve a NativeFileSystemHandle from a DataTransferItem

In order to integrate Drag & Drop with NativeFileSystem,
 we need to have a DataObjectItem own a remote to a
 NativeFileSystemTransferToken. A JavaScript exposed
function will then take the remote NativeFileSystemTransferToken,
redeem it for a NativeFileSystemFileHandle or
 NativeFileSystemDirectoryHandle, and expose the
resulting handle to the Web.

Bug: 1080811
Change-Id: I725626758d61272d548ccbe800014bbe433e02b0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2243998Reviewed-by: default avatarNate Chapin <japhet@chromium.org>
Reviewed-by: default avatarMarijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarDarwin Huang <huangdarwin@chromium.org>
Commit-Queue: Hazim Mohamed <hazimmohamed@google.com>
Cr-Commit-Position: refs/heads/master@{#784901}
parent dbd953b2
...@@ -2679,6 +2679,7 @@ enum WebFeature { ...@@ -2679,6 +2679,7 @@ enum WebFeature {
kV8Window_GetOriginPrivateDirectory_Method = 3343, kV8Window_GetOriginPrivateDirectory_Method = 3343,
kRTCConstraintEnableRtpDataChannelsTrue = 3344, kRTCConstraintEnableRtpDataChannelsTrue = 3344,
kRTCConstraintEnableRtpDataChannelsFalse = 3345, kRTCConstraintEnableRtpDataChannelsFalse = 3345,
kNativeFileSystemDragAndDrop = 3346,
// Add new features immediately above this line. Don't change assigned // Add new features immediately above this line. Don't change assigned
// numbers of any item, and don't reuse removed slots. // numbers of any item, and don't reuse removed slots.
......
...@@ -366,6 +366,7 @@ static_idl_files_in_modules = get_path_info( ...@@ -366,6 +366,7 @@ static_idl_files_in_modules = get_path_info(
"//third_party/blink/renderer/modules/mediastream/window_media_stream.idl", "//third_party/blink/renderer/modules/mediastream/window_media_stream.idl",
"//third_party/blink/renderer/modules/native_file_system/choose_file_system_entries_options.idl", "//third_party/blink/renderer/modules/native_file_system/choose_file_system_entries_options.idl",
"//third_party/blink/renderer/modules/native_file_system/choose_file_system_entries_options_accepts.idl", "//third_party/blink/renderer/modules/native_file_system/choose_file_system_entries_options_accepts.idl",
"//third_party/blink/renderer/modules/native_file_system/data_transfer_item_native_file_system.idl",
"//third_party/blink/renderer/modules/native_file_system/file_system_create_writer_options.idl", "//third_party/blink/renderer/modules/native_file_system/file_system_create_writer_options.idl",
"//third_party/blink/renderer/modules/native_file_system/file_system_directory_handle.idl", "//third_party/blink/renderer/modules/native_file_system/file_system_directory_handle.idl",
"//third_party/blink/renderer/modules/native_file_system/file_system_file_handle.idl", "//third_party/blink/renderer/modules/native_file_system/file_system_file_handle.idl",
......
...@@ -222,6 +222,29 @@ String DataObjectItem::FileSystemId() const { ...@@ -222,6 +222,29 @@ String DataObjectItem::FileSystemId() const {
return file_system_id_; return file_system_id_;
} }
bool DataObjectItem::HasNativeFileSystemEntry() const {
return native_file_system_entry_.has_value();
}
String DataObjectItem::NativeFileSystemFileName() const {
DCHECK(HasNativeFileSystemEntry());
return native_file_system_entry_->name;
}
bool DataObjectItem::NativeFileSystemEntryIsDirectory() const {
DCHECK(HasNativeFileSystemEntry());
return native_file_system_entry_->is_directory;
}
mojo::PendingRemote<mojom::blink::NativeFileSystemTransferToken>
DataObjectItem::CloneNativeFileSystemEntryToken() const {
DCHECK(HasNativeFileSystemEntry());
mojo::PendingRemote<mojom::blink::NativeFileSystemTransferToken> token_clone;
native_file_system_entry_->token_remote->Clone(
token_clone.InitWithNewPipeAndPassReceiver());
return token_clone;
}
void DataObjectItem::Trace(Visitor* visitor) const { void DataObjectItem::Trace(Visitor* visitor) const {
visitor->Trace(file_); visitor->Trace(file_);
visitor->Trace(system_clipboard_); visitor->Trace(system_clipboard_);
......
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_DATA_OBJECT_ITEM_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_DATA_OBJECT_ITEM_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_DATA_OBJECT_ITEM_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_CLIPBOARD_DATA_OBJECT_ITEM_H_
#include "base/optional.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_transfer_token.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/fileapi/file.h" #include "third_party/blink/renderer/core/fileapi/file.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
...@@ -89,14 +92,40 @@ class CORE_EXPORT DataObjectItem final ...@@ -89,14 +92,40 @@ class CORE_EXPORT DataObjectItem final
bool HasFileSystemId() const; bool HasFileSystemId() const;
String FileSystemId() const; String FileSystemId() const;
bool HasNativeFileSystemEntry() const;
String NativeFileSystemFileName() const;
bool NativeFileSystemEntryIsDirectory() const;
mojo::PendingRemote<mojom::blink::NativeFileSystemTransferToken>
CloneNativeFileSystemEntryToken() const;
void Trace(Visitor*) const; void Trace(Visitor*) const;
private: private:
// Incoming DroppedNativeFileSystemEntriy objects are stored using this
// intermediary type. This is because this class must own a
// Remote<NativeFileSystemTransferToken> to clone tokens, and the Mojo
// DroppedNativeFileSystemEntry struct only allows storing a
// PendingRemote<NativeFileSystemTransferToken>.
struct NativeFileSystemEntryData {
NativeFileSystemEntryData(
String name,
bool is_directory,
mojo::Remote<mojom::blink::NativeFileSystemTransferToken> token_remote)
: name(name),
is_directory(is_directory),
token_remote(std::move(token_remote)) {}
String name;
bool is_directory;
mojo::Remote<mojom::blink::NativeFileSystemTransferToken> token_remote;
};
enum class DataSource { enum class DataSource {
kClipboardSource, kClipboardSource,
kInternalSource, kInternalSource,
}; };
base::Optional<NativeFileSystemEntryData> native_file_system_entry_;
DataSource source_; DataSource source_;
ItemKind kind_; ItemKind kind_;
String type_; String type_;
......
...@@ -6,6 +6,8 @@ import("//third_party/blink/renderer/modules/modules.gni") ...@@ -6,6 +6,8 @@ import("//third_party/blink/renderer/modules/modules.gni")
blink_modules_sources("native_file_system") { blink_modules_sources("native_file_system") {
sources = [ sources = [
"data_transfer_item_native_file_system.cc",
"data_transfer_item_native_file_system.h",
"global_native_file_system.cc", "global_native_file_system.cc",
"global_native_file_system.h", "global_native_file_system.h",
"native_file_system_directory_handle.cc", "native_file_system_directory_handle.cc",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/native_file_system/data_transfer_item_native_file_system.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_directory_handle.mojom-blink.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_file_handle.mojom-blink.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_manager.mojom-blink.h"
#include "third_party/blink/public/mojom/native_file_system/native_file_system_transfer_token.mojom-blink.h"
#include "third_party/blink/renderer/core/clipboard/data_object_item.h"
#include "third_party/blink/renderer/core/clipboard/data_transfer.h"
#include "third_party/blink/renderer/core/clipboard/data_transfer_item.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/native_file_system/native_file_system_directory_handle.h"
#include "third_party/blink/renderer/modules/native_file_system/native_file_system_file_handle.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
// static
NativeFileSystemHandle* DataTransferItemNativeFileSystem::getAsFileSystemHandle(
ScriptState* script_state,
DataTransferItem& data_transfer_item) {
if (!data_transfer_item.GetDataTransfer()->CanReadData())
return nullptr;
// If the DataObjectItem doesn't have an associated NativeFileSystemEntry,
// return nullptr.
if (!data_transfer_item.GetDataObjectItem()->HasNativeFileSystemEntry()) {
return nullptr;
}
mojo::Remote<mojom::blink::NativeFileSystemManager> nfs_manager;
ExecutionContext* execution_context = ExecutionContext::From(script_state);
execution_context->GetBrowserInterfaceBroker().GetInterface(
nfs_manager.BindNewPipeAndPassReceiver());
const DataObjectItem& data_object_item =
*data_transfer_item.GetDataObjectItem();
// Since tokens are move-only, we need to create a clone in order
// to preserve the state of |data_object_item| for future calls.
mojo::PendingRemote<mojom::blink::NativeFileSystemTransferToken>
token_remote = data_object_item.CloneNativeFileSystemEntryToken();
// Resolve and return the token as a
// NativeFileSystemDirectoryHandle/NativeFileSystemFileHandle.
if (data_object_item.NativeFileSystemEntryIsDirectory()) {
mojo::PendingRemote<mojom::blink::NativeFileSystemDirectoryHandle>
dir_remote;
nfs_manager->GetDirectoryHandleFromToken(
std::move(token_remote), dir_remote.InitWithNewPipeAndPassReceiver());
return MakeGarbageCollected<blink::NativeFileSystemDirectoryHandle>(
execution_context, data_object_item.NativeFileSystemFileName(),
std::move(dir_remote));
} else {
mojo::PendingRemote<blink::mojom::blink::NativeFileSystemFileHandle>
file_remote;
nfs_manager->GetFileHandleFromToken(
std::move(token_remote), file_remote.InitWithNewPipeAndPassReceiver());
return MakeGarbageCollected<blink::NativeFileSystemFileHandle>(
execution_context, data_object_item.NativeFileSystemFileName(),
std::move(file_remote));
}
}
} // namespace blink
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_FILE_SYSTEM_DATA_TRANSFER_ITEM_NATIVE_FILE_SYSTEM_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_FILE_SYSTEM_DATA_TRANSFER_ITEM_NATIVE_FILE_SYSTEM_H_
#include "third_party/blink/renderer/core/clipboard/data_transfer_item.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/native_file_system/native_file_system_handle.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
namespace blink {
class DataTransferItemNativeFileSystem {
STATIC_ONLY(DataTransferItemNativeFileSystem);
public:
static NativeFileSystemHandle* getAsFileSystemHandle(ScriptState*,
DataTransferItem&);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_NATIVE_FILE_SYSTEM_DATA_TRANSFER_ITEM_NATIVE_FILE_SYSTEM_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
[
ImplementedAs=DataTransferItemNativeFileSystem,
RuntimeEnabled=NativeFileSystemDragAndDrop
] partial interface DataTransferItem {
[CallWith=ScriptState, MeasureAs=NativeFileSystemDragAndDrop] FileSystemHandle? getAsFileSystemHandle();
};
...@@ -31,4 +31,5 @@ modules_dictionary_idl_files = [ ...@@ -31,4 +31,5 @@ modules_dictionary_idl_files = [
modules_dependency_idl_files = [ modules_dependency_idl_files = [
"window_native_file_system.idl", "window_native_file_system.idl",
"worker_global_scope_native_file_system.idl", "worker_global_scope_native_file_system.idl",
"data_transfer_item_native_file_system.idl",
] ]
...@@ -1180,6 +1180,10 @@ ...@@ -1180,6 +1180,10 @@
origin_trial_feature_name: "NativeFileSystem2", origin_trial_feature_name: "NativeFileSystem2",
origin_trial_os: ["win", "macosx", "linux", "chromeos"], origin_trial_os: ["win", "macosx", "linux", "chromeos"],
}, },
{
name: "NativeFileSystemDragAndDrop",
status: "test"
},
{ {
name: "NativeIO", name: "NativeIO",
status: "experimental", status: "experimental",
......
...@@ -1480,6 +1480,7 @@ interface DataTransferItem ...@@ -1480,6 +1480,7 @@ interface DataTransferItem
getter type getter type
method constructor method constructor
method getAsFile method getAsFile
method getAsFileSystemHandle
method getAsString method getAsString
method webkitGetAsEntry method webkitGetAsEntry
interface DataTransferItemList interface DataTransferItemList
......
...@@ -28255,6 +28255,7 @@ Called by update_use_counter_feature_enum.py.--> ...@@ -28255,6 +28255,7 @@ Called by update_use_counter_feature_enum.py.-->
<int value="3343" label="V8Window_GetOriginPrivateDirectory_Method"/> <int value="3343" label="V8Window_GetOriginPrivateDirectory_Method"/>
<int value="3344" label="RTCConstraintEnableRtpDataChannelsTrue"/> <int value="3344" label="RTCConstraintEnableRtpDataChannelsTrue"/>
<int value="3345" label="RTCConstraintEnableRtpDataChannelsFalse"/> <int value="3345" label="RTCConstraintEnableRtpDataChannelsFalse"/>
<int value="3346" label="NativeFileSystemDragAndDrop"/>
</enum> </enum>
<enum name="FeaturePolicyAllowlistType"> <enum name="FeaturePolicyAllowlistType">
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