Commit 9771c52f authored by Michael Catanzaro's avatar Michael Catanzaro

Fix search provider horribly breaking history service

If the search provider is running, all database transactions will fail
because the search provider will take a write lock on the database.
Ouch! This is worth a good string of profanities....

Notably, this causes opening the database to fail if you searched for
anything in the shell overview in the minute prior to starting Epiphany.
(One minute is our search provider timeout.) Then no history will ever
get saved, ever. I think. Something like that.

So, although our history service has read-only mode, it's enforced at
the history service level, not the SQLite connection level. SQLite
actually has a read-only mode, which we are not using, and which we need
to use in the search provider if we want to have any chance of reliably
saving history.

Accordingly, give EphySQLiteConnection a mode property, to indicate
whether it is in writable mode or read-only mode. Teach all callers to
set it properly. Use it, rather than a boolean, when creating the
EphyHistoryService, since boolean parameters are hard to read at call
sites. And actually put the underlying SQLite connection in read-only
mode when set.

Don't open transactions or ever attempt to rollback in read-only mode,
because that doesn't make any sense. This should never have been
happening due to the history service level read-only checks, but it
should be enforced at the SQLite connection level now, too.

Avoid initializing tables when opening the database in read-only mode.
This is obviously writing to the database, and now that we really have a
read-only SQLite connection it fails. As it should.

SQLite connection creation will now fail in case the connection is
read-only and the database does not yet exist; it will no longer be
created anyway. So handle this case gracefully. It's fine for the
history service to return nothing in this case. This has the small
advantage that the history thread will quit immediately after it's
created in this case, so it's not constantly running if there's no
history in incognito mode anymore. To check for this condition, we
expose the underlying SQLite error; previously, only the error message
was exposed outside of EphySQLiteConnection. Exposing the error isn't
really necessary or sufficient, though, since it's super generic and we
have to check if the file actually exists on disk anyway.

Test it. Ensure that a read/write history service functions properly if
it's running at the same time as a read-only history service. Using two
read/write services here fails very badly, but when one of the services
is read-only it works fine.

Also, remove the original read-only service test. It only ever tested
that creating a read-only history service with an empty history database
would succeed. And, as I just explained, that fails now.

Lastly, stop running a second history service for the search provider.
It needed its own once upon a time when the search provider did not run
an EphyShell instance. That changed when we stopped using the default
web context, because nothing works without EphyEmbedShell now, as all
sorts of places need it to get the embed's web context. And since
EphyEmbedShell runs its own history service, the search provider can
just use that now instead of running its own separate one.

