[gnome-online-accounts/wip/rishi/drop-deprecated-gtimeval-gmemmove: 3/3] oauth, oauth2, utils: Split out & re-use the time conversion routines
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-online-accounts/wip/rishi/drop-deprecated-gtimeval-gmemmove: 3/3] oauth, oauth2, utils: Split out & re-use the time conversion routines
- Date: Wed, 4 Dec 2019 14:54:16 +0000 (UTC)
commit 9ddbd410d2803d079bb8bdae8cb0d3a5d41befbf
Author: Debarshi Ray <debarshir gnome org>
Date: Fri Nov 29 20:18:58 2019 +0100
oauth, oauth2, utils: Split out & re-use the time conversion routines
src/goabackend/goaoauth2provider.c | 30 +++-------------------------
src/goabackend/goaoauthprovider.c | 40 ++++++++------------------------------
src/goabackend/goautils.c | 24 +++++++++++++++++++++++
src/goabackend/goautils.h | 4 ++++
4 files changed, 39 insertions(+), 59 deletions(-)
---
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index 8bfb6e64..3715431d 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -1066,30 +1066,6 @@ add_account_cb (GoaManager *manager,
g_main_loop_quit (priv->loop);
}
-static gint64
-duration_to_abs_usec (gint duration_sec)
-{
- gint64 ret;
- GTimeVal now;
-
- g_get_current_time (&now);
- ret = ((gint64) now.tv_sec) * 1000L * 1000L + ((gint64) now.tv_usec);
- ret += ((gint64) duration_sec) * 1000L * 1000L;
- return ret;
-}
-
-static gint
-abs_usec_to_duration (gint64 abs_usec)
-{
- gint64 ret;
- GTimeVal now;
-
- g_get_current_time (&now);
- ret = abs_usec - (((gint64) now.tv_sec) * 1000L * 1000L + ((gint64) now.tv_usec));
- ret /= 1000L * 1000L;
- return (gint) ret;
-}
-
static void
add_credentials_key_values (GoaOAuth2Provider *self,
GVariantBuilder *credentials)
@@ -1104,7 +1080,7 @@ add_credentials_key_values (GoaOAuth2Provider *self,
g_variant_builder_add (credentials, "{sv}", "access_token", g_variant_new_string (priv->access_token));
if (priv->access_token_expires_in > 0)
g_variant_builder_add (credentials, "{sv}", "access_token_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (priv->access_token_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(priv->access_token_expires_in)));
if (priv->refresh_token != NULL)
g_variant_builder_add (credentials, "{sv}", "refresh_token", g_variant_new_string (priv->refresh_token));
if (priv->password != NULL)
@@ -1388,7 +1364,7 @@ goa_oauth2_provider_get_access_token_sync (GoaOAuth2Provider *self,
if (g_strcmp0 (key, "access_token") == 0)
access_token = g_variant_dup_string (value, NULL);
else if (g_strcmp0 (key, "access_token_expires_at") == 0)
- access_token_expires_in = abs_usec_to_duration (g_variant_get_int64 (value));
+ access_token_expires_in = goa_utils_convert_abs_usec_to_duration_sec (g_variant_get_int64 (value));
else if (g_strcmp0 (key, "refresh_token") == 0)
refresh_token = g_variant_dup_string (value, NULL);
else if (g_strcmp0 (key, "authorization_code") == 0)
@@ -1465,7 +1441,7 @@ goa_oauth2_provider_get_access_token_sync (GoaOAuth2Provider *self,
g_variant_builder_add (&builder, "{sv}", "access_token", g_variant_new_string (access_token));
if (access_token_expires_in > 0)
g_variant_builder_add (&builder, "{sv}", "access_token_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (access_token_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(access_token_expires_in)));
if (refresh_token != NULL)
g_variant_builder_add (&builder, "{sv}", "refresh_token", g_variant_new_string (refresh_token));
if (password != NULL)
diff --git a/src/goabackend/goaoauthprovider.c b/src/goabackend/goaoauthprovider.c
index 1fc49cc7..0bfab6b1 100644
--- a/src/goabackend/goaoauthprovider.c
+++ b/src/goabackend/goaoauthprovider.c
@@ -977,30 +977,6 @@ add_account_cb (GoaManager *manager,
g_main_loop_quit (data->loop);
}
-static gint64
-duration_to_abs_usec (gint duration_sec)
-{
- gint64 ret;
- GTimeVal now;
-
- g_get_current_time (&now);
- ret = ((gint64) now.tv_sec) * 1000L * 1000L + ((gint64) now.tv_usec);
- ret += ((gint64) duration_sec) * 1000L * 1000L;
- return ret;
-}
-
-static gint
-abs_usec_to_duration (gint64 abs_usec)
-{
- gint64 ret;
- GTimeVal now;
-
- g_get_current_time (&now);
- ret = abs_usec - (((gint64) now.tv_sec) * 1000L * 1000L + ((gint64) now.tv_usec));
- ret /= 1000L * 1000L;
- return (gint) ret;
-}
-
static GoaObject *
goa_oauth_provider_add_account (GoaProvider *_provider,
GoaClient *client,
@@ -1063,12 +1039,12 @@ goa_oauth_provider_add_account (GoaProvider *_provider,
g_variant_builder_add (&credentials, "{sv}", "access_token_secret", g_variant_new_string
(access_token_secret));
if (access_token_expires_in > 0)
g_variant_builder_add (&credentials, "{sv}", "access_token_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (access_token_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(access_token_expires_in)));
if (session_handle != NULL)
g_variant_builder_add (&credentials, "{sv}", "session_handle", g_variant_new_string (session_handle));
if (session_handle_expires_in > 0)
g_variant_builder_add (&credentials, "{sv}", "session_handle_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (session_handle_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(session_handle_expires_in)));
if (password != NULL)
g_variant_builder_add (&credentials, "{sv}", "password", g_variant_new_string (password));
@@ -1200,12 +1176,12 @@ goa_oauth_provider_refresh_account (GoaProvider *_provider,
g_variant_builder_add (&builder, "{sv}", "access_token_secret", g_variant_new_string
(access_token_secret));
if (access_token_expires_in > 0)
g_variant_builder_add (&builder, "{sv}", "access_token_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (access_token_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(access_token_expires_in)));
if (session_handle != NULL)
g_variant_builder_add (&builder, "{sv}", "session_handle", g_variant_new_string (session_handle));
if (session_handle_expires_in > 0)
g_variant_builder_add (&builder, "{sv}", "session_handle_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (session_handle_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(session_handle_expires_in)));
if (password != NULL)
g_variant_builder_add (&builder, "{sv}", "password", g_variant_new_string (password));
/* TODO: run in worker thread */
@@ -1345,11 +1321,11 @@ goa_oauth_provider_get_access_token_sync (GoaOAuthProvider *provider,
else if (g_strcmp0 (key, "access_token_secret") == 0)
access_token_secret = g_variant_dup_string (value, NULL);
else if (g_strcmp0 (key, "access_token_expires_at") == 0)
- access_token_expires_in = abs_usec_to_duration (g_variant_get_int64 (value));
+ access_token_expires_in = goa_utils_convert_abs_usec_to_duration_sec (g_variant_get_int64 (value));
else if (g_strcmp0 (key, "session_handle") == 0)
session_handle = g_variant_dup_string (value, NULL);
else if (g_strcmp0 (key, "session_handle_expires_at") == 0)
- session_handle_expires_in = abs_usec_to_duration (g_variant_get_int64 (value));
+ session_handle_expires_in = goa_utils_convert_abs_usec_to_duration_sec (g_variant_get_int64 (value));
else if (g_strcmp0 (key, "password") == 0)
password = g_variant_dup_string (value, NULL);
g_variant_unref (value);
@@ -1418,12 +1394,12 @@ goa_oauth_provider_get_access_token_sync (GoaOAuthProvider *provider,
g_variant_builder_add (&builder, "{sv}", "access_token_secret", g_variant_new_string
(access_token_secret));
if (access_token_expires_in > 0)
g_variant_builder_add (&builder, "{sv}", "access_token_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (access_token_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(access_token_expires_in)));
if (session_handle != NULL)
g_variant_builder_add (&builder, "{sv}", "session_handle", g_variant_new_string (session_handle));
if (session_handle_expires_in > 0)
g_variant_builder_add (&builder, "{sv}", "session_handle_expires_at",
- g_variant_new_int64 (duration_to_abs_usec (session_handle_expires_in)));
+ g_variant_new_int64 (goa_utils_convert_duration_sec_to_abs_usec
(session_handle_expires_in)));
if (password != NULL)
g_variant_builder_add (&builder, "{sv}", "password", g_variant_new_string (password));
diff --git a/src/goabackend/goautils.c b/src/goabackend/goautils.c
index e562e8da..d0863554 100644
--- a/src/goabackend/goautils.c
+++ b/src/goabackend/goautils.c
@@ -251,6 +251,30 @@ goa_utils_check_duplicate (GoaClient *client,
return ret;
}
+gint
+goa_utils_convert_abs_usec_to_duration_sec (gint64 abs_usec)
+{
+ gint64 ret;
+ GTimeVal now;
+
+ g_get_current_time (&now);
+ ret = abs_usec - (((gint64) now.tv_sec) * 1000L * 1000L + ((gint64) now.tv_usec));
+ ret /= 1000L * 1000L;
+ return (gint) ret;
+}
+
+gint64
+goa_utils_convert_duration_sec_to_abs_usec (gint duration_sec)
+{
+ gint64 ret;
+ GTimeVal now;
+
+ g_get_current_time (&now);
+ ret = ((gint64) now.tv_sec) * 1000L * 1000L + ((gint64) now.tv_usec);
+ ret += ((gint64) duration_sec) * 1000L * 1000L;
+ return ret;
+}
+
gchar *
goa_utils_data_input_stream_read_line (GDataInputStream *stream,
gsize *length,
diff --git a/src/goabackend/goautils.h b/src/goabackend/goautils.h
index 8b1a455d..3bd5ab47 100644
--- a/src/goabackend/goautils.h
+++ b/src/goabackend/goautils.h
@@ -53,6 +53,10 @@ gboolean goa_utils_check_duplicate (GoaClient *client,
GoaPeekInterfaceFunc func,
GError **error);
+gint goa_utils_convert_abs_usec_to_duration_sec (gint64 abs_usec);
+
+gint64 goa_utils_convert_duration_sec_to_abs_usec (gint duration_sec);
+
gchar *goa_utils_data_input_stream_read_line (GDataInputStream *stream,
gsize *length,
GCancellable *cancellable,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]