|
| 1 | +// Copyright 2023 Samsung Electronics Co., Ltd. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "flutter/shell/platform/tizen/tizen_autofill.h" |
| 6 | + |
| 7 | +#include <app_common.h> |
| 8 | +#include <autofill_common.h> |
| 9 | + |
| 10 | +#include <functional> |
| 11 | +#include <memory> |
| 12 | + |
| 13 | +#include "flutter/shell/platform/tizen/logger.h" |
| 14 | + |
| 15 | +TizenAutofill::TizenAutofill() { |
| 16 | + InitailizeAutofill(); |
| 17 | +} |
| 18 | + |
| 19 | +TizenAutofill::~TizenAutofill() { |
| 20 | + autofill_destroy(ah_); |
| 21 | +} |
| 22 | + |
| 23 | +void TizenAutofill::InitailizeAutofill() { |
| 24 | + autofill_create(&ah_); |
| 25 | + |
| 26 | + int ret; |
| 27 | + ret = autofill_connect( |
| 28 | + ah_, |
| 29 | + [](autofill_h ah, autofill_connection_status_e status, void* user_data) { |
| 30 | + }, |
| 31 | + NULL); |
| 32 | + if (ret != AUTOFILL_ERROR_NONE) { |
| 33 | + FT_LOG(Error) << __FUNCTION__ << "connect_autofill_daemon error"; |
| 34 | + } |
| 35 | + |
| 36 | + autofill_fill_response_set_received_cb( |
| 37 | + ah_, |
| 38 | + [](autofill_h ah, autofill_fill_response_h fill_response, void* data) { |
| 39 | + int count = 0; |
| 40 | + autofill_fill_response_get_group_count(fill_response, &count); |
| 41 | + autofill_fill_response_foreach_group( |
| 42 | + fill_response, |
| 43 | + [](autofill_fill_response_group_h group_h, void* user_data) { |
| 44 | + autofill_fill_response_group_foreach_item( |
| 45 | + group_h, |
| 46 | + [](autofill_fill_response_item_h item, void* user_data) { |
| 47 | + char* id = nullptr; |
| 48 | + char* value = nullptr; |
| 49 | + char* presentation_text = nullptr; |
| 50 | + |
| 51 | + autofill_fill_response_item_get_id(item, &id); |
| 52 | + autofill_fill_response_item_get_presentation_text( |
| 53 | + item, &presentation_text); |
| 54 | + autofill_fill_response_item_get_value(item, &value); |
| 55 | + |
| 56 | + std::unique_ptr<AutofillItem> response_item = |
| 57 | + std::make_unique<AutofillItem>(); |
| 58 | + response_item->label_ = std::string(presentation_text); |
| 59 | + response_item->id_ = std::string(id); |
| 60 | + response_item->value_ = std::string(value); |
| 61 | + |
| 62 | + TizenAutofill::GetInstance().StoreResponseItem( |
| 63 | + move(response_item)); |
| 64 | + |
| 65 | + if (id) { |
| 66 | + free(id); |
| 67 | + } |
| 68 | + |
| 69 | + if (value) { |
| 70 | + free(value); |
| 71 | + } |
| 72 | + |
| 73 | + if (presentation_text) { |
| 74 | + free(presentation_text); |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + }, |
| 79 | + group_h); |
| 80 | + return true; |
| 81 | + }, |
| 82 | + &count); |
| 83 | + TizenAutofill::GetInstance().CallPopupCallback(); |
| 84 | + }, |
| 85 | + nullptr); |
| 86 | + |
| 87 | + response_items_.clear(); |
| 88 | +} |
| 89 | + |
| 90 | +void TizenAutofill::RequestAutofill(std::vector<std::string> hints, |
| 91 | + std::string id) { |
| 92 | + char* app_id = nullptr; |
| 93 | + app_get_id(&app_id); |
| 94 | + |
| 95 | + autofill_view_info_h view_info = nullptr; |
| 96 | + autofill_view_info_create(&view_info); |
| 97 | + autofill_view_info_set_app_id(view_info, app_id); |
| 98 | + autofill_view_info_set_view_id(view_info, id.c_str()); |
| 99 | + |
| 100 | + for (auto hint : hints) { |
| 101 | + std::optional<autofill_hint_e> autofill_hint = ConvertAutofillHint(hint); |
| 102 | + if (autofill_hint.has_value()) { |
| 103 | + autofill_item_h item; |
| 104 | + autofill_item_create(&item); |
| 105 | + autofill_item_set_autofill_hint(item, autofill_hint.value()); |
| 106 | + autofill_item_set_id(item, id.c_str()); |
| 107 | + autofill_item_set_sensitive_data(item, false); |
| 108 | + autofill_view_info_add_item(view_info, item); |
| 109 | + autofill_item_destroy(item); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + int ret = autofill_fill_request(ah_, view_info); |
| 114 | + if (ret != AUTOFILL_ERROR_NONE) { |
| 115 | + FT_LOG(Error) << "autofill_fill_request error"; |
| 116 | + } |
| 117 | + autofill_view_info_destroy(view_info); |
| 118 | + |
| 119 | + response_items_.clear(); |
| 120 | +} |
| 121 | + |
| 122 | +void TizenAutofill::RegisterAutofillItem(std::string view_id, |
| 123 | + AutofillItem item) { |
| 124 | + autofill_save_item_h si_h; |
| 125 | + |
| 126 | + autofill_save_item_create(&si_h); |
| 127 | + autofill_save_item_set_autofill_hint(si_h, item.hint_); |
| 128 | + autofill_save_item_set_id(si_h, item.id_.c_str()); |
| 129 | + autofill_save_item_set_label(si_h, item.label_.c_str()); |
| 130 | + autofill_save_item_set_sensitive_data(si_h, item.sensitive_data_); |
| 131 | + autofill_save_item_set_value(si_h, item.value_.c_str()); |
| 132 | + |
| 133 | + char* app_id; |
| 134 | + app_get_id(&app_id); |
| 135 | + |
| 136 | + autofill_save_view_info_h svi_h; |
| 137 | + |
| 138 | + autofill_save_view_info_create(&svi_h); |
| 139 | + autofill_save_view_info_set_app_id(svi_h, app_id); |
| 140 | + autofill_save_view_info_set_view_id(svi_h, view_id.c_str()); |
| 141 | + |
| 142 | + autofill_save_view_info_add_item(svi_h, si_h); |
| 143 | + |
| 144 | + if (app_id) { |
| 145 | + free(app_id); |
| 146 | + } |
| 147 | + |
| 148 | + int ret; |
| 149 | + |
| 150 | + ret = autofill_commit(ah_, svi_h); |
| 151 | + if (ret != AUTOFILL_ERROR_NONE) { |
| 152 | + FT_LOG(Error) << "autofill_commit error"; |
| 153 | + } |
| 154 | + |
| 155 | + autofill_save_view_info_destroy(svi_h); |
| 156 | +} |
| 157 | + |
| 158 | +std::optional<autofill_hint_e> TizenAutofill::ConvertAutofillHint( |
| 159 | + std::string hint) { |
| 160 | + if (hint == "creditCardExpirationDate") { |
| 161 | + return AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE; |
| 162 | + } else if (hint == "creditCardExpirationDay") { |
| 163 | + return AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY; |
| 164 | + } else if (hint == "creditCardExpirationMonth") { |
| 165 | + return AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH; |
| 166 | + } else if (hint == "creditCardExpirationYear") { |
| 167 | + return AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR; |
| 168 | + } else if (hint == "email") { |
| 169 | + return AUTOFILL_HINT_EMAIL_ADDRESS; |
| 170 | + } else if (hint == "name") { |
| 171 | + return AUTOFILL_HINT_NAME; |
| 172 | + } else if (hint == "telephoneNumber") { |
| 173 | + return AUTOFILL_HINT_PHONE; |
| 174 | + } else if (hint == "postalAddress") { |
| 175 | + return AUTOFILL_HINT_POSTAL_ADDRESS; |
| 176 | + } else if (hint == "postalCode") { |
| 177 | + return AUTOFILL_HINT_POSTAL_CODE; |
| 178 | + } else if (hint == "username") { |
| 179 | + return AUTOFILL_HINT_ID; |
| 180 | + } else if (hint == "password") { |
| 181 | + return AUTOFILL_HINT_PASSWORD; |
| 182 | + } else if (hint == "creditCardSecurityCode") { |
| 183 | + return AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE; |
| 184 | + } |
| 185 | + FT_LOG(Error) << "Not supported autofill hint : " << hint; |
| 186 | + return std::nullopt; |
| 187 | +} |
0 commit comments