|
| 1 | +// AUTO-GENERATED. DO NOT EDIT. |
| 2 | +// Any manual changes WILL BE LOST when this file is regenerated. |
| 3 | + |
1 | 4 | #include "url_opener_c.h" |
2 | 5 |
|
3 | | -#include <exception> |
| 6 | +#include <cstdlib> |
| 7 | +#include <cstring> |
4 | 8 | #include <string> |
5 | 9 |
|
6 | 10 | #include "../url_opener.h" |
7 | | -#include "string_utils_c.h" |
8 | 11 |
|
9 | | -using namespace nativeapi; |
| 12 | +#include <cstdio> |
10 | 13 |
|
11 | 14 | namespace { |
12 | 15 |
|
13 | | -native_url_open_error_code_t ToCErrorCode(UrlOpenErrorCode code) { |
14 | | - switch (code) { |
15 | | - case UrlOpenErrorCode::kNone: |
16 | | - return NATIVE_URL_OPEN_ERROR_NONE; |
17 | | - case UrlOpenErrorCode::kInvalidUrlEmpty: |
18 | | - return NATIVE_URL_OPEN_ERROR_INVALID_URL_EMPTY; |
19 | | - case UrlOpenErrorCode::kInvalidUrlMissingScheme: |
20 | | - return NATIVE_URL_OPEN_ERROR_INVALID_URL_MISSING_SCHEME; |
21 | | - case UrlOpenErrorCode::kInvalidUrlUnsupportedScheme: |
22 | | - return NATIVE_URL_OPEN_ERROR_INVALID_URL_UNSUPPORTED_SCHEME; |
23 | | - case UrlOpenErrorCode::kUnsupportedPlatform: |
24 | | - return NATIVE_URL_OPEN_ERROR_UNSUPPORTED_PLATFORM; |
25 | | - case UrlOpenErrorCode::kInvocationFailed: |
26 | | - return NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED; |
| 16 | +char* DupString(const std::string& value) { |
| 17 | + if (value.empty()) { |
| 18 | + return nullptr; |
| 19 | + } |
| 20 | + const auto size = value.size() + 1; |
| 21 | + auto* buffer = static_cast<char*>(std::malloc(size)); |
| 22 | + if (!buffer) { |
| 23 | + return nullptr; |
| 24 | + } |
| 25 | + std::memcpy(buffer, value.c_str(), size); |
| 26 | + return buffer; |
| 27 | +} |
| 28 | + |
| 29 | +native_url_open_error_code_t ToCUrlOpenErrorCode(nativeapi::UrlOpenErrorCode value) { |
| 30 | + switch (value) { |
| 31 | + case nativeapi::UrlOpenErrorCode::kNone: |
| 32 | + return NATIVE_URL_OPEN_ERROR_CODE_NONE; |
| 33 | + case nativeapi::UrlOpenErrorCode::kInvalidUrlEmpty: |
| 34 | + return NATIVE_URL_OPEN_ERROR_CODE_INVALID_URL_EMPTY; |
| 35 | + case nativeapi::UrlOpenErrorCode::kInvalidUrlMissingScheme: |
| 36 | + return NATIVE_URL_OPEN_ERROR_CODE_INVALID_URL_MISSING_SCHEME; |
| 37 | + case nativeapi::UrlOpenErrorCode::kInvalidUrlUnsupportedScheme: |
| 38 | + return NATIVE_URL_OPEN_ERROR_CODE_INVALID_URL_UNSUPPORTED_SCHEME; |
| 39 | + case nativeapi::UrlOpenErrorCode::kUnsupportedPlatform: |
| 40 | + return NATIVE_URL_OPEN_ERROR_CODE_UNSUPPORTED_PLATFORM; |
| 41 | + case nativeapi::UrlOpenErrorCode::kInvocationFailed: |
| 42 | + return NATIVE_URL_OPEN_ERROR_CODE_INVOCATION_FAILED; |
27 | 43 | default: |
28 | | - return NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED; |
| 44 | + return NATIVE_URL_OPEN_ERROR_CODE_NONE; |
29 | 45 | } |
30 | 46 | } |
31 | 47 |
|
32 | | -native_url_open_result_t MakeUrlOpenResult(bool success, |
33 | | - native_url_open_error_code_t error_code, |
34 | | - const std::string& message) { |
| 48 | +native_url_open_result_t ToCUrlOpenResult(const nativeapi::UrlOpenResult& value) { |
35 | 49 | native_url_open_result_t result = {}; |
36 | | - result.success = success; |
37 | | - result.error_code = error_code; |
38 | | - result.error_message = message.empty() ? nullptr : to_c_str(message); |
| 50 | + result.success = value.success; |
| 51 | + result.error_code = ToCUrlOpenErrorCode(value.error_code); |
| 52 | + result.error_message = DupString(value.error_message); |
39 | 53 | return result; |
40 | 54 | } |
41 | 55 |
|
42 | 56 | } // namespace |
43 | 57 |
|
44 | 58 | bool native_url_opener_is_supported(void) { |
45 | 59 | try { |
46 | | - return UrlOpener::GetInstance().IsSupported(); |
| 60 | + return nativeapi::UrlOpener::GetInstance().IsSupported(); |
47 | 61 | } catch (...) { |
| 62 | + fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_url_opener_is_supported"); |
48 | 63 | return false; |
49 | 64 | } |
50 | 65 | } |
51 | 66 |
|
52 | | -native_url_open_result_t native_url_opener_open(const char* url) { |
53 | | - if (!url) { |
54 | | - return MakeUrlOpenResult(false, NATIVE_URL_OPEN_ERROR_INVALID_URL_EMPTY, "URL is empty."); |
| 67 | +bool native_url_opener_can_open(const char* url) { |
| 68 | + try { |
| 69 | + return nativeapi::UrlOpener::GetInstance().CanOpen(std::string(url ? url : "")); |
| 70 | + } catch (...) { |
| 71 | + fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_url_opener_can_open"); |
| 72 | + return false; |
55 | 73 | } |
| 74 | +} |
56 | 75 |
|
| 76 | +native_url_open_result_t native_url_opener_open(const char* url) { |
57 | 77 | try { |
58 | | - const UrlOpenResult result = UrlOpener::GetInstance().Open(std::string(url)); |
59 | | - return MakeUrlOpenResult(result.success, ToCErrorCode(result.error_code), result.error_message); |
60 | | - } catch (const std::exception& e) { |
61 | | - return MakeUrlOpenResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED, e.what()); |
| 78 | + const auto cpp_result = nativeapi::UrlOpener::GetInstance().Open(std::string(url ? url : "")); |
| 79 | + return ToCUrlOpenResult(cpp_result); |
62 | 80 | } catch (...) { |
63 | | - return MakeUrlOpenResult(false, NATIVE_URL_OPEN_ERROR_INVOCATION_FAILED, |
64 | | - "Unexpected error while opening URL."); |
| 81 | + fprintf(stderr, "[nativeapi] %s: unexpected exception\n", "native_url_opener_open"); |
| 82 | + native_url_open_result_t result = {}; |
| 83 | + result.success = false; |
| 84 | + return result; |
65 | 85 | } |
66 | 86 | } |
67 | 87 |
|
68 | | -void native_url_open_result_free(native_url_open_result_t* result) { |
69 | | - if (!result) { |
| 88 | +void native_url_open_result_free(native_url_open_result_t* value) { |
| 89 | + if (!value) { |
70 | 90 | return; |
71 | 91 | } |
72 | | - |
73 | | - if (result->error_message) { |
74 | | - free_c_str(result->error_message); |
75 | | - result->error_message = nullptr; |
76 | | - } |
77 | | - result->success = false; |
78 | | - result->error_code = NATIVE_URL_OPEN_ERROR_NONE; |
| 92 | + std::free(value->error_message); |
| 93 | + value->error_message = nullptr; |
79 | 94 | } |
| 95 | + |
0 commit comments