|
17 | 17 | #include <PackagerConnection.h> |
18 | 18 | #include <QuirkSettings.h> |
19 | 19 | #include <Shared/DevServerHelper.h> |
| 20 | +#include <ShlObj.h> |
20 | 21 | #include <Threading/MessageDispatchQueue.h> |
21 | 22 | #include <Threading/MessageQueueThreadFactory.h> |
22 | 23 | #include <TurboModuleManager.h> |
|
28 | 29 | #include <react/renderer/runtimescheduler/RuntimeScheduler.h> |
29 | 30 | #include <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h> |
30 | 31 | #include <winrt/Windows.Storage.h> |
| 32 | +#include <filesystem> |
31 | 33 | #include <tuple> |
32 | 34 | #include "BaseScriptStoreImpl.h" |
33 | 35 | #include "CrashManager.h" |
34 | 36 | #include "DevMenu.h" |
35 | 37 | #include "DynamicWriter.h" |
| 38 | +#include "Hasher.h" |
36 | 39 | #include "HermesRuntimeHolder.h" |
37 | 40 | #include "IReactContext.h" |
38 | 41 | #include "IReactDispatcher.h" |
@@ -412,13 +415,89 @@ Mso::DispatchQueueSettings CreateDispatchQueueSettings( |
412 | 415 | return queueSettings; |
413 | 416 | } |
414 | 417 |
|
| 418 | +// Unpackaged apps share %LOCALAPPDATA%, so give each executable its own cache folder. |
| 419 | +std::wstring GetUnpackagedAppCacheFolderName() noexcept { |
| 420 | + std::wstring exePath(MAX_PATH, L'\0'); |
| 421 | + for (;;) { |
| 422 | + DWORD length = GetModuleFileNameW(nullptr, exePath.data(), static_cast<DWORD>(exePath.size())); |
| 423 | + if (length == 0) { |
| 424 | + return {}; |
| 425 | + } |
| 426 | + if (length < exePath.size()) { |
| 427 | + exePath.resize(length); |
| 428 | + break; |
| 429 | + } |
| 430 | + if (exePath.size() >= 32768) { |
| 431 | + return {}; |
| 432 | + } |
| 433 | + exePath.resize(exePath.size() * 2); |
| 434 | + } |
| 435 | + |
| 436 | + CharLowerBuffW(exePath.data(), static_cast<DWORD>(exePath.size())); |
| 437 | + auto hash = Microsoft::ReactNative::GetSHA256Hash(exePath.data(), exePath.size() * sizeof(wchar_t)); |
| 438 | + if (!hash || hash->size() < 8) { |
| 439 | + return {}; |
| 440 | + } |
| 441 | + |
| 442 | + static constexpr wchar_t hexDigits[] = L"0123456789abcdef"; |
| 443 | + std::wstring name; |
| 444 | + for (size_t i = 0; i < 8; ++i) { |
| 445 | + name += hexDigits[(*hash)[i] >> 4]; |
| 446 | + name += hexDigits[(*hash)[i] & 0xF]; |
| 447 | + } |
| 448 | + return name; |
| 449 | +} |
| 450 | + |
| 451 | +// The Hermes prepared script (bytecode) cache lives in an app-specific folder so that different apps don't |
| 452 | +// overwrite each other's cache entries. |
| 453 | +std::wstring GetPreparedScriptStoreDirectory() noexcept { |
| 454 | + try { |
| 455 | + std::filesystem::path directory; |
| 456 | + if (Microsoft::ReactNative::HasPackageIdentity()) { |
| 457 | + directory = std::wstring{winrt::Windows::Storage::ApplicationData::Current().LocalCacheFolder().Path()}; |
| 458 | + directory /= L"ReactNative"; |
| 459 | + directory /= L"PreparedScripts"; |
| 460 | + } else { |
| 461 | + std::wstring appFolderName = GetUnpackagedAppCacheFolderName(); |
| 462 | + if (appFolderName.empty()) { |
| 463 | + return {}; |
| 464 | + } |
| 465 | + // UserDataPaths requires Windows 10 1903, so use the shell API which is also available on 1809. |
| 466 | + PWSTR localAppData = nullptr; |
| 467 | + HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, nullptr, &localAppData); |
| 468 | + if (FAILED(hr) || !localAppData) { |
| 469 | + CoTaskMemFree(localAppData); |
| 470 | + return {}; |
| 471 | + } |
| 472 | + directory = localAppData; |
| 473 | + CoTaskMemFree(localAppData); |
| 474 | + directory /= L"Microsoft"; |
| 475 | + directory /= L"ReactNative"; |
| 476 | + directory /= L"PreparedScripts"; |
| 477 | + directory /= appFolderName; |
| 478 | + } |
| 479 | + |
| 480 | + std::error_code ec; |
| 481 | + std::filesystem::create_directories(directory, ec); |
| 482 | + if (ec || !std::filesystem::is_directory(directory, ec)) { |
| 483 | + return {}; |
| 484 | + } |
| 485 | + |
| 486 | + std::wstring result = directory.wstring(); |
| 487 | + result += std::filesystem::path::preferred_separator; |
| 488 | + return result; |
| 489 | + } catch (...) { |
| 490 | + return {}; |
| 491 | + } |
| 492 | +} |
| 493 | + |
415 | 494 | std::unique_ptr<facebook::jsi::PreparedScriptStore> CreatePreparedScriptStore() noexcept { |
416 | | - std::unique_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore = nullptr; |
417 | | - wchar_t tempPath[MAX_PATH]; |
418 | | - if (GetTempPathW(static_cast<DWORD>(std::size(tempPath)), tempPath)) { |
419 | | - preparedScriptStore = std::make_unique<facebook::react::BasePreparedScriptStoreImpl>(winrt::to_string(tempPath)); |
| 495 | + std::wstring directory = GetPreparedScriptStoreDirectory(); |
| 496 | + if (directory.empty()) { |
| 497 | + // Run without a bytecode cache rather than falling back to a shared folder. |
| 498 | + return nullptr; |
420 | 499 | } |
421 | | - return preparedScriptStore; |
| 500 | + return std::make_unique<facebook::react::BasePreparedScriptStoreImpl>(winrt::to_string(directory)); |
422 | 501 | } |
423 | 502 |
|
424 | 503 | typedef HRESULT(__stdcall *SetThreadDescriptionFn)(HANDLE, PCWSTR); |
|
0 commit comments