/
rhoruntime.cpp
485 lines (393 loc) · 13.5 KB
/
rhoruntime.cpp
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*------------------------------------------------------------------------
* (The MIT License)
*
* Copyright (c) 2008-2011 Rhomobile, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* http://rhomobile.com
*------------------------------------------------------------------------*/
#include "pch_rhoruntime.h"
#include "rhoruntime.h"
//#include "../../shared/sqlite/sqlite3.h"
//#include "logging/RhoLogConf.h"
#include "common/RhoStd.h"
#include "common/RhodesApp.h"
#include "common/RhoFilePath.h"
#include "rubyext/WebView.h"
#include "common/RhoConvertWP8.h"
#include "api_generator\js_helpers.h"
#include <collection.h>
using namespace rhoruntime;
using namespace Platform;
using namespace Windows::Foundation::Collections;
CRhoRuntime^ CRhoRuntime::m_instance = nullptr;
CRhoRuntime::CRhoRuntime(IMainPage^ mainPage):
m_MainPage(mainPage)
{
}
CRhoRuntime^ CRhoRuntime::getInstance(IMainPage^ mainPage)
{
if (m_instance == nullptr)
m_instance = ref new CRhoRuntime(mainPage);
return m_instance;
}
CRhoRuntime^ CRhoRuntime::getInstance()
{
return m_instance;
}
IMainPage^ CRhoRuntime::getMainPage()
{
return m_MainPage;
}
// rhodes executed in a separate thread
void CRhoRuntime::Execute()
{
rho::String m_strRootPath = rho_native_rhopath(), m_logPort, m_strRuntimePath;
rho_logconf_Init(m_strRootPath.c_str(), m_strRootPath.c_str(), m_logPort.c_str());
rho::common::CRhodesApp::Create(m_strRootPath, m_strRootPath, m_strRuntimePath);
//RHODESAPP().setExtManager( &m_oExtManager );
//Create Main window
//createCustomMenu();
RHODESAPP().startApp();
}
// *** CALLBACKS from MainPage object ***
void CRhoRuntime::updateSizeProperties(int width, int height)
{
// TODO: implement
}
void CRhoRuntime::onActivate(int active)
{
while(!RHODESAPP().getInstance())
Sleep(50);
RHODESAPP().callUiCreatedCallback();
rho_rhodesapp_callAppActiveCallback(1);
}
void CRhoRuntime::logEvent(::Platform::String^ message)
{
LOG(INFO) + rho::common::convertStringWFromWP8(message);
}
void CRhoRuntime::createCustomMenu()
{
IMainPage^ mainPage = CRhoRuntime::getInstance()->getMainPage();
rho::Vector<rho::common::CAppMenuItem> m_arAppMenuItems;
RHODESAPP().getAppMenu().copyMenuItems(m_arAppMenuItems);
//update UI with custom menu items
mainPage->menuClear();
//RHODESAPP().setAppBackUrl("");
for ( unsigned int i = 0; i < m_arAppMenuItems.size(); i++)
{
rho::common::CAppMenuItem& oItem = m_arAppMenuItems.elementAt(i);
if (oItem.m_eType == rho::common::CAppMenuItem::emtSeparator)
mainPage->menuAddSeparator();
else {
//if ((oItem.m_eType == rho::common::CAppMenuItem::emtBack) && (oItem.m_strLink.compare("back") != 0))
// RHODESAPP().setAppBackUrl(oItem.m_strLink);
mainPage->menuAddAction((oItem.m_eType == rho::common::CAppMenuItem::emtClose ? "Exit" : rho::common::convertStringToWP8(oItem.m_strLabel)), i);
}
}
}
void CRhoRuntime::onCustomMenuItemCommand(int nItemPos)
{
rho::Vector<rho::common::CAppMenuItem> m_arAppMenuItems;
RHODESAPP().getAppMenu().copyMenuItems(m_arAppMenuItems);
if ( nItemPos < 0 || nItemPos >= (int)m_arAppMenuItems.size() )
return;
rho::common::CAppMenuItem& oMenuItem = m_arAppMenuItems.elementAt(nItemPos);
oMenuItem.processCommand();
}
void CRhoRuntime::onToolbarAction(::Platform::String^ action)
{
if ( action->Equals("forward") )
rho_webview_navigate_forward();
else
RHODESAPP().loadUrl(rho::common::convertStringAFromWP8(action));
}
void CRhoRuntime::onTabbarCurrentChanged(int index, ::Platform::String^ action)
{
// TODO: complete implementation
//if (tbrp["on_change_tab_callback"].toString().length() > 0) {
// QString body = QString("&rho_callback=1&tab_index=") + QVariant(index).toString();
// rho::String* cbStr = new rho::String(tbrp["on_change_tab_callback"].toString().toStdString());
// rho::String* bStr = new rho::String(body.toStdString());
// const char* b = bStr->c_str();
// rho_net_request_with_data(RHODESAPP().canonicalizeRhoUrl(*cbStr).c_str(), b);
//}
//if (tbrp["reload"].toBool() || (ui->webView && (ui->webView->history()->count()==0))) {
RHODESAPP().loadUrl(rho::common::convertStringAFromWP8(action), index);
//}
}
void CRhoRuntime::onWindowClose(void)
{
// TODO: implement
}
void CRhoRuntime::onWebViewUrlChanged(::Platform::String^ url)
{
RHODESAPP().keepLastVisitedUrl(rho::common::convertStringAFromWP8(url));
}
bool CRhoRuntime::onBackKeyPress()
{
// TODO: implement check for history size (return false if there's no history)
RHODESAPP().navigateBack();
return true;
}
::Platform::String^ CRhoRuntime::onJSInvoke(::Platform::String^ inJSON)
{
rho::String ret = rho::apiGenerator::js_entry_point(rho::common::convertToStringA(inJSON->Data()).c_str());
return rho::common::convertStringToWP8(ret);
}
void CRhoRuntime::executeRhoRunnable(int64 native)
{
((rho::common::IRhoRunnable*)native)->runObject();
}
// *** PUBLIC METHODS ***
bool CRhoRuntime::Initialize(::Platform::String^ title)
{
return true;
}
void CRhoRuntime::DestroyUi(void)
{
rho_rhodesapp_callUiDestroyedCallback();
rho_rhodesapp_callAppActiveCallback(0);
}
void CRhoRuntime::setCryptoEngine(ICryptoEngine^ cryptoEngine)
{
m_CryptoEngine = cryptoEngine;
}
// *** proxy methods needed for C# implementation of CoreAPI ***
::Platform::String^ CRhoRuntime::canonicalizeRhoUrl(::Platform::String^ url)
{
rho::String fullUrl = RHODESAPPBASE().canonicalizeRhoUrl(rho::common::convertStringAFromWP8(url));
return rho::common::convertStringToWP8(fullUrl);
}
::Platform::String^ CRhoRuntime::getRootPath(::Platform::String^ path)
{
return rho::common::convertStringToWP8( rho::common::CFilePath::join( RHODESAPP().getRhoRootPath(), rho::common::convertStringAFromWP8(path) ) );
}
::Platform::String^ CRhoRuntime::getAppRootPath(::Platform::String^ path)
{
return rho::common::convertStringToWP8( rho::common::CFilePath::join( RHODESAPP().getAppRootPath(), rho::common::convertStringAFromWP8(path) ) );
}
::Platform::String^ CRhoRuntime::getRERuntimePath(::Platform::String^ path)
{
return rho::common::convertStringToWP8( rho::common::CFilePath::join( rho_native_reruntimepath(), rho::common::convertStringAFromWP8(path)) );
}
// JSON parsing
CJSONEntryProxy::CJSONEntryProxy(::Platform::String^ data)
{
m_Entry = new rho::json::CJSONEntry(rho::common::convertStringAFromWP8(data).c_str());
}
bool CJSONEntryProxy::isEmpty()
{
return m_Entry->isEmpty();
}
bool CJSONEntryProxy::hasName(::Platform::String^ name)
{
return m_Entry->hasName(rho::common::convertStringAFromWP8(name));
}
bool CJSONEntryProxy::isString()
{
return m_Entry->isString();
}
bool CJSONEntryProxy::isArray()
{
return m_Entry->isArray();
}
bool CJSONEntryProxy::isObject()
{
return m_Entry->isObject();
}
bool CJSONEntryProxy::isNull()
{
return m_Entry->isNull();
}
bool CJSONEntryProxy::isInteger()
{
return m_Entry->isInteger();
}
bool CJSONEntryProxy::isFloat()
{
return m_Entry->isFloat();
}
bool CJSONEntryProxy::isBoolean()
{
return m_Entry->isBoolean();
}
int CJSONEntryProxy::getInt(::Platform::String^ name)
{
return m_Entry->getInt(rho::common::convertStringAFromWP8(name).c_str());
}
uint64 CJSONEntryProxy::getUInt64(::Platform::String^ name)
{
return m_Entry->getUInt64(rho::common::convertStringAFromWP8(name).c_str());
}
double CJSONEntryProxy::getDouble(::Platform::String^ name)
{
return m_Entry->getDouble(rho::common::convertStringAFromWP8(name).c_str());
}
int CJSONEntryProxy::getInt()
{
return m_Entry->getInt();
}
uint64 CJSONEntryProxy::getUInt64()
{
return m_Entry->getUInt64();
}
double CJSONEntryProxy::getDouble()
{
return m_Entry->getDouble();
}
bool CJSONEntryProxy::getBoolean()
{
return m_Entry->getBoolean();
}
::Platform::String^ CJSONEntryProxy::getString(::Platform::String^ name)
{
return rho::common::convertStringCToWP8(m_Entry->getString(rho::common::convertStringAFromWP8(name).c_str()));
}
::Platform::String^ CJSONEntryProxy::getString(::Platform::String^ name, ::Platform::String^ szDefValue)
{
return rho::common::convertStringCToWP8(m_Entry->getString(rho::common::convertStringAFromWP8(name).c_str(), rho::common::convertStringAFromWP8(szDefValue).c_str()));
}
::Platform::String^ CJSONEntryProxy::getString()
{
return rho::common::convertStringCToWP8(m_Entry->getString());
}
extern "C" void createMenu()
{
CRhoRuntime::getInstance()->createCustomMenu();
}
// old API functions
extern "C" void rho_sys_app_exit()
{
rho_rhodesapp_callUiDestroyedCallback();
//TODO: fix issue with stop thread - stopWait does not wakeup sleeping thread
//rho::common::CRhodesApp::Destroy();
CRhoRuntime::getInstance()->getMainPage()->exitCommand();
}
#ifdef OS_WP8
extern "C" void rho_net_impl_network_indicator(int active)
{
//TODO: rho_net_ping_network
//RAWLOGC_INFO("rhoruntime.cpp", "rho_net_impl_network_indicator() has no implementation on WP8.");
}
extern "C" int rho_net_ping_network(const char* szHost)
{
//TODO: rho_net_ping_network
//RAWLOGC_INFO("rhoruntime.cpp", "rho_net_ping_network() has no implementation on WP8.");
return 1;
}
#endif
extern "C" void rho_os_impl_performOnUiThread(rho::common::IRhoRunnable* pTask)
{
//CRhoRuntime::getInstance()->getMainPage()->performOnUiThread((int64)pTask);
pTask->runObject();
}
extern "C" void rho_title_change(const int tabIndex, const char* strTitle)
{
}
extern "C" const char* rho_native_rhopath()
{
return "rho"; //rho/apps?
}
void rho_webview_navigate_back_with_tab(int index)
{
CRhoRuntime::getInstance()->getMainPage()->GoBack(index);
}
extern "C" const char* rho_native_get_appname()
{
return "Rhodes";
}
RHO_GLOBAL const char* rho_native_reruntimepath()
{
return rho_native_rhopath();
}
/// <summary>Automatically closes a search handle upon destruction</summary>
class SearchHandleScope {
/// <summary>Initializes a new search handle closer</summary>
/// <param name="searchHandle">Search handle that will be closed on destruction</param>
public: SearchHandleScope(HANDLE searchHandle) :
searchHandle(searchHandle) {}
/// <summary>Closes the search handle</summary>
public: ~SearchHandleScope() {
::FindClose(this->searchHandle);
}
/// <summary>Search handle that will be closed when the instance is destroyed</summary>
private: HANDLE searchHandle;
};
/// <summary>Recursively deletes the specified directory and all its contents</summary>
/// <param name="path">Absolute path of the directory that will be deleted</param>
/// <remarks>
/// The path must not be terminated with a path separator.
/// </remarks>
extern "C" void recursiveDeleteDirectory(const std::wstring &path) {
static const std::wstring allFilesMask(L"\\*");
WIN32_FIND_DATAW findData;
// First, delete the contents of the directory, recursively for subdirectories
std::wstring searchMask = path + allFilesMask;
HANDLE searchHandle = ::FindFirstFileExW(
searchMask.c_str(), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0
);
if(searchHandle == INVALID_HANDLE_VALUE) {
DWORD lastError = ::GetLastError();
if(lastError != ERROR_FILE_NOT_FOUND) { // or ERROR_NO_MORE_FILES, ERROR_NOT_FOUND?
// throw std::runtime_error("Could not start directory enumeration");
return;
}
}
// Did this directory have any contents? If so, delete them first
if(searchHandle != INVALID_HANDLE_VALUE) {
SearchHandleScope scope(searchHandle);
for(;;) {
// Do not process the obligatory '.' and '..' directories
if(findData.cFileName[0] != '.') {
bool isDirectory =
((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) ||
((findData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0);
// Subdirectories need to be handled by deleting their contents first
std::wstring filePath = path + L'/' + findData.cFileName;
if(isDirectory) {
recursiveDeleteDirectory(filePath);
} else {
BOOL result = ::DeleteFileW(filePath.c_str());
if(result == FALSE) {
//throw std::runtime_error("Could not delete file");
return;
}
}
}
// Advance to the next file in the directory
BOOL result = ::FindNextFileW(searchHandle, &findData);
if(result == FALSE) {
DWORD lastError = ::GetLastError();
if(lastError != ERROR_NO_MORE_FILES) {
// throw std::runtime_error("Error enumerating directory");
return;
}
break; // All directory contents enumerated and deleted
}
} // for
}
// The directory is empty, we can now safely remove it
BOOL result = ::RemoveDirectory(path.c_str());
if(result == FALSE) {
//throw std::runtime_error("Could not remove directory");
return;
}
}