Skip to content
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/.vscode
/bin
/build
/venv
3 changes: 2 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"native-src/sync_root_interface/callbacks/FetchData/FetchData.cpp",
"native-src/sync_root_interface/callbacks/FetchData/FileCopierWithProgress.cpp",
"native-src/sync_root_interface/callbacks/FetchData/TransferContext.cpp",
"native-src/virtual_drive/Wrappers.cpp"
"native-src/virtual_drive/Wrappers.cpp",
"native-src/virtual_drive/register_sync_root.cpp"
],
"include_dirs": [
"include",
Expand Down
Binary file modified dist/addon.node
Binary file not shown.
21 changes: 21 additions & 0 deletions include/virtual_drive/napi_safe_wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef NAPI_SAFE_WRAP_H
#define NAPI_SAFE_WRAP_H

#include <node_api.h>
#include <exception>
#include <string>

template <typename Fn>
napi_value napi_safe_wrap(napi_env env, napi_callback_info info, Fn&& fn) {
try {
return fn(env, info);
} catch (const std::exception& e) {
napi_throw_error(env, nullptr, e.what());
} catch (...) {
napi_throw_error(env, nullptr, "Unknown native error");
}

return nullptr;
}

#endif
5 changes: 5 additions & 0 deletions include/virtual_drive/register_sync_root.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <node_api.h>

napi_value register_sync_root_impl(napi_env env, napi_callback_info args);
1 change: 0 additions & 1 deletion native-src/sync_root_interface/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void Utilities::AddFolderToSearchIndexer(_In_ PCWSTR folder)
void Utilities::ApplyTransferStateToFile(_In_ PCWSTR fullPath, _In_ CF_CALLBACK_INFO &callbackInfo, UINT64 total, UINT64 completed)
{
Logger::getInstance().log("ApplyTransferStateToFile", LogLevel::INFO);
printf("ApplyTransferStateToFile\n");
// Tell the Cloud File API about progress so that toasts can be displayed

HRESULT hr1 = CfReportProviderProgress(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ HRESULT FileCopierWithProgress::TransferData(
{
wprintf(L"Error in CfExecute(), HRESULT: %lx\n", hr);
}
printf("TransferData : %s\n", SUCCEEDED(hr) ? "Succeeded" : "Failed");
printf("TransferData: %s\n", SUCCEEDED(hr) ? "Succeeded" : "Failed");

return hr;
}
65 changes: 4 additions & 61 deletions native-src/virtual_drive/Wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <codecvt>
#include <locale>
#include <vector>
#include "register_sync_root.h"
#include "napi_safe_wrap.h"

std::string WStringToUTF8(const std::wstring &wstr)
{
Expand Down Expand Up @@ -180,67 +182,8 @@ napi_value UnregisterSyncRootWrapper(napi_env env, napi_callback_info args)
return napiResult;
}

napi_value RegisterSyncRootWrapper(napi_env env, napi_callback_info args)
{
size_t argc = 5;
napi_value argv[5];

napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr);

if (argc < 5)
{
napi_throw_error(env, nullptr, "5 arguments are required for RegisterSyncRoot");
return nullptr;
}

LPCWSTR syncRootPath;
size_t syncRootPathLength;
napi_get_value_string_utf16(env, argv[0], nullptr, 0, &syncRootPathLength);
syncRootPath = new WCHAR[syncRootPathLength + 1];
napi_get_value_string_utf16(env, argv[0], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(syncRootPath)), syncRootPathLength + 1, nullptr);

LPCWSTR providerName;
size_t providerNameLength;
napi_get_value_string_utf16(env, argv[1], nullptr, 0, &providerNameLength);
providerName = new WCHAR[providerNameLength + 1];
napi_get_value_string_utf16(env, argv[1], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerName)), providerNameLength + 1, nullptr);

LPCWSTR providerVersion;
size_t providerVersionLength;
napi_get_value_string_utf16(env, argv[2], nullptr, 0, &providerVersionLength);
providerVersion = new WCHAR[providerVersionLength + 1];
napi_get_value_string_utf16(env, argv[2], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerVersion)), providerVersionLength + 1, nullptr);

