Title: [211146] trunk/Tools
Revision
211146
Author
[email protected]
Date
2017-01-25 08:56:00 -0800 (Wed, 25 Jan 2017)

Log Message

[GTK] Add a private browsing mode to MiniBrowser
https://bugs.webkit.org/show_bug.cgi?id=167413

Reviewed by Michael Catanzaro.

Add -p/--private command line option to create a private instance. Also add CTRL+SHIFT+P shortcut to create
private windows, even on non-private instances.

* MiniBrowser/gtk/BrowserWindow.c:
(webViewTitleChanged): Add [Private] to title window for private windows.
(webViewCreate): Pass web context to browser_window_new.
(openPrivateWindow): Create a new ephemeral web view and add it to a new window.
(browserWindowFinalize): Disconnect web context signal handlers.
(browser_window_init): Add shortcut for opening private window.
(browser_window_new): It now receives the context and connect to download-started here.
(browser_window_get_web_context): Return the context.
* MiniBrowser/gtk/BrowserWindow.h:
* MiniBrowser/gtk/main.c:
(createBrowserTab): Create the web view for the window web context.
(aboutDataScriptMessageReceivedCallback): Do not use the default web context, but the window one.
(aboutDataHandleRequest): Ditto.
(aboutURISchemeRequestCallback): Ditto.
(main): Create ephemeral web context if private command line option is used.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (211145 => 211146)


--- trunk/Tools/ChangeLog	2017-01-25 16:54:28 UTC (rev 211145)
+++ trunk/Tools/ChangeLog	2017-01-25 16:56:00 UTC (rev 211146)
@@ -1,3 +1,29 @@
+2017-01-25  Carlos Garcia Campos  <[email protected]>
+
+        [GTK] Add a private browsing mode to MiniBrowser
+        https://bugs.webkit.org/show_bug.cgi?id=167413
+
+        Reviewed by Michael Catanzaro.
+
+        Add -p/--private command line option to create a private instance. Also add CTRL+SHIFT+P shortcut to create
+        private windows, even on non-private instances.
+
+        * MiniBrowser/gtk/BrowserWindow.c:
+        (webViewTitleChanged): Add [Private] to title window for private windows.
+        (webViewCreate): Pass web context to browser_window_new.
+        (openPrivateWindow): Create a new ephemeral web view and add it to a new window.
+        (browserWindowFinalize): Disconnect web context signal handlers.
+        (browser_window_init): Add shortcut for opening private window.
+        (browser_window_new): It now receives the context and connect to download-started here.
+        (browser_window_get_web_context): Return the context.
+        * MiniBrowser/gtk/BrowserWindow.h:
+        * MiniBrowser/gtk/main.c:
+        (createBrowserTab): Create the web view for the window web context.
+        (aboutDataScriptMessageReceivedCallback): Do not use the default web context, but the window one.
+        (aboutDataHandleRequest): Ditto.
+        (aboutURISchemeRequestCallback): Ditto.
+        (main): Create ephemeral web context if private command line option is used.
+
 2017-01-25  Ryosuke Niwa  <[email protected]>
 
         collectMatchingElementsInFlatTree should not find elements inside an user agent shadow tree

Modified: trunk/Tools/MiniBrowser/gtk/BrowserWindow.c (211145 => 211146)


