[ios] Adds an API for running Dart code without a PlatformViewIOS (flutter/engine#4294)
This commit is contained in:
@@ -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",
|
||||
|
||||
34
engine/src/flutter/shell/common/null_platform_view.cc
Normal file
34
engine/src/flutter/shell/common/null_platform_view.cc
Normal file
@@ -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<NullRasterizer>()), weak_factory_(this) {}
|
||||
|
||||
void NullPlatformView::Attach() {
|
||||
CreateEngine();
|
||||
}
|
||||
|
||||
NullPlatformView::~NullPlatformView() = default;
|
||||
|
||||
fxl::WeakPtr<NullPlatformView> 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
|
||||
38
engine/src/flutter/shell/common/null_platform_view.h
Normal file
38
engine/src/flutter/shell/common/null_platform_view.h
Normal file
@@ -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<NullPlatformView> 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<NullPlatformView> weak_factory_;
|
||||
|
||||
FXL_DISALLOW_COPY_AND_ASSIGN(NullPlatformView);
|
||||
};
|
||||
|
||||
} // namespace shell
|
||||
|
||||
#endif // COMMON_NULL_PLATFORM_VIEW_H_
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 <Foundation/Foundation.h>
|
||||
|
||||
#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_
|
||||
@@ -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;
|
||||
|
||||
@@ -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_
|
||||
|
||||
@@ -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 <memory>
|
||||
|
||||
#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<FlutterDartProject*> _dartProject;
|
||||
std::shared_ptr<shell::NullPlatformView> _platformView;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
_dartProject.reset([[FlutterDartProject alloc] initFromDefaultSourceForConfiguration]);
|
||||
_platformView = std::make_shared<shell::NullPlatformView>();
|
||||
_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
|
||||
Reference in New Issue
Block a user