fix issue,not free string
[apps/agl-service-homescreen.git] / src / hs-clientmanager.cpp
index 3dea3a6..1355c99 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
+#include <algorithm>
 #include "hs-clientmanager.h"
 #include "hmi-debug.h"
 
+static const char _homescreen[] = "homescreen";
+
 HS_ClientManager* HS_ClientManager::me = nullptr;
 
 static void cbRemoveClientCtxt(void *data)
@@ -69,151 +71,395 @@ HS_ClientManager* HS_ClientManager::instance(void)
 int HS_ClientManager::init(void)
 {
     HMI_NOTICE("homescreen-service","called.");
-    // TODO : connect to windowmanger
-    // get applist from appfw
 }
 
 /**
- * find HS_Client in client_list
+ * create client's afb_req_context
  *
  * #### Parameters
  *  - appid: app's id
  *
  * #### Return
- * found HS_Client pointer
+ * HS_ClientCtxt pointer
  *
  */
-HS_Client* HS_ClientManager::find(std::string appid)
+HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req_t req, std::string appid)
 {
-    std::lock_guard<std::mutex> lock(this->mtx);
-    HS_Client* p = nullptr;
-    auto ip = client_list.find(appid);
-    if(ip != client_list.end()) {
-        p = client_list[appid];
+    HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
+    if (!ctxt)
+    {
+        HMI_NOTICE("homescreen-service", "create new session for %s", appid.c_str());
+        HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
+        afb_req_session_set_LOA(req, 1);
+        afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
     }
-    return p;
+    return ctxt;
 }
 
 /**
- * get HS_Client
+ * add Client
+ *
+ * #### Parameters
+ *  - ctxt: app's id
+ *
+ * #### Return
+ * HS_Client pointer
+ *
+ */
+HS_Client* HS_ClientManager::addClient(afb_req_t req, std::string appid)
+{
+    return (client_list[appid] = new HS_Client(req, appid));
+}
+
+/**
+ * remove Client
  *
  * #### Parameters
  *  - appid: app's id
  *
  * #### Return
- * found HS_Client pointer
+ * None
  *
  */
-HS_Client* HS_ClientManager::getClient(afb_req req, std::string appid)
+void HS_ClientManager::removeClient(std::string appid)
 {
+    delete client_list[appid];
+    client_list.erase(appid);
+}
+
+/**
+ * remove Client from list
+ *
+ * #### Parameters
+ *  - data: HS_ClientCtxt pointer
+ *
+ * #### Return
+ * None
+ *
+ */
+void HS_ClientManager::removeClientCtxt(void *data)
+{
+    HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
+    if(ctxt == nullptr)
+    {
+        HMI_ERROR("homescreen-service", "data is nullptr");
+        return;
+    }
+
+    HMI_NOTICE("homescreen-service", "remove app %s", ctxt->id.c_str());
     std::lock_guard<std::mutex> lock(this->mtx);
-    HS_Client* p = nullptr;
-    auto ip = client_list.find(appid);
-    if(ip != client_list.end()) {
-        p = client_list[appid];
+    removeClient(ctxt->id);
+    delete appid2ctxt[ctxt->id];
+    appid2ctxt.erase(ctxt->id);
+}
+
+/**
+ * tap_shortcut
+ *
+ * #### Parameters
+ *  - request: the request to bindings
+ *
+ * #### Return
+ * result
+ *
+ */
+int HS_ClientManager::tap_shortcut(afb_req_t request)
+{
+    int ret = 0;
+    const char* value = afb_req_value(request, _application_id);
+    if (value) {
+        HMI_NOTICE("homescreen-service","request params = %s.", value);
+        std::lock_guard<std::mutex> lock(this->mtx);
+        auto ip = client_list.find(std::string(value));
+        if(ip != client_list.end()) {
+            ip->second->tap_shortcut(value);
+        }
     }
     else {
-        appid2ctxt[appid] = createClientCtxt(req, appid);
-        p = addClient(req, appid);
+        HMI_NOTICE("homescreen-service","Please input application_id");
+        ret = AFB_EVENT_BAD_REQUEST;
     }
-    return p;
+    return ret;
 }
 
 /**
- * get HS_Client pointers set
+ * on_screen_message
  *
  * #### Parameters
- *  - Nothing
+ *  - request: the request to bindings
  *
  * #### Return
- * HS_Client pointers set
+ * result
  *
  */
-std::vector<HS_Client*> HS_ClientManager::getAllClient(void)
+int HS_ClientManager::on_screen_message(afb_req_t request)
 {
-    std::lock_guard<std::mutex> lock(this->mtx);
-    std::vector<HS_Client*> v;
-    for(auto a : client_list)
-        v.push_back(a.second);
-    return v;
+    int ret = 0;
+    const char* value = afb_req_value(request, _display_message);
+    if (value) {
+        HMI_NOTICE("homescreen-service","request params = %s.", value);
+        std::lock_guard<std::mutex> lock(this->mtx);
+        for(auto m : client_list) {
+            m.second->on_screen_message(request, value);
+        }
+    }
+    else {
+        HMI_NOTICE("homescreen-service","Please input display_message");
+        ret = AFB_EVENT_BAD_REQUEST;
+    }
+    return ret;
 }
 
 /**
- * create client's afb_req_context
+ * on_screen_reply
  *
  * #### Parameters
- *  - appid: app's id
+ *  - request: the request to bindings
  *
  * #### Return
- * HS_ClientCtxt pointer
+ * result
  *
  */
-HS_ClientCtxt* HS_ClientManager::createClientCtxt(afb_req req, std::string appid)
+int HS_ClientManager::on_screen_reply(afb_req_t request)
 {
-    HS_ClientCtxt *ctxt = (HS_ClientCtxt *)afb_req_context_get(req);
-    if (!ctxt)
-    {
-        HMI_NOTICE("homescreen-service", "create new session for %s", appid.c_str());
-        HS_ClientCtxt *ctxt = new HS_ClientCtxt(appid.c_str());
-        afb_req_session_set_LOA(req, 1);
-        afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
+    int ret = 0;
+    const char* value = afb_req_value(request, _reply_message);
+    if (value) {
+      HMI_NOTICE("homescreen-service","request params = %s.", value);
+      std::lock_guard<std::mutex> lock(this->mtx);
+      for(auto m : client_list) {
+        m.second->on_screen_reply(request, value);
+      }
     }
-    return ctxt;
+    else {
+        HMI_NOTICE("homescreen-service","Please input reply_message");
+        ret = AFB_EVENT_BAD_REQUEST;
+    }
+    return ret;
 }
 
 /**
- * add Client
+ * subscribe
  *
  * #### Parameters
- *  - ctxt: app's id
+ *  - request: the request to bindings
  *
  * #### Return
- * HS_Client pointer
+ * result
  *
  */
-HS_Client* HS_ClientManager::addClient(afb_req req, std::string appid)
+int HS_ClientManager::subscribe(afb_req_t request)
 {
-    return (client_list[appid] = new HS_Client(req, appid));
+    int ret = 0;
+    const char *value = afb_req_value(request, "event");
+    HMI_NOTICE("homescreen-service","value is %s", value);
+    if(value) {
+        std::string appid =std::move(get_application_id(request));
+        if(appid.empty()) {
+            HMI_NOTICE("homescreen-service","can't get application identifier");
+            return AFB_REQ_GETAPPLICATIONID_ERROR;
+        }
+
+        std::lock_guard<std::mutex> lock(this->mtx);
+        HS_Client* client = nullptr;
+        auto ip = client_list.find(appid);
+        if(ip != client_list.end()) {
+            client = client_list[appid];
+        }
+        else {
+            appid2ctxt[appid] = createClientCtxt(request, appid);
+            client = addClient(request, appid);
+        }
+
+        if(client->subscribe(request, value) != 0) {
+            HMI_NOTICE("homescreen-service","subscribe failed");
+            ret = AFB_REQ_SUBSCRIBE_ERROR;
+        }
+    }
+    else {
+        HMI_NOTICE("homescreen-service","Please input event name");
+        ret = AFB_EVENT_BAD_REQUEST;
+    }
+    return ret;
 }
 
 /**
- * remove Client
+ * unsubscribe
  *
  * #### Parameters
- *  - appid: app's id
+ *  - request: the request to bindings
  *
  * #### Return
- * None
+ * result
  *
  */
-void HS_ClientManager::removeClient(std::string appid)
+int HS_ClientManager::unsubscribe(afb_req_t request)
 {
-    delete client_list[appid];
-    client_list.erase(appid);
+    const char *value = afb_req_value(request, "event");
+    HMI_NOTICE("homescreen-service","value is %s", value);
+    int ret = 0;
+    if(value) {
+        std::string appid = std::move(get_application_id(request));
+        if(appid.empty()) {
+            HMI_NOTICE("homescreen-service","can't get application identifier");
+            return AFB_REQ_GETAPPLICATIONID_ERROR;
+        }
+
+        std::lock_guard<std::mutex> lock(this->mtx);
+        auto ip = client_list.find(appid);
+        if(ip != client_list.end()
+        && ip->second->unsubscribe(request, value) != 0) {
+            HMI_NOTICE("homescreen-service","unsubscribe failed");
+            ret = AFB_REQ_UNSUBSCRIBE_ERROR;
+        }
+    }
+    else {
+        HMI_NOTICE("homescreen-service","Please input event name");
+        ret = AFB_EVENT_BAD_REQUEST;
+    }
+    return ret;
 }
 
 /**
- * remove Client from list
+ * showWindow event
  *
  * #### Parameters
- *  - data: HS_ClientCtxt pointer
+ *  - request : the request
  *
  * #### Return
- * None
+ * 0 : success
+ * others : fail
  *
  */
-void HS_ClientManager::removeClientCtxt(void *data)
+int HS_ClientManager::showWindow(afb_req_t request)
 {
-    HS_ClientCtxt *ctxt = (HS_ClientCtxt *)data;
-    if(ctxt == nullptr)
-    {
-        HMI_ERROR("homescreen-service", "data is nullptr");
-        return;
+    int ret = 0;
+    const char* value = afb_req_value(request, _application_id);
+    if (value) {
+        HMI_NOTICE("homescreen-service","request params = %s.", value);
+        std::lock_guard<std::mutex> lock(this->mtx);
+        auto ip = client_list.find(std::string(value));
+        if(ip != client_list.end()) {
+            ret = ip->second->showWindow(request, value);
+        }
+    }
+    else {
+        HMI_NOTICE("homescreen-service","Please input application_id");
+        ret = AFB_EVENT_BAD_REQUEST;
     }
+    return ret;
+}
 
-    HMI_NOTICE("homescreen-service", "remove app %s", ctxt->id.c_str());
+/**
+ * hideWindow event
+ *
+ * #### Parameters
+ *  - request : the request
+ *
+ * #### Return
+ * 0 : success
+ * others : fail
+ *
+ */
+int HS_ClientManager::hideWindow(afb_req_t request)
+{
+    int ret = 0;
+    const char* value = afb_req_value(request, _application_id);
+    if (value) {
+        HMI_NOTICE("homescreen-service","request params = %s.", value);
+        std::lock_guard<std::mutex> lock(this->mtx);
+        auto ip = client_list.find(std::string(value));
+        if(ip != client_list.end()) {
+            ret = ip->second->hideWindow(request);
+        }
+    }
+    else {
+        HMI_NOTICE("homescreen-service","Please input application_id");
+        ret = AFB_EVENT_BAD_REQUEST;
+    }
+    return ret;
+}
+
+/**
+ * replyShowWindow event
+ *
+ * #### Parameters
+ *  - request : the request
+ *
+ * #### Return
+ * 0 : success
+ * others : fail
+ *
+ */
+int HS_ClientManager::replyShowWindow(afb_req_t request)
+{
+    int ret = 0;
+    const char* value = afb_req_value(request, _application_id);
+    if (value) {
+        HMI_NOTICE("homescreen-service","request params = %s.", value);
+        std::lock_guard<std::mutex> lock(this->mtx);
+        auto ip = client_list.find(std::string(value));
+        if(ip != client_list.end()) {
+            ret = ip->second->replyShowWindow(request, value);
+        }
+    }
+    else {
+        HMI_NOTICE("homescreen-service","Please input application_id");
+        ret = AFB_EVENT_BAD_REQUEST;
+    }
+    return ret;
+}
+
+/**
+ * showNotification event
+ *
+ * #### Parameters
+ *  - request : the request
+ *
+ * #### Return
+ * 0 : success
+ * others : fail
+ *
+ */
+int HS_ClientManager::showNotification(afb_req_t request)
+{
+    int ret = 0;
     std::lock_guard<std::mutex> lock(this->mtx);
-    removeClient(ctxt->id);
-    delete appid2ctxt[ctxt->id];
-    appid2ctxt.erase(ctxt->id);
+    auto ip = client_list.find(_homescreen);
+    if(ip != client_list.end()) {
+        ret = ip->second->showNotification(request);
+    }
+    else {
+        HMI_NOTICE("homescreen-service","not exist sessiion with homescreen");
+        ret = AFB_REQ_SHOWNOTIFICATION_ERROR;
+    }
+
+    return ret;
+}
+
+/**
+ * showInformation event
+ *
+ * #### Parameters
+ *  - request : the request
+ *
+ * #### Return
+ * 0 : success
+ * others : fail
+ *
+ */
+int HS_ClientManager::showInformation(afb_req_t request)
+{
+    int ret = 0;
+    std::lock_guard<std::mutex> lock(this->mtx);
+    auto ip = client_list.find(_homescreen);
+    if(ip != client_list.end()) {
+        ret = ip->second->showInformation(request);
+    }
+    else {
+        HMI_NOTICE("homescreen-service","not exist sessiion with homescreen");
+        ret = AFB_REQ_SHOWINFORMATION_ERROR;
+    }
+
+    return ret;
 }