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 "BoxSchemeHandler.h"
31 using namespace Service;
33 static const std::string BOX_SCHEME("box://");
34 static const std::string BOX_SCHEME_RELOAD("box://reload");
35 static const std::string BOX_SCHEME_CHANGE_PERIOD("box://change-period");
36 static const std::string BOX_SCHEME_LAUNCH_BROWSER("box://launch-browser");
37 static const std::string BOX_SCHEME_SCROLL_START("box://scroll-start");
38 static const std::string BOX_SCHEME_SCROLL_STOP("box://scroll-stop");
39 static const std::string BOX_SCHEME_SEND_MESSAGE_TO_PD("box://send-message-to-pd");
40 static const std::string BOX_SCHEME_SEND_MESSAGE_TO_BOX("box://send-message-to-box");
42 static const std::string HTTP_SCHEME("http://");
43 static const std::string HTTPS_SCHEME("https://");
45 // static variable intialization
46 BoxSchemeHandler* BoxSchemeHandler::s_instance = NULL;
48 BoxSchemeHandler::BoxSchemeHandler()
54 BoxSchemeHandler::~BoxSchemeHandler()
59 BoxSchemeHandler* BoxSchemeHandler::Instance()
63 s_instance = new BoxSchemeHandler();
69 void BoxSchemeHandler::registerBox(std::string& instanceId, Box* box)
73 if (getBox(instanceId)) {
74 LogD("already registered");
78 m_boxMap.insert(BoxMapPair(instanceId, box));
81 void BoxSchemeHandler::unregisterBox(std::string& instanceId)
84 m_boxMap.erase(instanceId);
87 bool BoxSchemeHandler::process(std::string& instanceId, std::string& uri)
91 if (!isBoxScheme(uri)) {
95 if (!uri.compare(BOX_SCHEME_RELOAD)) {
96 return handleReload(instanceId);
101 BOX_SCHEME_CHANGE_PERIOD.size(),
102 BOX_SCHEME_CHANGE_PERIOD))
104 std::string key("period");
105 std::string period = parse(uri, key);
106 if (period.empty()) {
107 return handleChangePeriod(instanceId);
110 return handleChangePeriod(instanceId, std::atof(period.c_str()));
115 BOX_SCHEME_LAUNCH_BROWSER.size(),
116 BOX_SCHEME_LAUNCH_BROWSER))
118 std::string key("url");
119 std::string url = parse(uri, key);
120 return handleLaunchBrowser(instanceId, url);
123 if (!uri.compare(BOX_SCHEME_SCROLL_START)) {
124 return handleScroll(instanceId, true);
127 if (!uri.compare(BOX_SCHEME_SCROLL_STOP)) {
128 return handleScroll(instanceId, false);
133 BOX_SCHEME_SEND_MESSAGE_TO_BOX.size(),
134 BOX_SCHEME_SEND_MESSAGE_TO_BOX))
136 std::string key("message");
137 std::string message = parse(uri, key);
138 return handleSendMessage(instanceId, MessageManager::TO_BOX, message);
143 BOX_SCHEME_SEND_MESSAGE_TO_PD.size(),
144 BOX_SCHEME_SEND_MESSAGE_TO_PD))
146 std::string key("message");
147 std::string message = parse(uri, key);
148 return handleSendMessage(instanceId, MessageManager::TO_PD, message);
150 LogD("unknown box scheme protocol");
154 bool BoxSchemeHandler::isBoxScheme(std::string& uri)
157 if(!uri.compare(0, BOX_SCHEME.size(), BOX_SCHEME)) {
164 Box* BoxSchemeHandler::getBox(std::string& instanceId)
168 auto it = m_boxMap.find(instanceId);
169 if (it != m_boxMap.end()) {
170 LogD("registered: %s (%p)", it->first.c_str(), it->second);
177 bool BoxSchemeHandler::handleScroll(std::string& instanceId, bool start)
179 using namespace Service::ScrollHolder;
182 Box* box = getBox(instanceId);
184 LogD("unregistered instance");
188 holdHorizontalScroll(box->m_boxInfo->boxId, instanceId, start);
192 bool BoxSchemeHandler::handleReload(std::string& instanceId)
195 Box* box = getBox(instanceId);
197 LogD("unregistered instance");
201 // In the future, new content info can be set by caller
202 box->updateInternal();
206 bool BoxSchemeHandler::handleChangePeriod(std::string& instanceId, float requestedPeriod)
210 Box* box = getBox(instanceId);
212 LogD("no box for update period");
217 Service::PeriodChanger::create(
218 box->m_boxInfo->boxId, instanceId,
219 box->m_boxInfo->period, requestedPeriod);
221 return m_periodChanger->change();
224 bool BoxSchemeHandler::handleLaunchBrowser(std::string& instanceId, std::string& url)
227 if (!url.compare(0, HTTP_SCHEME.size(), HTTP_SCHEME) ||
228 !url.compare(0, HTTPS_SCHEME.size(), HTTPS_SCHEME))
230 return Service::AppControl::launchBrowser(url);
236 bool BoxSchemeHandler::handleSendMessage(
237 std::string& instanceId,
238 MessageManager::ReceiverType receiver,
239 std::string& message)
242 Box* box = getBox(instanceId);
244 LogD("no box for update period");
248 // set webview of receiver
249 Evas_Object* webview;
251 case MessageManager::TO_BOX:
252 webview = box->m_view->getBoxWebView();
254 case MessageManager::TO_PD:
255 webview = box->m_view->getPdWebView();
258 LogD("not supported receiver");
262 return m_messageManager->send(webview, receiver, message);
265 std::string BoxSchemeHandler::parse(std::string& uri, std::string& key)
269 // TODO url parameter SHOULD be parsed using std::regex, not manually
270 std::string value("");
272 unsigned found = uri.find_first_of("?");
273 if (found == std::string::npos) {
278 std::string query = std::string(uri, found + 1);
282 unsigned seperator = query.find_first_of("=", found + 1);
283 if (seperator == std::string::npos) {
284 LogD("no '=' character\n");
288 unsigned next = query.find_first_of("@", found + 1);
289 if (!query.compare(found, key.size(), key)) {
290 LogD("key matched!\n");
291 value = std::string(query, seperator + 1, next - seperator - 1);
296 } while (found && found != std::string::npos);
298 LogD("URL query parsing result: key -> %s, value -> %s", key.c_str(), value.c_str());