1d661d27b5b64c902d71e12cbe14b63a80968cae
[WebKit-https.git] / WebKit / gtk / webkit / webkitwebnavigationaction.cpp
1 /*
2  * Copyright (C) 2008 Collabora Ltd.
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
22 #include <wtf/Assertions.h>
23 #include "FrameLoaderTypes.h"
24
25 #include <glib/gi18n-lib.h>
26 #include "webkitwebnavigationaction.h"
27 #include "webkitprivate.h"
28 #include "webkitenumtypes.h"
29
30 #include <string.h>
31
32 /**
33  * SECTION:webkitwebnavigationaction
34  * @short_description: Object used to report details of navigation actions
35  *
36  * #WebKitWebNavigationAction is used in signals to provide details about
37  * what led the navigation to happen. This includes, for instance, if the user
38  * clicked a link to start that navigation, and what mouse button was used.
39  */
40
41 struct _WebKitWebNavigationActionPrivate {
42     WebKitWebNavigationReason reason;
43     gchar* originalUri;
44     gint button;
45     gint modifier_state;
46 };
47
48 #define WEBKIT_WEB_NAVIGATION_ACTION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_NAVIGATION_ACTION, WebKitWebNavigationActionPrivate))
49
50 enum  {
51     PROP_0,
52
53     PROP_REASON,
54     PROP_ORIGINAL_URI,
55     PROP_BUTTON,
56     PROP_MODIFIER_STATE
57 };
58
59 G_DEFINE_TYPE(WebKitWebNavigationAction, webkit_web_navigation_action, G_TYPE_OBJECT)
60
61
62 static void webkit_web_navigation_action_get_property(GObject* object, guint propertyId, GValue* value, GParamSpec* pspec)
63 {
64     WebKitWebNavigationAction* navigationAction = WEBKIT_WEB_NAVIGATION_ACTION(object);
65
66     switch(propertyId) {
67     case PROP_REASON:
68         g_value_set_enum(value, webkit_web_navigation_action_get_reason(navigationAction));
69         break;
70     case PROP_ORIGINAL_URI:
71         g_value_set_string(value, webkit_web_navigation_action_get_original_uri(navigationAction));
72         break;
73     case PROP_BUTTON:
74         g_value_set_int(value, webkit_web_navigation_action_get_button(navigationAction));
75         break;
76     case PROP_MODIFIER_STATE:
77         g_value_set_int(value, webkit_web_navigation_action_get_modifier_state(navigationAction));
78         break;
79     default:
80         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
81         break;
82     }
83 }
84
85 static void webkit_web_navigation_action_set_property(GObject* object, guint propertyId, const GValue* value, GParamSpec* pspec)
86 {
87     WebKitWebNavigationAction* navigationAction = WEBKIT_WEB_NAVIGATION_ACTION(object);
88     WebKitWebNavigationActionPrivate* priv = navigationAction->priv;
89
90     switch(propertyId) {
91     case PROP_REASON:
92         webkit_web_navigation_action_set_reason(navigationAction, (WebKitWebNavigationReason)g_value_get_enum(value));
93         break;
94     case PROP_ORIGINAL_URI:
95         webkit_web_navigation_action_set_original_uri(navigationAction, g_value_get_string(value));
96         break;
97     case PROP_BUTTON:
98         priv->button = g_value_get_int(value);
99         break;
100     case PROP_MODIFIER_STATE:
101         priv->modifier_state = g_value_get_int(value);
102         break;
103     default:
104         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propertyId, pspec);
105         break;
106     }
107 }
108
109 static void webkit_web_navigation_action_init(WebKitWebNavigationAction* navigationAction)
110 {
111     navigationAction->priv = WEBKIT_WEB_NAVIGATION_ACTION_GET_PRIVATE(navigationAction);
112
113     WebKitWebNavigationActionPrivate* priv = navigationAction->priv;
114 }
115
116 static void webkit_web_navigation_action_finalize(GObject* obj)
117 {
118     WebKitWebNavigationAction* navigationAction = WEBKIT_WEB_NAVIGATION_ACTION(obj);
119     WebKitWebNavigationActionPrivate* priv = navigationAction->priv;
120
121     g_free(priv->originalUri);
122
123     G_OBJECT_CLASS(webkit_web_navigation_action_parent_class)->finalize(obj);
124 }
125
126 static void webkit_web_navigation_action_class_init(WebKitWebNavigationActionClass* requestClass)
127 {
128     GObjectClass* objectClass = G_OBJECT_CLASS(requestClass);
129
130     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED) == static_cast<int>(WebCore::NavigationTypeLinkClicked), navigation_type_link_clicked_enum_match);
131     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_FORM_SUBMITTED) == static_cast<int>(WebCore::NavigationTypeFormSubmitted), navigation_type_form_submitted_enum_match);
132     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_BACK_FORWARD) == static_cast<int>(WebCore::NavigationTypeBackForward), navigation_type_back_forward_enum_match);
133     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_RELOAD) == static_cast<int>(WebCore::NavigationTypeReload), navigation_type_reload_enum_match);
134     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_FORM_RESUBMITTED) == static_cast<int>(WebCore::NavigationTypeFormResubmitted), navigation_type_form_resubmitted_enum_match);
135     COMPILE_ASSERT(static_cast<int>(WEBKIT_WEB_NAVIGATION_REASON_OTHER) == static_cast<int>(WebCore::NavigationTypeOther), navigation_type_other_enum_match);
136
137     objectClass->get_property = webkit_web_navigation_action_get_property;
138     objectClass->set_property = webkit_web_navigation_action_set_property;
139     objectClass->dispose = webkit_web_navigation_action_finalize;
140
141     /**
142      * WebKitWebNavigationAction:reason:
143      *
144      * The reason why this navigation is occuring.
145      *
146      * Since: 1.0.3
147      */
148     g_object_class_install_property(objectClass, PROP_REASON,
149                                     g_param_spec_enum("reason",
150                                                       _("Reason"),
151                                                       _("The reason why this navigation is occurring"),
152                                                       WEBKIT_TYPE_WEB_NAVIGATION_REASON,
153                                                       WEBKIT_WEB_NAVIGATION_REASON_OTHER,
154                                                       (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT)));
155
156     /**
157      * WebKitWebNavigationAction:original-uri:
158      *
159      * The URI that was requested as the target for the navigation.
160      *
161      * Since: 1.0.3
162      */
163     g_object_class_install_property(objectClass, PROP_ORIGINAL_URI,
164                                     g_param_spec_string("original-uri",
165                                                         _("Original URI"),
166                                                         _("The URI that was requested as the target for the navigation"),
167                                                         "",
168                                                         (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT)));
169     /**
170      * WebKitWebNavigationAction:button:
171      *
172      * The button used to click if the action was a mouse event.
173      *
174      * Since: 1.0.3
175      */
176     g_object_class_install_property(objectClass, PROP_BUTTON,
177                                     g_param_spec_int("button",
178                                                      _("Button"),
179                                                      _("The button used to click"),
180                                                      -1,
181                                                      G_MAXINT,
182                                                      -1,
183                                                      (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
184
185     /**
186      * WebKitWebNavigationAction:modifier-state:
187      *
188      * The state of the modifier keys when the action was requested.
189      * 
190      * Since: 1.0.3
191      */
192     g_object_class_install_property(objectClass, PROP_MODIFIER_STATE,
193                                     g_param_spec_int("modifier-state",
194                                                      _("Modifier state"),
195                                                      _("A bitmask representing the state of the modifier keys"),
196                                                      0,
197                                                      G_MAXINT,
198                                                      0,
199                                                      (GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)));
200
201
202
203     g_type_class_add_private(requestClass, sizeof(WebKitWebNavigationActionPrivate));
204 }
205
206 /**
207  * webkit_web_navigation_action_get_reason:
208  * @navigationAction: a #WebKitWebNavigationAction
209  *
210  * Returns the reason why WebKit is requesting a navigation.
211  *
212  * Return value: a #WebKitWebNavigationReason
213  *
214  * Since: 1.0.3
215  */
216 WebKitWebNavigationReason webkit_web_navigation_action_get_reason(WebKitWebNavigationAction* navigationAction)
217 {
218     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), WEBKIT_WEB_NAVIGATION_REASON_OTHER);
219
220     return navigationAction->priv->reason;
221 }
222
223 /**
224  * webkit_web_navigation_action_set_reason:
225  * @navigationAction: a #WebKitWebNavigationAction
226  * @reason: a #WebKitWebNavigationReason
227  *
228  * Sets the reason why WebKit is requesting a navigation.
229  *
230  * Since: 1.0.3
231  */
232 void webkit_web_navigation_action_set_reason(WebKitWebNavigationAction* navigationAction, WebKitWebNavigationReason reason)
233 {
234     g_return_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction));
235
236     if (navigationAction->priv->reason == reason)
237         return;
238
239     navigationAction->priv->reason = reason;
240     g_object_notify(G_OBJECT(navigationAction), "reason");
241 }
242
243 /**
244  * webkit_web_navigation_action_get_original_uri:
245  * @navigationAction: a #WebKitWebNavigationAction
246  *
247  * Returns the URI that was originally requested. This may differ from the
248  * navigation target, for instance because of a redirect.
249  *
250  * Return value: the originally requested URI
251  *
252  * Since: 1.0.3
253  */
254 const gchar* webkit_web_navigation_action_get_original_uri(WebKitWebNavigationAction* navigationAction)
255 {
256     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), NULL);
257
258     return navigationAction->priv->originalUri;
259 }
260
261 /**
262  * webkit_web_navigation_action_set_original_uri:
263  * @navigationAction: a #WebKitWebNavigationAction
264  * @originalUri: a URI
265  *
266  * Sets the URI that was originally requested. This may differ from the
267  * navigation target, for instance because of a redirect.
268  *
269  * Since: 1.0.3
270  */
271 void webkit_web_navigation_action_set_original_uri(WebKitWebNavigationAction* navigationAction, const gchar* originalUri)
272 {
273     g_return_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction));
274     g_return_if_fail(originalUri);
275
276     if (navigationAction->priv->originalUri &&
277         (!strcmp(navigationAction->priv->originalUri, originalUri)))
278         return;
279
280     g_free(navigationAction->priv->originalUri);
281     navigationAction->priv->originalUri = g_strdup(originalUri);
282     g_object_notify(G_OBJECT(navigationAction), "original-uri");
283 }
284
285 /**
286  * webkit_web_navigation_action_get_button:
287  * @navigationAction: a #WebKitWebNavigationAction
288  *
289  * Returns the mouse button used to click if the action was a mouse event.
290  * Otherwise returns -1.
291  *
292  * Return value: the mouse button used to click
293  *
294  * Since: 1.0.3
295  */
296 gint webkit_web_navigation_action_get_button(WebKitWebNavigationAction* navigationAction)
297 {
298     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), -1);
299
300     return navigationAction->priv->button;
301 }
302
303 /**
304  * webkit_web_navigation_action_get_modifier_state:
305  * @navigationAction: a #WebKitWebNavigationAction
306  *
307  * Returns a bitmask with the the state of the modifier keys.
308  *
309  * Return value: a bitmask with the state of the modifier keys
310  *
311  * Since: 1.0.3
312  */
313 gint webkit_web_navigation_action_get_modifier_state(WebKitWebNavigationAction* navigationAction)
314 {
315     g_return_val_if_fail(WEBKIT_IS_WEB_NAVIGATION_ACTION(navigationAction), 0);
316
317     return navigationAction->priv->modifier_state;
318 }