Handle the case WebIME reloads and losts init information 92/175392/1
authorJi-hoon Lee <dalton.lee@samsung.com>
Wed, 9 Aug 2017 12:39:46 +0000 (21:39 +0900)
committerJi-hoon Lee <dalton.lee@samsung.com>
Tue, 10 Apr 2018 05:05:59 +0000 (05:05 +0000)
Change-Id: I41ce3723ba0572c46c98ac5158a49cdeda1afecd

src/legacy_support/websocket.cpp
src/legacy_support/websocket.h

index 4fecacc..f3946d6 100644 (file)
@@ -75,6 +75,8 @@ static int callback_http(struct lws *wsi,
 struct per_session_data__keyboard {
     int session_id;
     int valid;
+    int need_init;
+    int initialized;
 };
 
 static int callback_keyboard(struct lws *wsi,
@@ -115,6 +117,8 @@ static int callback_keyboard(struct lws *wsi,
         pss->session_id = ++last_session_id;
         LOGD("LWS_CALLBACK_ESTABLISHED : %p %d", g_ws_server_context, pss->session_id);
         pss->valid = false;
+        pss->need_init = false;
+        pss->initialized = false;
         if (g_ws_server_context) {
             lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
         }
@@ -126,19 +130,46 @@ static int callback_keyboard(struct lws *wsi,
 
     case LWS_CALLBACK_SERVER_WRITEABLE:
         if (agent) {
+            /* Ignore valid check since the magic key is not used anymore */
+            if (!pss->valid) pss->valid = true;
+
             /* We allow data tranmission only if this client is guaranteed to be valid */
             if (pss->valid) {
                 pthread_mutex_lock(&g_ws_server_mutex);
                 std::queue<ISE_MESSAGE>& messages = agent->get_send_message_queue();
-                /* One write allowed per one writable callback */
-                if (messages.size() > 0) {
-                    ISE_MESSAGE &message = messages.front();
+
+                if (pss->need_init && !pss->initialized) {
+                    ISE_MESSAGE message;
+                    message.type = ISE_MESSAGE_TYPE_STRINGS[ISE_MESSAGE_TYPE_PLAIN];
+                    message.command = ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_INIT];
                     std::string str = CISEMessageSerializer::serialize(message);
-                    LOGD("SEND_WEBSOCKET_MESSAGE : %s", str.c_str());
+                    LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
                     n = snprintf((char *)p, bufsize, "%s", str.c_str());
                     /* too small for partial */
                     n = lws_write(wsi, p, n, LWS_WRITE_TEXT);
-                    messages.pop();
+                    pss->need_init = false;
+                    pss->initialized = true;
+                } else {
+                    /* One write allowed per one writable callback */
+                    if (messages.size() > 0) {
+                        ISE_MESSAGE &message = messages.front();
+                        bool drop = false;
+                        if (message.command.compare(ISE_MESSAGE_COMMAND_STRINGS[ISE_MESSAGE_COMMAND_INIT]) == 0) {
+                            if (pss->initialized) {
+                                drop = true;
+                            } else {
+                                pss->initialized = true;
+                            }
+                        }
+                        if (!drop) {
+                            std::string str = CISEMessageSerializer::serialize(message);
+                            LOGD("SEND_WEBSOCKET_MESSAGE : %d %s", pss->session_id, str.c_str());
+                            n = snprintf((char *)p, bufsize, "%s", str.c_str());
+                            /* too small for partial */
+                            n = lws_write(wsi, p, n, LWS_WRITE_TEXT);
+                        }
+                        messages.pop();
+                    }
                 }
                 if (messages.size() > 0) {
                     ecore_pipe_write(agent->get_message_pipe(), MESSAGE_LEFT, strlen(MESSAGE_LEFT));
@@ -146,7 +177,7 @@ static int callback_keyboard(struct lws *wsi,
                 pthread_mutex_unlock(&g_ws_server_mutex);
 
                 if (n < 0) {
-                    LOGE("ERROR %d writing to di socket\n", n);
+                    LOGE("ERROR %d writing to di socket %d", n, pss->session_id);
                     return -1;
                 }
 
@@ -154,7 +185,7 @@ static int callback_keyboard(struct lws *wsi,
                     lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
                 }
             } else {
-                LOGD("Rejecting data transmission since client is not valid");
+                LOGD("Rejecting data transmission since client is not valid : %d", pss->session_id);
             }
         }
         break;
@@ -175,8 +206,16 @@ static int callback_keyboard(struct lws *wsi,
                 }
                 */
                 pss->valid = true;
+
+                if (agent->initalized()) {
+                    pss->need_init = true;
+                    lws_callback_on_writable_all_protocol(g_ws_server_context, &protocols[PROTOCOL_KEYBOARD]);
+                }
             }
 
+            /* Ignore valid check since the magic key is not used anymore */
+            if (!pss->valid) pss->valid = true;
+
             if (pss->valid) {
                 pthread_mutex_lock(&g_ws_server_mutex);
                 std::queue<ISE_MESSAGE>& messages = agent->get_recv_message_queue();
@@ -192,7 +231,7 @@ static int callback_keyboard(struct lws *wsi,
                     pthread_mutex_unlock(&g_ws_query_mutex);
                 }
             } else {
-                LOGD("Ignoring data received since client is not valid");
+                LOGD("Ignoring data received since client is not valid %d", pss->session_id);
             }
         }
 
@@ -233,6 +272,7 @@ CWebHelperAgentWebSocket::CWebHelperAgentWebSocket()
     }
     m_current_instance = this;
     m_message_pipe = NULL;
+    m_initialized = false;
 }
 
 CWebHelperAgentWebSocket::~CWebHelperAgentWebSocket()
@@ -318,6 +358,8 @@ bool CWebHelperAgentWebSocket::init()
 
     on_init();
 
+    m_initialized = true;
+
     return ret;
 }
 
index 98483cb..d032ed9 100644 (file)
@@ -205,6 +205,8 @@ public:
     bool init();
     bool exit();
 
+    bool initalized() { return m_initialized; }
+
     void signal(int sig);
 
     void on_init();
@@ -258,6 +260,8 @@ protected:
     std::queue<ISE_MESSAGE> m_recv_message_queue;
 
     Ecore_Pipe *m_message_pipe;
+
+    bool m_initialized;
 };
 
 #endif // _WEB_HELPER_AGENT_WEBSOCKET_H_