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)
23 #include "Service/AppControl.h"
24 #include "Service/PeriodChanger.h"
25 #include "Service/ScrollHolder.h"
27 #include "BoxSchemeHandler.h"
29 static const std::string BOX_SCHEME("box://");
30 static const std::string BOX_SCHEME_RELOAD("box://reload");
31 static const std::string BOX_SCHEME_CHANGE_PERIOD("box://change-period");
32 static const std::string BOX_SCHEME_LAUNCH_BROWSER("box://launch-browser");
33 static const std::string BOX_SCHEME_SCROLL_START("box://scroll-start");
34 static const std::string BOX_SCHEME_SCROLL_STOP("box://scroll-stop");
36 static const std::string HTTP_SCHEME("http://");
37 static const std::string HTTPS_SCHEME("https://");
39 // static variable intialization
40 BoxSchemeHandler* BoxSchemeHandler::s_instance = NULL;
42 BoxSchemeHandler::BoxSchemeHandler()
48 BoxSchemeHandler::~BoxSchemeHandler()
53 BoxSchemeHandler* BoxSchemeHandler::Instance()
57 s_instance = new BoxSchemeHandler();
63 void BoxSchemeHandler::registerBox(std::string& instanceId, Box* box)
67 if (getBox(instanceId)) {
68 LogD("already registered");
72 m_boxMap.insert(BoxMapPair(instanceId, box));
75 void BoxSchemeHandler::unregisterBox(std::string& instanceId)
78 m_boxMap.erase(instanceId);
81 bool BoxSchemeHandler::process(std::string& instanceId, std::string& uri)
85 if (!isBoxScheme(uri)) {
89 if (!uri.compare(BOX_SCHEME_RELOAD)) {
90 return handleReload(instanceId);
95 BOX_SCHEME_CHANGE_PERIOD.size(),
96 BOX_SCHEME_CHANGE_PERIOD))
98 std::string key("period");
99 std::string period = parse(uri, key);
100 if (period.empty()) {
101 return handleChangePeriod(instanceId);
104 return handleChangePeriod(instanceId, std::atof(period.c_str()));
109 BOX_SCHEME_LAUNCH_BROWSER.size(),
110 BOX_SCHEME_LAUNCH_BROWSER))
112 std::string key("url");
113 std::string url = parse(uri, key);
114 return handleLaunchBrowser(instanceId, url);
117 if (!uri.compare(BOX_SCHEME_SCROLL_START)) {
118 return handleScroll(instanceId, true);
121 if (!uri.compare(BOX_SCHEME_SCROLL_STOP)) {
122 return handleScroll(instanceId, false);
125 LogD("unknown box scheme protocol");
129 bool BoxSchemeHandler::isBoxScheme(std::string& uri)
132 if(!uri.compare(0, BOX_SCHEME.size(), BOX_SCHEME)) {
139 Box* BoxSchemeHandler::getBox(std::string& instanceId)
143 auto it = m_boxMap.find(instanceId);
144 if (it != m_boxMap.end()) {
145 LogD("registered: %s (%p)", it->first.c_str(), it->second);
152 bool BoxSchemeHandler::handleScroll(std::string& instanceId, bool start)
154 using namespace Service::ScrollHolder;
157 Box* box = getBox(instanceId);
159 LogD("unregistered instance");
163 holdHorizontalScroll(box->m_boxInfo->boxId, instanceId, start);
167 bool BoxSchemeHandler::handleReload(std::string& instanceId)
170 Box* box = getBox(instanceId);
172 LogD("unregistered instance");
180 bool BoxSchemeHandler::handleChangePeriod(std::string& instanceId, float requestedPeriod)
184 Box* box = getBox(instanceId);
186 LogD("no box for update period");
191 Service::PeriodChanger::create(
192 box->m_boxInfo->boxId, instanceId,
193 box->m_boxInfo->period, requestedPeriod);
195 return m_periodChanger->change();
198 bool BoxSchemeHandler::handleLaunchBrowser(std::string& instanceId, std::string& url)
201 if(!url.compare(0, HTTP_SCHEME.size(), HTTP_SCHEME) ||
202 !url.compare(0, HTTPS_SCHEME.size(), HTTPS_SCHEME))
204 return Service::AppControl::launchBrowser(url);
210 std::string BoxSchemeHandler::parse(std::string& uri, std::string& key)
214 // TODO url parameter SHOULD be parsed using std::regex, not manually
215 std::string value("");
217 unsigned found = uri.find_first_of("?");
218 if (found == std::string::npos) {
223 std::string query = std::string(uri, found + 1);
227 unsigned seperator = query.find_first_of("=", found + 1);
228 if (seperator == std::string::npos) {
229 LogD("no '=' character\n");
233 unsigned next = query.find_first_of("@", found + 1);
234 if (!query.compare(found, key.size(), key)) {
235 LogD("key matched!\n");
236 value = std::string(query, seperator + 1, next - seperator - 1);
241 } while (found && found != std::string::npos);
243 LogD("URL query parsing result: key -> %s, value -> %s", key.c_str(), value.c_str());