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 {
43 const char *SCHEME_TYPE_HTTP = "http";
44 const char *SCHEME_TYPE_HTTPS = "https";
45 const char *AUTOSAVEIDPASS_OFF = "OFF";
46 const char *AUTOSAVEIDPASS_ON = "ON";
47 const char *AUTOSAVEIDPASS_ALWAYS_ASK = "ALWAYS_ASK";
48 // This message isn't confirmed by UX guide
49 // It should be change to use IDS for translate
50 const char AUTOSAVE_ASK_MSG[] = "Do you want to remember the password on ";
51 const char AUTOSAVE_ASK_TITLE[] = "Ask to save ID, password";
53 const char * const COMMA = ",";
54 const char * const DOUBLE_QUOTES = "\"";
55 const char * const FILE_PATH_AUTOSAVE_JS = "/usr/share/wrt-engine/AutoSave.js";
56 const char * const FILE_OPTION_READ = "r";
57 const std::string JS_KEY_TAG = "$KEY";
58 const std::string JS_VALUE_TAG = "$VALUE";
59 const unsigned int MIN_SUBMIT_FORM_DATA_SIZE = 0;
61 // Internal declaration
62 Eina_Bool submitFormDataSet(const Eina_Hash* hash,
67 Eina_Bool submitFormDataSet(const Eina_Hash* /*hash*/,
72 using namespace AutoSaveDB;
73 SubmitFormData *submitFormData = static_cast<SubmitFormData*>(fdata);
77 strlen(static_cast<const char *>(key)) == 0 ||
78 strlen(static_cast<char *>(data)) == 0)
80 LogDebug("input data is empty");
84 SubmitFormElement element;
85 element.key = DPL::FromUTF8String(static_cast<const char *>(key));
86 element.value = DPL::FromUTF8String(static_cast<char *>(data));
87 (*submitFormData).push_back(element);
94 void submitClicked(std::string uri, Eina_Hash* data)
96 using namespace AutoSaveDB;
98 LogDebug("submitClicked called");
100 // Temporary set to "always ask"
101 // After implement setting menu and database, it will be used that
102 const char* const autoSaveStatus = AUTOSAVEIDPASS_ALWAYS_ASK;
104 // check setting is always off
105 if (!strcmp(autoSaveStatus, AUTOSAVEIDPASS_OFF)) {
106 LogDebug("AutoSaveStatus is AUTOSAVEIDPASS_ALWAYS_OFF");
110 std::unique_ptr<iri_t> iri(iri_parse(uri.c_str()));
112 LogDebug("Fail to get iri");
116 if (strstr(uri.c_str(), SCHEME_TYPE_HTTP) == uri.c_str() ||
117 strstr(uri.c_str(), SCHEME_TYPE_HTTPS) == uri.c_str())
119 if (NULL == iri->host) {
120 LogDebug("host is invalid");
123 host = DPL::FromASCIIString(std::string(iri->host));
125 if (NULL == iri->path) {
126 LogDebug("path is invalid");
129 // case of local file, store full path
130 host = DPL::FromASCIIString(std::string(iri->path));
133 SubmitFormData submitFormData;
134 eina_hash_foreach(data, submitFormDataSet, &submitFormData);
136 if (MIN_SUBMIT_FORM_DATA_SIZE == submitFormData.size()) {
137 LogDebug("not enough data size " << submitFormData.size());
141 if (!strcmp(autoSaveStatus, AUTOSAVEIDPASS_ON)) {
142 LogDebug("AutoSaveStatus is AUTOSAVEIDPASS_ALWAYS_ON");
143 AutoSaveDAO::setAutoSaveSubmitFormData(host, submitFormData);
144 } else if (!strcmp(autoSaveStatus, AUTOSAVEIDPASS_ALWAYS_ASK)) {
145 LogDebug("AutoSaveStatus is AUTOSAVEIDPASS_ALWAYS_ASK");
147 bool answer = Wrt::Popup::PopupInvoker().askYesNo(AUTOSAVE_ASK_TITLE,
150 LogDebug("save data");
151 AutoSaveDAO::setAutoSaveSubmitFormData(host, submitFormData);
157 DPL::Optional<DPL::String> jsForAutoFillData(const char *uri)
159 using namespace AutoSaveDB;
161 LogDebug("jsForAutoFillData called");
164 // check uri is invalid
165 std::unique_ptr<iri_t> iri(iri_parse(uri));
167 LogDebug("Fail to get iri");
168 return DPL::OptionalString::Null;
171 if (strstr(uri, SCHEME_TYPE_HTTP) == uri ||
172 strstr(uri, SCHEME_TYPE_HTTPS) == uri)
174 if (NULL == iri->host) {
175 LogDebug("host is invalid");
176 return DPL::OptionalString::Null;
178 host = DPL::FromASCIIString(std::string(iri->host));
180 if (NULL == iri->path) {
181 LogDebug("path is invalid");
182 return DPL::OptionalString::Null;
184 // case of local file, compare fullpath
185 host = DPL::FromASCIIString(std::string(iri->path));
187 SubmitFormData submitData =
188 AutoSaveDAOReadOnly::getAutoSaveSubmitFormData(host);
190 if (submitData.empty()) {
191 LogDebug("no matching data");
192 return DPL::OptionalString::Null;
195 std::ostringstream keyOstring;
196 std::ostringstream valueOstring;
199 FOREACH(iterator, submitData) {
200 keyOstring << DOUBLE_QUOTES << iterator->key << DOUBLE_QUOTES;
201 valueOstring << DOUBLE_QUOTES << iterator->value << DOUBLE_QUOTES;
202 if (*iterator != submitData.back()) {
204 valueOstring << COMMA;
208 DPL::ScopedFClose fd;
209 fd.Reset(fopen(FILE_PATH_AUTOSAVE_JS, FILE_OPTION_READ));
211 LogError("Fail to open");
212 return DPL::OptionalString::Null;
215 fseek(fd.Get(), 0, SEEK_END);
216 long size = ftell(fd.Get());
217 fseek(fd.Get(), 0, SEEK_SET);
220 LogError("Size error: " << size);
221 return DPL::OptionalString::Null;
223 char* data = new char[size + 1];
225 memset(data, 0, size + 1);
226 size_t ret = fread(data, 1, size, fd.Get());
227 if (static_cast<long>(ret) != size) {
228 LogError("Read size is mismatched");
230 return DPL::OptionalString::Null;
233 std::string dataStr = data;
235 // replace $VALUE, $KEY to data from database
236 std::ostringstream jsOstring;
237 jsOstring << dataStr;
238 std::string jsStr = jsOstring.str();
239 jsStr.replace(jsStr.find(JS_KEY_TAG),
242 jsStr.replace(jsStr.find(JS_VALUE_TAG),
243 JS_VALUE_TAG.length(),
246 return DPL::FromUTF8String(jsStr);
249 } // namespaec PasswordSupport
250 } // namespaec ViewModule