--- trunk/Tools/MiniBrowser/gtk/BrowserWindow.c	2017-01-25 16:54:28 UTC (rev 211145)
+++ trunk/Tools/MiniBrowser/gtk/BrowserWindow.c	2017-01-25 16:56:00 UTC (rev 211146)
@@ -40,6 +40,8 @@
 struct _BrowserWindow {
     GtkWindow parent;
 
+    WebKitWebContext *webContext;
+
     GtkAccelGroup *accelGroup;
     GtkWidget *mainBox;
     GtkWidget *toolbar;
@@ -150,7 +152,13 @@
 static void webViewTitleChanged(WebKitWebView *webView, GParamSpec *pspec, BrowserWindow *window)
 {
     const char *title = webkit_web_view_get_title(webView);
-    gtk_window_set_title(GTK_WINDOW(window), title ? title : defaultWindowTitle);
+    if (!title)
+        title = defaultWindowTitle;
+    char *privateTitle = NULL;
+    if (webkit_web_view_is_ephemeral(webView))
+        privateTitle = g_strdup_printf("[Private] %s", title);
+    gtk_window_set_title(GTK_WINDOW(window), privateTitle ? privateTitle : title);
+    g_free(privateTitle);
 }
 
 static gboolean resetEntryProgress(BrowserWindow *window)
@@ -287,7 +295,7 @@
     WebKitWebView *newWebView = WEBKIT_WEB_VIEW(webkit_web_view_new_with_related_view(webView));
     webkit_web_view_set_settings(newWebView, webkit_web_view_get_settings(webView));
 
-    GtkWidget *newWindow = browser_window_new(GTK_WINDOW(window));
+    GtkWidget *newWindow = browser_window_new(GTK_WINDOW(window), window->webContext);
     browser_window_append_view(BROWSER_WINDOW(newWindow), newWebView);
     g_signal_connect(newWebView, "ready-to-show", G_CALLBACK(webViewReadyToShow), newWindow);
     g_signal_connect(newWebView, "run-as-modal", G_CALLBACK(webViewRunAsModal), newWindow);
@@ -484,6 +492,20 @@
     browser_tab_toggle_inspector(window->activeTab);
 }
 
