This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathcefclient_win.cpp
More file actions
432 lines (355 loc) · 13.2 KB
/
cefclient_win.cpp
File metadata and controls
432 lines (355 loc) · 13.2 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright (c) 2010 The Chromium Embedded Framework 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 "cefclient.h"
#include <windows.h>
#include <commdlg.h>
#include <direct.h>
#include <MMSystem.h>
#include <sstream>
#include <string>
#include "include/cef_app.h"
#include "include/cef_version.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_runnable.h"
#include "client_handler.h"
#include "config.h"
#include "resource.h"
#include "string_util.h"
#include "client_switches.h"
#include "native_menu_model.h"
#include "appshell_node_process.h"
#include <algorithm>
#include <ShellAPI.h>
#include <ShlObj.h>
#include "cef_registry.h"
#include "cef_main_window.h"
// Global Variables:
DWORD g_appStartupTime;
HINSTANCE hInst; // current instance
HACCEL hAccelTable;
std::wstring gFilesToOpen; // Filenames passed as arguments to app
cef_main_window* gMainWnd = NULL;
// static variables (not exported)
static char szWorkingDir[MAX_UNC_PATH]; // The current working directory
static wchar_t szInitialUrl[MAX_UNC_PATH] = {0};
// Forward declarations of functions included in this code module:
BOOL InitInstance(HINSTANCE, int);
// The global ClientHandler reference.
extern CefRefPtr<ClientHandler> g_handler;
#if defined(OS_WIN)
// Add Common Controls to the application manifest because it's required to
// support the default tooltip implementation.
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") // NOLINT(whitespace/line_length)
#endif
// If 'str' ends with a colon followed by some digits, then remove the colon and digits. For example:
// "c:\bob\abc.txt:123:456" will be changed to "c:\bob\abc.txt:123"
// "c:\bob\abc.txt:123" will be changed to "c:\bob\abc.txt"
// "c:\bob\abc.txt" will not be changed
// (Note: we could do this with a regular expression, but there is no regex library currently
// built into brackets-shell, and I don't want to add one just for this simple case).
void StripColonNumber(std::wstring& str) {
bool gotDigits = false;
int index;
for (index = str.size() - 1; index >= 0; index--) {
if (!isdigit(str[index]))
break;
gotDigits = true;
}
if (gotDigits && index >= 0 && str[index] == ':') {
str.resize(index);
}
}
// Determine if 'str' is a valid filename.
bool IsFilename(const std::wstring& str) {
// Strip off trailing line and column number, if any.
std::wstring temp(str);
StripColonNumber(temp);
StripColonNumber(temp);
// Return true if the OS thinks the filename is OK.
return (GetFileAttributes(temp.c_str()) != INVALID_FILE_ATTRIBUTES);
}
bool GetFullPath(const std::wstring& path, std::wstring& oFullPath)
{
DWORD retval;
TCHAR buffer[MAX_UNC_PATH] = TEXT("");
TCHAR buf[MAX_UNC_PATH] = TEXT("");
if(path.length() <= 0) {
return false;
}
if(path[0] == L'"' && path[path.length() -1] == L'"') {
// This is a special case where arguments are sent
// as quotes(e.g.:file names having spaces). In this
// case we first strip the quotes, get the path
// and finally append the quotes to the result.
std::wstring normalizedPath = path;
normalizedPath.erase (std::remove(normalizedPath.begin(), normalizedPath.end(), L'"'), normalizedPath.end());
retval = GetFullPathName( normalizedPath.c_str(),
MAX_UNC_PATH,
buffer,
NULL );
// Now add the quotes to the final string.
if(retval) {
oFullPath = L'"';
oFullPath = oFullPath + buffer + L'"';
}
}
else {
// Retrieve the full path name for a file.
// The file does not need to exist.
retval = GetFullPathName( path.c_str(),
MAX_UNC_PATH,
buffer,
NULL );
if(retval)
oFullPath = buffer;
}
return (retval == 0) ? false : true;
}
std::wstring GetFilenamesFromCommandLine() {
std::wstring result = L"[]";
if (AppGetCommandLine()->HasArguments()) {
bool firstEntry = true;
std::vector<CefString> args;
AppGetCommandLine()->GetArguments(args);
std::vector<CefString>::iterator iterator;
result = L"[";
for (iterator = args.begin(); iterator != args.end(); iterator++) {
std::wstring argument = (*iterator).ToWString();
if (IsFilename(argument)) {
std::wstring fullPath;
// We check if this is a valid file path. If not just ignore this parameter.
if( !GetFullPath(argument, fullPath) )
continue;
if (!firstEntry) {
result += L",";
}
firstEntry = false;
result += L"\"" + fullPath + L"\"";
}
}
result += L"]";
}
return result;
}
// forward declaration; implemented in appshell_extensions_win.cpp
void ConvertToUnixPath(ExtensionString& filename);
// Program entry point function.
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
g_appStartupTime = timeGetTime();
CefMainArgs main_args(hInstance);
CefRefPtr<ClientApp> app(new ClientApp);
// Execute the secondary process, if any.
int exit_code = CefExecuteProcess(main_args, app.get(), NULL);
if (exit_code >= 0)
return exit_code;
bool isRightShiftKeyDown = (GetAsyncKeyState(VK_RSHIFT) & 0x8000) ? true: false;
// Retrieve the current working directory.
if (_getcwd(szWorkingDir, MAX_UNC_PATH) == NULL)
szWorkingDir[0] = 0;
// Parse command line arguments. The passed in values are ignored on Windows.
AppInitCommandLine(0, NULL);
// Determine if we should use an already running instance of Brackets.
HANDLE hMutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, FIRST_INSTANCE_MUTEX_NAME);
if (hMutex != NULL) {
// for subsequent instances, re-use an already running instance if we're being called to
// open an existing file on the command-line (eg. Open With.. from Windows Explorer)
HWND hFirstInstanceWnd = cef_main_window::FindFirstTopLevelInstance();
if (hFirstInstanceWnd != NULL) {
::SetForegroundWindow(hFirstInstanceWnd);
if (::IsIconic(hFirstInstanceWnd))
::ShowWindow(hFirstInstanceWnd, SW_RESTORE);
// Check for arguments. If any pass it to the existing running instance.
if( AppGetCommandLine()->HasArguments() && (lpCmdLine != NULL) ){
// message the other Brackets instance to actually open the given filename
std::wstring filename = lpCmdLine;
std::wstring wstrFilename;
// We check if this is a valid file path. If not just ignore this parameter.
if (GetFullPath(filename, wstrFilename)) {
ConvertToUnixPath(wstrFilename);
// note: WM_COPYDATA will manage passing the string across process space
COPYDATASTRUCT data;
data.dwData = ID_WM_COPYDATA_SENDOPENFILECOMMAND;
data.cbData = (wstrFilename.length() + 1) * sizeof(WCHAR);
data.lpData = (LPVOID)wstrFilename.c_str();
::SendMessage(hFirstInstanceWnd, WM_COPYDATA, (WPARAM)(HWND)hFirstInstanceWnd, (LPARAM)(LPVOID)&data);
}
}
// exit this instance
return 0;
}
// otherwise, fall thru and launch a new instance
}
if (hMutex == NULL) {
// first instance of this app, so create the mutex and continue execution of this instance.
hMutex = ::CreateMutex(NULL, FALSE, FIRST_INSTANCE_MUTEX_NAME);
}
CefSettings settings;
// Populate the settings based on command line arguments.
AppGetSettings(settings, app);
// Check command
if (CefString(&settings.cache_path).length() == 0) {
CefString(&settings.cache_path) = AppGetCachePath();
}
// Initialize CEF.
CefInitialize(main_args, settings, app.get(), NULL);
CefRefPtr<CefCommandLine> cmdLine = AppGetCommandLine();
if (cmdLine->HasSwitch(cefclient::kStartupPath)) {
wcscpy(szInitialUrl, cmdLine->GetSwitchValue(cefclient::kStartupPath).c_str());
}
else {
// If the right shift key is not pressed, look for the index.html file
if (!isRightShiftKeyDown) {
// Get the full pathname for the app. We look for the index.html
// file relative to this location.
wchar_t appPath[MAX_UNC_PATH];
wchar_t *pathRoot;
GetModuleFileName(NULL, appPath, MAX_UNC_PATH);
// Strip the .exe filename (and preceding "\") from the appPath
// and store in pathRoot
pathRoot = wcsrchr(appPath, '\\');
// Look for .\dev\src\index.html first
wcscpy(pathRoot, L"\\dev\\src\\index.html");
// If the file exists, use it
if (GetFileAttributes(appPath) != INVALID_FILE_ATTRIBUTES) {
wcscpy(szInitialUrl, appPath);
}
if (!wcslen(szInitialUrl)) {
// Look for .\www\index.html next
wcscpy(pathRoot, L"\\www\\index.html");
if (GetFileAttributes(appPath) != INVALID_FILE_ATTRIBUTES) {
wcscpy(szInitialUrl, appPath);
}
}
}
}
if (!wcslen(szInitialUrl)) {
// If we got here, either the startup file couldn't be found, or the user pressed the
// shift key while launching. Prompt to select the index.html file.
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szInitialUrl;
ofn.nMaxFile = MAX_UNC_PATH;
ofn.lpstrFilter = L"Web Files\0*.htm;*.html\0\0";
ofn.lpstrTitle = L"Please select the " APP_NAME L" index.html file.";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_EXPLORER;
if (!GetOpenFileName(&ofn)) {
// User cancelled, exit the app
CefShutdown();
return 0;
}
}
// Perform application initialization
if (!InitInstance (hInstance, nCmdShow))
return FALSE;
// Start the node server process
startNodeProcess();
gFilesToOpen = GetFilenamesFromCommandLine();
int result = 0;
if (!settings.multi_threaded_message_loop) {
// Run the CEF message loop. This function will block until the application
// recieves a WM_QUIT message.
CefRunMessageLoop();
} else {
MSG msg;
// Run the application message loop.
while (GetMessage(&msg, NULL, 0, 0)) {
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
result = static_cast<int>(msg.wParam);
}
OnBeforeShutdown();
// Shut down CEF.
CefShutdown();
// release the first instance mutex
if (hMutex != NULL)
ReleaseMutex(hMutex);
return result;
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
hInst = hInstance; // Store instance handle in our global variable
gMainWnd = new cef_main_window();
return gMainWnd->Create();
}
// Global functions
std::string AppGetWorkingDirectory() {
return szWorkingDir;
}
CefString AppGetCachePath() {
std::wstring cachePath = ClientApp::AppGetSupportDirectory();
cachePath += L"/cef_data";
return CefString(cachePath);
}
CefString AppGetInitialURL() {
return szInitialUrl;
}
// Helper function for AppGetProductVersionString. Reads version info from
// VERSIONINFO and writes it into the passed in std::wstring.
void GetFileVersionString(std::wstring &retVersion) {
DWORD dwSize = 0;
BYTE *pVersionInfo = NULL;
VS_FIXEDFILEINFO *pFileInfo = NULL;
UINT pLenFileInfo = 0;
HMODULE module = GetModuleHandle(NULL);
TCHAR executablePath[MAX_UNC_PATH];
GetModuleFileName(module, executablePath, MAX_UNC_PATH);
dwSize = GetFileVersionInfoSize(executablePath, NULL);
if (dwSize == 0) {
return;
}
pVersionInfo = new BYTE[dwSize];
if (!GetFileVersionInfo(executablePath, 0, dwSize, pVersionInfo)) {
delete[] pVersionInfo;
return;
}
if (!VerQueryValue(pVersionInfo, TEXT("\\"), (LPVOID*) &pFileInfo, &pLenFileInfo)) {
delete[] pVersionInfo;
return;
}
int major = (pFileInfo->dwFileVersionMS >> 16) & 0xffff ;
int minor = (pFileInfo->dwFileVersionMS) & 0xffff;
int hotfix = (pFileInfo->dwFileVersionLS >> 16) & 0xffff;
int other = (pFileInfo->dwFileVersionLS) & 0xffff;
delete[] pVersionInfo;
std::wostringstream versionStream(L"");
versionStream << major << L"." << minor << L"." << hotfix << L"." << other;
retVersion = versionStream.str();
}
CefString AppGetProductVersionString() {
std::wstring s(APP_NAME);
size_t i = s.find(L" ");
while (i != std::wstring::npos) {
s.erase(i, 1);
i = s.find(L" ");
}
std::wstring version(L"");
GetFileVersionString(version);
s.append(L"/");
s.append(version);
return CefString(s);
}
CefString AppGetChromiumVersionString() {
std::wostringstream versionStream(L"");
versionStream << L"Chrome/" << cef_version_info(2) << L"." << cef_version_info(3)
<< L"." << cef_version_info(4) << L"." << cef_version_info(5);
return CefString(versionStream.str());
}