29344ddb8b3d2e8bd292e86597099ce54ff49fd7
[platform/framework/web/web-provider.git] / src / Core / Service / MessageManager.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.1 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://floralicense.org/license/
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    MessageMenager.h
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20
21 #include <string>
22 #include <Evas.h>
23 #include <Eina.h>
24 #include <ewk_view.h>
25 #include <Core/Util/Log.h>
26 #include "MessageManager.h"
27
28 namespace Service {
29
30 static const std::string jsFireWindowEventFunction("webprovider.fireAppWidgetEvent");
31 static const std::string jsPdMessageEvent("pdmessage");
32 static const std::string jsBoxMessageEvent("boxmessage");
33
34 MessageManager::MessageManager()
35 {
36     LogD("enter");
37 }
38
39 MessageManager::~MessageManager()
40 {
41     LogD("enter");
42 }
43
44 bool MessageManager::send(Evas_Object* webview, ReceiverType receiver, std::string& message)
45 {
46     LogD("enter");
47
48     std::string eventName;
49     if(!webview) {
50         return false;
51     }
52     // set message event name triggered by receiver
53     switch (receiver) {
54     case TO_BOX:
55         eventName = jsPdMessageEvent;
56         break;
57     case TO_PD:
58         eventName = jsBoxMessageEvent;
59         break;
60     default:
61         return false;
62     }
63
64     std::string script = jsFireWindowEventFunction;
65     script += "(\"";
66     script += eventName;
67     script += "\", \"";
68     script += message;
69     script +="\");";
70     LogD("calling javascript: %s", script.c_str());
71
72     // execute js code for sending message
73     if (EINA_FALSE == ewk_view_script_execute(
74                 webview, script.c_str(), executeScriptCallback, this)) {
75         LogD("ewk_view_script_execute fail.");
76     }
77
78     return true;
79 }
80
81 void MessageManager::executeScriptCallback(
82         Evas_Object* webview, const char* result, void* data)
83 {
84     LogD("enter");
85     std::string resultStr(result ? result : "null");
86     LogD("result: %s", resultStr.c_str());
87 }
88 } // Service