/
main.mm
107 lines (90 loc) · 2.91 KB
/
main.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// main.m
// CefViewWing
//
// Created by Sheen Tian on 2020/6/10.
//
#pragma region cef_headers
#include <include/cef_app.h>
#include <include/wrapper/cef_library_loader.h>
#if defined(CEF_USE_SANDBOX)
#include <include/cef_sandbox_mac.h>
#endif
#pragma endregion cef_headers
#pragma region mac_headers
#include <Foundation/Foundation.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#pragma endregion mac_headers
#pragma region project_heasers
#include "../CefRenderApp/CefViewAppBase.h"
#include "../CefRenderApp/CefViewOtherApp.h"
#include "../CefRenderApp/CefViewRenderApp.h"
#include <Common/CefViewCoreLog.h>
#pragma endregion project_heasers
const char kFrameworkPath[] = "Chromium Embedded Framework.framework/Chromium Embedded Framework";
const char kPathFromHelperExe[] = "../../..";
std::string GetFrameworkPath() {
uint32_t exec_path_size = 0;
int rv = _NSGetExecutablePath(NULL, &exec_path_size);
if (rv != -1) {
return std::string();
}
std::unique_ptr<char[]> exec_path(new char[exec_path_size]);
rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size);
if (rv != 0) {
return std::string();
}
// Get the directory path of the executable.
const char *parent_dir = dirname(exec_path.get());
if (!parent_dir) {
return std::string();
}
// Append the relative path to the framework.
std::stringstream ss;
ss << parent_dir << "/" << kPathFromHelperExe << "/" << kFrameworkPath;
return ss.str();
}
bool LoadCefLibrary() {
std::string cef_lib_path = GetFrameworkPath();
return (1 == cef_load_library(cef_lib_path.c_str()));
}
int CefViewWingMain(int argc, char *argv[]) {
logI("CefViewWing is launching....");
#if defined(CEF_USE_SANDBOX)
// Initialize the macOS sandbox for this helper process.
CefScopedSandboxContext sandbox_context;
logI("Initializing the sandbox context");
if (!sandbox_context.Initialize(argc, argv)) {
logE("Failed to initialize the cef sandbox context");
return 1;
}
#endif
logI("Loading the cef library");
if (!LoadCefLibrary()) {
logI("Failed to load cef library");
return 1;
}
CefMainArgs main_args(argc, argv);
CefRefPtr<CefApp> app;
CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
command_line->InitFromArgv(argc, argv);
auto process_type = CefViewAppBase::GetProcessType(command_line);
if (process_type == CefViewAppBase::RendererProcess) {
auto bridge_name = CefViewAppBase::GetBridgeObjectName(command_line);
app = new CefViewRenderApp(bridge_name);
} else if (process_type == CefViewAppBase::OtherProcess) {
app = new CefViewOtherApp();
} else {
logI("Parse process unknown, exit");
return 1;
}
// Execute the secondary process.
int rt = CefExecuteProcess(main_args, app, nullptr);
logI("process returned with code: %d", rt);
return rt;
}
int main(int argc, const char *argv[]) {
// main
return CefViewWingMain(argc, const_cast<char **>(argv));
}