提交 296c1c90 编写于 作者: C Chinmay Garde

Merge pull request #2484 from chinmaygarde/master

Allow PlatformMacMain to be called multiple times
...@@ -7,8 +7,14 @@ group("default") { ...@@ -7,8 +7,14 @@ group("default") {
testonly = true testonly = true
deps = [ deps = [
"//sky", "//sky",
"//sky/services/dynamic:sdk_lib",
] ]
if (is_ios) {
deps += [
"//sky/services/dynamic:sdk_lib",
"//sky/shell:flutter_framework",
]
}
} }
group("dist") { group("dist") {
......
...@@ -40,11 +40,6 @@ ...@@ -40,11 +40,6 @@
if (self) { if (self) {
_dartBundle = [dartBundleOrNil retain]; _dartBundle = [dartBundleOrNil retain];
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSString* icuDataPath = [bundle pathForResource:@"icudtl" ofType:@"dat"];
sky::shell::PlatformMacMain(0, nullptr, icuDataPath.UTF8String, nullptr);
[self performCommonViewControllerInitialization]; [self performCommonViewControllerInitialization];
} }
...@@ -75,6 +70,11 @@ ...@@ -75,6 +70,11 @@
_initialized = YES; _initialized = YES;
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
NSString* icuDataPath = [bundle pathForResource:@"icudtl" ofType:@"dat"];
sky::shell::PlatformMacMain(0, nullptr, icuDataPath.UTF8String);
_orientationPreferences = UIInterfaceOrientationMaskAll; _orientationPreferences = UIInterfaceOrientationMaskAll;
_dynamicServiceLoader = [[FlutterDynamicServiceLoader alloc] init]; _dynamicServiceLoader = [[FlutterDynamicServiceLoader alloc] init];
_viewportMetrics = sky::ViewportMetrics::New(); _viewportMetrics = sky::ViewportMetrics::New();
......
...@@ -8,8 +8,9 @@ ...@@ -8,8 +8,9 @@
#include "sky/shell/platform/mac/platform_mac.h" #include "sky/shell/platform/mac/platform_mac.h"
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
return sky::shell::PlatformMacMain(argc, argv, "", ^() { // iOS does use the FlutterViewController that initializes the platform but
return UIApplicationMain(argc, (char**)argv, nil, // we have the command line args here. So call it now.
NSStringFromClass([FlutterAppDelegate class])); sky::shell::PlatformMacMain(argc, argv, "");
}); return UIApplicationMain(argc, (char**)argv, nil,
NSStringFromClass([FlutterAppDelegate class]));
} }
...@@ -32,21 +32,21 @@ void AttachMessageLoopToMainRunLoop(void) { ...@@ -32,21 +32,21 @@ void AttachMessageLoopToMainRunLoop(void) {
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
[SkyApplication sharedApplication]; [SkyApplication sharedApplication];
return sky::shell::PlatformMacMain(argc, argv, "", ^() { sky::shell::PlatformMacMain(argc, argv, "");
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(sky::shell::switches::kHelp)) { base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
sky::shell::switches::PrintUsage("SkyShell"); if (command_line.HasSwitch(sky::shell::switches::kHelp)) {
return EXIT_SUCCESS; sky::shell::switches::PrintUsage("SkyShell");
} return EXIT_SUCCESS;
}
if (command_line.HasSwitch(sky::shell::switches::kNonInteractive)) {
if (!sky::shell::InitForTesting()) if (command_line.HasSwitch(sky::shell::switches::kNonInteractive)) {
return 1; if (!sky::shell::InitForTesting())
base::MessageLoop::current()->Run(); return 1;
return EXIT_SUCCESS; base::MessageLoop::current()->Run();
} return EXIT_SUCCESS;
}
sky::shell::AttachMessageLoopToMainRunLoop();
return NSApplicationMain(argc, argv); sky::shell::AttachMessageLoopToMainRunLoop();
}); return NSApplicationMain(argc, argv);
} }
...@@ -10,12 +10,7 @@ ...@@ -10,12 +10,7 @@
namespace sky { namespace sky {
namespace shell { namespace shell {
typedef int (^PlatformMacMainCallback)(void); void PlatformMacMain(int argc, const char* argv[], std::string icu_data_path);
int PlatformMacMain(int argc,
const char* argv[],
std::string icu_data_path,
PlatformMacMainCallback callback);
bool AttemptLaunchFromCommandLineSwitches(sky::SkyEnginePtr& engine); bool AttemptLaunchFromCommandLineSwitches(sky::SkyEnginePtr& engine);
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "mojo/edk/embedder/embedder.h" #include "mojo/edk/embedder/embedder.h"
#include "mojo/edk/embedder/simple_platform_support.h" #include "mojo/edk/embedder/simple_platform_support.h"
#include "sky/engine/wtf/MakeUnique.h"
#include "sky/shell/shell.h" #include "sky/shell/shell.h"
#include "sky/shell/switches.h" #include "sky/shell/switches.h"
#include "sky/shell/tracing_controller.h" #include "sky/shell/tracing_controller.h"
...@@ -45,80 +46,60 @@ static void RedirectIOConnectionsToSyslog() { ...@@ -45,80 +46,60 @@ static void RedirectIOConnectionsToSyslog() {
#endif #endif
} }
int PlatformMacMainOnce(int argc, class EmbedderState {
const char* argv[], public:
std::string icu_data_path, EmbedderState(int argc, const char* argv[], std::string icu_data_path) {
PlatformMacMainCallback callback) { RedirectIOConnectionsToSyslog();
CHECK([NSThread currentThread] == [NSThread mainThread])
<< "Platform initialization must occur on the main platform thread";
base::mac::ScopedNSAutoreleasePool pool; base::CommandLine::Init(argc, argv);
base::PlatformThread::SetName("platform_main"); InitializeLogging();
base::AtExitManager exit_manager; base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(sky::shell::switches::kTraceStartup)) {
// Usually, all tracing within flutter is managed via the tracing
// controller
// The tracing controller is accessed via the shell instance. This means
// that tracing can only be enabled once that instance is created. Traces
// early in startup are lost. This enables tracing only in base manually
// till the tracing controller takes over.
sky::shell::TracingController::StartBaseTracing();
}
RedirectIOConnectionsToSyslog(); // This is about as early as tracing of any kind can start. Add an instant
// marker that can be used as a reference for startup.
TRACE_EVENT_INSTANT0("flutter", "main", TRACE_EVENT_SCOPE_PROCESS);
bool result = false; #if TARGET_OS_IPHONE
// One cannot start the message loop on the platform main thread. Instead,
// we attach to the CFRunLoop
embedder_message_loop_.Attach();
#endif
result = base::CommandLine::Init(argc, argv); mojo::embedder::Init(mojo::embedder::CreateSimplePlatformSupport());
DLOG_ASSERT(result);
InitializeLogging(); CHECK(gfx::GLSurface::InitializeOneOff());
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); sky::shell::Shell::InitStandalone(icu_data_path);
if (command_line.HasSwitch(sky::shell::switches::kTraceStartup)) {
// Usually, all tracing within flutter is managed via the tracing controller
// The tracing controller is accessed via the shell instance. This means
// that tracing can only be enabled once that instance is created. Traces
// early in startup are lost. This enables tracing only in base manually
// till the tracing controller takes over.
sky::shell::TracingController::StartBaseTracing();
} }
// This is about as early as tracing of any kind can start. Add an instant private:
// marker that can be used as a reference for startup. base::AtExitManager exit_manager_;
TRACE_EVENT_INSTANT0("flutter", "main", TRACE_EVENT_SCOPE_PROCESS); base::MessageLoopForUI embedder_message_loop_;
std::unique_ptr<base::MessageLoopForUI> message_loop(
new base::MessageLoopForUI());
#if TARGET_OS_IPHONE
// One cannot start the message loop on the platform main thread. Instead,
// we attach to the CFRunLoop
message_loop->Attach();
#endif
mojo::embedder::Init(mojo::embedder::CreateSimplePlatformSupport());
CHECK(gfx::GLSurface::InitializeOneOff());
sky::shell::Shell::InitStandalone(icu_data_path);
int exit_code = callback != nullptr ? callback() : EXIT_SUCCESS; DISALLOW_COPY_AND_ASSIGN(EmbedderState);
};
#if !TARGET_OS_IPHONE void PlatformMacMain(int argc, const char* argv[], std::string icu_data_path) {
if (callback != nullptr) { CHECK([NSThread isMainThread])
// If we control the embedder, the callback is what we use to wrap << "Embedder initialization must occur on the main platform thread";
// UIApplicationMain. If we are here, it means that that method has returned
// and we need to perform cleanup.
message_loop->QuitNow();
}
#endif
return exit_code; static std::unique_ptr<EmbedderState> g_embedder;
} static std::once_flag once_main;
int PlatformMacMain(int argc, std::call_once(once_main, [&]() {
const char* argv[], g_embedder = WTF::MakeUnique<EmbedderState>(argc, argv, icu_data_path);
std::string icu_data_path,
PlatformMacMainCallback callback) {
__block int result = EXIT_SUCCESS;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
result = PlatformMacMainOnce(argc, argv, icu_data_path, callback);
}); });
return result;
} }
static bool FlagsValidForCommandLineLaunch(const std::string& dart_main, static bool FlagsValidForCommandLineLaunch(const std::string& dart_main,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册