Skip to content

Commit

Permalink
Make Engine::RunBundle* reuse an existing RuntimeController (flutter#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderso authored Oct 17, 2017
1 parent 2528e53 commit faaf321
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
45 changes: 27 additions & 18 deletions runtime/dart_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,18 @@ static void ReleaseFetchedBytes(uint8_t* buffer) {
tonic::DartErrorHandleType DartController::RunFromKernel(
const std::vector<uint8_t>& kernel) {
tonic::DartState::Scope scope(dart_state());
// Copy kernel bytes and pass ownership of the copy to the Dart_LoadKernel,
// which is expected to release them.
uint8_t* kernel_bytes = nullptr;
CopyVectorBytes(kernel, kernel_bytes);

Dart_Handle result = Dart_LoadKernel(
Dart_ReadKernelBinary(kernel_bytes, kernel.size(), ReleaseFetchedBytes));
LogIfError(result);
tonic::DartErrorHandleType error = tonic::GetErrorHandleType(result);
tonic::DartErrorHandleType error = tonic::kNoError;
if (Dart_IsNull(Dart_RootLibrary())) {
// Copy kernel bytes and pass ownership of the copy to the Dart_LoadKernel,
// which is expected to release them.
uint8_t* kernel_bytes = nullptr;
CopyVectorBytes(kernel, kernel_bytes);

Dart_Handle result = Dart_LoadKernel(Dart_ReadKernelBinary(
kernel_bytes, kernel.size(), ReleaseFetchedBytes));
LogIfError(result);
error = tonic::GetErrorHandleType(result);
}
if (SendStartMessage(Dart_RootLibrary())) {
return tonic::kUnknownErrorType;
}
Expand All @@ -141,9 +144,12 @@ tonic::DartErrorHandleType DartController::RunFromScriptSnapshot(
const uint8_t* buffer,
size_t size) {
tonic::DartState::Scope scope(dart_state());
Dart_Handle result = Dart_LoadScriptFromSnapshot(buffer, size);
LogIfError(result);
tonic::DartErrorHandleType error = tonic::GetErrorHandleType(result);
tonic::DartErrorHandleType error = tonic::kNoError;
if (Dart_IsNull(Dart_RootLibrary())) {
Dart_Handle result = Dart_LoadScriptFromSnapshot(buffer, size);
LogIfError(result);
error = tonic::GetErrorHandleType(result);
}
if (SendStartMessage(Dart_RootLibrary())) {
return tonic::kUnknownErrorType;
}
Expand All @@ -154,12 +160,15 @@ tonic::DartErrorHandleType DartController::RunFromSource(
const std::string& main,
const std::string& packages) {
tonic::DartState::Scope scope(dart_state());
tonic::FileLoader& loader = dart_state()->file_loader();
if (!packages.empty() && !loader.LoadPackagesMap(ResolvePath(packages)))
FXL_LOG(WARNING) << "Failed to load package map: " << packages;
Dart_Handle result = loader.LoadScript(main);
LogIfError(result);
tonic::DartErrorHandleType error = tonic::GetErrorHandleType(result);
tonic::DartErrorHandleType error = tonic::kNoError;
if (Dart_IsNull(Dart_RootLibrary())) {
tonic::FileLoader& loader = dart_state()->file_loader();
if (!packages.empty() && !loader.LoadPackagesMap(ResolvePath(packages)))
FXL_LOG(WARNING) << "Failed to load package map: " << packages;
Dart_Handle result = loader.LoadScript(main);
LogIfError(result);
error = tonic::GetErrorHandleType(result);
}
if (SendStartMessage(Dart_RootLibrary())) {
return tonic::kCompilationErrorType;
}
Expand Down
18 changes: 10 additions & 8 deletions shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,16 @@ void Engine::ConfigureAssetBundle(const std::string& path) {

void Engine::ConfigureRuntime(const std::string& script_uri,
const std::vector<uint8_t>& platform_kernel) {
runtime_ = blink::RuntimeController::Create(this);
runtime_->CreateDartController(
std::move(script_uri), default_isolate_snapshot_data,
default_isolate_snapshot_instr, platform_kernel);
runtime_->SetViewportMetrics(viewport_metrics_);
runtime_->SetLocale(language_code_, country_code_);
runtime_->SetTextScaleFactor(text_scale_factor_);
runtime_->SetSemanticsEnabled(semantics_enabled_);
if (!runtime_) {
runtime_ = blink::RuntimeController::Create(this);
runtime_->CreateDartController(
std::move(script_uri), default_isolate_snapshot_data,
default_isolate_snapshot_instr, platform_kernel);
runtime_->SetViewportMetrics(viewport_metrics_);
runtime_->SetLocale(language_code_, country_code_);
runtime_->SetTextScaleFactor(text_scale_factor_);
runtime_->SetSemanticsEnabled(semantics_enabled_);
}
}

void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
Expand Down
17 changes: 11 additions & 6 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,16 @@ public void surfaceDestroyed(SurfaceHolder holder) {
StringCodec.INSTANCE);
mFlutterSystemChannel = new BasicMessageChannel<>(this, "flutter/system",
JSONMessageCodec.INSTANCE);
PlatformPlugin platformPlugin = new PlatformPlugin((Activity) getContext());
MethodChannel flutterPlatformChannel = new MethodChannel(this,
"flutter/platform", JSONMethodCodec.INSTANCE);
flutterPlatformChannel.setMethodCallHandler(platformPlugin);
addActivityLifecycleListener(platformPlugin);

// TODO(plugins): Change PlatformPlugin to accept a Context. Disable the
// operations that require an Activity when a Context is passed.
if (getContext() instanceof Activity) {
PlatformPlugin platformPlugin = new PlatformPlugin((Activity) getContext());
MethodChannel flutterPlatformChannel = new MethodChannel(this,
"flutter/platform", JSONMethodCodec.INSTANCE);
flutterPlatformChannel.setMethodCallHandler(platformPlugin);
addActivityLifecycleListener(platformPlugin);
}
mTextInputPlugin = new TextInputPlugin(this);

setLocale(getResources().getConfiguration().locale);
Expand Down Expand Up @@ -285,7 +290,7 @@ private void setTextScaleFactor(float textScaleFactor) {
message.put("textScaleFactor", textScaleFactor);
mFlutterSystemChannel.send(message);
}

private void setLocale(Locale locale) {
mFlutterLocalizationChannel.invokeMethod("setLocale",
Arrays.asList(locale.getLanguage(), locale.getCountry()));
Expand Down

0 comments on commit faaf321

Please sign in to comment.