-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor cancel fetch data #197
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
Changes from all commits
21bd1ad
819a1f1
cc1b678
444a2d5
b5eff5e
fe31746
32c5ea9
75681d6
205f2be
9648c94
46ee84c
313dfbf
ff28084
d1a7164
70d004e
69ce991
1020526
e94646e
0601df9
73a1c9e
ae38d77
f7b6a44
41028a0
38a0e29
4832626
d947054
9d096c0
249d71c
2d39fb1
0a6216f
4577789
bc748b0
e17ca28
a5e3dc7
18b57ba
8d97f38
0fe50fb
20b0533
d65af96
71f1c31
5729e8e
394f12d
470f12e
5bd5c36
9e35bf5
fa70622
0c2fd4d
5d9d904
370eac2
aa5b393
5bbd137
04d62ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,43 +5,8 @@ | |
| #include "Utilities.h" | ||
| #include <Logger.h> | ||
|
|
||
| #define MSSEARCH_INDEX L"SystemIndex" | ||
| DEFINE_PROPERTYKEY(PKEY_StorageProviderTransferProgress, 0xE77E90DF, 0x6271, 0x4F5B, 0x83, 0x4F, 0x2D, 0xD1, 0xF2, 0x45, 0xDD, 0xA4, 4); | ||
|
|
||
| void Utilities::AddFolderToSearchIndexer(_In_ PCWSTR folder) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer necessary, the CfRegister already adds the InternxtDrive to the file explorer and indexes it when searching for files in the search bar of the file explorer. |
||
| { | ||
| HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); | ||
| if (FAILED(hr)) | ||
| { | ||
| wprintf(L"Failed to initialize COM library. Error code = %08x\n", hr); | ||
| return; | ||
| } | ||
|
|
||
| std::wstring url(L"file:///"); | ||
| url.append(folder); | ||
|
|
||
| try | ||
| { | ||
| winrt::com_ptr<ISearchManager> searchManager; | ||
| winrt::check_hresult(CoCreateInstance(__uuidof(CSearchManager), NULL, CLSCTX_SERVER, __uuidof(&searchManager), searchManager.put_void())); | ||
|
|
||
| winrt::com_ptr<ISearchCatalogManager> searchCatalogManager; | ||
| winrt::check_hresult(searchManager->GetCatalog(MSSEARCH_INDEX, searchCatalogManager.put())); | ||
|
|
||
| winrt::com_ptr<ISearchCrawlScopeManager> searchCrawlScopeManager; | ||
| winrt::check_hresult(searchCatalogManager->GetCrawlScopeManager(searchCrawlScopeManager.put())); | ||
|
|
||
| winrt::check_hresult(searchCrawlScopeManager->AddDefaultScopeRule(url.data(), TRUE, FOLLOW_FLAGS::FF_INDEXCOMPLEXURLS)); | ||
| winrt::check_hresult(searchCrawlScopeManager->SaveAll()); | ||
|
|
||
| // wprintf(L"Succesfully called AddFolderToSearchIndexer on \"%s\"\n", url.data()); | ||
| } | ||
| catch (...) | ||
| { | ||
| wprintf(L"Failed on call to AddFolderToSearchIndexer for \"%s\" with %08x\n", url.data(), static_cast<HRESULT>(winrt::to_hresult())); | ||
| } | ||
| } | ||
|
|
||
| void Utilities::ApplyTransferStateToFile(_In_ PCWSTR fullPath, _In_ CF_CALLBACK_INFO &callbackInfo, UINT64 total, UINT64 completed) | ||
| { | ||
| Logger::getInstance().log("ApplyTransferStateToFile", LogLevel::INFO); | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,20 +7,25 @@ | |
| #include <mutex> | ||
| #include <filesystem> | ||
|
|
||
| napi_threadsafe_function g_cancel_delete_fetch_data_threadsafe_callback = nullptr; | ||
| napi_threadsafe_function g_cancel_fetch_data_threadsafe_callback = nullptr; | ||
|
|
||
| inline std::mutex mtx; | ||
| inline std::condition_variable cv; | ||
| inline bool ready = false; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this variables are global we can parallelize the cancel fetch data because all files would share the same variables. |
||
| struct CallbackContext { | ||
| std::mutex mtx; | ||
| std::condition_variable cv; | ||
| bool ready = false; | ||
| }; | ||
|
|
||
| struct CancelFetchDataArgs | ||
| { | ||
| struct CancelFetchDataArgs { | ||
| std::wstring fileIdentityArg; | ||
| CallbackContext* context; | ||
|
|
||
| CancelFetchDataArgs(const std::wstring& fileId, CallbackContext* ctx) | ||
| : fileIdentityArg(fileId), context(ctx) {} | ||
| }; | ||
|
|
||
| void setup_global_tsfn_cancel_fetch_data(napi_threadsafe_function tsfn) | ||
| { | ||
| g_cancel_delete_fetch_data_threadsafe_callback = tsfn; | ||
| g_cancel_fetch_data_threadsafe_callback = tsfn; | ||
| } | ||
|
|
||
| void notify_cancel_fetch_data_call(napi_env env, napi_value js_callback, void *context, void *data) | ||
|
|
@@ -43,23 +48,27 @@ void notify_cancel_fetch_data_call(napi_env env, napi_value js_callback, void *c | |
| { | ||
| fprintf(stderr, "Failed to call JS function.\n"); | ||
| Logger::getInstance().log("Failed to call JS function in cancelFetchCallback.", LogLevel::ERROR); | ||
| return; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't return since we need to change |
||
| } | ||
|
|
||
| cv.notify_one(); | ||
| { | ||
| std::lock_guard<std::mutex> lock(args->context->mtx); | ||
| args->context->ready = true; | ||
| } | ||
|
|
||
| args->context->cv.notify_one(); | ||
| delete args; | ||
| } | ||
|
|
||
| void register_threadsafe_cancel_fetch_data_callback(const std::string &resource_name, napi_env env, InputSyncCallbacks input) | ||
| { | ||
| std::u16string converted_resource_name = std::u16string(resource_name.begin(), resource_name.end()); | ||
| std::u16string converted_resource_name(resource_name.begin(), resource_name.end()); | ||
|
|
||
| napi_value resource_name_value; | ||
| napi_create_string_utf16(env, converted_resource_name.c_str(), NAPI_AUTO_LENGTH, &resource_name_value); | ||
|
|
||
| napi_threadsafe_function tsfn_cancel_fetch_data; | ||
| napi_value cancel_fetch_data_value; | ||
| napi_status status_ref = napi_get_reference_value(env, input.cancel_fetch_data_callback_ref, &cancel_fetch_data_value); | ||
| napi_get_reference_value(env, input.cancel_fetch_data_callback_ref, &cancel_fetch_data_value); | ||
|
|
||
| napi_status status = napi_create_threadsafe_function( | ||
| env, | ||
|
|
@@ -76,47 +85,44 @@ void register_threadsafe_cancel_fetch_data_callback(const std::string &resource_ | |
|
|
||
| if (status != napi_ok) | ||
| { | ||
| fprintf(stderr, "Failed to create threadsafe function.\n"); | ||
| napi_throw_error(env, nullptr, "Failed to create cancel fetch data threadsafe function"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to throw an error in javascript if the connect function fails because we cannot register the cancel fetch data callback. |
||
| return; | ||
| } | ||
|
|
||
| setup_global_tsfn_cancel_fetch_data(tsfn_cancel_fetch_data); | ||
| } | ||
|
|
||
| void CALLBACK cancel_fetch_data_callback_wrapper( | ||
| _In_ CONST CF_CALLBACK_INFO *callbackInfo, | ||
| _In_ CONST CF_CALLBACK_PARAMETERS *callbackParameters) | ||
| { | ||
| printf("fetch_data_callback_wrapper\n"); | ||
| printf("cancel_fetch_data_callback_wrapper called\n"); | ||
|
|
||
| LPCVOID fileIdentity = callbackInfo->FileIdentity; | ||
| DWORD fileIdentityLength = callbackInfo->FileIdentityLength; | ||
|
|
||
| const wchar_t *wchar_ptr = static_cast<const wchar_t *>(fileIdentity); | ||
| std::wstring fileIdentityStr(wchar_ptr, fileIdentityLength / sizeof(wchar_t)); | ||
|
|
||
| if (g_cancel_delete_fetch_data_threadsafe_callback == nullptr) | ||
| if (g_cancel_fetch_data_threadsafe_callback == nullptr) | ||
| { | ||
| wprintf(L"Callback fetch_data_callback_wrapper called but g_fetch_data_threadsafe_callback is null\n"); | ||
| return; | ||
| } | ||
|
|
||
| CallbackContext context; | ||
| CancelFetchDataArgs *args = new CancelFetchDataArgs(fileIdentityStr, &context); | ||
|
|
||
| CancelFetchDataArgs *args = new CancelFetchDataArgs(); | ||
| args->fileIdentityArg = fileIdentityStr; | ||
|
|
||
| napi_status status = napi_call_threadsafe_function(g_cancel_delete_fetch_data_threadsafe_callback, args, napi_tsfn_blocking); | ||
| napi_call_threadsafe_function(g_cancel_fetch_data_threadsafe_callback, args, napi_tsfn_blocking); | ||
|
|
||
| if (status != napi_ok) | ||
| { | ||
| wprintf(L"Callback called unsuccessfully.\n"); | ||
| }; | ||
| std::unique_lock<std::mutex> lock(context.mtx); | ||
| auto timeout = std::chrono::seconds(30); | ||
|
|
||
| { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of locking the mutext without any condition, we lock it until context.ready becomes true. We also add a timeout of 30 seconds. |
||
| std::unique_lock<std::mutex> lock(mtx); | ||
| while (!ready) | ||
| { | ||
| cv.wait(lock); | ||
| if (context.cv.wait_for(lock, timeout, [&context] { return context.ready; })) { | ||
| wprintf(L"Cancel fetch completed\n"); | ||
| } else { | ||
| wprintf(L"Cancel fetch timed out\n"); | ||
| } | ||
| } | ||
|
|
||
| ready = false; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer necessary, the CfRegister already adds the InternxtDrive to the file explorer and indexes it when searching for files in the search bar of the file explorer.