initial import
[vuplus_webkit] / Source / WebKit / efl / ewk / ewk_auth_soup.cpp
1 /*
2     Copyright (C) 2009 Igalia S.L.
3     Copyright (C) 2011 Samsung Electronics
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Library General Public
7     License as published by the Free Software Foundation; either
8     version 2 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Library General Public License for more details.
14
15     You should have received a copy of the GNU Library General Public License
16     along with this library; see the file COPYING.LIB.  If not, write to
17     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22 #include "ewk_auth_soup.h"
23
24 #include "EWebKit.h"
25 #include "ewk_auth.h"
26 #include "ewk_logging.h"
27 #include <glib-object.h>
28 #include <glib.h>
29 #include <libsoup/soup.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /**
36  * #Ewk_Soup_Auth_Dialog is a #SoupSessionFeature that you can attach to your
37  * #SoupSession to provide a simple authentication dialog while
38  * handling HTTP basic auth. It is built as a simple C-only module
39  * to ease reuse.
40  */
41
42 typedef struct _Ewk_Auth_Data {
43     SoupMessage *msg;
44     SoupAuth *auth;
45     SoupSession *session;
46 } Ewk_Auth_Data;
47
48 static Ewk_Auth_Show_Dialog_Callback ewk_auth_show_dialog_callback = 0;
49
50 static void ewk_auth_soup_dialog_session_feature_init(SoupSessionFeatureInterface*, gpointer);
51 static void attach(SoupSessionFeature*, SoupSession*);
52 static void detach(SoupSessionFeature*, SoupSession*);
53 static void free_auth_data(Ewk_Auth_Data*);
54
55 G_DEFINE_TYPE_WITH_CODE(Ewk_Soup_Auth_Dialog, ewk_auth_soup_dialog, G_TYPE_OBJECT,
56                         G_IMPLEMENT_INTERFACE(SOUP_TYPE_SESSION_FEATURE, ewk_auth_soup_dialog_session_feature_init))
57
58 static void ewk_auth_soup_dialog_class_init(Ewk_Soup_Auth_DialogClass *klass)
59 {
60 }
61
62 static void ewk_auth_soup_dialog_init(Ewk_Soup_Auth_Dialog *instance)
63 {
64 }
65
66 static void ewk_auth_soup_dialog_session_feature_init(SoupSessionFeatureInterface *feature_interface, gpointer interface_data)
67 {
68     feature_interface->attach = attach;
69     feature_interface->detach = detach;
70 }
71
72 void ewk_auth_soup_show_dialog_callback_set(Ewk_Auth_Show_Dialog_Callback callback)
73 {
74     ewk_auth_show_dialog_callback = callback;
75 }
76
77 void ewk_auth_soup_credentials_set(const char *username, const char *password, void *data)
78 {
79     if (!data)
80         return;
81
82     Ewk_Auth_Data *auth_data = (Ewk_Auth_Data *)data;
83     soup_auth_authenticate(auth_data->auth, username, password);
84     soup_session_unpause_message(auth_data->session, auth_data->msg);
85     free_auth_data(auth_data);
86 }
87
88 static void session_authenticate(SoupSession *session, SoupMessage *msg, SoupAuth *auth, gboolean retrying, gpointer /* user_data */)
89 {
90     SoupURI *uri;
91     Ewk_Auth_Data *auth_data;
92     const char *realm;
93
94     if (!ewk_auth_show_dialog_callback)
95         return;
96
97     auth_data = (Ewk_Auth_Data *)calloc(1, sizeof(Ewk_Auth_Data));
98
99     if (!auth_data) {
100         CRITICAL("could not allocate Ewk_Auth_Data");
101         return;
102     }
103
104     soup_session_pause_message(session, msg);
105     // We need to make sure the message sticks around when pausing it.
106     g_object_ref(msg);
107     g_object_ref(session);
108     g_object_ref(auth);
109
110     auth_data->msg = msg;
111     auth_data->auth = auth;
112     auth_data->session = session;
113
114     uri = soup_message_get_uri(auth_data->msg);
115     realm = soup_auth_get_realm(auth_data->auth);
116
117     // Call application method responsible for showing authentication dialog and sending back authorization data.
118     ewk_auth_show_dialog_callback(realm, soup_uri_to_string(uri, TRUE), auth_data);
119 }
120
121 static void free_auth_data(Ewk_Auth_Data *auth_data)
122 {
123     g_object_unref(auth_data->msg);
124     g_object_unref(auth_data->session);
125     g_object_unref(auth_data->auth);
126     free(auth_data);
127 }
128
129 static void attach(SoupSessionFeature *manager, SoupSession *session)
130 {
131     g_signal_connect(session, "authenticate", G_CALLBACK(session_authenticate), manager);
132 }
133
134 static void detach(SoupSessionFeature *manager, SoupSession *session)
135 {
136     g_signal_handlers_disconnect_by_func(session, (void *)session_authenticate, manager);
137 }
138
139 #ifdef __cplusplus
140 }
141 #endif