Skip to content

Commit

Permalink
Add setInitialRoute on FlutterView. (flutter#3727)
Browse files Browse the repository at this point in the history
  • Loading branch information
szakarias authored Jun 6, 2017
1 parent 62c6f65 commit f37aafd
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 27 deletions.
18 changes: 0 additions & 18 deletions lib/ui/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,7 @@ void _updateSemanticsEnabled(bool enabled) {
window.onSemanticsEnabledChanged();
}

void _handleNavigationMessage(ByteData data) {
if (window._defaultRouteName != null)
return;
try {
final dynamic message = _decodeJSON(_decodeUTF8(data));
final dynamic method = message['method'];
if (method != 'pushRoute')
return;
final dynamic args = message['args'];
window._defaultRouteName = args[0];
} catch (e) {
// We ignore any exception and just let the message be dispatched as usual.
}
}

void _dispatchPlatformMessage(String name, ByteData data, int responseId) {
if (name == 'flutter/navigation')
_handleNavigationMessage(data);

if (window.onPlatformMessage != null) {
window.onPlatformMessage(name, data, (ByteData responseData) {
window._respondToPlatformMessage(responseId, responseData);
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ class Window {

/// The route or path that the operating system requested when the application
/// was launched.
String get defaultRouteName => _defaultRouteName;
String _defaultRouteName;
String get defaultRouteName => _defaultRouteName();
String _defaultRouteName() native "Window_defaultRouteName";

/// Requests that, at the next appropriate opportunity, the [onBeginFrame]
/// and [onDrawFrame] callbacks be invoked.
Expand Down
6 changes: 6 additions & 0 deletions lib/ui/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
return data_handle;
}

void DefaultRouteName(Dart_NativeArguments args) {
std::string routeName = UIDartState::Current()->window()->client()->DefaultRouteName();
Dart_SetReturnValue(args, StdStringToDart(routeName));
}

void ScheduleFrame(Dart_NativeArguments args) {
UIDartState::Current()->window()->client()->ScheduleFrame();
}
Expand Down Expand Up @@ -253,6 +258,7 @@ void Window::CompletePlatformMessageResponse(int response_id,

void Window::RegisterNatives(tonic::DartLibraryNatives* natives) {
natives->Register({
{"Window_defaultRouteName", DefaultRouteName, 1, true},
{"Window_scheduleFrame", ScheduleFrame, 1, true},
{"Window_sendPlatformMessage", _SendPlatformMessage, 4, true},
{"Window_respondToPlatformMessage", _RespondToPlatformMessage, 3, true},
Expand Down
1 change: 1 addition & 0 deletions lib/ui/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Scene;

class WindowClient {
public:
virtual std::string DefaultRouteName() = 0;
virtual void ScheduleFrame() = 0;
virtual void Render(Scene* scene) = 0;
virtual void UpdateSemantics(SemanticsUpdate* update) = 0;
Expand Down
4 changes: 4 additions & 0 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ Window* RuntimeController::GetWindow() {
return dart_controller_->dart_state()->window();
}

std::string RuntimeController::DefaultRouteName() {
return client_->DefaultRouteName();
}

void RuntimeController::ScheduleFrame() {
client_->ScheduleFrame();
}
Expand Down
1 change: 1 addition & 0 deletions runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class RuntimeController : public WindowClient, public IsolateClient {

Window* GetWindow();

std::string DefaultRouteName() override;
void ScheduleFrame() override;
void Render(Scene* scene) override;
void UpdateSemantics(SemanticsUpdate* update) override;
Expand Down
1 change: 1 addition & 0 deletions runtime/runtime_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace blink {

class RuntimeDelegate {
public:
virtual std::string DefaultRouteName() = 0;
virtual void ScheduleFrame() = 0;
virtual void Render(std::unique_ptr<flow::LayerTree> layer_tree) = 0;
virtual void UpdateSemantics(std::vector<SemanticsNode> update) = 0;
Expand Down
17 changes: 11 additions & 6 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void Engine::DispatchPlatformMessage(
return;
}

// If there's no runtime_, we need to buffer some navigation messages.
// If there's no runtime_, we may still need to set the initial route.
if (message->channel() == kNavigationChannel)
HandleNavigationPlatformMessage(std::move(message));
}
Expand Down Expand Up @@ -341,10 +341,10 @@ bool Engine::HandleNavigationPlatformMessage(
return false;
auto root = document.GetObject();
auto method = root.FindMember("method");
if (method == root.MemberEnd() || method->value != "pushRoute")
if (method->value != "setInitialRoute")
return false;

pending_push_route_message_ = std::move(message);
auto route = root.FindMember("args");
initial_route_ = std::move(route->value.GetString());
return true;
}

Expand Down Expand Up @@ -427,8 +427,6 @@ void Engine::ConfigureRuntime(const std::string& script_uri) {
runtime_->SetViewportMetrics(viewport_metrics_);
runtime_->SetLocale(language_code_, country_code_);
runtime_->SetSemanticsEnabled(semantics_enabled_);
if (pending_push_route_message_)
runtime_->DispatchPlatformMessage(std::move(pending_push_route_message_));
}

void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
Expand All @@ -450,6 +448,13 @@ void Engine::StartAnimatorIfPossible() {
animator_->Start();
}

std::string Engine::DefaultRouteName() {
if (!initial_route_.empty()) {
return initial_route_;
}
return "/";
}

void Engine::ScheduleFrame() {
animator_->RequestFrame();
}
Expand Down
3 changes: 2 additions & 1 deletion shell/common/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Engine : public blink::RuntimeDelegate {

private:
// RuntimeDelegate methods:
std::string DefaultRouteName() override;
void ScheduleFrame() override;
void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
void UpdateSemantics(std::vector<blink::SemanticsNode> update) override;
Expand Down Expand Up @@ -98,7 +99,7 @@ class Engine : public blink::RuntimeDelegate {
std::unique_ptr<Animator> animator_;
std::unique_ptr<blink::RuntimeController> runtime_;
tonic::DartErrorHandleType load_script_error_;
ftl::RefPtr<blink::PlatformMessage> pending_push_route_message_;
std::string initial_route_;
blink::ViewportMetrics viewport_metrics_;
std::string language_code_;
std::string country_code_;
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ public void onMemoryPressure() {
mFlutterSystemChannel.send(message);
}

public void setInitialRoute(String route) {
mFlutterNavigationChannel.invokeMethod("setInitialRoute", route);
}

public void pushRoute(String route) {
mFlutterNavigationChannel.invokeMethod("pushRoute", route);
}
Expand Down

0 comments on commit f37aafd

Please sign in to comment.