diff --git a/engine/src/flutter/shell/common/BUILD.gn b/engine/src/flutter/shell/common/BUILD.gn index e1008b7b72..f9e25a3c8d 100644 --- a/engine/src/flutter/shell/common/BUILD.gn +++ b/engine/src/flutter/shell/common/BUILD.gn @@ -61,6 +61,8 @@ source_set("common") { "animator.h", "engine.cc", "engine.h", + "null_platform_view.cc", + "null_platform_view.h", "null_rasterizer.cc", "null_rasterizer.h", "picture_serializer.cc", diff --git a/engine/src/flutter/shell/common/null_platform_view.cc b/engine/src/flutter/shell/common/null_platform_view.cc new file mode 100644 index 0000000000..65e61800f2 --- /dev/null +++ b/engine/src/flutter/shell/common/null_platform_view.cc @@ -0,0 +1,34 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/common/null_platform_view.h" + +#include "flutter/shell/common/null_rasterizer.h" +#include "flutter/shell/common/shell.h" + +namespace shell { + +NullPlatformView::NullPlatformView() + : PlatformView(std::make_unique()), weak_factory_(this) {} + +void NullPlatformView::Attach() { + CreateEngine(); +} + +NullPlatformView::~NullPlatformView() = default; + +fxl::WeakPtr NullPlatformView::GetWeakPtr() { + return weak_factory_.GetWeakPtr(); +} + +bool NullPlatformView::ResourceContextMakeCurrent() { + return false; +} + +// Hot-reload of the null platform view is not supported. +void NullPlatformView::RunFromSource(const std::string& assets_directory, + const std::string& main, + const std::string& packages) {} + +} // namespace shell diff --git a/engine/src/flutter/shell/common/null_platform_view.h b/engine/src/flutter/shell/common/null_platform_view.h new file mode 100644 index 0000000000..f740977731 --- /dev/null +++ b/engine/src/flutter/shell/common/null_platform_view.h @@ -0,0 +1,38 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMMON_NULL_PLATFORM_VIEW_H_ +#define COMMON_NULL_PLATFORM_VIEW_H_ + +#include "flutter/shell/common/platform_view.h" +#include "lib/fxl/macros.h" +#include "lib/fxl/memory/weak_ptr.h" + +namespace shell { + +class NullPlatformView : public PlatformView { + public: + NullPlatformView(); + + ~NullPlatformView(); + + fxl::WeakPtr GetWeakPtr(); + + virtual void Attach() override; + + bool ResourceContextMakeCurrent() override; + + void RunFromSource(const std::string& assets_directory, + const std::string& main, + const std::string& packages) override; + + private: + fxl::WeakPtrFactory weak_factory_; + + FXL_DISALLOW_COPY_AND_ASSIGN(NullPlatformView); +}; + +} // namespace shell + +#endif // COMMON_NULL_PLATFORM_VIEW_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index d186e24369..ebffd7e431 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -18,6 +18,7 @@ _flutter_framework_headers = [ "framework/Headers/FlutterChannels.h", "framework/Headers/FlutterCodecs.h", "framework/Headers/FlutterDartProject.h", + "framework/Headers/FlutterHeadlessDartRunner.h", "framework/Headers/FlutterMacros.h", "framework/Headers/FlutterNavigationController.h", "framework/Headers/FlutterPlugin.h", @@ -37,6 +38,7 @@ shared_library("create_flutter_framework_dylib") { "framework/Source/FlutterCodecs.mm", "framework/Source/FlutterDartProject.mm", "framework/Source/FlutterDartProject_Internal.h", + "framework/Source/FlutterHeadlessDartRunner.mm", "framework/Source/FlutterDartSource.h", "framework/Source/FlutterDartSource.mm", "framework/Source/FlutterNavigationController.mm", diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h index a47e239bd8..609eb70694 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h @@ -10,6 +10,7 @@ #include "FlutterChannels.h" #include "FlutterCodecs.h" #include "FlutterDartProject.h" +#include "FlutterHeadlessDartRunner.h" #include "FlutterMacros.h" #include "FlutterNavigationController.h" #include "FlutterPlugin.h" diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h new file mode 100644 index 0000000000..522766e410 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h @@ -0,0 +1,34 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_FLUTTERHEADLESSDARTRUNNER_H_ +#define FLUTTER_FLUTTERHEADLESSDARTRUNNER_H_ + +#import + +#include "FlutterDartProject.h" +#include "FlutterMacros.h" + +/** + The FlutterHeadlessDartRunner runs Flutter Dart code with a null rasterizer, + and no native drawing surface. It is appropriate for use in running Dart + code e.g. in the background from a plugin. +*/ +FLUTTER_EXPORT +@interface FlutterHeadlessDartRunner : NSObject + +/** + Runs a Dart function on an Isolate that is not the main application's Isolate. + The first call will create a new Isolate. Subsequent calls will reuse that + Isolate. The Isolate is destroyed when the FlutterHeadlessDartRunner is + destroyed. + + - Parameter entrypoint: The name of a top-level function from the same Dart + library that contains the app's main() function. +*/ +- (void)runWithEntrypoint:(NSString*)entrypoint; + +@end + +#endif // FLUTTER_FLUTTERHEADLESSDARTRUNNER_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm index 1da486d9b7..e220b326ad 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm @@ -168,6 +168,7 @@ static NSString* NSStringFromVMType(VMType type) { } - (void)launchInEngine:(shell::Engine*)engine + withEntrypoint:(NSString*)entrypoint embedderVMType:(VMType)embedderVMType result:(LaunchResult)result { if (_vmTypeRequirement == VMTypeInvalid) { @@ -193,10 +194,48 @@ static NSString* NSStringFromVMType(VMType type) { switch (_vmTypeRequirement) { case VMTypeInterpreter: - [self runFromSourceInEngine:engine result:result]; + [self runFromSourceInEngine:engine withEntrypoint:entrypoint result:result]; return; case VMTypePrecompilation: - [self runFromPrecompiledSourceInEngine:engine result:result]; + [self runFromPrecompiledSourceInEngine:engine withEntrypoint:entrypoint result:result]; + return; + case VMTypeInvalid: + break; + } + + return result(NO, @"Internal error"); +} + +- (void)launchInEngine:(shell::Engine*)engine + embedderVMType:(VMType)embedderVMType + result:(LaunchResult)result { + if (_vmTypeRequirement == VMTypeInvalid) { + result(NO, @"The Dart project is invalid and cannot be loaded by any VM."); + return; + } + + if (embedderVMType == VMTypeInvalid) { + result(NO, @"The embedder is invalid."); + return; + } + + if (_vmTypeRequirement != embedderVMType) { + NSString* message = + [NSString stringWithFormat: + @"Could not load the project because of differing project type. " + @"The project can run in '%@' but the embedder is configured as " + @"'%@'", + NSStringFromVMType(_vmTypeRequirement), NSStringFromVMType(embedderVMType)]; + result(NO, message); + return; + } + + switch (_vmTypeRequirement) { + case VMTypeInterpreter: + [self runFromSourceInEngine:engine withEntrypoint:@"main" result:result]; + return; + case VMTypePrecompilation: + [self runFromPrecompiledSourceInEngine:engine withEntrypoint:@"main" result:result]; return; case VMTypeInvalid: break; @@ -207,7 +246,9 @@ static NSString* NSStringFromVMType(VMType type) { #pragma mark - Running from precompiled application bundles -- (void)runFromPrecompiledSourceInEngine:(shell::Engine*)engine result:(LaunchResult)result { +- (void)runFromPrecompiledSourceInEngine:(shell::Engine*)engine + withEntrypoint:(NSString*)entrypoint + result:(LaunchResult)result { if (![_precompiledDartBundle load]) { NSString* message = [NSString stringWithFormat:@"Could not load the framework ('%@') containing precompiled code.", @@ -227,9 +268,11 @@ static NSString* NSStringFromVMType(VMType type) { } std::string bundle_path = path.UTF8String; - blink::Threads::UI()->PostTask([ engine = engine->GetWeakPtr(), bundle_path ] { + blink::Threads::UI()->PostTask([ + engine = engine->GetWeakPtr(), bundle_path, entrypoint = std::string([entrypoint UTF8String]) + ] { if (engine) - engine->RunBundle(bundle_path); + engine->RunBundle(bundle_path, entrypoint); }); result(YES, @"Success"); @@ -237,7 +280,9 @@ static NSString* NSStringFromVMType(VMType type) { #pragma mark - Running from source -- (void)runFromSourceInEngine:(shell::Engine*)engine result:(LaunchResult)result { +- (void)runFromSourceInEngine:(shell::Engine*)engine + withEntrypoint:(NSString*)entrypoint + result:(LaunchResult)result { if (_dartSource == nil) { result(NO, @"Dart source not specified."); return; @@ -251,9 +296,12 @@ static NSString* NSStringFromVMType(VMType type) { std::string bundle_path = _dartSource.flxArchive.absoluteURL.path.UTF8String; if (_dartSource.archiveContainsScriptSnapshot) { - blink::Threads::UI()->PostTask([ engine = engine->GetWeakPtr(), bundle_path ] { + blink::Threads::UI()->PostTask([ + engine = engine->GetWeakPtr(), bundle_path, + entrypoint = std::string([entrypoint UTF8String]) + ] { if (engine) - engine->RunBundle(bundle_path); + engine->RunBundle(bundle_path, entrypoint); }); } else { std::string main = _dartSource.dartMain.absoluteURL.path.UTF8String; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h index 4310dc6182..8a17300154 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h @@ -25,6 +25,11 @@ typedef void (^LaunchResult)(BOOL success, NSString* message); embedderVMType:(VMType)type result:(LaunchResult)result; +- (void)launchInEngine:(shell::Engine*)engine + withEntrypoint:(NSString*)entrypoint + embedderVMType:(VMType)type + result:(LaunchResult)result; + @end #endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERDARTPROJECT_INTERNAL_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterHeadlessDartRunner.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterHeadlessDartRunner.mm new file mode 100644 index 0000000000..2143f362f7 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterHeadlessDartRunner.mm @@ -0,0 +1,39 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h" + +#include + +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/shell/common/null_platform_view.h" +#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h" + +@interface FlutterHeadlessDartRunner () +@end + +@implementation FlutterHeadlessDartRunner { + fml::scoped_nsprotocol _dartProject; + std::shared_ptr _platformView; +} + +- (instancetype)init { + _dartProject.reset([[FlutterDartProject alloc] initFromDefaultSourceForConfiguration]); + _platformView = std::make_shared(); + _platformView->Attach(); + return self; +} + +- (void)runWithEntrypoint:(NSString*)entrypoint { + const enum VMType type = Dart_IsPrecompiledRuntime() ? VMTypePrecompilation : VMTypeInterpreter; + [_dartProject launchInEngine:&_platformView->engine() + withEntrypoint:entrypoint + embedderVMType:type + result:^(BOOL success, NSString* message) { + if (!success) + NSLog(@"%@", message); + }]; +} + +@end