[epiphany/wip/sync] sync: Let EphyShell start the sync after sign in



commit 9726774e8912f96809d0a7318da4f1fa816eb7bf
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Wed Jul 5 16:09:42 2017 +0300

    sync: Let EphyShell start the sync after sign in
    
    It's wrong to trust PrefsDialog to start the sync because the preferences
    dialog can be closed while EphySyncService is signing in, therefore
    causing the STORE_FINISHED signal callback in PrefsDialog to not be
    called.

 src/ephy-shell.c   |   57 ++++++++++++++++++++++++++++++++-----
 src/prefs-dialog.c |   79 ++++++++++++++++------------------------------------
 2 files changed, 73 insertions(+), 63 deletions(-)
---
diff --git a/src/ephy-shell.c b/src/ephy-shell.c
index 979683f..4830102 100644
--- a/src/ephy-shell.c
+++ b/src/ephy-shell.c
@@ -312,8 +312,8 @@ download_started_cb (WebKitWebContext *web_context,
 }
 
 static void
-sync_secrets_load_finished_cb (EphySyncService *service,
-                               EphyShell       *shell)
+register_synchronizable_managers (EphyShell       *shell,
+                                  EphySyncService *service)
 {
   EphySynchronizableManager *manager;
 
@@ -339,7 +339,41 @@ sync_secrets_load_finished_cb (EphySyncService *service,
     manager = EPHY_SYNCHRONIZABLE_MANAGER (ephy_shell_get_open_tabs_manager (shell));
     ephy_sync_service_register_manager (service, manager);
   }
+}
+
+static gboolean
+start_sync_after_sign_in (EphySyncService *service)
+{
+  g_assert (EPHY_IS_SYNC_SERVICE (service));
+
+  ephy_sync_service_start_sync (service);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+sync_secrets_store_finished_cb (EphySyncService *service,
+                                GError          *error,
+                                EphyShell       *shell)
+{
+  g_assert (EPHY_IS_SYNC_SERVICE (service));
+  g_assert (EPHY_IS_SHELL (shell));
+
+  if (!error) {
+    register_synchronizable_managers (shell, service);
+    /* Allow a 30 seconds window for the user to select their sync options. */
+    g_timeout_add_seconds (30, (GSourceFunc)start_sync_after_sign_in, service);
+  }
+}
+
+static void
+sync_secrets_load_finished_cb (EphySyncService *service,
+                               EphyShell       *shell)
+{
+  g_assert (EPHY_IS_SYNC_SERVICE (service));
+  g_assert (EPHY_IS_SHELL (shell));
 
+  register_synchronizable_managers (shell, service);
   ephy_sync_service_start_sync (service);
 }
 
@@ -349,7 +383,6 @@ ephy_shell_startup (GApplication *application)
   EphyEmbedShell *embed_shell = EPHY_EMBED_SHELL (application);
   EphyShell *shell = EPHY_SHELL (application);
   EphyEmbedShellMode mode;
-  EphySyncService *service;
   GtkBuilder *builder;
 
   G_APPLICATION_CLASS (ephy_shell_parent_class)->startup (application);
@@ -383,10 +416,8 @@ ephy_shell_startup (GApplication *application)
                               G_BINDING_SYNC_CREATE);
 
       if (ephy_sync_utils_user_is_signed_in ()) {
-        service = ephy_shell_get_sync_service (shell);
-        g_signal_connect_object (service, "sync-secrets-load-finished",
-                                 G_CALLBACK (sync_secrets_load_finished_cb),
-                                 shell, 0);
+        /* Create the sync service. */
+        ephy_shell_get_sync_service (shell);
       }
     }
 
@@ -825,9 +856,19 @@ ephy_shell_get_sync_service (EphyShell *shell)
 {
   g_return_val_if_fail (EPHY_IS_SHELL (shell), NULL);
 
-  if (shell->sync_service == NULL)
+  if (shell->sync_service == NULL) {
     shell->sync_service = ephy_sync_service_new (TRUE);
 
+    g_signal_connect_object (shell->sync_service,
+                             "sync-secrets-store-finished",
+                             G_CALLBACK (sync_secrets_store_finished_cb),
+                             shell, 0);
+    g_signal_connect_object (shell->sync_service,
+                             "sync-secrets-load-finished",
+                             G_CALLBACK (sync_secrets_load_finished_cb),
+                             shell, 0);
+  }
+
   return shell->sync_service;
 }
 
