Flutter Engine
fl_dart_project.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h"
6 
7 #include <gmodule.h>
8 
9 #include <string>
10 #include <vector>
11 
12 #include "flutter/shell/platform/common/cpp/engine_switches.h"
13 #include "flutter/shell/platform/linux/fl_dart_project_private.h"
14 
16  GObject parent_instance;
17 
18  gboolean enable_mirrors;
20  gchar* assets_path;
21  gchar* icu_data_path;
23 };
24 
25 G_DEFINE_TYPE(FlDartProject, fl_dart_project, G_TYPE_OBJECT)
26 
27 // Gets the directory the current executable is in.
28 static gchar* get_executable_dir() {
29  g_autoptr(GError) error = nullptr;
30  g_autofree gchar* exe_path = g_file_read_link("/proc/self/exe", &error);
31  if (exe_path == nullptr) {
32  g_critical("Failed to determine location of executable: %s",
33  error->message);
34  return nullptr;
35  }
36 
37  return g_path_get_dirname(exe_path);
38 }
39 
40 static void fl_dart_project_dispose(GObject* object) {
41  FlDartProject* self = FL_DART_PROJECT(object);
42 
43  g_clear_pointer(&self->aot_library_path, g_free);
44  g_clear_pointer(&self->assets_path, g_free);
45  g_clear_pointer(&self->icu_data_path, g_free);
46  g_clear_pointer(&self->dart_entrypoint_args, g_strfreev);
47 
48  G_OBJECT_CLASS(fl_dart_project_parent_class)->dispose(object);
49 }
50 
51 static void fl_dart_project_class_init(FlDartProjectClass* klass) {
52  G_OBJECT_CLASS(klass)->dispose = fl_dart_project_dispose;
53 }
54 
55 static void fl_dart_project_init(FlDartProject* self) {}
56 
57 G_MODULE_EXPORT FlDartProject* fl_dart_project_new() {
58  FlDartProject* self =
59  FL_DART_PROJECT(g_object_new(fl_dart_project_get_type(), nullptr));
60 
61  g_autofree gchar* executable_dir = get_executable_dir();
62  self->aot_library_path =
63  g_build_filename(executable_dir, "lib", "libapp.so", nullptr);
64  self->assets_path =
65  g_build_filename(executable_dir, "data", "flutter_assets", nullptr);
66  self->icu_data_path =
67  g_build_filename(executable_dir, "data", "icudtl.dat", nullptr);
68 
69  return self;
70 }
71 
73  FlDartProject* self,
74  gboolean enable_mirrors) {
75  g_return_if_fail(FL_IS_DART_PROJECT(self));
76  self->enable_mirrors = enable_mirrors;
77 }
78 
79 G_MODULE_EXPORT gboolean
80 fl_dart_project_get_enable_mirrors(FlDartProject* self) {
81  g_return_val_if_fail(FL_IS_DART_PROJECT(self), FALSE);
82  return self->enable_mirrors;
83 }
84 
85 G_MODULE_EXPORT const gchar* fl_dart_project_get_aot_library_path(
86  FlDartProject* self) {
87  g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
88  return self->aot_library_path;
89 }
90 
91 G_MODULE_EXPORT const gchar* fl_dart_project_get_assets_path(
92  FlDartProject* self) {
93  g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
94  return self->assets_path;
95 }
96 
97 G_MODULE_EXPORT const gchar* fl_dart_project_get_icu_data_path(
98  FlDartProject* self) {
99  g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
100  return self->icu_data_path;
101 }
102 
104  FlDartProject* self) {
105  g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
106  return self->dart_entrypoint_args;
107 }
108 
110  FlDartProject* self,
111  char** argv) {
112  g_return_if_fail(FL_IS_DART_PROJECT(self));
113  g_clear_pointer(&self->dart_entrypoint_args, g_strfreev);
114  self->dart_entrypoint_args = g_strdupv(argv);
115 }
116 
117 GPtrArray* fl_dart_project_get_switches(FlDartProject* self) {
118  GPtrArray* switches = g_ptr_array_new_with_free_func(g_free);
119  std::vector<std::string> env_switches = flutter::GetSwitchesFromEnvironment();
120  for (const auto& env_switch : env_switches) {
121  g_ptr_array_add(switches, g_strdup(env_switch.c_str()));
122  }
123  if (self->enable_mirrors) {
124  g_ptr_array_add(switches, g_strdup("--dart-flags=--enable_mirrors=true"));
125  }
126  return switches;
127 }
G_MODULE_EXPORT const gchar * fl_dart_project_get_icu_data_path(FlDartProject *self)
static void fl_dart_project_class_init(FlDartProjectClass *klass)
G_MODULE_EXPORT void fl_dart_project_set_dart_entrypoint_arguments(FlDartProject *self, char **argv)
gchar * aot_library_path
FlMethodResponse GError ** error
G_MODULE_EXPORT gchar ** fl_dart_project_get_dart_entrypoint_arguments(FlDartProject *self)
G_MODULE_EXPORT const gchar * fl_dart_project_get_aot_library_path(FlDartProject *self)
GPtrArray * fl_dart_project_get_switches(FlDartProject *self)
gboolean enable_mirrors
std::vector< std::string > GetSwitchesFromEnvironment()
GObject parent_instance
G_MODULE_EXPORT gboolean fl_dart_project_get_enable_mirrors(FlDartProject *self)
static gchar * get_executable_dir()
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
static void fl_dart_project_dispose(GObject *object)
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
G_MODULE_EXPORT void fl_dart_project_set_enable_mirrors(FlDartProject *self, gboolean enable_mirrors)
static void fl_dart_project_init(FlDartProject *self)
G_MODULE_EXPORT const gchar * fl_dart_project_get_assets_path(FlDartProject *self)
gchar ** dart_entrypoint_args