https://bugzilla.gnome.org/show_bug.cgi?id=778649
parent c88537ed
......@@ -488,10 +488,16 @@ ephy_embed_shell_get_global_history_service (EphyEmbedShell *shell)
if (priv->global_history_service == NULL) {
char *filename;
EphySQLiteConnectionMode mode;
if (priv->mode == EPHY_EMBED_SHELL_MODE_INCOGNITO ||
priv->mode == EPHY_EMBED_SHELL_MODE_SEARCH_PROVIDER)
mode = EPHY_SQLITE_CONNECTION_MODE_READ_ONLY;
else
mode = EPHY_SQLITE_CONNECTION_MODE_READWRITE;
filename = g_build_filename (ephy_dot_dir (), EPHY_HISTORY_FILE, NULL);
priv->global_history_service = ephy_history_service_new (filename,
priv->mode == EPHY_EMBED_SHELL_MODE_INCOGNITO);
priv->global_history_service = ephy_history_service_new (filename, mode);
g_free (filename);
g_return_val_if_fail (priv->global_history_service, NULL);
g_signal_connect (priv->global_history_service, "urls-visited",
......
......@@ -3,7 +3,8 @@ SUBDIRS = widgets egg history
pkglib_LTLIBRARIES = libephymisc.la
TYPES_H_FILES = \
ephy-security-levels.h
ephy-security-levels.h \
ephy-sqlite-connection.h
libephymisc_la_SOURCES = \
ephy-dbus-names.h \
......
......@@ -21,15 +21,27 @@
#include "config.h"
#include "ephy-sqlite-connection.h"
#include "ephy-lib-type-builtins.h"
#include <sqlite3.h>
struct _EphySQLiteConnection {
GObject parent_instance;
sqlite3 *database;
EphySQLiteConnectionMode mode;
};
G_DEFINE_TYPE (EphySQLiteConnection, ephy_sqlite_connection, G_TYPE_OBJECT);
enum {
PROP_0,
PROP_MODE,
LAST_PROP
};
static GParamSpec *obj_properties[LAST_PROP];
static void
ephy_sqlite_connection_finalize (GObject *self)
{
......@@ -37,11 +49,41 @@ ephy_sqlite_connection_finalize (GObject *self)
G_OBJECT_CLASS (ephy_sqlite_connection_parent_class)->finalize (self);
}
static void
ephy_sqlite_connection_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
EphySQLiteConnection *self = EPHY_SQLITE_CONNECTION (object);
switch (property_id) {
case PROP_MODE:
self->mode = g_value_get_enum (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
break;
}
}
static void
ephy_sqlite_connection_class_init (EphySQLiteConnectionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ephy_sqlite_connection_finalize;
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = ephy_sqlite_connection_finalize;
gobject_class->set_property = ephy_sqlite_connection_set_property;
obj_properties[PROP_MODE] =
g_param_spec_enum ("mode",
"SQLite connection mode",
"Whether the SQLite connection is read-only or writable",
EPHY_TYPE_SQ_LITE_CONNECTION_MODE,
EPHY_SQLITE_CONNECTION_MODE_READWRITE,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, LAST_PROP, obj_properties);
}
static void
......@@ -50,7 +92,7 @@ ephy_sqlite_connection_init (EphySQLiteConnection *self)
self->database = NULL;
}
static GQuark get_ephy_sqlite_quark (void)
GQuark ephy_sqlite_error_quark (void)
{
return g_quark_from_static_string ("ephy-sqlite");
}
......@@ -59,13 +101,15 @@ static void
set_error_from_string (const char *string, GError **error)
{
if (error)
*error = g_error_new_literal (get_ephy_sqlite_quark (), 0, string);
*error = g_error_new_literal (ephy_sqlite_error_quark (), 0, string);
}
EphySQLiteConnection *
ephy_sqlite_connection_new (void)
ephy_sqlite_connection_new (EphySQLiteConnectionMode mode)
{
return EPHY_SQLITE_CONNECTION (g_object_new (EPHY_TYPE_SQLITE_CONNECTION, NULL));
return EPHY_SQLITE_CONNECTION (g_object_new (EPHY_TYPE_SQLITE_CONNECTION,
"mode", mode,
NULL));
}
gboolean
......@@ -76,7 +120,11 @@ ephy_sqlite_connection_open (EphySQLiteConnection *self, const gchar *filename,
return FALSE;
}
if (sqlite3_open (filename, &self->database) != SQLITE_OK) {
if (sqlite3_open_v2 (filename,
&self->database,
self->mode == EPHY_SQLITE_CONNECTION_MODE_READ_ONLY ? SQLITE_OPEN_READONLY
: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
NULL) != SQLITE_OK) {
ephy_sqlite_connection_get_error (self, error);
self->database = NULL;
return FALSE;
......@@ -98,7 +146,7 @@ void
ephy_sqlite_connection_get_error (EphySQLiteConnection *self, GError **error)
{
if (error)
*error = g_error_new_literal (get_ephy_sqlite_quark (), 0, sqlite3_errmsg (self->database));
*error = g_error_new_literal (ephy_sqlite_error_quark (), sqlite3_errcode (self->database), sqlite3_errmsg (self->database));
}
gboolean
......@@ -146,18 +194,24 @@ ephy_sqlite_connection_get_last_insert_id (EphySQLiteConnection *self)
gboolean
ephy_sqlite_connection_begin_transaction (EphySQLiteConnection *self, GError **error)
{
if (self->mode == EPHY_SQLITE_CONNECTION_MODE_READ_ONLY)
return TRUE;
return ephy_sqlite_connection_execute (self, "BEGIN TRANSACTION", error);
}
gboolean
ephy_sqlite_connection_rollback_transaction (EphySQLiteConnection *self, GError **error)
{
if (self->mode == EPHY_SQLITE_CONNECTION_MODE_READ_ONLY)
return TRUE;
return ephy_sqlite_connection_execute (self, "ROLLBACK", error);
}
gboolean
ephy_sqlite_connection_commit_transaction (EphySQLiteConnection *self, GError **error)
{
if (self->mode == EPHY_SQLITE_CONNECTION_MODE_READ_ONLY)
return TRUE;
return ephy_sqlite_connection_execute (self, "COMMIT", error);
}
......
......@@ -23,13 +23,20 @@
#include <glib-object.h>
#include "ephy-sqlite-statement.h"
#include <sqlite3.h>
G_BEGIN_DECLS
#define EPHY_TYPE_SQLITE_CONNECTION (ephy_sqlite_connection_get_type ())
G_DECLARE_FINAL_TYPE (EphySQLiteConnection, ephy_sqlite_connection, EPHY, SQLITE_CONNECTION, GObject)
EphySQLiteConnection * ephy_sqlite_connection_new (void);
typedef enum {
EPHY_SQLITE_CONNECTION_MODE_READ_ONLY,
EPHY_SQLITE_CONNECTION_MODE_READWRITE
} EphySQLiteConnectionMode;
EphySQLiteConnection * ephy_sqlite_connection_new (EphySQLiteConnectionMode mode);
gboolean ephy_sqlite_connection_open (EphySQLiteConnection *self, const gchar *filename, GError **error);
void ephy_sqlite_connection_close (EphySQLiteConnection *self);
......@@ -46,4 +53,8 @@ gboolean ephy_sqlite_connection_commit_transaction (EphySQLi
gboolean ephy_sqlite_connection_table_exists (EphySQLiteConnection *self, const char *table_name);
GQuark ephy_sqlite_error_quark (void);
#define EPHY_SQLITE_ERROR ephy_sqlite_error_quark ()
G_END_DECLS
......@@ -274,11 +274,11 @@ ephy_history_service_init (EphyHistoryService *self)
EphyHistoryService *
ephy_history_service_new (const char *history_filename,
gboolean read_only)
EphySQLiteConnectionMode mode)
{
return EPHY_HISTORY_SERVICE (g_object_new (EPHY_TYPE_HISTORY_SERVICE,
"history-filename", history_filename,
"read-only", read_only,
"read-only", mode == EPHY_SQLITE_CONNECTION_MODE_READ_ONLY,
NULL));
}
......@@ -378,18 +378,29 @@ ephy_history_service_open_database_connections (EphyHistoryService *self)
g_assert (self->history_thread == g_thread_self ());
self->history_database = ephy_sqlite_connection_new ();
self->history_database = ephy_sqlite_connection_new (self->read_only ? EPHY_SQLITE_CONNECTION_MODE_READ_ONLY
: EPHY_SQLITE_CONNECTION_MODE_READWRITE);
ephy_sqlite_connection_open (self->history_database, self->history_filename, &error);
if (error) {
g_object_unref (self->history_database);
self->history_database = NULL;
/* Opening the database is expected to fail if it's being opened in read-
* only mode and does not already exist. Otherwise, this is bad. */
if (!self->read_only ||
!g_error_matches (error, EPHY_SQLITE_ERROR, SQLITE_CANTOPEN) ||
g_file_test (self->history_filename, G_FILE_TEST_EXISTS)) {
g_warning ("Could not open history database at %s: %s", self->history_filename, error->message);
}
g_error_free (error);
return FALSE;
}
ephy_history_service_enable_foreign_keys (self);
if (self->read_only)
return TRUE;
ephy_sqlite_connection_begin_transaction (self->history_database, &error);
if (error) {
g_warning ("Could not begin long running transaction in history database: %s", error->message);
......
......@@ -22,7 +22,9 @@
#include <glib-object.h>
#include <gio/gio.h>
#include "ephy-history-types.h"
#include "ephy-sqlite-connection.h"
G_BEGIN_DECLS
......@@ -32,7 +34,7 @@ G_DECLARE_FINAL_TYPE (EphyHistoryService, ephy_history_service, EPHY, HISTORY_SE
typedef void (*EphyHistoryJobCallback) (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data);
EphyHistoryService * ephy_history_service_new (const char *history_filename, gboolean read_only);
EphyHistoryService * ephy_history_service_new (const char *history_filename, EphySQLiteConnectionMode mode);
void ephy_history_service_add_visit (EphyHistoryService *self, EphyHistoryPageVisit *visit, GCancellable *cancellable, EphyHistoryJobCallback callback, gpointer user_data);
void ephy_history_service_add_visits (EphyHistoryService *self, GList *visits, GCancellable *cancellable, EphyHistoryJobCallback callback, gpointer user_data);
......
......@@ -228,7 +228,7 @@ ephy_bookmarks_import_from_firefox (EphyBookmarksManager *manager,
FIREFOX_BOOKMARKS_FILE,
NULL);
connection = ephy_sqlite_connection_new ();
connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READ_ONLY);
ephy_sqlite_connection_open (connection, filename, error);
if (*error) {
g_warning ("Could not open database at %s: %s", filename, (*error)->message);
......
......@@ -222,7 +222,7 @@ migrate_new_urls_table (void)
GError *error = NULL;
filename = g_build_filename (ephy_dot_dir (), EPHY_HISTORY_FILE, NULL);
history_database = ephy_sqlite_connection_new ();
history_database = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE);
ephy_sqlite_connection_open (history_database, filename, &error);
if (error) {
......
......@@ -42,7 +42,6 @@ struct _EphySearchProvider {
GCancellable *cancellable;
GSettings *settings;
EphyHistoryService *history_service;
EphyBookmarksManager *bookmarks_manager;
EphyCompletionModel *model;
};
......@@ -350,6 +349,7 @@ static void
ephy_search_provider_init (EphySearchProvider *self)
{
char *filename;
EphyEmbedShell *shell = ephy_embed_shell_get_default ();
g_application_set_flags (G_APPLICATION (self), G_APPLICATION_IS_SERVICE);
......@@ -369,9 +369,9 @@ ephy_search_provider_init (EphySearchProvider *self)
self->settings = g_settings_new (EPHY_PREFS_SCHEMA);
filename = g_build_filename (ephy_dot_dir (), EPHY_HISTORY_FILE, NULL);
self->history_service = ephy_history_service_new (filename, TRUE);
self->bookmarks_manager = ephy_bookmarks_manager_new ();
self->model = ephy_completion_model_new (self->history_service, self->bookmarks_manager);
self->model = ephy_completion_model_new (EPHY_HISTORY_SERVICE (ephy_embed_shell_get_global_history_service (shell)),
self->bookmarks_manager);
g_free (filename);
self->cancellable = g_cancellable_new ();
......@@ -430,7 +430,6 @@ ephy_search_provider_dispose (GObject *object)
g_clear_object (&self->settings);
g_clear_object (&self->cancellable);
g_clear_object (&self->model);
g_clear_object (&self->history_service);
g_clear_object (&self->bookmarks_manager);
G_OBJECT_CLASS (ephy_search_provider_parent_class)->dispose (object);
......
......@@ -25,29 +25,19 @@
#include <gtk/gtk.h>
static EphyHistoryService *
ensure_empty_history (const char *filename, gboolean readonly)
ensure_empty_history (const char *filename)
{
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
g_unlink (filename);
return ephy_history_service_new (filename, readonly);
return ephy_history_service_new (filename, EPHY_SQLITE_CONNECTION_MODE_READWRITE);
}
static void
test_create_history_service (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
g_free (temporary_file);
g_object_unref (service);
}
static void
test_create_readonly_history_service (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, TRUE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
g_free (temporary_file);
g_object_unref (service);
......@@ -67,7 +57,7 @@ static void
test_create_history_service_and_destroy_later (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
g_free (temporary_file);
g_timeout_add (100, (GSourceFunc)destroy_history_service_and_end_main_loop, service);
......@@ -77,9 +67,12 @@ test_create_history_service_and_destroy_later (void)
static void
page_vist_created (EphyHistoryService *service, gboolean success, gpointer result_data, gpointer user_data)
{
if (user_data != NULL) {
g_assert (EPHY_IS_HISTORY_SERVICE (user_data));
g_object_unref (user_data);
}
g_object_unref (service);
g_assert (result_data == NULL);
g_assert (user_data == NULL);
g_assert (success);
gtk_main_quit ();
}
......@@ -88,7 +81,7 @@ static void
test_create_history_entry (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED);
ephy_history_service_add_visit (service, visit, NULL, page_vist_created, NULL);
......@@ -98,6 +91,29 @@ test_create_history_entry (void)
gtk_main ();
}
static void
test_readonly_mode (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
/* FIXME: This is racy, since we need to be sure the history thread of the
* first EphyHistoryService object has created the database before starting
* the history thread of the second EphyHistoryService object. This is a test
* bug, not an application bug.
*/
EphyHistoryService *service = ensure_empty_history (temporary_file);
EphyHistoryService *readonly_service = ephy_history_service_new (temporary_file, EPHY_SQLITE_CONNECTION_MODE_READ_ONLY);
/* Having the database open read-only should not break normal connections.
* https://bugzilla.gnome.org/show_bug.cgi?id=778649 */
EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED);
ephy_history_service_add_visit (service, visit, NULL, page_vist_created, readonly_service);
ephy_history_page_visit_free (visit);
g_free (temporary_file);
gtk_main ();
}
static GList *
create_test_page_visit_list (void)
{
......@@ -161,7 +177,7 @@ static void
test_create_history_entries (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
GList *visits = create_test_page_visit_list ();
......@@ -210,7 +226,7 @@ static void
test_set_url_title_helper (gboolean test_results)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
EphyHistoryPageVisit *visit = ephy_history_page_visit_new ("http://www.gnome.org", 0, EPHY_PAGE_VISIT_TYPED);
ephy_history_service_add_visit (service, visit, NULL, set_url_title_visit_created, GINT_TO_POINTER (test_results));
......@@ -244,7 +260,7 @@ static void
test_set_url_title_url_not_existent (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
g_free (temporary_file);
ephy_history_service_set_url_title (service, "http://www.gnome.org", "GNOME", NULL, set_url_title_url_not_existent, NULL);
......@@ -286,7 +302,7 @@ static void
test_get_url_helper (gboolean add_entry)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
g_free (temporary_file);
if (add_entry == TRUE) {
......@@ -383,7 +399,7 @@ static void
test_complex_url_query (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
GList *visits;
visits = create_visits_for_complex_tests ();
......@@ -423,7 +439,7 @@ static void
test_complex_url_query_with_time_range (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
GList *visits;
visits = create_visits_for_complex_tests ();
......@@ -472,7 +488,7 @@ static void
test_clear (void)
{
gchar *temporary_file = g_build_filename (g_get_tmp_dir (), "epiphany-history-test.db", NULL);
EphyHistoryService *service = ensure_empty_history (temporary_file, FALSE);
EphyHistoryService *service = ensure_empty_history (temporary_file);
GList *visits = create_test_page_visit_list ();
ephy_history_service_add_visits (service, visits, NULL, NULL, NULL);
......@@ -487,9 +503,9 @@ main (int argc, char *argv[])
gtk_test_init (&argc, &argv);
g_test_add_func ("/embed/history/test_create_history_service", test_create_history_service);
g_test_add_func ("/embed/history/test_create_readonly_history_service", test_create_readonly_history_service);
g_test_add_func ("/embed/history/test_create_history_service_and_destroy_later", test_create_history_service_and_destroy_later);
g_test_add_func ("/embed/history/test_create_history_entry", test_create_history_entry);
g_test_add_func ("/embed/history/test_readonly_mode", test_readonly_mode);
g_test_add_func ("/embed/history/test_create_history_entries", test_create_history_entries);
g_test_add_func ("/embed/history/test_set_url_title", test_set_url_title);
g_test_add_func ("/embed/history/test_set_url_title_is_correct", test_set_url_title_is_correct);
......
......@@ -29,7 +29,7 @@
static EphySQLiteConnection *
ensure_empty_database (const char *filename)
{
EphySQLiteConnection *connection = ephy_sqlite_connection_new ();
EphySQLiteConnection *connection = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE);
GError *error = NULL;
if (g_file_test (filename, G_FILE_TEST_IS_REGULAR))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment