Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 41e109d

Browse files
committed
[url_launcher_linux] Support file scheme
1 parent b401c84 commit 41e109d

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

packages/url_launcher/url_launcher_linux/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.3
2+
3+
* Fix canLaunch detection for `file` scheme
4+
15
## 2.0.2
26

37
* Replaced reference to `shared_preferences` plugin with the `url_launcher` in the README.

packages/url_launcher/url_launcher_linux/linux/test/url_launcher_linux_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ TEST(UrlLauncherPlugin, CanLaunchFailureUnhandled) {
3939
expected));
4040
}
4141

42+
TEST(UrlLauncherPlugin, CanLaunchFileSuccess) {
43+
g_autoptr(FlValue) args = fl_value_new_map();
44+
fl_value_set_string_take(args, "url", fl_value_new_string("file:/"));
45+
FlMethodResponse* response = can_launch(nullptr, args);
46+
ASSERT_NE(response, nullptr);
47+
ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
48+
g_autoptr(FlValue) expected = fl_value_new_bool(true);
49+
EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
50+
FL_METHOD_SUCCESS_RESPONSE(response)),
51+
expected));
52+
}
53+
54+
TEST(UrlLauncherPlugin, CanLaunchFailureInvalidFileExtension) {
55+
g_autoptr(FlValue) args = fl_value_new_map();
56+
fl_value_set_string_take(
57+
args, "url", fl_value_new_string("file:///madeup.madeupextension"));
58+
FlMethodResponse* response = can_launch(nullptr, args);
59+
ASSERT_NE(response, nullptr);
60+
ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
61+
g_autoptr(FlValue) expected = fl_value_new_bool(false);
62+
EXPECT_TRUE(fl_value_equal(fl_method_success_response_get_result(
63+
FL_METHOD_SUCCESS_RESPONSE(response)),
64+
expected));
65+
}
66+
4267
// For consistency with the established mobile implementations,
4368
// an invalid URL should return false, not an error.
4469
TEST(UrlLauncherPlugin, CanLaunchFailureInvalidUrl) {

packages/url_launcher/url_launcher_linux/linux/url_launcher_plugin.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ static gchar* get_url(FlValue* args, GError** error) {
4545
return g_strdup(fl_value_get_string(url_value));
4646
}
4747

48+
// Called to check if URL with file scheme can be launched.
49+
static gboolean can_launch_url_with_file_scheme(FlUrlLauncherPlugin* self,
50+
const gchar* url) {
51+
g_autoptr(GError) error = nullptr;
52+
GFile* file = g_file_new_for_uri(url);
53+
GAppInfo* app_info = g_file_query_default_handler(file, NULL, &error);
54+
g_object_unref(file);
55+
gboolean is_launchable = FALSE;
56+
if (app_info != nullptr) {
57+
is_launchable = TRUE;
58+
g_object_unref(app_info);
59+
}
60+
return is_launchable;
61+
}
62+
4863
// Called to check if a URL can be launched.
4964
FlMethodResponse* can_launch(FlUrlLauncherPlugin* self, FlValue* args) {
5065
g_autoptr(GError) error = nullptr;
@@ -60,6 +75,10 @@ FlMethodResponse* can_launch(FlUrlLauncherPlugin* self, FlValue* args) {
6075
g_autoptr(GAppInfo) app_info =
6176
g_app_info_get_default_for_uri_scheme(scheme);
6277
is_launchable = app_info != nullptr;
78+
79+
if (!is_launchable) {
80+
is_launchable = can_launch_url_with_file_scheme(self, url);
81+
}
6382
}
6483

6584
g_autoptr(FlValue) result = fl_value_new_bool(is_launchable);

packages/url_launcher/url_launcher_linux/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: url_launcher_linux
22
description: Linux implementation of the url_launcher plugin.
33
repository: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_linux
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
5-
version: 2.0.2
5+
version: 2.0.3
66

77
environment:
88
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)