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

Commit f37aafd

Browse files
authored
Add setInitialRoute on FlutterView. (#3727)
1 parent 62c6f65 commit f37aafd

File tree

10 files changed

+32
-27
lines changed

10 files changed

+32
-27
lines changed

lib/ui/hooks.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,7 @@ void _updateSemanticsEnabled(bool enabled) {
4040
window.onSemanticsEnabledChanged();
4141
}
4242

43-
void _handleNavigationMessage(ByteData data) {
44-
if (window._defaultRouteName != null)
45-
return;
46-
try {
47-
final dynamic message = _decodeJSON(_decodeUTF8(data));
48-
final dynamic method = message['method'];
49-
if (method != 'pushRoute')
50-
return;
51-
final dynamic args = message['args'];
52-
window._defaultRouteName = args[0];
53-
} catch (e) {
54-
// We ignore any exception and just let the message be dispatched as usual.
55-
}
56-
}
57-
5843
void _dispatchPlatformMessage(String name, ByteData data, int responseId) {
59-
if (name == 'flutter/navigation')
60-
_handleNavigationMessage(data);
61-
6244
if (window.onPlatformMessage != null) {
6345
window.onPlatformMessage(name, data, (ByteData responseData) {
6446
window._respondToPlatformMessage(responseId, responseData);

lib/ui/window.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ class Window {
275275

276276
/// The route or path that the operating system requested when the application
277277
/// was launched.
278-
String get defaultRouteName => _defaultRouteName;
279-
String _defaultRouteName;
278+
String get defaultRouteName => _defaultRouteName();
279+
String _defaultRouteName() native "Window_defaultRouteName";
280280

281281
/// Requests that, at the next appropriate opportunity, the [onBeginFrame]
282282
/// and [onDrawFrame] callbacks be invoked.

lib/ui/window/window.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
3939
return data_handle;
4040
}
4141

42+
void DefaultRouteName(Dart_NativeArguments args) {
43+
std::string routeName = UIDartState::Current()->window()->client()->DefaultRouteName();
44+
Dart_SetReturnValue(args, StdStringToDart(routeName));
45+
}
46+
4247
void ScheduleFrame(Dart_NativeArguments args) {
4348
UIDartState::Current()->window()->client()->ScheduleFrame();
4449
}
@@ -253,6 +258,7 @@ void Window::CompletePlatformMessageResponse(int response_id,
253258

254259
void Window::RegisterNatives(tonic::DartLibraryNatives* natives) {
255260
natives->Register({
261+
{"Window_defaultRouteName", DefaultRouteName, 1, true},
256262
{"Window_scheduleFrame", ScheduleFrame, 1, true},
257263
{"Window_sendPlatformMessage", _SendPlatformMessage, 4, true},
258264
{"Window_respondToPlatformMessage", _RespondToPlatformMessage, 3, true},

lib/ui/window/window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Scene;
2323

2424
class WindowClient {
2525
public:
26+
virtual std::string DefaultRouteName() = 0;
2627
virtual void ScheduleFrame() = 0;
2728
virtual void Render(Scene* scene) = 0;
2829
virtual void UpdateSemantics(SemanticsUpdate* update) = 0;

runtime/runtime_controller.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ Window* RuntimeController::GetWindow() {
9898
return dart_controller_->dart_state()->window();
9999
}
100100

101+
std::string RuntimeController::DefaultRouteName() {
102+
return client_->DefaultRouteName();
103+
}
104+
101105
void RuntimeController::ScheduleFrame() {
102106
client_->ScheduleFrame();
103107
}

runtime/runtime_controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class RuntimeController : public WindowClient, public IsolateClient {
5252

5353
Window* GetWindow();
5454

55+
std::string DefaultRouteName() override;
5556
void ScheduleFrame() override;
5657
void Render(Scene* scene) override;
5758
void UpdateSemantics(SemanticsUpdate* update) override;

runtime/runtime_delegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace blink {
1717

1818
class RuntimeDelegate {
1919
public:
20+
virtual std::string DefaultRouteName() = 0;
2021
virtual void ScheduleFrame() = 0;
2122
virtual void Render(std::unique_ptr<flow::LayerTree> layer_tree) = 0;
2223
virtual void UpdateSemantics(std::vector<SemanticsNode> update) = 0;

shell/common/engine.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ void Engine::DispatchPlatformMessage(
310310
return;
311311
}
312312

313-
// If there's no runtime_, we need to buffer some navigation messages.
313+
// If there's no runtime_, we may still need to set the initial route.
314314
if (message->channel() == kNavigationChannel)
315315
HandleNavigationPlatformMessage(std::move(message));
316316
}
@@ -341,10 +341,10 @@ bool Engine::HandleNavigationPlatformMessage(
341341
return false;
342342
auto root = document.GetObject();
343343
auto method = root.FindMember("method");
344-
if (method == root.MemberEnd() || method->value != "pushRoute")
344+
if (method->value != "setInitialRoute")
345345
return false;
346-
347-
pending_push_route_message_ = std::move(message);
346+
auto route = root.FindMember("args");
347+
initial_route_ = std::move(route->value.GetString());
348348
return true;
349349
}
350350

@@ -427,8 +427,6 @@ void Engine::ConfigureRuntime(const std::string& script_uri) {
427427
runtime_->SetViewportMetrics(viewport_metrics_);
428428
runtime_->SetLocale(language_code_, country_code_);
429429
runtime_->SetSemanticsEnabled(semantics_enabled_);
430-
if (pending_push_route_message_)
431-
runtime_->DispatchPlatformMessage(std::move(pending_push_route_message_));
432430
}
433431

434432
void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
@@ -450,6 +448,13 @@ void Engine::StartAnimatorIfPossible() {
450448
animator_->Start();
451449
}
452450

451+
std::string Engine::DefaultRouteName() {
452+
if (!initial_route_.empty()) {
453+
return initial_route_;
454+
}
455+
return "/";
456+
}
457+
453458
void Engine::ScheduleFrame() {
454459
animator_->RequestFrame();
455460
}

shell/common/engine.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Engine : public blink::RuntimeDelegate {
7171

7272
private:
7373
// RuntimeDelegate methods:
74+
std::string DefaultRouteName() override;
7475
void ScheduleFrame() override;
7576
void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
7677
void UpdateSemantics(std::vector<blink::SemanticsNode> update) override;
@@ -98,7 +99,7 @@ class Engine : public blink::RuntimeDelegate {
9899
std::unique_ptr<Animator> animator_;
99100
std::unique_ptr<blink::RuntimeController> runtime_;
100101
tonic::DartErrorHandleType load_script_error_;
101-
ftl::RefPtr<blink::PlatformMessage> pending_push_route_message_;
102+
std::string initial_route_;
102103
blink::ViewportMetrics viewport_metrics_;
103104
std::string language_code_;
104105
std::string country_code_;

shell/platform/android/io/flutter/view/FlutterView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ public void onMemoryPressure() {
241241
mFlutterSystemChannel.send(message);
242242
}
243243

244+
public void setInitialRoute(String route) {
245+
mFlutterNavigationChannel.invokeMethod("setInitialRoute", route);
246+
}
247+
244248
public void pushRoute(String route) {
245249
mFlutterNavigationChannel.invokeMethod("pushRoute", route);
246250
}

0 commit comments

Comments
 (0)