2 * Copyright (c) 2011 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 view_logic_password_support.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
19 * @brief Implementation file of PasswordSupport API used by ViewLogic
21 #include "view_logic_password_support.h"
25 #include <dpl/assert.h>
26 #include <dpl/log/log.h>
27 #include <dpl/scoped_ptr.h>
28 #include <dpl/scoped_fclose.h>
29 #include <dpl/string.h>
30 #include <dpl/optional_typedefs.h>
31 #include <dpl/foreach.h>
32 #include <wrt-commons/auto-save-dao-rw/auto_save_dao.h>
33 #include <popup-runner/PopupInvoker.h>
39 namespace ViewModule {
40 namespace PasswordSupport {
42 const char *SCHEME_TYPE_HTTP = "http";
43 const char *SCHEME_TYPE_HTTPS = "https";
44 const char *AUTOSAVEIDPASS_OFF = "OFF";
45 const char *AUTOSAVEIDPASS_ON = "ON";
46 const char *AUTOSAVEIDPASS_ALWAYS_ASK = "ALWAYS_ASK";
47 // This message isn't confirmed by UX guide
48 // It should be change to use IDS for translate
49 const char AUTOSAVE_ASK_MSG[] = "Do you want to remember the password on ";
50 const char AUTOSAVE_ASK_TITLE[] = "Ask to save ID, password";
52 const char * const COMMA = ",";
53 const char * const DOUBLE_QUOTES = "\"";
54 const char * const FILE_PATH_AUTOSAVE_JS = "/usr/share/wrt-engine/AutoSave.js";
55 const char * const FILE_OPTION_READ = "r";
56 const std::string JS_KEY_TAG = "$KEY";
57 const std::string JS_VALUE_TAG = "$VALUE";
58 const unsigned int MIN_SUBMIT_FORM_DATA_SIZE = 0;
60 // Internal declaration
61 Eina_Bool submitFormDataSet(const Eina_Hash* hash,
66 Eina_Bool submitFormDataSet(const Eina_Hash* /*hash*/,
71 using namespace AutoSaveDB;
72 SubmitFormData *submitFormData = static_cast<SubmitFormData*>(fdata);
76 strlen(static_cast<const char *>(key)) == 0 ||
77 strlen(static_cast<char *>(data)) == 0)
79 LogDebug("input data is empty");
83 SubmitFormElement element;
84 element.key = DPL::FromUTF8String(static_cast<const char *>(key));
85 element.value = DPL::FromUTF8String(static_cast<char *>(data));
86 (*submitFormData).push_back(element);
92 void submitClicked(std::string uri, Eina_Hash* data)
94 using namespace AutoSaveDB;
96 LogDebug("submitClicked called");
98 // Temporary set to "always ask"
99 // After implement setting menu and database, it will be used that
100 const char* const autoSaveStatus = AUTOSAVEIDPASS_ALWAYS_ASK;
102 // check setting is always off
103 if (!strcmp(autoSaveStatus, AUTOSAVEIDPASS_OFF)) {
104 LogDebug("AutoSaveStatus is AUTOSAVEIDPASS_ALWAYS_OFF");
108 std::unique_ptr<iri_t> iri(iri_parse(uri.c_str()));
110 LogDebug("Fail to get iri");
114 if (strstr(uri.c_str(), SCHEME_TYPE_HTTP) == uri.c_str() ||
115 strstr(uri.c_str(), SCHEME_TYPE_HTTPS) == uri.c_str())
117 if (NULL == iri->host) {
118 LogDebug("host is invalid");
121 host = DPL::FromASCIIString(std::string(iri->host));
123 if (NULL == iri->path) {
124 LogDebug("path is invalid");
127 // case of local file, store full path
128 host = DPL::FromASCIIString(std::string(iri->path));
131 SubmitFormData submitFormData;
132 eina_hash_foreach(data, submitFormDataSet, &submitFormData);
134 if (MIN_SUBMIT_FORM_DATA_SIZE == submitFormData.size()) {
135 LogDebug("not enough data size " << submitFormData.size());
139 if (!strcmp(autoSaveStatus, AUTOSAVEIDPASS_ON)) {
140 LogDebug("AutoSaveStatus is AUTOSAVEIDPASS_ALWAYS_ON");
141 AutoSaveDAO::setAutoSaveSubmitFormData(host, submitFormData);
142 } else if (!strcmp(autoSaveStatus, AUTOSAVEIDPASS_ALWAYS_ASK)) {
143 LogDebug("AutoSaveStatus is AUTOSAVEIDPASS_ALWAYS_ASK");
145 bool answer = Wrt::Popup::PopupInvoker().askYesNo(AUTOSAVE_ASK_TITLE,
148 LogDebug("save data");
149 AutoSaveDAO::setAutoSaveSubmitFormData(host, submitFormData);
155 DPL::Optional<DPL::String> jsForAutoFillData(const char *uri)
157 using namespace AutoSaveDB;
159 LogDebug("jsForAutoFillData called");
162 // check uri is invalid
163 std::unique_ptr<iri_t> iri(iri_parse(uri));
165 LogDebug("Fail to get iri");
166 return DPL::OptionalString::Null;
169 if (strstr(uri, SCHEME_TYPE_HTTP) == uri ||
170 strstr(uri, SCHEME_TYPE_HTTPS) == uri)
172 if (NULL == iri->host) {
173 LogDebug("host is invalid");
174 return DPL::OptionalString::Null;
176 host = DPL::FromASCIIString(std::string(iri->host));
178 if (NULL == iri->path) {
179 LogDebug("path is invalid");
180 return DPL::OptionalString::Null;
182 // case of local file, compare fullpath
183 host = DPL::FromASCIIString(std::string(iri->path));
185 SubmitFormData submitData =
186 AutoSaveDAOReadOnly::getAutoSaveSubmitFormData(host);
188 if (submitData.empty()) {
189 LogDebug("no matching data");
190 return DPL::OptionalString::Null;
193 std::ostringstream keyOstring;
194 std::ostringstream valueOstring;
197 FOREACH(iterator, submitData) {
198 keyOstring << DOUBLE_QUOTES << iterator->key << DOUBLE_QUOTES;
199 valueOstring << DOUBLE_QUOTES << iterator->value << DOUBLE_QUOTES;
200 if (*iterator != submitData.back()) {
202 valueOstring << COMMA;
206 DPL::ScopedFClose fd;
207 fd.Reset(fopen(FILE_PATH_AUTOSAVE_JS, FILE_OPTION_READ));
209 LogError("Fail to open");
210 return DPL::OptionalString::Null;
213 fseek(fd.Get(), 0, SEEK_END);
214 long size = ftell(fd.Get());
215 fseek(fd.Get(), 0, SEEK_SET);
218 LogError("Size error: " << size);
219 return DPL::OptionalString::Null;
221 char* data = new char[size + 1];
223 memset(data, 0, size + 1);
224 size_t ret = fread(data, 1, size, fd.Get());
225 if (static_cast<long>(ret) != size) {
226 LogError("Read size is mismatched");
228 return DPL::OptionalString::Null;
231 std::string dataStr = data;
233 // replace $VALUE, $KEY to data from database
234 std::ostringstream jsOstring;
235 jsOstring << dataStr;
236 std::string jsStr = jsOstring.str();
237 jsStr.replace(jsStr.find(JS_KEY_TAG),
240 jsStr.replace(jsStr.find(JS_VALUE_TAG),
241 JS_VALUE_TAG.length(),
244 return DPL::FromUTF8String(jsStr);
246 } // namespaec PasswordSupport
247 } // namespaec ViewModule