[Release] livebox.web-provider-1.2
[platform/framework/web/web-provider.git] / src / Core / BoxSchemeHandler.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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    BoxSchemeHandler.cpp
18  * @author  Yunchan Cho (yunchan.cho@samsung.com)
19  */
20
21 #include <string.h>
22 #include "Box.h"
23 #include "Service/AppControl.h"
24 #include "Util/Log.h"
25 #include "BoxSchemeHandler.h"
26
27 static const std::string BOX_SCHEME("box://");
28 static const std::string BOX_SCHEME_RELOAD("box://reload");
29 static const std::string BOX_SCHEME_CHANGE_PERIOD("box://change-period");
30 static const std::string BOX_SCHEME_LAUNCH_BROWSER("box://launch-browser");
31
32 static const std::string HTTP_SCHEME("http://");
33 static const std::string HTTPS_SCHEME("https://");
34
35 // static variable intialization
36 BoxSchemeHandler* BoxSchemeHandler::s_instance = NULL;
37
38 BoxSchemeHandler::BoxSchemeHandler()
39     : m_boxMap()
40 {
41     LogD("enter");
42 }
43
44 BoxSchemeHandler::~BoxSchemeHandler()
45 {
46     LogD("enter");
47 }
48
49 BoxSchemeHandler* BoxSchemeHandler::Instance()
50 {
51     LogD("enter");
52     if (!s_instance) {
53         s_instance = new BoxSchemeHandler();
54     }
55
56     return s_instance;
57 }
58
59 void BoxSchemeHandler::registerBox(std::string& instanceId, Box* box)
60 {
61     LogD("enter");
62
63     if (getBox(instanceId)) {
64         LogD("already registered");
65         return;
66     }
67
68     m_boxMap.insert(BoxMapPair(instanceId, box));
69 }
70
71 void BoxSchemeHandler::unregisterBox(std::string& instanceId)
72 {
73     LogD("enter");
74     m_boxMap.erase(instanceId);
75 }
76
77 bool BoxSchemeHandler::process(std::string& instanceId, std::string& uri)
78 {
79     LogD("enter");
80
81     if (!isBoxScheme(uri)) {
82         return false;
83     }
84
85     if (!uri.compare(BOX_SCHEME_RELOAD)) {
86        return handleReload(instanceId);
87     }
88
89     if (!uri.compare(BOX_SCHEME_CHANGE_PERIOD)) {
90        return handleChangePeriod(instanceId);
91     }
92
93     if (!uri.compare(
94                 0,
95                 BOX_SCHEME_LAUNCH_BROWSER.size(),
96                 BOX_SCHEME_LAUNCH_BROWSER))
97     {
98         std::string key("url");
99         std::string url = parse(uri, key);
100         return handleLaunchBrowser(instanceId, url);
101     }
102
103     LogD("unknown box scheme protocol");
104     return false;
105 }
106
107 bool BoxSchemeHandler::isBoxScheme(std::string& uri)
108 {
109     LogD("enter");
110     if(!uri.compare(0, BOX_SCHEME.size(), BOX_SCHEME)) {
111         return true;
112     }
113
114     return false;
115 }
116
117 Box* BoxSchemeHandler::getBox(std::string& instanceId)
118 {
119     LogD("enter");
120
121     auto it = m_boxMap.find(instanceId);
122     if (it != m_boxMap.end()) {
123         LogD("registered: %s (%p)", it->first.c_str(), it->second);
124         return it->second;
125     }
126
127     return NULL;
128 }
129
130 bool BoxSchemeHandler::handleReload(std::string& instanceId)
131 {
132     LogD("enter");
133     Box* box = getBox(instanceId);
134     if (!box) {
135         LogD("unregistered instance");
136         return false;
137     }
138
139     box->update();
140     return true;
141 }
142
143 bool BoxSchemeHandler::handleChangePeriod(std::string& instanceId)
144 {
145     LogD("enter");
146     // TODO show special efl window for this
147     return true;
148 }
149
150 bool BoxSchemeHandler::handleLaunchBrowser(std::string& instanceId, std::string& url)
151 {
152     LogD("enter");
153     if(!url.compare(0, HTTP_SCHEME.size(), HTTP_SCHEME) ||
154        !url.compare(0, HTTPS_SCHEME.size(), HTTPS_SCHEME))
155     {
156         return Service::AppControl::launchBrowser(url);
157     }
158
159     return false;
160 }
161
162 std::string BoxSchemeHandler::parse(std::string& uri, std::string& key)
163 {
164     LogD("enter");
165
166     // TODO url parameter SHOULD be parsed using std::regex, not manually
167     std::string value("");
168
169     unsigned found = uri.find_first_of("?");
170     if (found == std::string::npos) {
171         LogD("no query");
172         return value;
173     }
174
175     std::string query = std::string(uri, found + 1);
176     found = 0;
177     do {
178         LogD("enter\n");
179         unsigned seperator = query.find_first_of("=", found + 1);
180         if (seperator == std::string::npos) {
181             LogD("no '=' character\n");
182             break;
183         }
184
185         unsigned next = query.find_first_of("&", found + 1);
186         if (!query.compare(found, key.size(), key)) {
187             LogD("key matched!\n");
188             value = std::string(query, seperator + 1, next - seperator);
189             break;
190         }
191
192         found = next + 1;
193     } while (found && found != std::string::npos);
194
195     LogD("URL query parsing result: key -> %s, value -> %s", key.c_str(), value.c_str());
196     return value;
197 }