⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 211146 in webkit


Ignore:
Timestamp:
Jan 25, 2017, 8:56:00 AM (10 years ago)
Author:
Carlos Garcia Campos
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.

Location:
trunk/Tools
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r211139 r211146  
     12017-01-25  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        [GTK] Add a private browsing mode to MiniBrowser
     4        https://bugs.webkit.org/show_bug.cgi?id=167413
     5
     6        Reviewed by Michael Catanzaro.
     7
     8        Add -p/--private command line option to create a private instance. Also add CTRL+SHIFT+P shortcut to create
     9        private windows, even on non-private instances.
     10
     11        * MiniBrowser/gtk/BrowserWindow.c:
     12        (webViewTitleChanged): Add [Private] to title window for private windows.
     13        (webViewCreate): Pass web context to browser_window_new.
     14        (openPrivateWindow): Create a new ephemeral web view and add it to a new window.
     15        (browserWindowFinalize): Disconnect web context signal handlers.
     16        (browser_window_init): Add shortcut for opening private window.
     17        (browser_window_new): It now receives the context and connect to download-started here.
     18        (browser_window_get_web_context): Return the context.
     19        * MiniBrowser/gtk/BrowserWindow.h:
     20        * MiniBrowser/gtk/main.c:
     21        (createBrowserTab): Create the web view for the window web context.
     22        (aboutDataScriptMessageReceivedCallback): Do not use the default web context, but the window one.
     23        (aboutDataHandleRequest): Ditto.
     24        (aboutURISchemeRequestCallback): Ditto.
     25        (main): Create ephemeral web context if private command line option is used.
     26
    1272017-01-25  Ryosuke Niwa  <rniwa@webkit.org>
    228
  • trunk/Tools/MiniBrowser/gtk/BrowserWindow.c

    r211079 r211146  
    4141    GtkWindow parent;
    4242
     43    WebKitWebContext *webContext;
     44
    4345    GtkAccelGroup *accelGroup;
    4446    GtkWidget *mainBox;
     
    151153{
    152154    const char *title = webkit_web_view_get_title(webView);
    153     gtk_window_set_title(GTK_WINDOW(window), title ? title : defaultWindowTitle);
     155    if (!title)
     156        title = defaultWindowTitle;
     157    char *privateTitle = NULL;
     158    if (webkit_web_view_is_ephemeral(webView))
     159        privateTitle = g_strdup_printf("[Private] %s", title);
     160    gtk_window_set_title(GTK_WINDOW(window), privateTitle ? privateTitle : title);
     161    g_free(privateTitle);
    154162}
    155163
     
    288296    webkit_web_view_set_settings(newWebView, webkit_web_view_get_settings(webView));
    289297
    290     GtkWidget *newWindow = browser_window_new(GTK_WINDOW(window));
     298    GtkWidget *newWindow = browser_window_new(GTK_WINDOW(window), window->webContext);
    291299    browser_window_append_view(BROWSER_WINDOW(newWindow), newWebView);
    292300    g_signal_connect(newWebView, "ready-to-show", G_CALLBACK(webViewReadyToShow), newWindow);
     
    485493}
    486494
     495static void openPrivateWindow(BrowserWindow *window)
     496{
     497    WebKitWebView *webView = browser_tab_get_web_view(window->activeTab);
     498    WebKitWebView *newWebView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
     499        "web-context", webkit_web_view_get_context(webView),
     500        "settings", webkit_web_view_get_settings(webView),
     501        "user-content-manager", webkit_web_view_get_user_content_manager(webView),
     502        "is-ephemeral", TRUE,
     503        NULL));
     504    GtkWidget *newWindow = browser_window_new(GTK_WINDOW(window), window->webContext);
     505    browser_window_append_view(BROWSER_WINDOW(newWindow), newWebView);
     506    gtk_widget_show(GTK_WIDGET(newWindow));
     507}
     508
    487509static void reloadPage(BrowserWindow *window)
    488510{
     
    609631{
    610632    BrowserWindow *window = BROWSER_WINDOW(gObject);
     633
     634    g_signal_handlers_disconnect_matched(window->webContext, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, window);
     635
    611636    if (window->favicon) {
    612637        g_object_unref(window->favicon);
     
    840865    gtk_accel_group_connect(window->accelGroup, GDK_KEY_F12, 0, GTK_ACCEL_VISIBLE,
    841866        g_cclosure_new_swap(G_CALLBACK(toggleWebInspector), window, NULL));
     867    gtk_accel_group_connect(window->accelGroup, GDK_KEY_P, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_ACCEL_VISIBLE,
     868        g_cclosure_new_swap(G_CALLBACK(openPrivateWindow), window, NULL));
    842869
    843870    /* Reload page */
     
    891918        g_cclosure_new_swap(G_CALLBACK(printPage), window, NULL));
    892919
    893     g_signal_connect(webkit_web_context_get_default(), "download-started", G_CALLBACK(downloadStarted), window);
    894 
    895920    GtkWidget *toolbar = gtk_toolbar_new();
    896921    window->toolbar = toolbar;
     
    10181043
    10191044/* Public API. */
    1020 GtkWidget *browser_window_new(GtkWindow *parent)
    1021 {
     1045GtkWidget *browser_window_new(GtkWindow *parent, WebKitWebContext *webContext)
     1046{
     1047    g_return_val_if_fail(WEBKIT_IS_WEB_CONTEXT(webContext), NULL);
     1048
    10221049    BrowserWindow *window = BROWSER_WINDOW(g_object_new(BROWSER_TYPE_WINDOW,
    10231050        "type", GTK_WINDOW_TOPLEVEL, NULL));
    10241051
     1052    window->webContext = webContext;
     1053    g_signal_connect(window->webContext, "download-started", G_CALLBACK(downloadStarted), window);
    10251054    if (parent) {
    10261055        window->parentWindow = parent;
     
    10291058
    10301059    return GTK_WIDGET(window);
     1060}
     1061
     1062WebKitWebContext *browser_window_get_web_context(BrowserWindow *window)
     1063{
     1064    g_return_val_if_fail(BROWSER_IS_WINDOW(window), NULL);
     1065
     1066    return window->webContext;
    10311067}
    10321068
  • trunk/Tools/MiniBrowser/gtk/BrowserWindow.h

    r203271 r211146  
    4646GType browser_window_get_type(void);
    4747
    48 GtkWidget* browser_window_new(GtkWindow*);
     48GtkWidget* browser_window_new(GtkWindow*, WebKitWebContext*);
     49WebKitWebContext* browser_window_get_web_context(BrowserWindow*);
    4950void browser_window_append_view(BrowserWindow*, WebKitWebView*);
    5051void browser_window_load_uri(BrowserWindow*, const char *uri);
  • trunk/Tools/MiniBrowser/gtk/main.c

    r211079 r211146  
    4242static const char *sessionFile;
    4343static char *geometry;
     44static gboolean privateMode;
    4445
    4546typedef enum {
     
    6364static WebKitWebView *createBrowserTab(BrowserWindow *window, WebKitSettings *webkitSettings, WebKitUserContentManager *userContentManager)
    6465{
    65     WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new_with_user_content_manager(userContentManager));
    66     if (webkitSettings)
    67         webkit_web_view_set_settings(webView, webkitSettings);
     66    WebKitWebView *webView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
     67        "web-context", browser_window_get_web_context(window),
     68        "settings", webkitSettings,
     69        "user-content-manager", userContentManager,
     70        NULL));
     71
    6872    if (editorMode)
    6973        webkit_web_view_set_editable(webView, TRUE);
     
    9195    { "session-file", 's', 0, G_OPTION_ARG_FILENAME, &sessionFile, "Session file", "FILE" },
    9296    { "geometry", 'g', 0, G_OPTION_ARG_STRING, &geometry, "Set the size and position of the window (WIDTHxHEIGHT+X+Y)", "GEOMETRY" },
     97    { "private", 'p', 0, G_OPTION_ARG_NONE, &privateMode, "Run in private browsing mode", NULL },
    9398    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &uriArguments, 0, "[URL…]" },
    9499    { 0, 0, 0, 0, 0, 0, 0 }
     
    279284}
    280285
    281 static void aboutDataScriptMessageReceivedCallback(WebKitUserContentManager *userContentManager, WebKitJavascriptResult *message)
     286static void aboutDataScriptMessageReceivedCallback(WebKitUserContentManager *userContentManager, WebKitJavascriptResult *message, WebKitWebContext *webContext)
    282287{
    283288    JSValueRef jsValue = webkit_javascript_result_get_value(message);
     
    308313    }
    309314
    310     WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webkit_web_context_get_default());
     315    WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webContext);
    311316    guint64 types = g_ascii_strtoull(tokens[1], NULL, 10);
    312317    if (tokenCount == 2)
     
    408413}
    409414
    410 static void aboutDataHandleRequest(WebKitURISchemeRequest *request)
     415static void aboutDataHandleRequest(WebKitURISchemeRequest *request, WebKitWebContext *webContext)
    411416{
    412417    AboutDataRequest *dataRequest = aboutDataRequestNew(request);
    413     WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webkit_web_context_get_default());
     418    WebKitWebsiteDataManager *manager = webkit_web_context_get_website_data_manager(webContext);
    414419    webkit_website_data_manager_fetch(manager, WEBKIT_WEBSITE_DATA_ALL, NULL, (GAsyncReadyCallback)gotWebsiteDataCallback, dataRequest);
    415420}
    416421
    417 static void aboutURISchemeRequestCallback(WebKitURISchemeRequest *request, gpointer userData)
     422static void aboutURISchemeRequestCallback(WebKitURISchemeRequest *request, WebKitWebContext *webContext)
    418423{
    419424    GInputStream *stream;
     
    435440        g_object_unref(stream);
    436441    } else if (!g_strcmp0(path, "data"))
    437         aboutDataHandleRequest(request);
     442        aboutDataHandleRequest(request, webContext);
    438443    else {
    439444        error = g_error_new(MINI_BROWSER_ERROR, MINI_BROWSER_ERROR_INVALID_ABOUT_PATH, "Invalid about:%s page.", path);
     
    449454    g_setenv("WEBKIT_INJECTED_BUNDLE_PATH", WEBKIT_INJECTED_BUNDLE_PATH, FALSE);
    450455#endif
    451 
    452     const gchar *singleprocess = g_getenv("MINIBROWSER_SINGLEPROCESS");
    453     webkit_web_context_set_process_model(webkit_web_context_get_default(), (singleprocess && *singleprocess) ?
    454         WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS : WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
    455456
    456457    GOptionContext *context = g_option_context_new(NULL);
     
    475476    g_option_context_free (context);
    476477
     478    WebKitWebContext *webContext = privateMode ? webkit_web_context_new_ephemeral() : webkit_web_context_get_default();
     479
     480    const gchar *singleprocess = g_getenv("MINIBROWSER_SINGLEPROCESS");
     481    webkit_web_context_set_process_model(webContext, (singleprocess && *singleprocess) ?
     482        WEBKIT_PROCESS_MODEL_SHARED_SECONDARY_PROCESS : WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
     483
    477484    // Enable the favicon database, by specifying the default directory.
    478     webkit_web_context_set_favicon_database_directory(webkit_web_context_get_default(), NULL);
    479 
    480     webkit_web_context_register_uri_scheme(webkit_web_context_get_default(), BROWSER_ABOUT_SCHEME, aboutURISchemeRequestCallback, NULL, NULL);
     485    webkit_web_context_set_favicon_database_directory(webContext, NULL);
     486
     487    webkit_web_context_register_uri_scheme(webContext, BROWSER_ABOUT_SCHEME, (WebKitURISchemeRequestCallback)aboutURISchemeRequestCallback, webContext, NULL);
    481488
    482489    WebKitUserContentManager *userContentManager = webkit_user_content_manager_new();
    483490    webkit_user_content_manager_register_script_message_handler(userContentManager, "aboutData");
    484     g_signal_connect(userContentManager, "script-message-received::aboutData", G_CALLBACK(aboutDataScriptMessageReceivedCallback), NULL);
    485 
    486     BrowserWindow *mainWindow = BROWSER_WINDOW(browser_window_new(NULL));
     491    g_signal_connect(userContentManager, "script-message-received::aboutData", G_CALLBACK(aboutDataScriptMessageReceivedCallback), webContext);
     492
     493    BrowserWindow *mainWindow = BROWSER_WINDOW(browser_window_new(NULL, webContext));
    487494    if (geometry)
    488495        gtk_window_parse_geometry(GTK_WINDOW(mainWindow), geometry);
     
    523530    gtk_main();
    524531
     532    if (privateMode)
     533        g_object_unref(webContext);
     534
    525535    return 0;
    526536}
Note: See TracChangeset for help on using the changeset viewer.