Skip to content

Commit 14a0ac7

Browse files
committed
Refactor UrlOpener to singleton and simplify C API
Convert UrlOpener to a process-local singleton (UrlOpener::GetInstance()) and make IsSupported() a const instance method. Remove the opaque native_url_opener_t and the create/destroy C API functions; native_url_opener_is_supported() now takes no handle and native_url_opener_open(const char*) replaces the instance-based open call. Rename internal helper MakeResult -> MakeUrlOpenResult, update platform implementations to provide GetInstance(), and add deleted copy/move constructors and a private default ctor to UrlOpener. Update the C example to match the simplified API. These changes simplify lifetime management and usage of the URL opener across platforms.
1 parent e6bdd3e commit 14a0ac7

File tree

10 files changed

+63
-72
lines changed

10 files changed

+63
-72
lines changed

examples/url_opener_c_example/main.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@ int main(void) {
55
printf("URL Opener C API Example\n");
66
printf("========================\n\n");
77

8-
native_url_opener_t opener = native_url_opener_create();
9-
if (!opener) {
10-
fprintf(stderr, "Failed to create URL opener instance.\n");
11-
return 1;
12-
}
13-
14-
if (!native_url_opener_is_supported(opener)) {
8+
if (!native_url_opener_is_supported()) {
159
printf("URL opening is not supported on this platform.\n");
16-
native_url_opener_destroy(opener);
1710
return 0;
1811
}
1912

2013
printf("URL opening is supported.\n");
2114
printf("Opening https://example.com ...\n");
2215

23-
native_url_open_result_t result = native_url_opener_open(opener, "https://example.com");
16+
native_url_open_result_t result = native_url_opener_open("https://example.com");
2417
if (result.success) {
2518
printf("URL opened successfully.\n");
2619
} else {
@@ -30,6 +23,5 @@ int main(void) {
3023
}
3124

3225
native_url_open_result_free(&result);
33-
native_url_opener_destroy(opener);
3426
return result.success ? 0 : 1;
3527
}

src/capi/url_opener_c.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ using namespace nativeapi;
1010

1111
namespace {
1212

13-
UrlOpener* GetUrlOpener(native_url_opener_t opener) {
14-
return static_cast<UrlOpener*>(opener);
15-
}
16-
1713
native_url_open_error_code_t ToCErrorCode(UrlOpenErrorCode code) {
1814
switch (code) {
1915
case UrlOpenErrorCode::kNone:
@@ -33,9 +29,9 @@ native_url_open_error_code_t ToCErrorCode(UrlOpenErrorCode code) {
3329
}
3430
}
3531

36-
native_url_open_result_t MakeResult(bool success,
37-
native_url_open_error_code_t error_code,
38-
const std::string& message) {
32+
native_url_open_result_t MakeUrlOpenResult(bool success,
33+
native_url_open_error_code_t error_code,
34+
const std::string& message) {
3935
native_url_open_result_t result = {};
4036
result.success = success;
4137
result.error_code = error_code;
@@ -45,47 +41,27 @@ native_url_open_result_t MakeResult(bool success,
4541

4642
} // namespace
4743

48-
native_url_opener_t native_url_opener_create(void) {
49-
try {
50-
return static_cast<native_url_opener_t>(new UrlOpener());
51-
} catch (...) {
52-
return nullptr;
53-
}
54-
}
55-
56-
void native_url_opener_destroy(native_url_opener_t opener) {
57-
delete GetUrlOpener(opener);
58-
}
59-
60-
bool native_url_opener_is_supported(native_url_opener_t opener) {
61-
if (!opener) {
62-
return false;
63-
}
64-
44+
bool native_url_opener_is_supported(void) {
6545
try {
66-
return UrlOpener::IsSupported();
46+
return UrlOpener::GetInstance().IsSupported();
6747
} catch (...) {
6848
return false;
6949
}
7050
}
7151

72-
native_url_open_result_t native_url_opener_open(native_url_opener_t opener, const char* url) {
73-
if (!opener) {
74-
return MakeResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED, "URL opener is null.");
75-
}
76-
52+
native_url_open_result_t native_url_opener_open(const char* url) {
7753
if (!url) {
78-
return MakeResult(false, NATIVE_URL_OPEN_ERROR_INVALID_URL_EMPTY, "URL is empty.");
54+
return MakeUrlOpenResult(false, NATIVE_URL_OPEN_ERROR_INVALID_URL_EMPTY, "URL is empty.");
7955
}
8056

8157
try {
82-
const UrlOpenResult result = GetUrlOpener(opener)->Open(std::string(url));
83-
return MakeResult(result.success, ToCErrorCode(result.error_code), result.error_message);
58+
const UrlOpenResult result = UrlOpener::GetInstance().Open(std::string(url));
59+
return MakeUrlOpenResult(result.success, ToCErrorCode(result.error_code), result.error_message);
8460
} catch (const std::exception& e) {
85-
return MakeResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED, e.what());
61+
return MakeUrlOpenResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED, e.what());
8662
} catch (...) {
87-
return MakeResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED,
88-
"Unexpected error while opening URL.");
63+
return MakeUrlOpenResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED,
64+
"Unexpected error while opening URL.");
8965
}
9066
}
9167

src/capi/url_opener_c.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
extern "C" {
1313
#endif
1414

15-
/**
16-
* @brief Opaque handle for a URL opener instance.
17-
*/
18-
typedef void* native_url_opener_t;
19-
2015
/**
2116
* @brief Error codes returned by URL opening APIs.
2217
*/
@@ -38,31 +33,19 @@ typedef struct {
3833
char* error_message;
3934
} native_url_open_result_t;
4035

41-
/**
42-
* @brief Create a URL opener instance.
43-
*/
44-
FFI_PLUGIN_EXPORT
45-
native_url_opener_t native_url_opener_create(void);
46-
47-
/**
48-
* @brief Destroy a URL opener instance.
49-
*/
50-
FFI_PLUGIN_EXPORT
51-
void native_url_opener_destroy(native_url_opener_t opener);
52-
5336
/**
5437
* @brief Check whether URL opening is supported on this platform.
5538
*/
5639
FFI_PLUGIN_EXPORT
57-
bool native_url_opener_is_supported(native_url_opener_t opener);
40+
bool native_url_opener_is_supported(void);
5841

5942
/**
6043
* @brief Attempt to open URL with the system default browser.
6144
*
6245
* Caller must release result.error_message via native_url_open_result_free().
6346
*/
6447
FFI_PLUGIN_EXPORT
65-
native_url_open_result_t native_url_opener_open(native_url_opener_t opener, const char* url);
48+
native_url_open_result_t native_url_opener_open(const char* url);
6649

6750
/**
6851
* @brief Free owned memory inside a native_url_open_result_t.

src/platform/android/url_opener_android.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ UrlLaunchOutcome LaunchUnsupported(const std::string& url) {
1111

1212
} // namespace
1313

14-
bool UrlOpener::IsSupported() {
14+
UrlOpener& UrlOpener::GetInstance() {
15+
static UrlOpener instance;
16+
return instance;
17+
}
18+
19+
bool UrlOpener::IsSupported() const {
1520
return false;
1621
}
1722

src/platform/ios/url_opener_ios.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ UrlLaunchOutcome LaunchUrl(const std::string& url) {
7676

7777
} // namespace
7878

79-
bool UrlOpener::IsSupported() {
79+
UrlOpener& UrlOpener::GetInstance() {
80+
static UrlOpener instance;
81+
return instance;
82+
}
83+
84+
bool UrlOpener::IsSupported() const {
8085
return true;
8186
}
8287

src/platform/linux/url_opener_linux.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ UrlLaunchOutcome LaunchUrl(const std::string& url) {
2424

2525
} // namespace
2626

27-
bool UrlOpener::IsSupported() {
27+
UrlOpener& UrlOpener::GetInstance() {
28+
static UrlOpener instance;
29+
return instance;
30+
}
31+
32+
bool UrlOpener::IsSupported() const {
2833
return true;
2934
}
3035

src/platform/macos/url_opener_macos.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ UrlLaunchOutcome LaunchUrl(const std::string& url) {
2929

3030
} // namespace
3131

32-
bool UrlOpener::IsSupported() {
32+
UrlOpener& UrlOpener::GetInstance() {
33+
static UrlOpener instance;
34+
return instance;
35+
}
36+
37+
bool UrlOpener::IsSupported() const {
3338
return true;
3439
}
3540

src/platform/ohos/url_opener_ohos.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ UrlLaunchOutcome LaunchUnsupported(const std::string& url) {
1111

1212
} // namespace
1313

14-
bool UrlOpener::IsSupported() {
14+
UrlOpener& UrlOpener::GetInstance() {
15+
static UrlOpener instance;
16+
return instance;
17+
}
18+
19+
bool UrlOpener::IsSupported() const {
1520
return false;
1621
}
1722

src/platform/windows/url_opener_windows.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ UrlLaunchOutcome LaunchUrl(const std::string& url) {
2323

2424
} // namespace
2525

26-
bool UrlOpener::IsSupported() {
26+
UrlOpener& UrlOpener::GetInstance() {
27+
static UrlOpener instance;
28+
return instance;
29+
}
30+
31+
bool UrlOpener::IsSupported() const {
2732
return true;
2833
}
2934

src/url_opener.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@ struct UrlOpenResult {
2121

2222
class UrlOpener {
2323
public:
24-
static bool IsSupported();
24+
static UrlOpener& GetInstance();
25+
26+
bool IsSupported() const;
2527

2628
UrlOpenResult Open(const std::string& url) const;
29+
30+
UrlOpener(const UrlOpener&) = delete;
31+
UrlOpener& operator=(const UrlOpener&) = delete;
32+
UrlOpener(UrlOpener&&) = delete;
33+
UrlOpener& operator=(UrlOpener&&) = delete;
34+
35+
private:
36+
UrlOpener() = default;
2737
};
2838

2939
} // namespace nativeapi

0 commit comments

Comments
 (0)