Remove support for dynamic Mojo services (flutter/engine#3089)

Instead, clients should use host messaging.
This commit is contained in:
Adam Barth
2016-10-03 21:02:52 -07:00
committed by GitHub
parent f2310a49ff
commit 119877f035
10 changed files with 6 additions and 295 deletions

View File

@@ -20,12 +20,6 @@ group("flutter") {
"//flutter/sky",
]
}
if (is_ios) {
deps += [
"//flutter/services/dynamic:sdk_lib_archive",
]
}
}
if (!is_fuchsia) {

View File

@@ -90,7 +90,6 @@ public class FlutterMain {
private static final String DEFAULT_FLX = "app.flx";
private static final String MANIFEST = "flutter.yaml";
private static final String SERVICES = "services.json";
private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "sky_shell";
private static final Set<String> SKY_RESOURCES = ImmutableSetBuilder.<String>newInstance()
@@ -199,8 +198,6 @@ public class FlutterMain {
private static native void nativeRecordStartTimestamp(long initTimeMillis);
private static void onServiceRegistryAvailable(final Context applicationContext, ServiceRegistry registry) {
parseServicesConfig(applicationContext, registry);
registry.register(Activity.MANAGER.getName(), new ServiceFactory() {
@Override
public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) {
@@ -274,56 +271,6 @@ public class FlutterMain {
});
}
/**
* Parses the auto-generated services.json file, which contains additional services to register.
*/
private static void parseServicesConfig(Context applicationContext, ServiceRegistry registry) {
final AssetManager manager = applicationContext.getResources().getAssets();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(manager.open(SERVICES)))) {
StringBuffer json = new StringBuffer();
while (true) {
String line = reader.readLine();
if (line == null)
break;
json.append(line);
}
JSONObject object = (JSONObject) new JSONTokener(json.toString()).nextValue();
JSONArray services = object.getJSONArray("services");
for (int i = 0; i < services.length(); ++i) {
JSONObject service = services.getJSONObject(i);
String serviceName = service.getString("name");
String className = service.getString("class");
registerService(registry, serviceName, className);
}
} catch (FileNotFoundException e) {
// Not all apps will have a services.json file.
return;
} catch (Exception e) {
Log.e(TAG, "Failure parsing service configuration file", e);
return;
}
}
/**
* Registers a third-party service.
*/
private static void registerService(ServiceRegistry registry, final String serviceName, final String className) {
registry.register(serviceName, new ServiceFactory() {
@Override
public Binding connectToService(FlutterView view, Core core, MessagePipeHandle pipe) {
try {
return (Binding) Class.forName(className)
.getMethod("connectToService", FlutterView.class, Core.class, MessagePipeHandle.class)
.invoke(null, view, core, pipe);
} catch(Exception e) {
Log.e(TAG, "Failed to register service '" + serviceName + "'", e);
throw new RuntimeException(e);
}
}
});
}
/**
* Initialize our Flutter config values by obtaining them from the
* manifest XML file, falling back to default values.

View File

@@ -22,9 +22,8 @@
namespace shell {
PlatformServiceProvider::PlatformServiceProvider(
mojo::InterfaceRequest<mojo::ServiceProvider> request,
DynamicServiceProviderCallback callback)
: dynamic_service_provider_(callback), binding_(this, request.Pass()) {}
mojo::InterfaceRequest<mojo::ServiceProvider> request)
: binding_(this, request.Pass()) {}
PlatformServiceProvider::~PlatformServiceProvider() {}
@@ -94,8 +93,6 @@ void PlatformServiceProvider::ConnectToService(
return;
}
#endif // TARGET_OS_IPHONE
dynamic_service_provider_.Run(service_name, client_handle.Pass());
}
} // namespace shell

View File

@@ -14,12 +14,7 @@ namespace shell {
class PlatformServiceProvider : public mojo::ServiceProvider {
public:
using DynamicServiceProviderCallback =
base::Callback<void(const mojo::String& service_name,
mojo::ScopedMessagePipeHandle)>;
PlatformServiceProvider(mojo::InterfaceRequest<mojo::ServiceProvider> request,
DynamicServiceProviderCallback callback);
PlatformServiceProvider(mojo::InterfaceRequest<mojo::ServiceProvider> request);
~PlatformServiceProvider() override;
@@ -27,7 +22,6 @@ class PlatformServiceProvider : public mojo::ServiceProvider {
mojo::ScopedMessagePipeHandle client_handle) override;
private:
DynamicServiceProviderCallback dynamic_service_provider_;
mojo::StrongBinding<mojo::ServiceProvider> binding_;
FTL_DISALLOW_COPY_AND_ASSIGN(PlatformServiceProvider);

View File

@@ -33,11 +33,6 @@ PlatformViewMac::PlatformViewMac(NSOpenGLView* gl_view)
PlatformViewMac::~PlatformViewMac() = default;
static void DynamicServiceResolve(const mojo::String& service_name,
mojo::ScopedMessagePipeHandle handle) {
// This platform does not support dynamic service loading.
}
static void IgnoreRequest(
mojo::InterfaceRequest<flutter::platform::ApplicationMessages>) {}
@@ -45,8 +40,7 @@ void PlatformViewMac::ConnectToEngineAndSetupServices() {
ConnectToEngine(mojo::GetProxy(&sky_engine_));
mojo::ServiceProviderPtr service_provider;
new PlatformServiceProvider(mojo::GetProxy(&service_provider),
base::Bind(DynamicServiceResolve));
new PlatformServiceProvider(mojo::GetProxy(&service_provider));
mojo::ServiceProviderPtr view_service_provider;
new ViewServiceProvider(IgnoreRequest,

View File

@@ -33,21 +33,18 @@ shared_library("flutter_framework_dylib") {
"framework/Source/FlutterDartProject_Internal.h",
"framework/Source/FlutterDartSource.h",
"framework/Source/FlutterDartSource.mm",
"framework/Source/FlutterDynamicServiceLoader.h",
"framework/Source/FlutterDynamicServiceLoader.mm",
"framework/Source/FlutterView.h",
"framework/Source/FlutterView.mm",
"framework/Source/FlutterViewController.mm",
"platform_view_ios.h",
"platform_view_ios.mm",
]
deps = [
"//base:base",
"//dart/runtime:libdart",
"//flutter/services/activity",
"//flutter/services/dynamic:embedder",
"//flutter/services/editing",
"//flutter/services/engine:interfaces",
"//flutter/services/media",

View File

@@ -1,18 +0,0 @@
// Copyright 2016 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 SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_DYNAMIC_SERVICE_LOADER_H_
#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_DYNAMIC_SERVICE_LOADER_H_
#import <Foundation/Foundation.h>
#include "flutter/shell/platform/darwin/common/platform_service_provider.h"
@interface FlutterDynamicServiceLoader : NSObject
- (void)resolveService:(NSString*)name
handle:(mojo::ScopedMessagePipeHandle)handle;
@end
#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTER_DYNAMIC_SERVICE_LOADER_H_

View File

@@ -1,178 +0,0 @@
// Copyright 2016 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/platform/darwin/ios/framework/Source/FlutterDynamicServiceLoader.h"
#include "base/logging.h"
#include "flutter/services/dynamic/dynamic_service_embedder.h"
#include "flutter/services/dynamic/dynamic_service_definition.h"
#include <Foundation/Foundation.h>
#include <dlfcn.h>
@interface FlutterServiceDefinition : NSObject
@property(nonatomic, readonly) NSString* serviceName;
@property(nonatomic, readonly) NSString* containerFramework;
- (instancetype)initWithName:(NSString*)name framework:(NSString*)framework;
- (void)invokeService:(NSString*)serviceName
handle:(mojo::ScopedMessagePipeHandle)handle;
@end
@implementation FlutterServiceDefinition {
std::unique_ptr<sky::services::DynamicServiceDefinition> _definition;
BOOL _initializationAttempted;
}
- (instancetype)initWithName:(NSString*)name framework:(NSString*)framework {
self = [super init];
if (self) {
if (name.length == 0 || framework.length == 0) {
[self release];
return nil;
}
_serviceName = [name copy];
_containerFramework = [framework copy];
}
return self;
}
- (void)openIfNecessary {
if (_definition || _initializationAttempted) {
return;
}
mojo::String dylib_path([[NSBundle mainBundle]
pathForResource:_containerFramework
ofType:@"dylib"
inDirectory:@"Frameworks"]
.UTF8String);
_definition = sky::services::DynamicServiceDefinition::Initialize(dylib_path);
if (!_definition) {
LOG(ERROR) << "Could not load dynamic service for '"
<< _serviceName.UTF8String << "'";
}
// All known services are in the application bundle. If the service cannot
// be initialized once, dont attempt any more loads.
_initializationAttempted = YES;
}
- (void)invokeService:(NSString*)serviceName
handle:(mojo::ScopedMessagePipeHandle)handle {
[self openIfNecessary];
if (_definition) {
_definition->InvokeServiceHandler(handle.Pass(), serviceName.UTF8String);
}
}
- (void)closeIfNecessary {
_definition = nullptr;
_initializationAttempted = NO;
}
- (void)dealloc {
[self closeIfNecessary];
[_serviceName release];
[_containerFramework release];
[super dealloc];
};
@end
@implementation FlutterDynamicServiceLoader {
NSMutableDictionary* _services;
}
- (instancetype)init {
self = [super init];
if (self) {
_services = [[NSMutableDictionary alloc] init];
[self populateServiceDefinitions];
}
return self;
}
- (void)resolveService:(NSString*)serviceName
handle:(mojo::ScopedMessagePipeHandle)handle {
if (serviceName.length == 0) {
LOG(INFO) << "Invalid service name";
return;
}
[_services[serviceName] invokeService:serviceName handle:handle.Pass()];
}
- (void)populateServiceDefinitions {
NSString* definitionsPath =
[[NSBundle mainBundle] pathForResource:@"ServiceDefinitions"
ofType:@"json"];
if (definitionsPath.length == 0) {
return;
}
NSData* data = [NSData dataWithContentsOfFile:definitionsPath];
if (data.length == 0) {
LOG(INFO) << "The service definitions manifest could not be read. No "
"custom services will be available.";
return;
}
NSError* error = nil;
NSDictionary* servicesData =
[NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error != nil || ![servicesData isKindOfClass:[NSDictionary class]]) {
LOG(INFO) << "Corrupt service definitions manifest. No custom services "
"will be available";
return;
}
NSArray* services = servicesData[@"services"];
if (![services isKindOfClass:[NSArray class]]) {
return;
}
for (NSDictionary* service in services) {
if (![service isKindOfClass:[NSDictionary class]]) {
continue;
}
NSString* serviceName = service[@"name"];
FlutterServiceDefinition* definition =
[[FlutterServiceDefinition alloc] initWithName:serviceName
framework:service[@"framework"]];
if (definition != nil) {
_services[serviceName] = definition;
}
[definition release];
}
}
- (void)dealloc {
[_services release];
[super dealloc];
}
@end

View File

@@ -13,7 +13,6 @@
#include "flutter/services/platform/app_messages.mojom.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/application_messages_impl.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDynamicServiceLoader.h"
#include "flutter/shell/common/platform_view.h"
@class CAEAGLLayer;
@@ -60,7 +59,6 @@ class PlatformViewIOS : public PlatformView {
flutter::platform::ApplicationMessagesPtr app_message_sender_;
ApplicationMessagesImpl app_message_receiver_;
std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
base::scoped_nsprotocol<FlutterDynamicServiceLoader*> dynamic_service_loader_;
ftl::WeakPtrFactory<PlatformViewIOS> weak_factory_;
void SetupAndLoadFromSource(const std::string& main,

View File

@@ -273,7 +273,6 @@ class IOSGLContext {
PlatformViewIOS::PlatformViewIOS(CAEAGLLayer* layer)
: context_(WTF::MakeUnique<IOSGLContext>(surface_config_, layer)),
dynamic_service_loader_([[FlutterDynamicServiceLoader alloc] init]),
weak_factory_(this) {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
@@ -306,14 +305,6 @@ void PlatformViewIOS::ToggleAccessibility(UIView* view, bool enable) {
}
}
static void DynamicServiceResolve(void* baton,
const mojo::String& service_name,
mojo::ScopedMessagePipeHandle handle) {
base::mac::ScopedNSAutoreleasePool pool;
auto loader = reinterpret_cast<FlutterDynamicServiceLoader*>(baton);
[loader resolveService:@(service_name.data()) handle:handle.Pass()];
}
void PlatformViewIOS::ConnectToEngineAndSetupServices() {
ConnectToEngine(mojo::GetProxy(&engine_));
@@ -321,12 +312,7 @@ void PlatformViewIOS::ConnectToEngineAndSetupServices() {
auto service_provider_proxy = mojo::GetProxy(&service_provider);
auto service_resolution_callback = base::Bind(
&DynamicServiceResolve,
base::Unretained(reinterpret_cast<void*>(dynamic_service_loader_.get())));
new PlatformServiceProvider(service_provider_proxy.Pass(),
service_resolution_callback);
new PlatformServiceProvider(service_provider_proxy.Pass());
ftl::WeakPtr<ApplicationMessagesImpl> appplication_messages_impl =
app_message_receiver_.GetWeakPtr();