2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://floralicense.org/license/
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.
17 * @file BoxSchemeHandler.cpp
18 * @author Yunchan Cho (yunchan.cho@samsung.com)
24 #include "Service/AppControl.h"
25 #include "Service/PeriodChanger.h"
26 #include "Service/ScrollHolder.h"
27 #include "Service/MessageManager.h"
29 #include "Util/Util.h"
30 #include "BoxSchemeHandler.h"
32 using namespace Service;
34 static const std::string BOX_SCHEME("box://");
35 static const std::string BOX_SCHEME_RELOAD("box://reload");
36 static const std::string BOX_SCHEME_CHANGE_PERIOD("box://change-period");
37 static const std::string BOX_SCHEME_LAUNCH_BROWSER("box://launch-browser");
38 static const std::string BOX_SCHEME_SCROLL_START("box://scroll-start");
39 static const std::string BOX_SCHEME_SCROLL_STOP("box://scroll-stop");
40 static const std::string BOX_SCHEME_SEND_MESSAGE_TO_PD("box://send-message-to-pd");
41 static const std::string BOX_SCHEME_SEND_MESSAGE_TO_BOX("box://send-message-to-box");
43 static const std::string HTTP_SCHEME("http://");
44 static const std::string HTTPS_SCHEME("https://");
46 // static variable intialization
47 BoxSchemeHandler* BoxSchemeHandler::s_instance = NULL;
49 BoxSchemeHandler::BoxSchemeHandler()
55 BoxSchemeHandler::~BoxSchemeHandler()
60 BoxSchemeHandler* BoxSchemeHandler::Instance()
64 s_instance = new BoxSchemeHandler();
70 void BoxSchemeHandler::registerBox(std::string& instanceId, Box* box)
74 if (getBox(instanceId)) {
75 LogD("already registered");
79 m_boxMap.insert(BoxMapPair(instanceId, box));
82 void BoxSchemeHandler::unregisterBox(std::string& instanceId)
85 m_boxMap.erase(instanceId);
88 bool BoxSchemeHandler::process(std::string& instanceId, std::string& uri)
92 if (!isBoxScheme(uri)) {
96 if (!uri.compare(BOX_SCHEME_RELOAD)) {
97 return handleReload(instanceId);
102 BOX_SCHEME_CHANGE_PERIOD.size(),
103 BOX_SCHEME_CHANGE_PERIOD))
105 std::string key("period");
106 std::string period = parse(uri, key);
107 if (period.empty()) {
108 return handleChangePeriod(instanceId);
111 return handleChangePeriod(instanceId, std::atof(period.c_str()));
116 BOX_SCHEME_LAUNCH_BROWSER.size(),
117 BOX_SCHEME_LAUNCH_BROWSER))
119 std::string key("url");
120 std::string url = parse(uri, key);
121 return handleLaunchBrowser(instanceId, url);
124 if (!uri.compare(BOX_SCHEME_SCROLL_START)) {
125 return handleScroll(instanceId, true);
128 if (!uri.compare(BOX_SCHEME_SCROLL_STOP)) {
129 return handleScroll(instanceId, false);
134 BOX_SCHEME_SEND_MESSAGE_TO_BOX.size(),
135 BOX_SCHEME_SEND_MESSAGE_TO_BOX))
137 std::string key("message");
138 std::string message = parse(uri, key);
139 return handleSendMessage(instanceId, MessageManager::TO_BOX, message);
144 BOX_SCHEME_SEND_MESSAGE_TO_PD.size(),
145 BOX_SCHEME_SEND_MESSAGE_TO_PD))
147 std::string key("message");
148 std::string message = parse(uri, key);
149 return handleSendMessage(instanceId, MessageManager::TO_PD, message);
151 LogD("unknown box scheme protocol");
155 bool BoxSchemeHandler::isBoxScheme(std::string& uri)
158 if(!uri.compare(0, BOX_SCHEME.size(), BOX_SCHEME)) {
165 Box* BoxSchemeHandler::getBox(std::string& instanceId)
169 auto it = m_boxMap.find(instanceId);
170 if (it != m_boxMap.end()) {
171 LogD("registered: %s (%p)", it->first.c_str(), it->second);
178 bool BoxSchemeHandler::handleScroll(std::string& instanceId, bool start)
180 using namespace Service::ScrollHolder;
183 Box* box = getBox(instanceId);
185 LogD("unregistered instance");
189 holdHorizontalScroll(box->m_boxInfo->boxId, instanceId, start);
193 bool BoxSchemeHandler::handleReload(std::string& instanceId)
196 Box* box = getBox(instanceId);
198 LogD("unregistered instance");
202 // In the future, new content info can be set by caller
203 box->updateInternal();
207 bool BoxSchemeHandler::handleChangePeriod(std::string& instanceId, float requestedPeriod)
211 Box* box = getBox(instanceId);
213 LogD("no box for update period");
218 Service::PeriodChanger::create(
219 box->m_boxInfo->boxId, instanceId,
220 box->m_boxInfo->period, requestedPeriod);
222 return m_periodChanger->change();
225 bool BoxSchemeHandler::handleLaunchBrowser(std::string& instanceId, std::string& url)
228 UNUSED_PARAM(instanceId);
230 if (!url.compare(0, HTTP_SCHEME.size(), HTTP_SCHEME) ||
231 !url.compare(0, HTTPS_SCHEME.size(), HTTPS_SCHEME))
233 return Service::AppControl::launchBrowser(url);
239 bool BoxSchemeHandler::handleSendMessage(
240 std::string& instanceId,
241 MessageManager::ReceiverType receiver,
242 std::string& message)
245 Box* box = getBox(instanceId);
247 LogD("no box for update period");
251 // set webview of receiver
252 Evas_Object* webview;
254 case MessageManager::TO_BOX:
255 webview = box->m_view->getBoxWebView();
257 case MessageManager::TO_PD:
258 webview = box->m_view->getPdWebView();
261 LogD("not supported receiver");
265 return m_messageManager->send(webview, receiver, message);
268 std::string BoxSchemeHandler::parse(std::string& uri, std::string& key)
272 // TODO url parameter SHOULD be parsed using std::regex, not manually
273 std::string value("");
275 unsigned found = uri.find_first_of("?");
276 if (found == std::string::npos) {
281 std::string query = std::string(uri, found + 1);
285 unsigned seperator = query.find_first_of("=", found + 1);
286 if (seperator == std::string::npos) {
287 LogD("no '=' character\n");
291 unsigned next = query.find_first_of("@", found + 1);
292 if (!query.compare(found, key.size(), key)) {
293 LogD("key matched!\n");
294 value = std::string(query, seperator + 1, next - seperator - 1);
299 } while (found && found != std::string::npos);
301 LogD("URL query parsing result: key -> %s, value -> %s", key.c_str(), value.c_str());