Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_auth_challenge.cpp
1 /*
2    Copyright (C) 2012 Samsung Electronics
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 "ewk_auth_challenge.h"
22
23 #if OS(TIZEN)
24 #include "WKAPICast.h"
25 #include "WKAuthenticationChallenge.h"
26 #include "WKAuthenticationDecisionListener.h"
27 #include "WKCredential.h"
28 #include "WKProtectionSpace.h"
29 #include "WKRetainPtr.h"
30 #include "WKString.h"
31 #include "ewk_auth_challenge_private.h"
32 #include <wtf/text/CString.h>
33
34 using namespace WebKit;
35
36 /**
37  * @brief  Structure used to send credential for authentication challenge.
38  *
39  * Details If Authentication challenge requirement is received, AuthenticationChallenge is created,
40  * and realm, host url are received from server.
41  * These information are sent to notify by evas_object_smart_callback_call.
42  */
43 struct _Ewk_Auth_Challenge {
44     WKAuthenticationChallengeRef authenticationChallenge;
45 #if ENABLE(TIZEN_ON_AUTHENTICATION_REQUESTED)
46     WKPageRef page;
47 #endif
48
49     CString realm;
50     CString url;
51
52     bool isDecided;
53     bool isSuspended;
54 };
55
56 #if ENABLE(TIZEN_ON_AUTHENTICATION_REQUESTED)
57 Ewk_Auth_Challenge* ewkAuthChallengeCreate(WKPageRef page, WKAuthenticationChallengeRef authenticationChallenge)
58 #else
59 Ewk_Auth_Challenge* ewkAuthChallengeCreate(WKAuthenticationChallengeRef authenticationChallenge)
60 #endif
61 {
62     EINA_SAFETY_ON_NULL_RETURN_VAL(authenticationChallenge, 0);
63
64     WKProtectionSpaceRef protectionSpace = WKAuthenticationChallengeGetProtectionSpace(authenticationChallenge);
65     EINA_SAFETY_ON_NULL_RETURN_VAL(protectionSpace, 0);
66
67     WKRetainPtr<WKStringRef> hostString(AdoptWK, WKProtectionSpaceCopyHost(protectionSpace));
68     WKRetainPtr<WKStringRef> realmString(AdoptWK, WKProtectionSpaceCopyRealm(protectionSpace));
69
70     Ewk_Auth_Challenge* authChallenge = new Ewk_Auth_Challenge;
71
72 #if ENABLE(TIZEN_ON_AUTHENTICATION_REQUESTED)
73     authChallenge->page = page;
74 #endif
75     authChallenge->authenticationChallenge = authenticationChallenge;
76     authChallenge->realm = toImpl(realmString.get())->string().utf8();
77     authChallenge->url = toImpl(hostString.get())->string().utf8();
78
79     authChallenge->isDecided = false;
80     authChallenge->isSuspended = false;
81
82     return authChallenge;
83 }
84
85 void ewkAuthChallengeDelete(Ewk_Auth_Challenge* authChallenge)
86 {
87     EINA_SAFETY_ON_NULL_RETURN(authChallenge);
88
89     delete authChallenge;
90 }
91
92 bool ewkAuthChallengeDecided(Ewk_Auth_Challenge* authChallenge)
93 {
94     EINA_SAFETY_ON_NULL_RETURN_VAL(authChallenge, false);
95
96     return authChallenge->isDecided;
97 }
98
99 bool ewkAuthChallengeSuspended(Ewk_Auth_Challenge* authChallenge)
100 {
101     EINA_SAFETY_ON_NULL_RETURN_VAL(authChallenge, false);
102
103     return authChallenge->isSuspended;
104 }
105
106 const char* ewk_auth_challenge_realm_get(Ewk_Auth_Challenge* authChallenge)
107 {
108     EINA_SAFETY_ON_NULL_RETURN_VAL(authChallenge, 0);
109
110     return authChallenge->realm.data();
111 }
112
113 const char* ewk_auth_challenge_url_get(Ewk_Auth_Challenge* authChallenge)
114 {
115     EINA_SAFETY_ON_NULL_RETURN_VAL(authChallenge, 0);
116
117     return authChallenge->url.data();
118 }
119
120 void ewk_auth_challenge_suspend(Ewk_Auth_Challenge* authChallenge)
121 {
122     EINA_SAFETY_ON_NULL_RETURN(authChallenge);
123
124     authChallenge->isSuspended = true;
125 }
126
127 void ewk_auth_challenge_credential_use(Ewk_Auth_Challenge* authChallenge, char* user, char* password)
128 {
129     EINA_SAFETY_ON_NULL_RETURN(authChallenge);
130     EINA_SAFETY_ON_NULL_RETURN(user);
131     EINA_SAFETY_ON_NULL_RETURN(password);
132
133     authChallenge->isDecided = true;
134     toImpl(authChallenge->page)->replyReceiveAuthenticationChallengeInFrame(true);
135
136     WKAuthenticationChallengeRef authenticationChallenge = authChallenge->authenticationChallenge;
137     WKAuthenticationDecisionListenerRef authenticationDecisionListener = WKAuthenticationChallengeGetDecisionListener(authenticationChallenge);
138
139     WKRetainPtr<WKStringRef> userString(AdoptWK, WKStringCreateWithUTF8CString(user));
140     WKRetainPtr<WKStringRef> passwordString(AdoptWK, WKStringCreateWithUTF8CString(password));
141
142     WKRetainPtr<WKCredentialRef> credential(AdoptWK, WKCredentialCreate(userString.get(), passwordString.get(), kWKCredentialPersistenceNone));
143
144     WKAuthenticationDecisionListenerUseCredential(authenticationDecisionListener, credential.get());
145 }
146
147 void ewk_auth_challenge_credential_cancel(Ewk_Auth_Challenge* authChallenge)
148 {
149     EINA_SAFETY_ON_NULL_RETURN(authChallenge);
150
151     authChallenge->isDecided = true;
152     toImpl(authChallenge->page)->replyReceiveAuthenticationChallengeInFrame(false);
153
154     WKAuthenticationChallengeRef authenticationChallenge = authChallenge->authenticationChallenge;
155     WKAuthenticationDecisionListenerRef authenticationDecisionListener = WKAuthenticationChallengeGetDecisionListener(authenticationChallenge);
156
157     WKAuthenticationDecisionListenerCancel(authenticationDecisionListener);
158 }
159
160 #endif // #if OS(TIZEN)