When wrt reset the page uri path was wrong whith double prefix uri.
[platform/framework/web/wrt.git] / src / view / common / view_logic_uri_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    view_logic_uri_support.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @brief   Implementation file of UriSupport API used by ViewLogic
20  */
21
22 #include "view_logic_uri_support.h"
23 #include <dpl/string.h>
24 #include <dpl/log/log.h>
25 #include <dpl/localization/w3c_file_localization.h>
26 //#include <dpl/wrt-dao-ro/common_dao_types.h>
27 #include <widget_model.h>
28 #include <bundle.h>
29 #include <appsvc.h>
30 #include <application_data.h>
31
32 namespace ViewModule {
33 namespace UriSupport {
34
35 namespace {
36 char const * const SCHEME_TYPE_FILE = "file";
37 char const * const SCHEME_TYPE_WIDGET = "widget";
38 }
39
40 std::string getAppServiceUri(bundle *bundle, WidgetModel *widgetModel)
41 {
42     if(!bundle)
43     {
44         LogError("Bundle is empty");
45         return std::string("");
46     }
47
48     const char* value = NULL;
49     value = appsvc_get_operation(bundle);
50     std::string operation = value ? value : "";
51     value = appsvc_get_uri(bundle);
52     std::string scheme = value ? value : "";
53     value = appsvc_get_mime(bundle);
54     std::string mimeType = value ? value : "";
55
56     // get scheme type from scheme uri
57     std::string schemeType = "";
58     if (!scheme.empty())
59     {
60         const char *end = strstr(scheme.c_str(), ":");
61         if(!end)
62         {
63             LogError("Lack of scheme");
64         } else {
65             std::string schemeTmp(scheme.c_str(), end);
66             schemeType = schemeTmp;
67         }
68     }
69
70     LogDebug("operation : " << operation);
71     LogDebug("schemeType : " << schemeType);
72     LogDebug("mimetype : " << mimeType);
73
74     WidgetApplicationServiceList appServiceList =
75         widgetModel->AppServiceList.Get();
76     FOREACH(appServiceIt, appServiceList) {
77         if (DPL::ToUTF8String(appServiceIt->operation) == operation &&
78             (DPL::ToUTF8String(appServiceIt->scheme) == schemeType ||
79              DPL::ToUTF8String(appServiceIt->scheme) == "*/*") &&
80             (DPL::ToUTF8String(appServiceIt->mime) == mimeType ||
81              DPL::ToUTF8String(appServiceIt->mime) == "*/*"))
82         {
83             // ignore default operation that is reserved by system
84             if (operation == APPSVC_OPERATION_DEFAULT) {
85                 return std::string("");
86             }
87             return DPL::ToUTF8String(appServiceIt->src);
88         }
89     }
90     LogDebug("no matching result");
91     return std::string("");
92 }
93
94 std::string getUri(WidgetModel *widgetModel, const std::string &defaultUri)
95 {
96     DPL::String uri;
97     std::string startUri;
98
99     bundle *originBundle = ApplicationDataSingleton::Instance().getBundle();
100     // search application service
101     startUri = getAppServiceUri(originBundle, widgetModel).c_str();
102     LogInfo("application service start uri is " << startUri);
103     if (startUri == "") {
104         LogInfo("application service doesn't have matched data");
105         startUri = defaultUri;
106     } else {
107         // create encoded bundle data only for uri for application service
108         ApplicationDataSingleton::Instance().setEncodedBundle(originBundle);
109     }
110
111     // insert prefix path
112     std::string preFix = DPL::ToUTF8String(widgetModel->PrefixURL.Get());
113
114     if (strstr(startUri.c_str(), "http") == startUri.c_str() ||
115             strstr( startUri.c_str(), preFix.c_str()) == startUri.c_str()) {
116         uri = DPL::FromUTF8String(startUri);
117     } else {
118         uri = widgetModel->PrefixURL.Get() +
119         DPL::FromUTF8String(startUri);
120     }
121     return DPL::ToUTF8String(uri).c_str();
122 }
123
124 DPL::OptionalString localizeURI(const DPL::String& inputURI,
125                                 const WidgetModel* model)
126 {
127     auto uri = DPL::ToUTF8String(inputURI);
128     LogDebug("localizing url: " << uri);
129
130     auto urlcstr = uri.c_str();
131
132     const char *end = strstr(urlcstr, ":");
133     if (!end) {
134         LogDebug("no schema in link, return null");
135         // lack of schema
136         return DPL::Optional<DPL::String>::Null;
137     }
138
139     std::string scheme(urlcstr, end);
140     if (scheme != SCHEME_TYPE_WIDGET && scheme != SCHEME_TYPE_FILE) {
141         LogDebug("scheme doesn't need to localize");
142         return DPL::OptionalString(inputURI);
143     }
144
145     DPL::Optional<DPL::String> found =
146         W3CFileLocalization::getFilePathInWidgetPackageFromUrl(
147             model->Handle.Get(),
148             model->LanguageTags.Get(),
149             DPL::FromUTF8String(uri));
150
151     if (found.IsNull()) {
152         // In this case, path doesn't need to localize. return input uri
153         LogDebug("Path not found within current locale in current widget");
154         return DPL::OptionalString(inputURI);
155     } else {
156         DPL::String uri(L"file://" + *found);
157         LogDebug("Will load resource: " << uri);
158         LogDebug("finished");
159         return DPL::OptionalString(uri);
160     }
161 }
162
163 } // namespace UriSupportImplementation
164 } // namespace ViewModule