GUID providerId;
LPCWSTR providerIdStr;
size_t providerIdStrLength;
napi_get_value_string_utf16(env, argv[3], nullptr, 0, &providerIdStrLength);
providerIdStr = new WCHAR[providerIdStrLength + 1];
napi_get_value_string_utf16(env, argv[3], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerIdStr)), providerIdStrLength + 1, nullptr);

if (FAILED(CLSIDFromString(providerIdStr, &providerId)))
{
napi_throw_error(env, nullptr, "Invalid GUID format");
delete[] syncRootPath;
delete[] providerName;
delete[] providerVersion;
delete[] providerIdStr;
return nullptr;
}

LPCWSTR logoPath;
size_t logoPathLength;
napi_get_value_string_utf16(env, argv[4], nullptr, 0, &logoPathLength);
logoPath = new WCHAR[logoPathLength + 1];
napi_get_value_string_utf16(env, argv[4], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(logoPath)), logoPathLength + 1, nullptr);

HRESULT result = SyncRoot::RegisterSyncRoot(syncRootPath, providerName, providerVersion, providerId, logoPath);

delete[] providerIdStr;

napi_value napiResult;
napi_create_int32(env, static_cast<int32_t>(result), &napiResult);
return napiResult;
napi_value RegisterSyncRootWrapper(napi_env env, napi_callback_info info) {
return napi_safe_wrap(env, info, register_sync_root_impl);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are wrapping the register sync root function.

}

napi_value GetRegisteredSyncRootsWrapper(napi_env env, napi_callback_info args)
Expand Down
65 changes: 65 additions & 0 deletions native-src/virtual_drive/register_sync_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <Windows.h>
#include <SyncRoot.h>

napi_value register_sync_root_impl(napi_env env, napi_callback_info args)
{
size_t argc = 5;
napi_value argv[5];

napi_get_cb_info(env, args, &argc, argv, nullptr, nullptr);

if (argc < 5)
{
napi_throw_error(env, nullptr, "5 arguments are required for RegisterSyncRoot");
return nullptr;
}

LPCWSTR syncRootPath;
size_t syncRootPathLength;
napi_get_value_string_utf16(env, argv[0], nullptr, 0, &syncRootPathLength);
syncRootPath = new WCHAR[syncRootPathLength + 1];
napi_get_value_string_utf16(env, argv[0], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(syncRootPath)), syncRootPathLength + 1, nullptr);

LPCWSTR providerName;
size_t providerNameLength;
napi_get_value_string_utf16(env, argv[1], nullptr, 0, &providerNameLength);
providerName = new WCHAR[providerNameLength + 1];
napi_get_value_string_utf16(env, argv[1], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerName)), providerNameLength + 1, nullptr);

LPCWSTR providerVersion;
size_t providerVersionLength;
napi_get_value_string_utf16(env, argv[2], nullptr, 0, &providerVersionLength);
providerVersion = new WCHAR[providerVersionLength + 1];
napi_get_value_string_utf16(env, argv[2], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerVersion)), providerVersionLength + 1, nullptr);

GUID providerId;
LPCWSTR providerIdStr;
size_t providerIdStrLength;
napi_get_value_string_utf16(env, argv[3], nullptr, 0, &providerIdStrLength);
providerIdStr = new WCHAR[providerIdStrLength + 1];
napi_get_value_string_utf16(env, argv[3], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(providerIdStr)), providerIdStrLength + 1, nullptr);

if (FAILED(CLSIDFromString(providerIdStr, &providerId)))
{
napi_throw_error(env, nullptr, "Invalid GUID format");
delete[] syncRootPath;
delete[] providerName;
delete[] providerVersion;
delete[] providerIdStr;
return nullptr;
}

LPCWSTR logoPath;
size_t logoPathLength;
napi_get_value_string_utf16(env, argv[4], nullptr, 0, &logoPathLength);
logoPath = new WCHAR[logoPathLength + 1];
napi_get_value_string_utf16(env, argv[4], reinterpret_cast<char16_t *>(const_cast<wchar_t *>(logoPath)), logoPathLength + 1, nullptr);

HRESULT result = SyncRoot::RegisterSyncRoot(syncRootPath, providerName, providerVersion, providerId, logoPath);

delete[] providerIdStr;

napi_value napiResult;
napi_create_int32(env, static_cast<int32_t>(result), &napiResult);
return napiResult;
}