|
21 | 21 | #include "flutter/shell/common/shorebird/snapshots_data_handle.h" |
22 | 22 | #include "flutter/shell/common/switches.h" |
23 | 23 | #include "fml/logging.h" |
| 24 | +#include "shell/platform/embedder/embedder.h" |
24 | 25 | #include "third_party/dart/runtime/include/dart_tools_api.h" |
25 | 26 |
|
26 | 27 | #include "third_party/updater/library/include/updater.h" |
@@ -79,8 +80,100 @@ FileCallbacks ShorebirdFileCallbacks() { |
79 | 80 | }; |
80 | 81 | } |
81 | 82 |
|
| 83 | +// FIXME: consolidate this with the other ConfigureShorebird |
| 84 | +bool ConfigureShorebird(const ShorebirdConfigArgs& args, |
| 85 | + std::string& patch_path) { |
| 86 | + patch_path = args.release_app_library_path; |
| 87 | + auto shorebird_updater_dir_name = "shorebird_updater"; |
| 88 | + |
| 89 | + auto code_cache_dir = fml::paths::JoinPaths( |
| 90 | + {std::move(args.code_cache_path), shorebird_updater_dir_name}); |
| 91 | + auto app_storage_dir = fml::paths::JoinPaths( |
| 92 | + {std::move(args.app_storage_path), shorebird_updater_dir_name}); |
| 93 | + |
| 94 | + fml::CreateDirectory(fml::paths::GetCachesDirectory(), |
| 95 | + {shorebird_updater_dir_name}, |
| 96 | + fml::FilePermission::kReadWrite); |
| 97 | + |
| 98 | + bool init_result; |
| 99 | + // Using a block to make AppParameters lifetime explicit. |
| 100 | + { |
| 101 | + AppParameters app_parameters; |
| 102 | + // Combine version and version_code into a single string. |
| 103 | + // We could also pass these separately through to the updater if needed. |
| 104 | + auto release_version = |
| 105 | + args.release_version.version + "+" + args.release_version.build_number; |
| 106 | + app_parameters.release_version = release_version.c_str(); |
| 107 | + app_parameters.code_cache_dir = code_cache_dir.c_str(); |
| 108 | + app_parameters.app_storage_dir = app_storage_dir.c_str(); |
| 109 | + |
| 110 | + // https://stackoverflow.com/questions/26032039/convert-vectorstring-into-char-c |
| 111 | + std::vector<const char*> c_paths{}; |
| 112 | + c_paths.push_back(args.release_app_library_path.c_str()); |
| 113 | + // Do not modify application_library_path or c_strings will invalidate. |
| 114 | + |
| 115 | + app_parameters.original_libapp_paths = c_paths.data(); |
| 116 | + app_parameters.original_libapp_paths_size = c_paths.size(); |
| 117 | + |
| 118 | + // shorebird_init copies from app_parameters and shorebirdYaml. |
| 119 | + init_result = shorebird_init(&app_parameters, ShorebirdFileCallbacks(), |
| 120 | + args.shorebird_yaml.c_str()); |
| 121 | + } |
| 122 | + |
| 123 | + // We've decided not to support synchronous updates on launch for now. |
| 124 | + // It's a terrible user experience (having the app hang on launch) and |
| 125 | + // instead we will provide examples of how to build a custom update UI |
| 126 | + // within Dart, including updating as part of login, etc. |
| 127 | + // https://github.com/shorebirdtech/shorebird/issues/950 |
| 128 | + |
| 129 | + // We only set the base snapshot on iOS for now. |
| 130 | + // TODO: this won't compile as we don't have a settings object here. |
| 131 | + // #if FML_OS_IOS || FML_OS_MACOSX |
| 132 | + // SetBaseSnapshot(settings); |
| 133 | + // #endif |
| 134 | + |
| 135 | + FML_LOG(INFO) << "Checking for active patch"; |
| 136 | + char* c_active_path = shorebird_next_boot_patch_path(); |
| 137 | + if (c_active_path != NULL) { |
| 138 | + patch_path = c_active_path; |
| 139 | + shorebird_free_string(c_active_path); |
| 140 | + FML_LOG(INFO) << "Shorebird updater: patch path: " << patch_path; |
| 141 | + } else { |
| 142 | + FML_LOG(INFO) << "Shorebird updater: no active patch."; |
| 143 | + } |
| 144 | + |
| 145 | + // We are careful only to report a launch start in the case where it's the |
| 146 | + // first time we've configured shorebird this process. Otherwise we could end |
| 147 | + // up in a case where we report a launch start, but never a completion (e.g. |
| 148 | + // from package:flutter_work_manager which sometimes creates a FlutterEngine |
| 149 | + // (and thus configures shorebird) but never runs it. The proper fix for this |
| 150 | + // is probably to move the launch_start() call to be later in the lifecycle |
| 151 | + // (when the snapshot is loaded and run, rather than when FlutterEngine is |
| 152 | + // initialized). This "hack" will still have a problem where FlutterEngine is |
| 153 | + // initialized but never run before the app is quit, could still cause us to |
| 154 | + // suddenly mark-bad a patch that was never actually attempted to launch. |
| 155 | + if (!init_result) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + // Once start_update_thread is called, the next_boot_patch* functions may |
| 160 | + // change their return values if the shorebird_report_launch_failed |
| 161 | + // function is called. |
| 162 | + shorebird_report_launch_start(); |
| 163 | + |
| 164 | + if (shorebird_should_auto_update()) { |
| 165 | + FML_LOG(INFO) << "Starting Shorebird update"; |
| 166 | + shorebird_start_update_thread(); |
| 167 | + } else { |
| 168 | + FML_LOG(INFO) |
| 169 | + << "Shorebird auto_update disabled, not checking for updates."; |
| 170 | + } |
| 171 | + |
| 172 | + return true; |
| 173 | +} |
| 174 | + |
82 | 175 | void ConfigureShorebird(const ShorebirdFlutterProjectArgs& args, |
83 | | - flutter::Settings& settings) { |
| 176 | + Settings& settings) { |
84 | 177 | // cache_path is used for both code_cache and app_storage, as we don't persist |
85 | 178 | // any data between releases. args.app_path is appended to |
86 | 179 | // the settings.application_library_path vector at this function's call site. |
|
0 commit comments