Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <gtk/gtk.h> |
michael@0 | 7 | |
michael@0 | 8 | #include "nsColor.h" |
michael@0 | 9 | #include "nsColorPicker.h" |
michael@0 | 10 | #include "nsGtkUtils.h" |
michael@0 | 11 | #include "nsIWidget.h" |
michael@0 | 12 | #include "WidgetUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | NS_IMPL_ISUPPORTS(nsColorPicker, nsIColorPicker) |
michael@0 | 15 | |
michael@0 | 16 | int nsColorPicker::convertGdkColorComponent(guint16 color_component) { |
michael@0 | 17 | // GdkColor value is in range [0..65535]. We need something in range [0..255] |
michael@0 | 18 | return (color_component * 255 + 127) / 65535; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | guint16 nsColorPicker::convertToGdkColorComponent(int color_component) { |
michael@0 | 22 | return color_component * 65535 / 255; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | GdkColor nsColorPicker::convertToGdkColor(nscolor color) { |
michael@0 | 26 | GdkColor result = { 0 /* obsolete, unused 'pixel' value */, |
michael@0 | 27 | convertToGdkColorComponent(NS_GET_R(color)), |
michael@0 | 28 | convertToGdkColorComponent(NS_GET_G(color)), |
michael@0 | 29 | convertToGdkColorComponent(NS_GET_B(color)) }; |
michael@0 | 30 | |
michael@0 | 31 | return result; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | GtkColorSelection* nsColorPicker::WidgetGetColorSelection(GtkWidget* widget) |
michael@0 | 35 | { |
michael@0 | 36 | return GTK_COLOR_SELECTION(gtk_color_selection_dialog_get_color_selection( |
michael@0 | 37 | GTK_COLOR_SELECTION_DIALOG(widget))); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | /* void init (in nsIDOMWindow parent, in AString title, in short mode); */ |
michael@0 | 41 | NS_IMETHODIMP nsColorPicker::Init(nsIDOMWindow *parent, |
michael@0 | 42 | const nsAString& title, |
michael@0 | 43 | const nsAString& initialColor) |
michael@0 | 44 | { |
michael@0 | 45 | mParentWidget = mozilla::widget::WidgetUtils::DOMWindowToWidget(parent); |
michael@0 | 46 | mTitle = title; |
michael@0 | 47 | mInitialColor = initialColor; |
michael@0 | 48 | |
michael@0 | 49 | return NS_OK; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | /* void open (in nsIColorPickerShownCallback aColorPickerShownCallback); */ |
michael@0 | 53 | NS_IMETHODIMP nsColorPicker::Open(nsIColorPickerShownCallback *aColorPickerShownCallback) |
michael@0 | 54 | { |
michael@0 | 55 | |
michael@0 | 56 | // Input color string should be 7 length (i.e. a string representing a valid |
michael@0 | 57 | // simple color) |
michael@0 | 58 | if (mInitialColor.Length() != 7) { |
michael@0 | 59 | return NS_ERROR_FAILURE; |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | const nsAString& withoutHash = StringTail(mInitialColor, 6); |
michael@0 | 63 | nscolor color; |
michael@0 | 64 | if (!NS_HexToRGB(withoutHash, &color)) { |
michael@0 | 65 | return NS_ERROR_FAILURE; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | GdkColor color_gdk = convertToGdkColor(color); |
michael@0 | 69 | |
michael@0 | 70 | if (mCallback) { |
michael@0 | 71 | // It means Open has already been called: this is not allowed |
michael@0 | 72 | NS_WARNING("mCallback is already set. Open called twice?"); |
michael@0 | 73 | return NS_ERROR_FAILURE; |
michael@0 | 74 | } |
michael@0 | 75 | mCallback = aColorPickerShownCallback; |
michael@0 | 76 | |
michael@0 | 77 | nsXPIDLCString title; |
michael@0 | 78 | title.Adopt(ToNewUTF8String(mTitle)); |
michael@0 | 79 | GtkWidget *color_chooser = gtk_color_selection_dialog_new(title); |
michael@0 | 80 | |
michael@0 | 81 | GtkWindow *parent_window = GTK_WINDOW(mParentWidget->GetNativeData(NS_NATIVE_SHELLWIDGET)); |
michael@0 | 82 | if (parent_window) { |
michael@0 | 83 | GtkWindow *window = GTK_WINDOW(color_chooser); |
michael@0 | 84 | gtk_window_set_transient_for(window, parent_window); |
michael@0 | 85 | gtk_window_set_destroy_with_parent(window, TRUE); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | gtk_color_selection_set_current_color(WidgetGetColorSelection(color_chooser), |
michael@0 | 89 | &color_gdk); |
michael@0 | 90 | |
michael@0 | 91 | NS_ADDREF_THIS(); |
michael@0 | 92 | g_signal_connect(WidgetGetColorSelection(color_chooser), "color-changed", |
michael@0 | 93 | G_CALLBACK(OnColorChanged), this); |
michael@0 | 94 | g_signal_connect(color_chooser, "response", G_CALLBACK(OnResponse), this); |
michael@0 | 95 | g_signal_connect(color_chooser, "destroy", G_CALLBACK(OnDestroy), this); |
michael@0 | 96 | gtk_widget_show(color_chooser); |
michael@0 | 97 | |
michael@0 | 98 | return NS_OK; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /* static */ void |
michael@0 | 102 | nsColorPicker::OnColorChanged(GtkColorSelection* colorselection, |
michael@0 | 103 | gpointer user_data) |
michael@0 | 104 | { |
michael@0 | 105 | static_cast<nsColorPicker*>(user_data)->Update(colorselection); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | /* static */ void |
michael@0 | 109 | nsColorPicker::OnResponse(GtkWidget* color_chooser, gint response_id, |
michael@0 | 110 | gpointer user_data) |
michael@0 | 111 | { |
michael@0 | 112 | static_cast<nsColorPicker*>(user_data)-> |
michael@0 | 113 | Done(color_chooser, response_id); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /* static */ void |
michael@0 | 117 | nsColorPicker::OnDestroy(GtkWidget* color_chooser, gpointer user_data) |
michael@0 | 118 | { |
michael@0 | 119 | static_cast<nsColorPicker*>(user_data)-> |
michael@0 | 120 | Done(color_chooser, GTK_RESPONSE_CANCEL); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | void |
michael@0 | 124 | nsColorPicker::Update(GtkColorSelection* colorselection) |
michael@0 | 125 | { |
michael@0 | 126 | ReadValueFromColorSelection(colorselection); |
michael@0 | 127 | if (mCallback) { |
michael@0 | 128 | mCallback->Update(mColor); |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | void |
michael@0 | 133 | nsColorPicker::Done(GtkWidget* color_chooser, gint response) |
michael@0 | 134 | { |
michael@0 | 135 | switch (response) { |
michael@0 | 136 | case GTK_RESPONSE_OK: |
michael@0 | 137 | case GTK_RESPONSE_ACCEPT: |
michael@0 | 138 | ReadValueFromColorSelection(WidgetGetColorSelection(color_chooser)); |
michael@0 | 139 | break; |
michael@0 | 140 | case GTK_RESPONSE_CANCEL: |
michael@0 | 141 | case GTK_RESPONSE_CLOSE: |
michael@0 | 142 | case GTK_RESPONSE_DELETE_EVENT: |
michael@0 | 143 | mColor = mInitialColor; |
michael@0 | 144 | break; |
michael@0 | 145 | default: |
michael@0 | 146 | NS_WARNING("Unexpected response"); |
michael@0 | 147 | break; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // A "response" signal won't be sent again but "destroy" will be. |
michael@0 | 151 | g_signal_handlers_disconnect_by_func(color_chooser, |
michael@0 | 152 | FuncToGpointer(OnDestroy), this); |
michael@0 | 153 | |
michael@0 | 154 | gtk_widget_destroy(color_chooser); |
michael@0 | 155 | if (mCallback) { |
michael@0 | 156 | mCallback->Done(mColor); |
michael@0 | 157 | mCallback = nullptr; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | NS_RELEASE_THIS(); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | nsString nsColorPicker::ToHexString(int n) |
michael@0 | 164 | { |
michael@0 | 165 | nsString result; |
michael@0 | 166 | if (n <= 0x0F) { |
michael@0 | 167 | result.Append('0'); |
michael@0 | 168 | } |
michael@0 | 169 | result.AppendInt(n, 16); |
michael@0 | 170 | return result; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | void nsColorPicker::ReadValueFromColorSelection(GtkColorSelection* colorselection) |
michael@0 | 174 | { |
michael@0 | 175 | GdkColor rgba; |
michael@0 | 176 | gtk_color_selection_get_current_color(colorselection, &rgba); |
michael@0 | 177 | |
michael@0 | 178 | mColor.AssignLiteral("#"); |
michael@0 | 179 | mColor += ToHexString(convertGdkColorComponent(rgba.red)); |
michael@0 | 180 | mColor += ToHexString(convertGdkColorComponent(rgba.green)); |
michael@0 | 181 | mColor += ToHexString(convertGdkColorComponent(rgba.blue)); |
michael@0 | 182 | } |