2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
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)
23 #include "Service/AppControl.h"
24 #include "Service/PeriodChanger.h"
25 #include "Service/ScrollHolder.h"
26 #include "Service/MessageManager.h"
28 #include "BoxSchemeHandler.h"
30 using namespace Service;
32 static const std::string BOX_SCHEME("box://");
33 static const std::string BOX_SCHEME_RELOAD("box://reload");
34 static const std::string BOX_SCHEME_CHANGE_PERIOD("box://change-period");
35 static const std::string BOX_SCHEME_LAUNCH_BROWSER("box://launch-browser");
36 static const std::string BOX_SCHEME_SCROLL_START("box://scroll-start");
37 static const std::string BOX_SCHEME_SCROLL_STOP("box://scroll-stop");
38 static const std::string BOX_SCHEME_SEND_MESSAGE_TO_PD("box://send-message-to-pd");
39 static const std::string BOX_SCHEME_SEND_MESSAGE_TO_BOX("box://send-message-to-box");
41 static const std::string HTTP_SCHEME("http://");
42 static const std::string HTTPS_SCHEME("https://");
44 // static variable intialization
45 BoxSchemeHandler* BoxSchemeHandler::s_instance = NULL;
47 BoxSchemeHandler::BoxSchemeHandler()
53 BoxSchemeHandler::~BoxSchemeHandler()
58 BoxSchemeHandler* BoxSchemeHandler::Instance()
62 s_instance = new BoxSchemeHandler();
68 void BoxSchemeHandler::registerBox(std::string& instanceId, Box* box)
72 if (getBox(instanceId)) {
73 LogD("already registered");
77 m_boxMap.insert(BoxMapPair(instanceId, box));
80 void BoxSchemeHandler::unregisterBox(std::string& instanceId)
83 m_boxMap.erase(instanceId);
86 bool BoxSchemeHandler::process(std::string& instanceId, std::string& uri)
90 if (!isBoxScheme(uri)) {
94 if (!uri.compare(BOX_SCHEME_RELOAD)) {
95 return handleReload(instanceId);
100 BOX_SCHEME_CHANGE_PERIOD.size(),
101 BOX_SCHEME_CHANGE_PERIOD))
103 std::string key("period");
104 std::string period = parse(uri, key);
105 if (period.empty()) {
106 return handleChangePeriod(instanceId);
109 return handleChangePeriod(instanceId, std::atof(period.c_str()));
114 BOX_SCHEME_LAUNCH_BROWSER.size(),
115 BOX_SCHEME_LAUNCH_BROWSER))
117 std::string key("url");
118 std::string url = parse(uri, key);
119 return handleLaunchBrowser(instanceId, url);
122 if (!uri.compare(BOX_SCHEME_SCROLL_START)) {
123 return handleScroll(instanceId, true);
126 if (!uri.compare(BOX_SCHEME_SCROLL_STOP)) {
127 return handleScroll(instanceId, false);
132 BOX_SCHEME_SEND_MESSAGE_TO_BOX.size(),
133 BOX_SCHEME_SEND_MESSAGE_TO_BOX))
135 std::string key("message");
136 std::string message = parse(uri, key);
137 return handleSendMessage(instanceId, MessageManager::TO_BOX, message);
142 BOX_SCHEME_SEND_MESSAGE_TO_PD.size(),
143 BOX_SCHEME_SEND_MESSAGE_TO_PD))
145 std::string key("message");
146 std::string message = parse(uri, key);
147 return handleSendMessage(instanceId, MessageManager::TO_PD, message);
149 LogD("unknown box scheme protocol");
153 bool BoxSchemeHandler::isBoxScheme(std::string& uri)
156 if(!uri.compare(0, BOX_SCHEME.size(), BOX_SCHEME)) {
163 Box* BoxSchemeHandler::getBox(std::string& instanceId)
167 auto it = m_boxMap.find(instanceId);
168 if (it != m_boxMap.end()) {
169 LogD("registered: %s (%p)", it->first.c_str(), it->second);
176 bool BoxSchemeHandler::handleScroll(std::string& instanceId, bool start)
178 using namespace Service::ScrollHolder;
181 Box* box = getBox(instanceId);
183 LogD("unregistered instance");
187 holdHorizontalScroll(box->m_boxInfo->boxId, instanceId, start);
191 bool BoxSchemeHandler::handleReload(std::string& instanceId)
194 Box* box = getBox(instanceId);
196 LogD("unregistered instance");
204 bool BoxSchemeHandler::handleChangePeriod(std::string& instanceId, float requestedPeriod)
208 Box* box = getBox(instanceId);
210 LogD("no box for update period");
215 Service::PeriodChanger::create(
216 box->m_boxInfo->boxId, instanceId,
217 box->m_boxInfo->period, requestedPeriod);
219 return m_periodChanger->change();
222 bool BoxSchemeHandler::handleLaunchBrowser(std::string& instanceId, std::string& url)
225 if (!url.compare(0, HTTP_SCHEME.size(), HTTP_SCHEME) ||
226 !url.compare(0, HTTPS_SCHEME.size(), HTTPS_SCHEME))
228 return Service::AppControl::launchBrowser(url);
234 bool BoxSchemeHandler::handleSendMessage(
235 std::string& instanceId,
236 MessageManager::ReceiverType receiver,
237 std::string& message)
240 Box* box = getBox(instanceId);
242 LogD("no box for update period");
246 // set webview of receiver
247 Evas_Object* webview;
249 case MessageManager::TO_BOX:
250 webview = box->m_view->getBoxWebView();
252 case MessageManager::TO_PD:
253 webview = box->m_view->getPdWebView();
256 LogD("not supported receiver");
260 return m_messageManager->send(webview, receiver, message);
263 std::string BoxSchemeHandler::parse(std::string& uri, std::string& key)
267 // TODO url parameter SHOULD be parsed using std::regex, not manually
268 std::string value("");
270 unsigned found = uri.find_first_of("?");
271 if (found == std::string::npos) {
276 std::string query = std::string(uri, found + 1);
280 unsigned seperator = query.find_first_of("=", found + 1);
281 if (seperator == std::string::npos) {
282 LogD("no '=' character\n");
286 unsigned next = query.find_first_of("@", found + 1);
287 if (!query.compare(found, key.size(), key)) {
288 LogD("key matched!\n");
289 value = std::string(query, seperator + 1, next - seperator - 1);
294 } while (found && found != std::string::npos);
296 LogD("URL query parsing result: key -> %s, value -> %s", key.c_str(), value.c_str());