platform/shared/common/RhodesApp.cpp in rhodes-3.3.5 vs platform/shared/common/RhodesApp.cpp in rhodes-3.4.2
- old
+ new
@@ -40,10 +40,11 @@
#include "ruby/ext/rho/rhoruby.h"
#include "net/AsyncHttp.h"
#include "rubyext/WebView.h"
#include "rubyext/GeoLocation.h"
#include "common/app_build_configs.h"
+#include "common/app_build_capabilities.h"
#include "unzip/unzip.h"
#include "common/Tokenizer.h"
#include <algorithm>
@@ -333,11 +334,10 @@
:CRhodesAppBase(strRootPath, strUserPath, strRuntimePath)
{
m_bExit = false;
m_bDeactivationMode = false;
m_bRestartServer = false;
- m_bSendingLog = false;
//m_activateCounter = 0;
m_pExtManager = 0;
m_appCallbacksQueue = new CAppCallbacksQueue();
@@ -451,42 +451,10 @@
#endif
// net::CAsyncHttp::Destroy();
}
-template <typename T>
-class CRhoCallInThread : public common::CRhoThread
-{
-public:
- CRhoCallInThread(T* cb)
- :CRhoThread(), m_cb(cb)
- {
- start(epNormal);
- }
-
-private:
- virtual void run()
- {
- m_cb->run(*this);
- }
-
- virtual void runObject()
- {
- common::CRhoThread::runObject();
- delete this;
- }
-
-private:
- common::CAutoPtr<T> m_cb;
-};
-
-template <typename T>
-void rho_rhodesapp_call_in_thread(T *cb)
-{
- new CRhoCallInThread<T>(cb);
-}
-
class CRhoCallbackCall
{
String m_strCallback, m_strBody;
public:
CRhoCallbackCall(const String& strCallback, const String& strBody)
@@ -676,10 +644,11 @@
class CJsonResponse : public rho::ICallbackObject
{
String m_strJson;
public:
+ CJsonResponse(const String& strJson) : m_strJson(strJson) { }
CJsonResponse(const char* szJson) : m_strJson(szJson) { }
virtual unsigned long getObjectValue()
{
char* szError = 0;
unsigned long valBody = rjson_tokener_parse(m_strJson.c_str(), &szError);
@@ -1240,24 +1209,24 @@
LOG(INFO) + "Trying to get free listening port.";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if ( sockfd < 0 )
{
- LOG(WARNING) + "Unable to open socket";
+ LOG(ERROR) + "Unable to open socket";
noerrors = 0;
}
int disable = 0;
if (noerrors && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&disable, sizeof(disable)) != 0)
{
- LOG(WARNING) + "Unable to set socket option";
+ LOG(ERROR) + "Unable to set socket option";
noerrors = 0;
}
#if defined(OS_MACOSX)
if (noerrors && setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char *)&disable, sizeof(disable)) != 0)
{
- LOG(WARNING) + "Unable to set socket option";
+ LOG(ERROR) + "Unable to set socket option";
noerrors = 0;
}
#endif
if (noerrors)
@@ -1275,12 +1244,14 @@
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
serv_addr.sin_port = htons((short)listenPort);
LOG(INFO) + "Trying to bind of " + listenPort + " port...";
- if ( bind( sockfd, (struct sockaddr *) &serv_addr, sizeof( serv_addr ) ) != 0 )
+ int nBindRes = bind( sockfd, (struct sockaddr *) &serv_addr, sizeof( serv_addr ) );
+ if ( nBindRes != 0 )
{
+ LOG(INFO) + "Bind of " + listenPort + " port is failed with code: " + nBindRes;
if (listenPort != 0)
{
// Fill serv_addr again but with dynamically selected port
#if defined(OS_MACOSX)
serv_addr.sin_len = sizeof(serv_addr);
@@ -1311,11 +1282,11 @@
socklen_t length = sizeof( serv_addr );
if (getsockname( sockfd, (struct sockaddr *)&serv_addr, &length ) != 0)
{
- LOG(WARNING) + "Can not get socket info";
+ LOG(ERROR) + "Can not get socket info";
nFreePort = 0;
}
else
{
nFreePort = (int)ntohs(serv_addr.sin_port);
@@ -1349,13 +1320,19 @@
m_strHomeUrl = "http://127.0.0.1:";
#else
m_strHomeUrl = "http://127.0.0.1:";
#endif
m_strHomeUrl += getFreeListeningPort();
+ m_strHomeUrlLocalHost = String("http://localhost:") + getFreeListeningPort();
+#ifndef RHODES_EMULATOR
m_strLoadingPagePath = "file://" + getRhoRootPath() + "apps/app/loading.html";
m_strLoadingPngPath = getRhoRootPath() + "apps/app/loading.png";
+#else
+ m_strLoadingPagePath = "file://" + getRhoRootPath() + "app/loading.html";
+ m_strLoadingPngPath = getRhoRootPath() + "app/loading.png";
+#endif
}
void CRhodesApp::keepLastVisitedUrl(String strUrl)
{
//LOG(INFO) + "Current URL: " + strUrl;
@@ -1431,11 +1408,11 @@
}
String CRhodesApp::getAppName()
{
String strAppName;
-#ifdef OS_WINCE
+#ifdef WINDOWS_PLATFORM
String path = rho_native_rhopath();
String_replace(path, '/', '\\');
int nEnd = path.find_last_of('\\');
nEnd = path.find_last_of('\\', nEnd-1)-1;
@@ -1520,69 +1497,10 @@
void CRhodesApp::navigateToUrl( const String& strUrl)
{
rho_webview_navigate(strUrl.c_str(), -1);
}
-class CRhoSendLogCall
-{
- String m_strCallback;
-public:
- CRhoSendLogCall(const String& strCallback): m_strCallback(strCallback){}
-
- void run(common::CRhoThread &)
- {
- String strDevicePin = rho::sync::CClientRegister::getInstance() ? rho::sync::CClientRegister::getInstance()->getDevicePin() : "";
- String strClientID = rho::sync::CSyncThread::getSyncEngine().readClientID();
-
- String strLogUrl = RHOCONF().getPath("logserver");
- if ( strLogUrl.length() == 0 )
- strLogUrl = RHOCONF().getPath("syncserver");
-
- String strQuery = strLogUrl + "client_log?" +
- "client_id=" + strClientID + "&device_pin=" + strDevicePin + "&log_name=" + RHOCONF().getString("logname");
-
- net::CMultipartItem oItem;
- oItem.m_strFilePath = LOGCONF().getLogFilePath();
- oItem.m_strContentType = "application/octet-stream";
-
- boolean bOldSaveToFile = LOGCONF().isLogToFile();
- LOGCONF().setLogToFile(false);
- NetRequest oNetRequest;
- oNetRequest.setSslVerifyPeer(false);
-
- NetResponse resp = getNetRequest(&oNetRequest).pushMultipartData( strQuery, oItem, &(rho::sync::CSyncThread::getSyncEngine()), null );
- LOGCONF().setLogToFile(bOldSaveToFile);
-
- boolean isOK = true;
-
- if ( !resp.isOK() )
- {
- LOG(ERROR) + "send_log failed : network error - " + resp.getRespCode() + "; Body - " + resp.getCharData();
- isOK = false;
- }
-
- if (m_strCallback.length() > 0)
- {
- const char* body = isOK ? "rho_callback=1&status=ok" : "rho_callback=1&status=error";
-
- rho_net_request_with_data(RHODESAPP().canonicalizeRhoUrl(m_strCallback).c_str(), body);
- }
-
- RHODESAPP().setSendingLog(false);
- }
-};
-
-boolean CRhodesApp::sendLog( const String& strCallbackUrl)
-{
- if ( m_bSendingLog )
- return true;
-
- m_bSendingLog = true;
- rho_rhodesapp_call_in_thread( new CRhoSendLogCall(strCallbackUrl) );
- return true;
-}
-
String CRhodesApp::addCallbackObject(ICallbackObject* pCallbackObject, String strName)
{
int nIndex = -1;
for (int i = 0; i < (int)m_arCallbackObjects.size(); i++)
{
@@ -1617,20 +1535,41 @@
delete pCallbackObject;
return valRes;
}
-void CRhodesApp::setPushNotification(String strUrl, String strParams )
+void CRhodesApp::setPushNotification(const String& strUrl, const String& strParams, const String& strType )
{
- synchronized(m_mxPushCallback)
+ if(strType == "legacy")
{
- m_strPushCallback = canonicalizeRhoUrl(strUrl);
- m_strPushCallbackParams = strParams;
+ synchronized(m_mxPushCallback)
+ {
+ m_strPushCallback = canonicalizeRhoUrl(strUrl);
+ m_strPushCallbackParams = strParams;
+ }
}
+ else
+ {
+ if (strUrl.length())
+ {
+ String canonicalUrl = canonicalizeRhoUrl(strUrl);
+ if(strType.length())
+ m_appPushMgr.registerClient(canonicalUrl, strParams, strType);
+ else
+ m_appPushMgr.registerAllClients(canonicalUrl, strParams);
+ } else
+ {
+ if(strType.length())
+ m_appPushMgr.unregisterClient(strType);
+ else
+ m_appPushMgr.unregisterAllClients();
+ }
+ }
}
-boolean CRhodesApp::callPushCallback(String strData)
+// Deprecated
+boolean CRhodesApp::callPushCallback(const String& strData) const
{
synchronized(m_mxPushCallback)
{
if ( m_strPushCallback.length() == 0 )
return false;
@@ -1650,10 +1589,35 @@
}
return false;
}
+boolean CRhodesApp::callPushCallbackWithJsonBody(const String& strUrl, const String& strData)
+{
+ synchronized(m_mxPushCallback)
+ {
+ if (strUrl.length() == 0)
+ return false;
+
+ String strCanonicalUrl = canonicalizeRhoUrl(strUrl);
+
+ String strBody = addCallbackObject( new CJsonResponse( strData ), "__rho_inline" ) + "&rho_callback=1";
+
+ NetResponse resp = getNetRequest().pushData( strCanonicalUrl, strBody, null );
+ if (!resp.isOK())
+ LOG(ERROR) + "Push notification failed. Code: " + resp.getRespCode() + "; Error body: " + resp.getCharData();
+ else
+ {
+ const char* szData = resp.getCharData();
+ LOG(TRACE) + "Push callback resp data: " + (szData ? szData : "NULL");
+ return !(szData && strcmp(szData,"rho_push") == 0);
+ }
+ }
+
+ return false;
+}
+
void CRhodesApp::setScreenRotationNotification(String strUrl, String strParams)
{
synchronized(m_mxScreenRotationCallback)
{
if (strUrl.length() > 0) {
@@ -1986,10 +1950,19 @@
return 1;
return RHODESAPP().callPushCallback(szData?szData:"") ? 1 : 0;
}
+int rho_rhodesapp_callPushCallbackWithJsonBody(const char* szUrl, const char* szData)
+{
+ if ( !rho::common::CRhodesApp::getInstance() )
+ return 1;
+
+ return RHODESAPP().callPushCallbackWithJsonBody(szUrl, szData) ? 1 : 0;
+}
+
+
void rho_rhodesapp_callScreenRotationCallback(int width, int height, int degrees)
{
if ( !rho::common::CRhodesApp::getInstance() )
return;
RHODESAPP().callScreenRotationCallback(width, height, degrees);
@@ -2008,28 +1981,15 @@
int rho_rhodesapp_isrubycompiler()
{
return 0;
}
-int rho_conf_send_log(const char* callback_url)
-{
- rho::String s_callback_url = "";
- if (callback_url != NULL) {
- s_callback_url = callback_url;
- }
- return RHODESAPP().sendLog(s_callback_url);
-}
-
void rho_net_request(const char *url)
{
getNetRequest().pullData(url, null);
}
-void rho_net_request_with_data(const char *url, const char *str_body)
-{
- getNetRequest().pushData(url, str_body, null);
-}
void rho_rhodesapp_load_url(const char *url)
{
RHODESAPP().loadUrl(url);
}
@@ -2106,30 +2066,25 @@
if ((szMotorolaLicence == NULL) || (szMotorolaLicenceCompany == NULL)) {
return 0;
}
int res_check = 1;
-#ifdef OS_ANDROID
+#if defined( OS_ANDROID ) || defined( OS_MACOSX )
res_check = MotorolaLicence_check(szMotorolaLicenceCompany, szMotorolaLicence);
#endif
-#ifdef OS_MACOSX
- res_check = MotorolaLicence_check(szMotorolaLicenceCompany, szMotorolaLicence);
-#endif
-
return res_check;
}
int rho_is_rho_elements_extension_can_be_used() {
int res_check = 1;
-
-#if defined( OS_ANDROID ) || defined( OS_MACOS )
- const char* szMotorolaLicence = get_app_build_config_item("motorola_license");
- const char* szMotorolaLicenceCompany = get_app_build_config_item("motorola_license_company");
+#if defined( OS_MACOSX ) || (defined( OS_ANDROID ) && !defined ( APP_BUILD_CAPABILITY_MOTOROLA ))
+ const char* szMotorolaLicence = get_app_build_config_item("motorola_license");
+ const char* szMotorolaLicenceCompany = get_app_build_config_item("motorola_license_company");
- if ((szMotorolaLicence == NULL) || (szMotorolaLicenceCompany == NULL))
- res_check = 0;
+ if ((szMotorolaLicence == NULL) || (szMotorolaLicenceCompany == NULL))
+ res_check = 0;
#endif
return res_check;
}
@@ -2140,19 +2095,11 @@
if ((szMotorolaLicence == NULL) || (szMotorolaLicenceCompany == NULL)) {
return 1;
}
int res_check = 1;
-#ifdef OS_ANDROID
-#ifdef APP_BUILD_CAPABILITY_MOTOROLA
- // ET1
- res_check = 1;
-#else
- res_check = rho_is_motorola_licence_checked();
-#endif
-#endif
-#ifdef OS_MACOSX
- res_check = rho_is_motorola_licence_checked();
+#if defined( OS_MACOSX ) || (defined( OS_ANDROID ) && !defined ( APP_BUILD_CAPABILITY_MOTOROLA ))
+ res_check = rho_is_motorola_licence_checked();
#endif
return res_check;
}