diff --git a/src/prefs-dialog.c b/src/prefs-dialog.c
index 48b19b0..9f87317 100644
--- a/src/prefs-dialog.c
+++ b/src/prefs-dialog.c
@@ -116,7 +116,6 @@ struct _PrefsDialog {
   GHashTable *iso_3166_table;
 
   /* sync */
-  EphySyncService *sync_service;
   GtkWidget *sync_page_box;
   GtkWidget *sync_firefox_iframe_box;
   GtkWidget *sync_firefox_iframe_label;
@@ -141,7 +140,6 @@ struct _PrefsDialog {
   GtkWidget *sync_device_name_cancel_button;
   GtkWidget *sync_last_sync_time_box;
   GtkWidget *sync_last_sync_time_label;
-  gboolean sync_was_signed_in;
 
   WebKitWebView *fxa_web_view;
   WebKitUserContentManager *fxa_manager;
@@ -179,11 +177,6 @@ prefs_dialog_finalize (GObject *object)
     g_object_unref (dialog->fxa_manager);
   }
 
-  if (dialog->sync_service != NULL) {
-    if (ephy_sync_utils_user_is_signed_in () && !dialog->sync_was_signed_in)
-      ephy_sync_service_start_sync (dialog->sync_service);
-  }
-
   G_OBJECT_CLASS (prefs_dialog_parent_class)->finalize (object);
 }
 
