| 1 | /* |
| 2 | * Copyright (C) 2013 Samsung Electronics Inc. All rights reserved. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "WebKitCredential.h" |
| 22 | |
| 23 | #include "WebCredential.h" |
| 24 | #include "WebKitCredentialPrivate.h" |
| 25 | #include <wtf/text/CString.h> |
| 26 | |
| 27 | using namespace WebKit; |
| 28 | |
| 29 | struct _WebKitCredential { |
| 30 | _WebKitCredential(const WebCore::Credential& coreCredential) |
| 31 | : credential(coreCredential) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | WebCore::Credential credential; |
| 36 | CString username; |
| 37 | CString password; |
| 38 | }; |
| 39 | |
| 40 | G_DEFINE_BOXED_TYPE(WebKitCredential, webkit_credential, webkit_credential_copy, webkit_credential_free) |
| 41 | |
| 42 | static inline WebKitCredentialPersistence toWebKitCredentialPersistence(WebCore::CredentialPersistence corePersistence) |
| 43 | { |
| 44 | switch (corePersistence) { |
| 45 | case WebCore::CredentialPersistenceNone: |
| 46 | return WEBKIT_CREDENTIAL_PERSISTENCE_NONE; |
| 47 | case WebCore::CredentialPersistenceForSession: |
| 48 | return WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION; |
| 49 | case WebCore::CredentialPersistencePermanent: |
| 50 | return WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT; |
| 51 | default: |
| 52 | ASSERT_NOT_REACHED(); |
| 53 | return WEBKIT_CREDENTIAL_PERSISTENCE_NONE; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static inline WebCore::CredentialPersistence toWebCoreCredentialPersistence(WebKitCredentialPersistence kitPersistence) |
| 58 | { |
| 59 | switch (kitPersistence) { |
| 60 | case WEBKIT_CREDENTIAL_PERSISTENCE_NONE: |
| 61 | return WebCore::CredentialPersistenceNone; |
| 62 | case WEBKIT_CREDENTIAL_PERSISTENCE_FOR_SESSION: |
| 63 | return WebCore::CredentialPersistenceForSession; |
| 64 | case WEBKIT_CREDENTIAL_PERSISTENCE_PERMANENT: |
| 65 | return WebCore::CredentialPersistencePermanent; |
| 66 | default: |
| 67 | ASSERT_NOT_REACHED(); |
| 68 | return WebCore::CredentialPersistenceNone; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | WebKitCredential* webkitCredentialCreate(const WebCore::Credential& coreCredential) |
| 73 | { |
| 74 | WebKitCredential* credential = static_cast<WebKitCredential*>(fastMalloc(sizeof(WebKitCredential))); |
| 75 | new (credential) WebKitCredential(coreCredential); |
| 76 | return credential; |
| 77 | } |
| 78 | |
| 79 | const WebCore::Credential& webkitCredentialGetCredential(WebKitCredential* credential) |
| 80 | { |
| 81 | ASSERT(credential); |
| 82 | return credential->credential; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * webkit_credential_new: |
| 87 | * @username: The username for the new credential |
| 88 | * @password: The password for the new credential |
| 89 | * @persistence: The #WebKitCredentialPersistence of the new credential |
| 90 | * |
| 91 | * Create a new credential from the provided username, password and persistence mode. |
| 92 | * |
| 93 | * Returns: (transfer full): A #WebKitCredential. |
| 94 | * |
| 95 | * Since: 2.2 |
| 96 | */ |
| 97 | WebKitCredential* webkit_credential_new(const gchar* username, const gchar* password, WebKitCredentialPersistence persistence) |
| 98 | { |
| 99 | g_return_val_if_fail(username, 0); |
| 100 | g_return_val_if_fail(password, 0); |
| 101 | |
| 102 | return webkitCredentialCreate(WebCore::Credential(String::fromUTF8(username), String::fromUTF8(password), toWebCoreCredentialPersistence(persistence))); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * webkit_credential_copy: |
| 107 | * @credential: a #WebKitCredential |
| 108 | * |
| 109 | * Make a copy of the #WebKitCredential. |
| 110 | * |
| 111 | * Returns: (transfer full): A copy of passed in #WebKitCredential |
| 112 | * |
| 113 | * Since: 2.2 |
| 114 | */ |
| 115 | WebKitCredential* webkit_credential_copy(WebKitCredential* credential) |
| 116 | { |
| 117 | g_return_val_if_fail(credential, 0); |
| 118 | |
| 119 | return webkitCredentialCreate(credential->credential); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * webkit_credential_free: |
| 124 | * @credential: A #WebKitCredential |
| 125 | * |
| 126 | * Free the #WebKitCredential. |
| 127 | * |
| 128 | * Since: 2.2 |
| 129 | */ |
| 130 | void webkit_credential_free(WebKitCredential* credential) |
| 131 | { |
| 132 | g_return_if_fail(credential); |
| 133 | |
| 134 | credential->~WebKitCredential(); |
| 135 | fastFree(credential); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * webkit_credential_get_username: |
| 140 | * @credential: a #WebKitCredential |
| 141 | * |
| 142 | * Get the username currently held by this #WebKitCredential. |
| 143 | * |
| 144 | * Returns: The username stored in the #WebKitCredential. |
| 145 | * |
| 146 | * Since: 2.2 |
| 147 | */ |
| 148 | const gchar* webkit_credential_get_username(WebKitCredential* credential) |
| 149 | { |
| 150 | g_return_val_if_fail(credential, 0); |
| 151 | |
| 152 | if (credential->username.isNull()) |
| 153 | credential->username = credential->credential.user().utf8(); |
| 154 | return credential->username.data(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * webkit_credential_get_password: |
| 159 | * @credential: a #WebKitCredential |
| 160 | * |
| 161 | * Get the password currently held by this #WebKitCredential. |
| 162 | * |
| 163 | * Returns: The password stored in the #WebKitCredential. |
| 164 | * |
| 165 | * Since: 2.2 |
| 166 | */ |
| 167 | const gchar* webkit_credential_get_password(WebKitCredential* credential) |
| 168 | { |
| 169 | g_return_val_if_fail(credential, 0); |
| 170 | |
| 171 | if (credential->password.isNull()) |
| 172 | credential->password = credential->credential.password().utf8(); |
| 173 | return credential->password.data(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * webkit_credential_has_password: |
| 178 | * @credential: a #WebKitCredential |
| 179 | * |
| 180 | * Determine whether this credential has a password stored. |
| 181 | * |
| 182 | * Returns: %TRUE if the credential has a password or %FALSE otherwise. |
| 183 | * |
| 184 | * Since: 2.2 |
| 185 | */ |
| 186 | gboolean webkit_credential_has_password(WebKitCredential* credential) |
| 187 | { |
| 188 | g_return_val_if_fail(credential, FALSE); |
| 189 | |
| 190 | return credential->credential.hasPassword(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * webkit_credential_get_persistence: |
| 195 | * @credential: a #WebKitCredential |
| 196 | * |
| 197 | * Get the persistence mode currently held by this #WebKitCredential. |
| 198 | * |
| 199 | * Returns: The #WebKitCredentialPersistence stored in the #WebKitCredential. |
| 200 | * |
| 201 | * Since: 2.2 |
| 202 | */ |
| 203 | WebKitCredentialPersistence webkit_credential_get_persistence(WebKitCredential* credential) |
| 204 | { |
| 205 | g_return_val_if_fail(credential, WEBKIT_CREDENTIAL_PERSISTENCE_NONE); |
| 206 | |
| 207 | return toWebKitCredentialPersistence(credential->credential.persistence()); |
| 208 | } |
| 209 | |