+static void openPrivateWindow(BrowserWindow *window)
+{
+    WebKitWebView *webView = browser_tab_get_web_view(window->activeTab);
+    WebKitWebView *newWebView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
+        "web-context", webkit_web_view_get_context(webView),
+        "settings", webkit_web_view_get_settings(webView),
+        "user-content-manager", webkit_web_view_get_user_content_manager(webView),
+        "is-ephemeral", TRUE,
+        NULL));
+    GtkWidget *newWindow = browser_window_new(GTK_WINDOW(window), window->webContext);
+    browser_window_append_view(BROWSER_WINDOW(newWindow), newWebView);
+    gtk_widget_show(GTK_WIDGET(newWindow));
+}
+
 static void reloadPage(BrowserWindow *window)
 {
     WebKitWebView *webView = browser_tab_get_web_view(window->activeTab);
@@ -608,6 +630,9 @@
 static void browserWindowFinalize(GObject *gObject)
 {
     BrowserWindow *window = BROWSER_WINDOW(gObject);
+
+    g_signal_handlers_disconnect_matched(window->webContext, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, window);
+
     if (window->favicon) {
         g_object_unref(window->favicon);
         window->favicon = NULL;
@@ -839,6 +864,8 @@
         g_cclosure_new_swap(G_CALLBACK(toggleWebInspector), window, NULL));
     gtk_accel_group_connect(window->accelGroup, GDK_KEY_F12, 0, GTK_ACCEL_VISIBLE,
         g_cclosure_new_swap(G_CALLBACK(toggleWebInspector), window, NULL));
+    gtk_accel_group_connect(window->accelGroup, GDK_KEY_P, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE,
+        g_cclosure_new_swap(G_CALLBACK(openPrivateWindow), window, NULL));
 
     /* Reload page */
     gtk_accel_group_connect(window->accelGroup, GDK_KEY_F5, 0, GTK_ACCEL_VISIBLE,
@@ -890,8 +917,6 @@
     gtk_accel_group_connect(window->accelGroup, GDK_KEY_P, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE,
         g_cclosure_new_swap(G_CALLBACK(printPage), window, NULL));
 
-    g_signal_connect(webkit_web_context_get_default(), "download-started", G_CALLBACK(downloadStarted), window);
-
     GtkWidget *toolbar = gtk_toolbar_new();
     window->toolbar = toolbar;
     gtk_orientable_set_orientation(GTK_ORIENTABLE(toolbar), GTK_ORIENTATION_HORIZONTAL);
@@ -1017,11 +1042,15 @@
 }
 
 /* Public API. */
-GtkWidget *browser_window_new(GtkWindow *parent)
+GtkWidget *browser_window_new(GtkWindow *parent, WebKitWebContext *webContext)
 {
+    g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(webContext), NULL);
+
     BrowserWindow *window = BROWSER_WINDOW(g_object_new(BROWSER_TYPE_WINDOW,
         "type", GTK_WINDOW_TOPLEVEL, NULL));
 
+    window->webContext = webContext;
+    g_signal_connect(window->webContext, "download-started", G_CALLBACK(downloadStarted), window);
     if (parent) {
         window->parentWindow = parent;
         g_object_add_weak_pointer(G_OBJECT(parent), (gpointer *)&window->parentWindow);
@@ -1030,6 +1059,13 @@
     return GTK_WIDGET(window);
 }
 
+WebKitWebContext *browser_window_get_web_context(BrowserWindow *window)
+{
+    g_return_val_if_fail(BROWSER_IS_WINDOW(window), NULL);
+
+    return window->webContext;
+}
+
 void browser_window_append_view(BrowserWindow *window, WebKitWebView *webView)
 {
     g_return_if_fail(BROWSER_IS_WINDOW(window));

Modified: trunk/Tools/MiniBrowser/gtk/BrowserWindow.h (211145 => 211146)


--- trunk/Tools/MiniBrowser/gtk/BrowserWindow.h	2017-01-25 16:54:28 UTC (rev 211145)
+++ trunk/Tools/MiniBrowser/gtk/BrowserWindow.h	2017-01-25 16:56:00 UTC (rev 211146)
@@ -45,7 +45,8 @@
 
 GType browser_window_get_type(void);
 
-GtkWidget* browser_window_new(GtkWindow*);
+GtkWidget* browser_window_new(GtkWindow*, WebKitWebContext*);
+WebKitWebContext* browser_window_get_web_context(BrowserWindow*);
 void browser_window_append_view(BrowserWindow*, WebKitWebView*);
 void browser_window_load_uri(BrowserWindow*, const char *uri);
 void browser_window_load_session(BrowserWindow *, const char *sessionFile);

Modified: trunk/Tools/MiniBrowser/gtk/main.c (211145 => 211146)


--- trunk/Tools/MiniBrowser/gtk/main.c	2017-01-25 16:54:28 UTC (rev 211145)
+++ trunk/Tools/MiniBrowser/gtk/main.c	2017-01-25 16:56:00 UTC (rev 211146)
@@ -41,6 +41,7 @@
 static gboolean editorMode;
 static const char *sessionFile;
 static char *geometry;
+static gboolean privateMode;
 
 typedef enum {
     MINI_BROWSER_ERROR_INVALID_ABOUT_PATH
@@ -62,9 +63,12 @@
 
 static WebKitWebView *createBrowserTab(BrowserWindow *window, WebKitSettings *webkitSettings, WebKitUserContentManager *userContentManager)
 {
-    WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new_with_user_content_manager(userContentManager));
-    if (webkitSettings)
-        webkit_web_view_set_settings(webView, webkitSettings);
+    WebKitWebView *webView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
+        "web-context", browser_window_get_web_context(window),
+        "settings", webkitSettings,
+        "user-content-manager", userContentManager,
+        NULL));
+
     if (editorMode)
         webkit_web_view_set_editable(webView, TRUE);
 
@@ -90,6 +94,7 @@
     { "editor-mode", 'e', 0, G_OPTION_ARG_NONE, &editorMode, "Run in editor mode", NULL },
     { "session-file", 's', 0, G_OPTION_ARG_FILENAME, &sessionFile, "Session file", "FILE" },
     { "geometry", 'g', 0, G_OPTION_ARG_STRING, &geometry, "Set the size and position of the window (WIDTHxHEIGHT+X+Y)", "GEOMETRY" },
+    { "private", 'p', 0, G_OPTION_ARG_NONE, &privateMode, "Run in private browsing mode", NULL },
     { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL…]" },
     { 0, 0, 0, 0, 0, 0, 0 }
 };
@@ -278,7 +283,7 @@
         webkit_web_view_reload(webkit_uri_scheme_request_get_web_view(dataRequest->request));
 }
 
-static void aboutDataScriptMessageReceivedCallback(WebKitUserContentManager *userContentManager, WebKitJavascriptResult *message)
+static void aboutDataScriptMessageReceivedCallback(WebKitUserContentManager *userContentManager, WebKitJavascriptResult *message, WebKitWebContext *webContext)
 {
     JSValueRef jsValue = webkit_javascript_result_get_value(message);
     JSStringRef jsString = JSValueToStringCopy(webkit_javascript_result_get_global_context(message), jsValue, NULL);
@@ -307,7 +312,7 @@
         return;
     }
 
-    WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webkit_web_context_get_default());
+    WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webContext);
     guint64 types = g_ascii_strtoull(tokens[1], NULL, 10);
     if (tokenCount == 2)
         webkit_website_data_manager_clear(manager, types, 0, NULL, (GAsyncReadyCallback)websiteDataClearedCallback, dataRequest);
