blob: c3d6699e41da32d8cbad87f84ece758addc04027 [file] [log] [blame]
// Copyright 2013 The Flutter 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 "FLTWebViewFlutterPlugin.h"
#import "FWFGeneratedWebKitApis.h"
#import "FWFHTTPCookieStoreHostApi.h"
#import "FWFInstanceManager.h"
#import "FWFNavigationDelegateHostApi.h"
#import "FWFObjectHostApi.h"
#import "FWFPreferencesHostApi.h"
#import "FWFScriptMessageHandlerHostApi.h"
#import "FWFScrollViewHostApi.h"
#import "FWFUIDelegateHostApi.h"
#import "FWFUIViewHostApi.h"
#import "FWFURLHostApi.h"
#import "FWFUserContentControllerHostApi.h"
#import "FWFWebViewConfigurationHostApi.h"
#import "FWFWebViewHostApi.h"
#import "FWFWebsiteDataStoreHostApi.h"
@interface FWFWebViewFactory : NSObject <FlutterPlatformViewFactory>
@property(nonatomic, weak) FWFInstanceManager *instanceManager;
- (instancetype)initWithManager:(FWFInstanceManager *)manager;
@end
@implementation FWFWebViewFactory
- (instancetype)initWithManager:(FWFInstanceManager *)manager {
self = [self init];
if (self) {
_instanceManager = manager;
}
return self;
}
- (NSObject<FlutterMessageCodec> *)createArgsCodec {
return [FlutterStandardMessageCodec sharedInstance];
}
- (NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args {
NSNumber *identifier = (NSNumber *)args;
FWFWebView *webView =
(FWFWebView *)[self.instanceManager instanceForIdentifier:identifier.longValue];
webView.frame = frame;
return webView;
}
@end
@implementation FLTWebViewFlutterPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
FWFInstanceManager *instanceManager =
[[FWFInstanceManager alloc] initWithDeallocCallback:^(long identifier) {
FWFObjectFlutterApiImpl *objectApi = [[FWFObjectFlutterApiImpl alloc]
initWithBinaryMessenger:registrar.messenger
instanceManager:[[FWFInstanceManager alloc] init]];
dispatch_async(dispatch_get_main_queue(), ^{
[objectApi disposeObjectWithIdentifier:identifier
completion:^(FlutterError *error) {
NSAssert(!error, @"%@", error);
}];
});
}];
SetUpFWFWKHttpCookieStoreHostApi(
registrar.messenger,
[[FWFHTTPCookieStoreHostApiImpl alloc] initWithInstanceManager:instanceManager]);
SetUpFWFWKNavigationDelegateHostApi(
registrar.messenger,
[[FWFNavigationDelegateHostApiImpl alloc] initWithBinaryMessenger:registrar.messenger
instanceManager:instanceManager]);
SetUpFWFNSObjectHostApi(registrar.messenger,
[[FWFObjectHostApiImpl alloc] initWithInstanceManager:instanceManager]);
SetUpFWFWKPreferencesHostApi(registrar.messenger, [[FWFPreferencesHostApiImpl alloc]
initWithInstanceManager:instanceManager]);
SetUpFWFWKScriptMessageHandlerHostApi(
registrar.messenger,
[[FWFScriptMessageHandlerHostApiImpl alloc] initWithBinaryMessenger:registrar.messenger
instanceManager:instanceManager]);
SetUpFWFUIScrollViewHostApi(registrar.messenger, [[FWFScrollViewHostApiImpl alloc]
initWithInstanceManager:instanceManager]);
SetUpFWFWKUIDelegateHostApi(registrar.messenger, [[FWFUIDelegateHostApiImpl alloc]
initWithBinaryMessenger:registrar.messenger
instanceManager:instanceManager]);
SetUpFWFUIViewHostApi(registrar.messenger,
[[FWFUIViewHostApiImpl alloc] initWithInstanceManager:instanceManager]);
SetUpFWFWKUserContentControllerHostApi(
registrar.messenger,
[[FWFUserContentControllerHostApiImpl alloc] initWithInstanceManager:instanceManager]);
SetUpFWFWKWebsiteDataStoreHostApi(
registrar.messenger,
[[FWFWebsiteDataStoreHostApiImpl alloc] initWithInstanceManager:instanceManager]);
SetUpFWFWKWebViewConfigurationHostApi(
registrar.messenger,
[[FWFWebViewConfigurationHostApiImpl alloc] initWithBinaryMessenger:registrar.messenger
instanceManager:instanceManager]);
SetUpFWFWKWebViewHostApi(registrar.messenger, [[FWFWebViewHostApiImpl alloc]
initWithBinaryMessenger:registrar.messenger
instanceManager:instanceManager]);
SetUpFWFNSUrlHostApi(registrar.messenger,
[[FWFURLHostApiImpl alloc] initWithBinaryMessenger:registrar.messenger
instanceManager:instanceManager]);
FWFWebViewFactory *webviewFactory = [[FWFWebViewFactory alloc] initWithManager:instanceManager];
[registrar registerViewFactory:webviewFactory withId:@"plugins.flutter.io/webview"];
// InstanceManager is published so that a strong reference is maintained.
[registrar publish:instanceManager];
}
- (void)detachFromEngineForRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[registrar publish:[NSNull null]];
}
@end