2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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"
25 #include "BoxSchemeHandler.h"
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");
32 static const std::string HTTP_SCHEME("http://");
33 static const std::string HTTPS_SCHEME("https://");
35 // static variable intialization
36 BoxSchemeHandler* BoxSchemeHandler::s_instance = NULL;
38 BoxSchemeHandler::BoxSchemeHandler()
44 BoxSchemeHandler::~BoxSchemeHandler()
49 BoxSchemeHandler* BoxSchemeHandler::Instance()
53 s_instance = new BoxSchemeHandler();
59 void BoxSchemeHandler::registerBox(std::string& instanceId, Box* box)
63 if (getBox(instanceId)) {
64 LogD("already registered");
68 m_boxMap.insert(BoxMapPair(instanceId, box));
71 void BoxSchemeHandler::unregisterBox(std::string& instanceId)
74 m_boxMap.erase(instanceId);
77 bool BoxSchemeHandler::process(std::string& instanceId, std::string& uri)
81 if (!isBoxScheme(uri)) {
85 if (!uri.compare(BOX_SCHEME_RELOAD)) {
86 return handleReload(instanceId);
89 if (!uri.compare(BOX_SCHEME_CHANGE_PERIOD)) {
90 return handleChangePeriod(instanceId);
95 BOX_SCHEME_LAUNCH_BROWSER.size(),
96 BOX_SCHEME_LAUNCH_BROWSER))
98 std::string key("url");
99 std::string url = parse(uri, key);
100 return handleLaunchBrowser(instanceId, url);
103 LogD("unknown box scheme protocol");
107 bool BoxSchemeHandler::isBoxScheme(std::string& uri)
110 if(!uri.compare(0, BOX_SCHEME.size(), BOX_SCHEME)) {
117 Box* BoxSchemeHandler::getBox(std::string& instanceId)
121 auto it = m_boxMap.find(instanceId);
122 if (it != m_boxMap.end()) {
123 LogD("registered: %s (%p)", it->first.c_str(), it->second);
130 bool BoxSchemeHandler::handleReload(std::string& instanceId)
133 Box* box = getBox(instanceId);
135 LogD("unregistered instance");
143 bool BoxSchemeHandler::handleChangePeriod(std::string& instanceId)
146 // TODO show special efl window for this
150 bool BoxSchemeHandler::handleLaunchBrowser(std::string& instanceId, std::string& url)
153 if(!url.compare(0, HTTP_SCHEME.size(), HTTP_SCHEME) ||
154 !url.compare(0, HTTPS_SCHEME.size(), HTTPS_SCHEME))
156 return Service::AppControl::launchBrowser(url);
162 std::string BoxSchemeHandler::parse(std::string& uri, std::string& key)
166 // TODO url parameter SHOULD be parsed using std::regex, not manually
167 std::string value("");
169 unsigned found = uri.find_first_of("?");
170 if (found == std::string::npos) {
175 std::string query = std::string(uri, found + 1);
179 unsigned seperator = query.find_first_of("=", found + 1);
180 if (seperator == std::string::npos) {
181 LogD("no '=' character\n");
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);
193 } while (found && found != std::string::npos);
195 LogD("URL query parsing result: key -> %s, value -> %s", key.c_str(), value.c_str());