@@ -193,6 +186,7 @@ sync_collection_toggled_cb (GtkToggleButton *button,
 {
   EphySynchronizableManager *manager = NULL;
   EphyShell *shell = ephy_shell_get_default ();
+  EphySyncService *service = ephy_shell_get_sync_service (shell);
 
   if (GTK_WIDGET (button) == dialog->sync_bookmarks_checkbutton) {
     manager = EPHY_SYNCHRONIZABLE_MANAGER (ephy_shell_get_bookmarks_manager (shell));
@@ -208,9 +202,9 @@ sync_collection_toggled_cb (GtkToggleButton *button,
   }
 
   if (gtk_toggle_button_get_active (button)) {
-    ephy_sync_service_register_manager (dialog->sync_service, manager);
+    ephy_sync_service_register_manager (service, manager);
   } else {
-    ephy_sync_service_unregister_manager (dialog->sync_service, manager);
+    ephy_sync_service_unregister_manager (service, manager);
     ephy_synchronizable_manager_set_is_initial_sync (manager, TRUE);
   }
 }
@@ -293,20 +287,15 @@ sync_secrets_store_finished_cb (EphySyncService *service,
                                 GError          *error,
                                 PrefsDialog     *dialog)
 {
-  EphySynchronizableManager *manager;
-  EphyShell *shell = ephy_shell_get_default ();
-
   g_assert (EPHY_IS_SYNC_SERVICE (service));
   g_assert (EPHY_IS_PREFS_DIALOG (dialog));
 
   if (!error) {
-    char *text;
-    char *user;
-
     /* Show sync options panel. */
-    user = g_strdup_printf ("<b>%s</b>", ephy_sync_utils_get_sync_user ());
+    char *user = g_strdup_printf ("<b>%s</b>", ephy_sync_utils_get_sync_user ());
     /* Translators: the %s refers to the email of the currently logged in user. */
-    text = g_strdup_printf (_("Logged in as %s"), user);
+    char *text = g_strdup_printf (_("Logged in as %s"), user);
+
     gtk_label_set_markup (GTK_LABEL (dialog->sync_firefox_account_label), text);
     gtk_container_remove (GTK_CONTAINER (dialog->sync_page_box),
                           dialog->sync_firefox_iframe_box);
@@ -317,23 +306,6 @@ sync_secrets_store_finished_cb (EphySyncService *service,
                         dialog->sync_options_box,
                         FALSE, FALSE, 0);
 
-    if (ephy_sync_utils_bookmarks_sync_is_enabled ()) {
-      manager = EPHY_SYNCHRONIZABLE_MANAGER (ephy_shell_get_bookmarks_manager (shell));
-      ephy_sync_service_register_manager (service, manager);
-    }
-    if (ephy_sync_utils_passwords_sync_is_enabled ()) {
-      manager = EPHY_SYNCHRONIZABLE_MANAGER (ephy_shell_get_password_manager (shell));
-      ephy_sync_service_register_manager (service, manager);
-    }
-    if (ephy_sync_utils_history_sync_is_enabled ()) {
-      manager = EPHY_SYNCHRONIZABLE_MANAGER (ephy_shell_get_history_manager (shell));
-      ephy_sync_service_register_manager (service, manager);
-    }
-    if (ephy_sync_utils_open_tabs_sync_is_enabled ()) {
-      manager = EPHY_SYNCHRONIZABLE_MANAGER (ephy_shell_get_open_tabs_manager (shell));
-      ephy_sync_service_register_manager (service, manager);
-    }
-
     g_free (text);
     g_free (user);
   } else {
@@ -552,8 +524,8 @@ sync_message_from_fxa_content_cb (WebKitUserContentManager *manager,
       sync_sign_in_details_show (dialog, _("Please don’t leave this page until "
                                            "you have completed the verification."));
 
-    ephy_sync_service_sign_in (dialog->sync_service, email, uid,
-                               session_token, key_fetch_token, unwrap_kb);
+    ephy_sync_service_sign_in (ephy_shell_get_sync_service (ephy_shell_get_default ()),
+                               email, uid, session_token, key_fetch_token, unwrap_kb);
   }
 
 out:
@@ -626,8 +598,9 @@ static void
 on_sync_sign_out_button_clicked (GtkWidget   *button,
                                  PrefsDialog *dialog)
 {
+  EphySyncService *service = ephy_shell_get_sync_service (ephy_shell_get_default ());
 
-  ephy_sync_service_sign_out (dialog->sync_service, TRUE);
+  ephy_sync_service_sign_out (service, TRUE);
 
   /* Show Firefox Accounts iframe. */
   sync_setup_firefox_iframe (dialog);
@@ -639,16 +612,16 @@ on_sync_sign_out_button_clicked (GtkWidget   *button,
                       dialog->sync_firefox_iframe_box,
                       FALSE, FALSE, 0);
   gtk_widget_set_visible (dialog->sync_last_sync_time_box, FALSE);
-
-  dialog->sync_was_signed_in = FALSE;
 }
 
 static void
 on_sync_sync_now_button_clicked (GtkWidget   *button,
                                  PrefsDialog *dialog)
 {
+  EphySyncService *service = ephy_shell_get_sync_service (ephy_shell_get_default ());
+
   gtk_widget_set_sensitive (button, FALSE);
-  ephy_sync_service_sync (dialog->sync_service);
+  ephy_sync_service_sync (service);
 }
 
 static void
@@ -679,10 +652,11 @@ static void
 on_sync_device_name_save_button_clicked (GtkWidget   *button,
                                          PrefsDialog *dialog)
 {
+  EphySyncService *service = ephy_shell_get_sync_service (ephy_shell_get_default ());
   const char *name;
 
   name = gtk_entry_get_text (GTK_ENTRY (dialog->sync_device_name_entry));
-  ephy_sync_service_register_device (dialog->sync_service, name);
+  ephy_sync_service_register_device (service, name);
 
   gtk_widget_set_sensitive (GTK_WIDGET (dialog->sync_device_name_entry), FALSE);
   gtk_widget_set_visible (GTK_WIDGET (dialog->sync_device_name_change_button), TRUE);
@@ -1842,11 +1816,13 @@ setup_language_page (PrefsDialog *dialog)
 static void
 setup_sync_page (PrefsDialog *dialog)
 {
-  GSettings *sync_settings;
-  char *user;
-  char *name;
+  EphySyncService *service = ephy_shell_get_sync_service (ephy_shell_get_default ());
+  GSettings *sync_settings = ephy_settings_get (EPHY_PREFS_SYNC_SCHEMA);
+  char *user = ephy_sync_utils_get_sync_user ();
+  char *name = ephy_sync_utils_get_device_name ();
+
+  gtk_entry_set_text (GTK_ENTRY (dialog->sync_device_name_entry), name);
 
-  user = ephy_sync_utils_get_sync_user ();
   if (!user) {
     sync_setup_firefox_iframe (dialog);
     gtk_container_remove (GTK_CONTAINER (dialog->sync_page_box),
@@ -1867,13 +1843,6 @@ setup_sync_page (PrefsDialog *dialog)
     g_free (text);
   }
 
-  name = ephy_sync_utils_get_device_name ();
-  gtk_entry_set_text (GTK_ENTRY (dialog->sync_device_name_entry), name);
-
-  sync_settings = ephy_settings_get (EPHY_PREFS_SYNC_SCHEMA);
-  dialog->sync_service = ephy_shell_get_sync_service (ephy_shell_get_default ());
-  dialog->sync_was_signed_in = user != NULL;
-
   g_settings_bind (sync_settings,
                    EPHY_PREFS_SYNC_WITH_FIREFOX,
                    dialog->sync_with_firefox_checkbutton,
@@ -1940,13 +1909,13 @@ setup_sync_page (PrefsDialog *dialog)
                           dialog->synced_tabs_button, "sensitive",
                           G_BINDING_SYNC_CREATE);
 
-  g_signal_connect_object (dialog->sync_service, "sync-secrets-store-finished",
+  g_signal_connect_object (service, "sync-secrets-store-finished",
                            G_CALLBACK (sync_secrets_store_finished_cb),
                            dialog, 0);
-  g_signal_connect_object (dialog->sync_service, "sync-sign-in-error",
+  g_signal_connect_object (service, "sync-sign-in-error",
                            G_CALLBACK (sync_sign_in_error_cb),
                            dialog, 0);
-  g_signal_connect_object (dialog->sync_service, "sync-finished",
+  g_signal_connect_object (service, "sync-finished",
                            G_CALLBACK (sync_finished_cb),
                            dialog, 0);
   g_signal_connect_object (dialog->sync_with_firefox_checkbutton, "toggled",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]