Skip to content

Commit 3531b2d

Browse files
committed
Created Extension.h and Extension.cpp files and moved all extension-related code into them.
1 parent 3040cbd commit 3531b2d

6 files changed

Lines changed: 77 additions & 53 deletions

File tree

WebView2/Extension.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
4+
5+
#include "Extension.h"
6+
#include "Plugin.h"
7+
#include "../API/RainmeterAPI.h"
8+
#include <sstream>
9+
10+
bool g_extensions_checked = false;
11+
12+
std::vector<std::wstring> GetExtensionsID(const std::wstring& input)
13+
{
14+
std::vector<std::wstring> result;
15+
std::wstringstream ss(input);
16+
std::wstring token;
17+
18+
while (std::getline(ss, token, L',')) {
19+
token.erase(0, token.find_first_not_of(L" \t"));
20+
token.erase(token.find_last_not_of(L" \t") + 1);
21+
22+
if (!token.empty()) {
23+
result.push_back(token);
24+
}
25+
}
26+
return result;
27+
}
28+
29+
void EnableExtension(ICoreWebView2BrowserExtension* extension, BOOL enable)
30+
{
31+
extension->Enable(
32+
enable,
33+
Callback<ICoreWebView2BrowserExtensionEnableCompletedHandler>(
34+
[](HRESULT hr) -> HRESULT
35+
{
36+
if (FAILED(hr))
37+
ShowFailure(hr, L"Enable extension failed");
38+
return S_OK;
39+
}).Get());
40+
}
41+
42+
void RemoveExtension(void* rm, ICoreWebView2BrowserExtension* extension, const std::wstring& name)
43+
{
44+
extension->Remove(
45+
Callback<ICoreWebView2BrowserExtensionRemoveCompletedHandler>(
46+
[](HRESULT hr) -> HRESULT
47+
{
48+
if (FAILED(hr))
49+
ShowFailure(hr, L"Uninstall extension failed");
50+
return S_OK;
51+
}).Get());
52+
53+
RmLogF(rm, LOG_DEBUG, L"WebView2: \"%s\" extension removed.", name.c_str());
54+
}

WebView2/Extension.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
** Copyright (C) 2025 nstechbytes. All rights reserved.
3+
*/
4+
5+
#pragma once
6+
7+
#include <Windows.h>
8+
#include <WebView2.h>
9+
#include <string>
10+
#include <vector>
11+
12+
// Global flag for extension checking
13+
extern bool g_extensions_checked;
14+
15+
// Extension utility functions
16+
std::vector<std::wstring> GetExtensionsID(const std::wstring& input);
17+
void EnableExtension(ICoreWebView2BrowserExtension* extension, BOOL enable);
18+
void RemoveExtension(void* rm, ICoreWebView2BrowserExtension* extension, const std::wstring& name);

WebView2/Plugin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ extern wil::com_ptr<ITypeLib> g_typeLib;
2828

2929
struct SkinSubclassData;
3030

31-
extern bool g_extensions_checked;
32-
3331
// Measure structure containing WebView2 state
3432
struct Measure
3533
{

WebView2/WebView2.cpp

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,12 @@
55
#include "Plugin.h"
66
#include "Utils.h"
77
#include "PathUtils.h"
8+
#include "Extension.h"
89
#include "HostObjectRmAPI.h"
910
#include "../API/RainmeterAPI.h"
1011
#include <WebView2EnvironmentOptions.h>
1112
#include <filesystem>
1213

13-
bool g_extensions_checked = false;
14-
15-
std::vector<std::wstring> GetExtensionsID(const std::wstring& input)
16-
{
17-
std::vector<std::wstring> result;
18-
std::wstringstream ss(input);
19-
std::wstring token;
20-
21-
while (std::getline(ss, token, L',')) {
22-
token.erase(0, token.find_first_not_of(L" \t"));
23-
token.erase(token.find_last_not_of(L" \t") + 1);
24-
25-
if (!token.empty()) {
26-
result.push_back(token);
27-
}
28-
}
29-
return result;
30-
}
31-
32-
static void EnableExtension(
33-
ICoreWebView2BrowserExtension* extension,
34-
BOOL enable)
35-
{
36-
extension->Enable(
37-
enable,
38-
Callback<ICoreWebView2BrowserExtensionEnableCompletedHandler>(
39-
[](HRESULT hr) -> HRESULT
40-
{
41-
if (FAILED(hr))
42-
ShowFailure(hr, L"Enable extension failed");
43-
return S_OK;
44-
}).Get());
45-
}
46-
47-
static void RemoveExtension(void* rm,
48-
ICoreWebView2BrowserExtension* extension,
49-
const std::wstring& name)
50-
{
51-
extension->Remove(
52-
Callback<ICoreWebView2BrowserExtensionRemoveCompletedHandler>(
53-
[](HRESULT hr) -> HRESULT
54-
{
55-
if (FAILED(hr))
56-
ShowFailure(hr, L"Uninstall extension failed");
57-
return S_OK;
58-
}).Get());
59-
60-
RmLogF(rm, LOG_DEBUG, L"WebView2: \"%s\" extension removed.", name.c_str());
61-
}
62-
63-
6414
// Create WebView2 environment and controller
6515
void CreateWebView2(Measure* measure)
6616
{

WebView2/WebView2.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
<ResourceCompile Include="WebView2.rc" />
2323
</ItemGroup>
2424
<ItemGroup>
25+
<ClCompile Include="Extension.cpp" />
2526
<ClCompile Include="HostObjectRmAPI.cpp" />
2627
<ClCompile Include="PathUtils.cpp" />
2728
<ClCompile Include="Plugin.cpp" />
2829
<ClCompile Include="Utils.cpp" />
2930
<ClCompile Include="WebView2.cpp" />
3031
</ItemGroup>
3132
<ItemGroup>
33+
<ClInclude Include="Extension.h" />
3234
<ClInclude Include="HostObjectRmAPI.h" />
3335
<ClInclude Include="PathUtils.h" />
3436
<ClInclude Include="Plugin.h" />

WebView2/WebView2.vcxproj.filters

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<ClCompile Include="Plugin.cpp" />
1515
<ClCompile Include="Utils.cpp" />
1616
<ClCompile Include="PathUtils.cpp" />
17+
<ClCompile Include="Extension.cpp" />
1718
</ItemGroup>
1819
<ItemGroup>
1920
<ClInclude Include="HostObjectRmAPI.h" />
@@ -22,6 +23,7 @@
2223
<ClInclude Include="HostObject_h.h" />
2324
<ClInclude Include="Utils.h" />
2425
<ClInclude Include="PathUtils.h" />
26+
<ClInclude Include="Extension.h" />
2527
<ClInclude Include="Ini\SimpleIni.h">
2628
<Filter>Ini</Filter>
2729
</ClInclude>

0 commit comments

Comments
 (0)