@@ -407,14 +412,14 @@
     g_list_free_full(dataList, (GDestroyNotify)webkit_website_data_unref);
 }
 
-static void aboutDataHandleRequest(WebKitURISchemeRequest *request)
+static void aboutDataHandleRequest(WebKitURISchemeRequest *request, WebKitWebContext *webContext)
 {
     AboutDataRequest *dataRequest = aboutDataRequestNew(request);
-    WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webkit_web_context_get_default());
+    WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webContext);
     webkit_website_data_manager_fetch(manager, WEBKIT_WEBSITE_DATA_ALL, NULL, (GAsyncReadyCallback)gotWebsiteDataCallback, dataRequest);
 }
 
-static void aboutURISchemeRequestCallback(WebKitURISchemeRequest *request, gpointer userData)
+static void aboutURISchemeRequestCallback(WebKitURISchemeRequest *request, WebKitWebContext *webContext)
 {
     GInputStream *stream;
     gsize streamLength;
@@ -434,7 +439,7 @@
         webkit_uri_scheme_request_finish(request, stream, streamLength, "text/html");
         g_object_unref(stream);
     } else if (!g_strcmp0(path, "data"))
-        aboutDataHandleRequest(request);
+        aboutDataHandleRequest(request, webContext);
     else {
         error = g_error_new(MINI_BROWSER_ERROR, MINI_BROWSER_ERROR_INVALID_ABOUT_PATH, "Invalid about:%s page.", path);
         webkit_uri_scheme_request_finish_error(request, error);
@@ -449,10 +454,6 @@
     g_setenv("WEBKIT_INJECTED_BUNDLE_PATH", WEBKIT_INJECTED_BUNDLE_PATH, FALSE);
 #endif
 
-    const gchar *singleprocess = g_getenv("MINIBROWSER_SINGLEPROCESS");
-    webkit_web_context_set_process_model(webkit_web_context_get_default(), (singleprocess && *singleprocess) ?
-        WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS : WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
-
     GOptionContext *context = g_option_context_new(NULL);
     g_option_context_add_main_entries(context, commandLineOptions, 0);
     g_option_context_add_group(context, gtk_get_option_group(TRUE));
@@ -474,16 +475,22 @@
     }
     g_option_context_free (context);
 
+    WebKitWebContext *webContext = privateMode ? webkit_web_context_new_ephemeral() : webkit_web_context_get_default();
+
+    const gchar *singleprocess = g_getenv("MINIBROWSER_SINGLEPROCESS");
+    webkit_web_context_set_process_model(webContext, (singleprocess && *singleprocess) ?
+        WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS : WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
+
     // Enable the favicon database, by specifying the default directory.
-    webkit_web_context_set_favicon_database_directory(webkit_web_context_get_default(), NULL);
+    webkit_web_context_set_favicon_database_directory(webContext, NULL);
 
-    webkit_web_context_register_uri_scheme(webkit_web_context_get_default(), BROWSER_ABOUT_SCHEME, aboutURISchemeRequestCallback, NULL, NULL);
+    webkit_web_context_register_uri_scheme(webContext, BROWSER_ABOUT_SCHEME, (WebKitURISchemeRequestCallback)aboutURISchemeRequestCallback, webContext, NULL);
 
     WebKitUserContentManager *userContentManager = webkit_user_content_manager_new();
     webkit_user_content_manager_register_script_message_handler(userContentManager, "aboutData");
-    g_signal_connect(userContentManager, "script-message-received::aboutData", G_CALLBACK(aboutDataScriptMessageReceivedCallback), NULL);
+    g_signal_connect(userContentManager, "script-message-received::aboutData", G_CALLBACK(aboutDataScriptMessageReceivedCallback), webContext);
 
-    BrowserWindow *mainWindow = BROWSER_WINDOW(browser_window_new(NULL));
+    BrowserWindow *mainWindow = BROWSER_WINDOW(browser_window_new(NULL, webContext));
     if (geometry)
         gtk_window_parse_geometry(GTK_WINDOW(mainWindow), geometry);
 
@@ -522,5 +529,8 @@
 
     gtk_main();
 
+    if (privateMode)
+        g_object_unref(webContext);
+
     return 0;
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to