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

Commit 4f1c6f3

Browse files
authored
[url_launcher_linux] file resource URI support for canLaunch (#4491)
1 parent 3638e67 commit 4f1c6f3

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

.ci/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo ap
4141
RUN echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list
4242
RUN apt-get update && apt-get install -y --no-install-recommends google-chrome-stable
4343

44-
# Make Chrome the default so http: has a handler for url_launcher tests.
44+
# Make Chrome the default so http: and file: has a handler for url_launcher tests.
4545
RUN apt-get install -y xdg-utils
4646
RUN xdg-settings set default-web-browser google-chrome.desktop
47+
RUN xdg-mime default google-chrome.desktop inode/directory

packages/url_launcher/url_launcher_linux/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
## NEXT
1+
## 2.0.3
22

33
* Updates code for new analysis options.
44
* Fix minor memory leak in Linux url_launcher tests.
5+
* Fixes canLaunch detection for URIs addressing on local or network file systems
56

67
## 2.0.2
78

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+
g_autoptr(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+
g_autoptr(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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ static gchar* get_url(FlValue* args, GError** error) {
4545
return g_strdup(fl_value_get_string(url_value));
4646
}
4747

48+
// Checks if URI has launchable file resource.
49+
static gboolean can_launch_uri_with_file_resource(FlUrlLauncherPlugin* self,
50+
const gchar* url) {
51+
g_autoptr(GError) error = nullptr;
52+
g_autoptr(GFile) file = g_file_new_for_uri(url);
53+
g_autoptr(GAppInfo) app_info =
54+
g_file_query_default_handler(file, NULL, &error);
55+
return app_info != nullptr;
56+
}
57+
4858
// Called to check if a URL can be launched.
4959
FlMethodResponse* can_launch(FlUrlLauncherPlugin* self, FlValue* args) {
5060
g_autoptr(GError) error = nullptr;
@@ -60,6 +70,10 @@ FlMethodResponse* can_launch(FlUrlLauncherPlugin* self, FlValue* args) {
6070
g_autoptr(GAppInfo) app_info =
6171
g_app_info_get_default_for_uri_scheme(scheme);
6272
is_launchable = app_info != nullptr;
73+
74+
if (!is_launchable) {
75+
is_launchable = can_launch_uri_with_file_resource(self, url);
76+
}
6377
}
6478

6579
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)