Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement BlobCollector #12269

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Implement BlobCollector",
"packageName": "react-native-windows",
"email": "julio.rocha@microsoft.com",
"dependentChangeType": "patch"
}
21 changes: 21 additions & 0 deletions vnext/Shared/Modules/BlobCollector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "BlobCollector.h"

using std::string;

namespace Microsoft::React {

using Networking::IBlobResource;

BlobCollector::BlobCollector(string blobId, std::shared_ptr<IBlobResource> resource) noexcept
: m_blobId{blobId}, m_weakResource{std::weak_ptr<IBlobResource>(resource)} {}

BlobCollector::~BlobCollector() noexcept {
if (auto rc = m_weakResource.lock()) {
rc->Release(std::move(m_blobId));
}
}

} // namespace Microsoft::React
23 changes: 23 additions & 0 deletions vnext/Shared/Modules/BlobCollector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include <Networking/IBlobResource.h>

// JSI
#include <jsi/jsi.h>

namespace Microsoft::React {

class JSI_EXPORT BlobCollector : public facebook::jsi::HostObject {
std::string m_blobId;
std::weak_ptr<Networking::IBlobResource> m_weakResource;

public:
BlobCollector(std::string blobId, std::shared_ptr<Networking::IBlobResource> resource) noexcept;

~BlobCollector() noexcept;
};

} // namespace Microsoft::React
19 changes: 19 additions & 0 deletions vnext/Shared/Modules/BlobModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "BlobModule.h"

#include <CreateModules.h>
#include <JSI/JsiApiContext.h>
#include <Modules/CxxModuleUtilities.h>
#include "BlobCollector.h"

// React Native
#include <cxxreact/JsArgumentHelpers.h>
Expand Down Expand Up @@ -38,6 +40,23 @@ void BlobTurboModule::Initialize(msrn::ReactContext const &reactContext) noexcep
m_resource->Callbacks().OnError = [&reactContext](string &&errorText) {
Modules::SendEvent(reactContext, L"blobFailed", {errorText});
};

namespace jsi = facebook::jsi;
msrn::ExecuteJsi(reactContext, [resource = m_resource](jsi::Runtime &runtime) {
runtime.global().setProperty(
runtime,
"__blobCollectorProvider",
jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "__blobCollectorProvider"),
1,
[resource](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, size_t count) {
auto blobId = args[0].asString(rt).utf8(rt);
auto collector = std::make_shared<BlobCollector>(blobId, resource);

return jsi::Object::createFromHostObject(rt, collector);
}));
});
}

ReactNativeSpecs::BlobModuleSpec_Constants BlobTurboModule::GetConstants() noexcept {
Expand Down
4 changes: 3 additions & 1 deletion vnext/Shared/Shared.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
<ClCompile Include="$(MSBuildThisFileDirectory)LayoutAnimation.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Logging.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)MemoryMappedBuffer.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\BlobModule.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\CxxModuleUtilities.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\ExceptionsManagerModule.cpp" />
Expand Down Expand Up @@ -327,6 +328,7 @@
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\RuntimeHolder.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)JSI\ScriptStore.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobModule.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\CxxModuleUtilities.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\FileReaderModule.h" />
Expand Down Expand Up @@ -565,4 +567,4 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
</ClCompile>
</ItemGroup>
</Project>
</Project>
6 changes: 6 additions & 0 deletions vnext/Shared/Shared.vcxitems.filters
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@
</ClCompile>
<ClCompile Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\Fabric\platform\react\renderer\components\view\HostPlatformViewProps.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\Fabric\platform\react\renderer\components\view\HostPlatformViewEventEmitter.cpp" />
<ClCompile Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.cpp">
<Filter>Source Files\Modules</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="Source Files">
Expand Down Expand Up @@ -750,6 +753,9 @@
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\Fabric\platform\react\renderer\components\view\HostPlatformTouch.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\Fabric\platform\react\renderer\components\view\HostPlatformViewEventEmitter.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobCollector.h">
<Filter>Header Files\Modules</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)tracing\rnw.wprp">
Expand Down
Loading