Support fast opening of PD
[platform/framework/web/web-provider.git] / src / Core / View / PdHelper.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Flora License, Version 1.0 (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    PdHelper.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20 #include <string>
21 #include <Evas.h>
22 #include <ewk_view.h>
23 #include <Core/Util/Log.h>
24 #include "IRenderView.h"
25 #include "IPdHelper.h"
26 #include "PdHelper.h"
27
28 PdHelper::PdHelper(Evas_Object* pdWin, std::string pdStartUrl, RenderInfoPtr pdRenderInfo)
29     : m_win(pdWin)
30     , m_boxWebView()
31     , m_pdWebView()
32     , m_startUrl(pdStartUrl)
33     , m_renderInfo(pdRenderInfo)
34     , m_opened(false)
35 {
36 }
37
38 PdHelper::~PdHelper()
39 {
40 }
41
42 void PdHelper::startOpen()
43 {
44     LogD("enter");
45     if (!m_boxWebView) {
46         return;
47     }
48
49     //make javascript string for pd 
50     std::string script = "var pdWindow = window.open(\"";
51     script += validateUrl(m_startUrl);
52     script += "\", \"_blank\");";
53
54     // execute javascript for opening new webview for pd
55     LogD("executed script: %s", script.c_str());
56     ewk_view_script_execute(
57             m_boxWebView, script.c_str(), executeScriptCallback, this);
58 }
59
60 void PdHelper::finishOpen(Evas_Object* child)
61 {
62     LogD("enter");
63
64     // pd webview set and resize
65     m_pdWebView = child;
66     evas_object_resize(m_pdWebView, m_renderInfo->width, m_renderInfo->height);
67     m_opened = true;
68 }
69
70 void PdHelper::close()
71 {
72     LogD("enter");
73     evas_object_del(m_pdWebView);
74 }
75
76 void PdHelper::setBoxWebView(Evas_Object* webview)
77 {
78     LogD("enter");
79     m_boxWebView = webview;
80 }
81
82 void PdHelper::setPdWebView(Evas_Object* webview)
83 {
84     LogD("enter");
85     m_pdWebView = webview;
86 }
87
88 Evas_Object* PdHelper::getBoxWebView() const
89 {
90     LogD("enter");
91     return m_boxWebView;
92 }
93
94 Evas_Object* PdHelper::getPdWebView() const
95 {
96     LogD("enter");
97     return m_pdWebView;
98 }
99
100 Evas* PdHelper::getPdCanvas() const  
101 {
102     LogD("enter");
103     evas_object_evas_get(m_win);
104 }
105
106 bool PdHelper::isPdOpened() const
107 {
108     LogD("enter");
109     return m_opened;
110 }
111
112 void PdHelper::didExecuteScript(Evas_Object* webview, std::string& result)
113 {
114     LogD("enter");
115     LogD("javascript execution result: %s", result.c_str());
116 }
117
118 std::string PdHelper::validateUrl(std::string& url)
119 {
120     LogD("enter");
121
122     if (url.empty()) {
123         return std::string();
124     }
125
126     if((!url.compare(0, 4, "http")) ||
127             (!url.compare(0, 5, "https")) ||
128             (!url.compare(0, 4, "file")))
129     {
130         return url;
131     }
132
133     std::string newUrl("file://");
134     newUrl += url;
135     return newUrl;
136 }
137
138 void PdHelper::executeScriptCallback(
139         Evas_Object* webview, const char* result, void* data)
140 {
141     LogD("enter");
142
143     PdHelper* This = static_cast<PdHelper*>(data);
144     std::string resultStr(result ? result : "null");
145     This->didExecuteScript(webview, resultStr);
146 }
147