MCPcopy Create free account
hub / github.com/YtFlow/Maple / initializeWebView

Method initializeWebView

Maple.App/MonacoEditPage.cpp:38–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36 }
37 }
38 IAsyncAction MonacoEditPage::initializeWebView() {
39 auto const lifetime{ get_strong() };
40 auto const webview = WebView();
41 if (m_webviewState != MonacoEditPageWebViewState::Uninitialized)
42 {
43 co_return;
44 }
45 m_webviewState = MonacoEditPageWebViewState::AwaitingEditorReady;
46 co_await webview.EnsureCoreWebView2Async();
47 if (!m_webviewResourceRequestedEventHandle)
48 {
49 webview.CoreWebView2().AddWebResourceRequestedFilter(
50 hstring(CONFIG_ROOT_VIRTUAL_HOSTW) + L"*",
51 CoreWebView2WebResourceContext::Fetch
52 );
53 webview.CoreWebView2().AddWebResourceRequestedFilter(
54 hstring(CONFIG_ROOT_VIRTUAL_HOSTW) + L"*",
55 CoreWebView2WebResourceContext::XmlHttpRequest
56 );
57 webview.CoreWebView2().AddWebResourceRequestedFilter(
58 hstring(CONFIG_ROOT_VIRTUAL_HOSTW) + L"*",
59 CoreWebView2WebResourceContext::Document
60 );
61 m_webviewResourceRequestedEventHandle = webview.CoreWebView2().WebResourceRequested(
62 [weak = weak_ref(lifetime)](auto const webview, CoreWebView2WebResourceRequestedEventArgs const e) -> fire_and_forget
63 {
64 auto const deferral = e.GetDeferral();
65 try {
66 if (auto const self{ weak.get() }) {
67 auto const configDir = co_await ConfigUtil::GetConfigFolder();
68 if (configDir.Path() != self->m_lastConfigFolderPath)
69 {
70 co_return;
71 }
72 auto path = to_string(Uri::UnescapeComponent(e.Request().Uri()));
73 if (auto const pos{ path.find(CONFIG_ROOT_VIRTUAL_HOST) }; pos != std::string::npos)
74 {
75 path = path.replace(pos, strlen(CONFIG_ROOT_VIRTUAL_HOST), "");
76 }
77
78 auto const file = co_await configDir.GetFileAsync(to_hstring(path));
79 auto const fstream = co_await file.OpenReadAsync();
80 e.Response(webview.Environment().CreateWebResourceResponse(
81 std::move(fstream),
82 200,
83 L"OK",
84 hstring(L"Content-Length: ") + to_hstring(fstream.Size()) + L"\nContent-Type: text/plain\nAccess-Control-Allow-Origin: http://maple-monaco-editor-app-root.com"
85 ));
86 }
87 }
88 catch (...)
89 {
90 UI::NotifyException(L"Handling web requests");
91 }
92 deferral.Complete();
93 });
94 }
95 webview.CoreWebView2().SetVirtualHostNameToFolderMapping(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected