toolkit/system/dbus/nsDBusService.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim:expandtab:shiftwidth=4:tabstop=4:
michael@0 3 */
michael@0 4 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #include "nsDBusService.h"
michael@0 9 #include "nsComponentManagerUtils.h"
michael@0 10 #include "nsAutoPtr.h"
michael@0 11
michael@0 12 #include <glib.h>
michael@0 13 #include <dbus/dbus-glib-lowlevel.h>
michael@0 14 #include <dbus/dbus-glib.h>
michael@0 15
michael@0 16 nsDBusService::nsDBusService() {
michael@0 17 mConnection = nullptr;
michael@0 18 mSingleClient = nullptr;
michael@0 19 }
michael@0 20
michael@0 21 nsDBusService::~nsDBusService() {
michael@0 22 NS_ASSERTION(!mSingleClient, "Client failed to unregister");
michael@0 23 DropConnection();
michael@0 24 if (mReconnectTimer) {
michael@0 25 mReconnectTimer->Cancel();
michael@0 26 }
michael@0 27 gSingleton = nullptr;
michael@0 28 }
michael@0 29
michael@0 30 NS_IMPL_ISUPPORTS(nsDBusService, nsDBusService)
michael@0 31
michael@0 32 nsDBusService* nsDBusService::gSingleton = nullptr;
michael@0 33
michael@0 34 already_AddRefed<nsDBusService>
michael@0 35 nsDBusService::Get() {
michael@0 36 if (!gSingleton) {
michael@0 37 gSingleton = new nsDBusService();
michael@0 38 }
michael@0 39 nsRefPtr<nsDBusService> ret = gSingleton;
michael@0 40 return ret.forget();
michael@0 41 }
michael@0 42
michael@0 43 nsresult
michael@0 44 nsDBusService::AddClient(DBusClient* client) {
michael@0 45 NS_ASSERTION(!mSingleClient, "Only one client supported right now");
michael@0 46 mSingleClient = client;
michael@0 47 nsresult rv = CreateConnection();
michael@0 48 if (NS_FAILED(rv)) {
michael@0 49 mSingleClient = nullptr;
michael@0 50 }
michael@0 51 return rv;
michael@0 52 }
michael@0 53
michael@0 54 void
michael@0 55 nsDBusService::RemoveClient(DBusClient* client) {
michael@0 56 NS_ASSERTION(mSingleClient == client, "Removing wrong client");
michael@0 57 mSingleClient = nullptr;
michael@0 58 }
michael@0 59
michael@0 60 DBusPendingCall*
michael@0 61 nsDBusService::SendWithReply(DBusClient* client, DBusMessage* message) {
michael@0 62 DBusPendingCall* reply = nullptr;
michael@0 63 if (mConnection) {
michael@0 64 if (!dbus_connection_send_with_reply(mConnection, message, &reply, -1)) {
michael@0 65 reply = nullptr;
michael@0 66 }
michael@0 67 }
michael@0 68 dbus_message_unref(message);
michael@0 69 return reply;
michael@0 70 }
michael@0 71
michael@0 72 bool nsDBusService::HandleMessage(DBusMessage* message) {
michael@0 73 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
michael@0 74 "Disconnected")) {
michael@0 75 HandleDBusDisconnect();
michael@0 76 return false;
michael@0 77 }
michael@0 78
michael@0 79 return mSingleClient && mSingleClient->HandleMessage(message);
michael@0 80 }
michael@0 81
michael@0 82 static DBusHandlerResult dbus_filter(DBusConnection* connection,
michael@0 83 DBusMessage* message,
michael@0 84 void* user_data) {
michael@0 85 return static_cast<nsDBusService*>(user_data)->HandleMessage(message)
michael@0 86 ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
michael@0 87 }
michael@0 88
michael@0 89 void nsDBusService::DoTimerCallback(nsITimer *aTimer) {
michael@0 90 if (aTimer == mReconnectTimer.get()) {
michael@0 91 nsresult rv = CreateConnection();
michael@0 92 if (NS_SUCCEEDED(rv)) {
michael@0 93 mReconnectTimer->Cancel();
michael@0 94 mReconnectTimer = nullptr;
michael@0 95 }
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 static void TimerCallback(nsITimer *aTimer, void *aClosure) {
michael@0 100 static_cast<nsDBusService*>(aClosure)->DoTimerCallback(aTimer);
michael@0 101 }
michael@0 102
michael@0 103 void nsDBusService::DropConnection() {
michael@0 104 if (mConnection) {
michael@0 105 dbus_connection_remove_filter(mConnection, dbus_filter, this);
michael@0 106 if (mSingleClient) {
michael@0 107 mSingleClient->UnregisterWithConnection(mConnection);
michael@0 108 }
michael@0 109 dbus_connection_unref(mConnection);
michael@0 110 mConnection = nullptr;
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 void nsDBusService::HandleDBusDisconnect() {
michael@0 115 DropConnection();
michael@0 116
michael@0 117 nsresult rv;
michael@0 118 mReconnectTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
michael@0 119 if (NS_FAILED(rv))
michael@0 120 return;
michael@0 121 rv = mReconnectTimer->InitWithFuncCallback(TimerCallback, this,
michael@0 122 5000, nsITimer::TYPE_REPEATING_SLACK);
michael@0 123 if (NS_FAILED(rv)) {
michael@0 124 mReconnectTimer = nullptr;
michael@0 125 return;
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 nsresult nsDBusService::CreateConnection() {
michael@0 130 mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr);
michael@0 131 if (!mConnection)
michael@0 132 return NS_ERROR_FAILURE;
michael@0 133
michael@0 134 dbus_connection_set_exit_on_disconnect(mConnection, false);
michael@0 135 dbus_connection_setup_with_g_main(mConnection, nullptr);
michael@0 136
michael@0 137 if (!dbus_connection_add_filter(mConnection, dbus_filter, this, nullptr))
michael@0 138 return NS_ERROR_FAILURE;
michael@0 139
michael@0 140 mSingleClient->RegisterWithConnection(mConnection);
michael@0 141 return NS_OK;
michael@0 142 }

mercurial