Skip to content

Commit

Permalink
[iOS] Ensure FlutterMain is called before interacting with the engine…
Browse files Browse the repository at this point in the history
… in any way. (flutter#3383)

* [iOS] Ensure FlutterMain is called before interacting with the engine in any way.

* Licenses
  • Loading branch information
chinmaygarde authored Feb 1, 2017
1 parent 20bdd2f commit 01afda4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
2 changes: 2 additions & 0 deletions shell/platform/darwin/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ shared_library("flutter_framework_dylib") {
"framework/Source/FlutterViewController.mm",
"framework/Source/accessibility_bridge.h",
"framework/Source/accessibility_bridge.mm",
"framework/Source/flutter_main_ios.h",
"framework/Source/flutter_main_ios.mm",
"framework/Source/flutter_touch_mapper.h",
"framework/Source/flutter_touch_mapper.mm",
"framework/Source/platform_message_router.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "flutter/common/threads.h"
#include "flutter/shell/common/switches.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartSource.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/flutter_main_ios.h"

static NSURL* URLForSwitch(const char* name) {
auto cmd = *base::CommandLine::ForCurrentProcess();
Expand All @@ -31,6 +32,12 @@ @implementation FlutterDartProject {
VMType _vmTypeRequirement;
}

+ (void)initialize {
if (self == [FlutterDartProject class]) {
shell::FlutterMain();
}
}

#pragma mark - Override base class designated initializers

- (instancetype)init {
Expand Down
24 changes: 10 additions & 14 deletions shell/platform/darwin/ios/framework/Source/FlutterViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/flutter_main_ios.h"
#include "flutter/shell/platform/darwin/ios/framework/Source/flutter_touch_mapper.h"
#include "flutter/shell/platform/darwin/ios/platform_view_ios.h"
#include "lib/ftl/functional/make_copyable.h"
Expand Down Expand Up @@ -59,15 +60,6 @@ void FlutterInit(int argc, const char* argv[]) {
// Deprecated. To be removed.
}

static void FlutterInitShell() {
NSBundle* bundle = [NSBundle bundleForClass:[FlutterViewController class]];
NSString* icuDataPath = [bundle pathForResource:@"icudtl" ofType:@"dat"];
NSString* libraryName =
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTLibraryPath"];
shell::PlatformMacMain(icuDataPath.UTF8String,
libraryName != nil ? libraryName.UTF8String : "");
}

@implementation FlutterViewController {
base::scoped_nsprotocol<FlutterDartProject*> _dartProject;
UIInterfaceOrientationMask _orientationPreferences;
Expand All @@ -81,13 +73,17 @@ @implementation FlutterViewController {
BOOL _initialized;
}

+ (void)initialize {
if (self == [FlutterViewController class]) {
shell::FlutterMain();
}
}

#pragma mark - Manage and override all designated initializers

- (instancetype)initWithProject:(FlutterDartProject*)project
nibName:(NSString*)nibNameOrNil
bundle:(NSBundle*)nibBundleOrNil {
FlutterInitShell();

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {
Expand Down Expand Up @@ -508,7 +504,7 @@ - (void)dealloc {
// Standard iOS status bar height in pixels.
constexpr CGFloat kStandardStatusBarHeight = 20.0;

- (void)handleStatusBarTouches:(UIEvent *)event {
- (void)handleStatusBarTouches:(UIEvent*)event {
// If the status bar is double-height, don't handle status bar taps. iOS
// should open the app associated with the status bar.
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
Expand All @@ -517,12 +513,12 @@ - (void)handleStatusBarTouches:(UIEvent *)event {
}

// If we detect a touch in the status bar, synthesize a fake touch begin/end.
for (UITouch *touch in event.allTouches) {
for (UITouch* touch in event.allTouches) {
if (touch.phase == UITouchPhaseBegan && touch.tapCount > 0) {
CGPoint windowLoc = [touch locationInView:nil];
CGPoint screenLoc = [touch.window convertPoint:windowLoc toWindow:nil];
if (CGRectContainsPoint(statusBarFrame, screenLoc)) {
NSSet *statusbarTouches = [NSSet setWithObject:touch];
NSSet* statusbarTouches = [NSSet setWithObject:touch];
[self dispatchTouches:statusbarTouches phase:UITouchPhaseBegan];
[self dispatchTouches:statusbarTouches phase:UITouchPhaseEnded];
return;
Expand Down
18 changes: 18 additions & 0 deletions shell/platform/darwin/ios/framework/Source/flutter_main_ios.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTER_MAIN_IOS_H_
#define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTER_MAIN_IOS_H_

#include "lib/ftl/macros.h"

namespace shell {

/// Initializes the Flutter shell. This must be called before interacting with
/// the engine in any way. It is safe to call this method multiple times.
void FlutterMain();

} // namespace shell

#endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTER_MAIN_IOS_H_
20 changes: 20 additions & 0 deletions shell/platform/darwin/ios/framework/Source/flutter_main_ios.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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/platform/darwin/ios/framework/Source/flutter_main_ios.h"
#include "flutter/shell/platform/darwin/common/platform_mac.h"
#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"

namespace shell {

void FlutterMain() {
NSBundle* bundle = [NSBundle bundleForClass:[FlutterViewController class]];
NSString* icuDataPath = [bundle pathForResource:@"icudtl" ofType:@"dat"];
NSString* libraryName =
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTLibraryPath"];
shell::PlatformMacMain(icuDataPath.UTF8String,
libraryName != nil ? libraryName.UTF8String : "");
}

} // namespace shell
4 changes: 3 additions & 1 deletion travis/licenses.golden
Original file line number Diff line number Diff line change
Expand Up @@ -21172,6 +21172,8 @@ FILE: ../../../flutter/content_handler/software_rasterizer.cc
FILE: ../../../flutter/content_handler/software_rasterizer.h
FILE: ../../../flutter/content_handler/vulkan_rasterizer.cc
FILE: ../../../flutter/content_handler/vulkan_rasterizer.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/flutter_main_ios.h
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/flutter_main_ios.mm
FILE: ../../../flutter/vulkan/vulkan_native_surface_magma.cc
FILE: ../../../flutter/vulkan/vulkan_native_surface_magma.h
----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -72164,4 +72166,4 @@ freely, subject to the following restrictions:
3. This notice may not be removed or altered from any source distribution.
====================================================================================================
Total license count: 693
31677 of 31677 ██████████ 100% (0 missing licenses) Done.
31679 of 31679 ██████████ 100% (0 missing licenses) Done.

0 comments on commit 01afda4

Please sign in to comment.