uriloader/exthandler/nsDBusHandlerApp.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim:expandtab:shiftwidth=2:tabstop=2:cin:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <dbus/dbus.h>
michael@0 8 #include "nsDBusHandlerApp.h"
michael@0 9 #include "nsIURI.h"
michael@0 10 #include "nsIClassInfoImpl.h"
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "nsCExternalHandlerService.h"
michael@0 13
michael@0 14 // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
michael@0 15 // here too?
michael@0 16 NS_IMPL_CLASSINFO(nsDBusHandlerApp, nullptr, 0, NS_DBUSHANDLERAPP_CID)
michael@0 17 NS_IMPL_ISUPPORTS_CI(nsDBusHandlerApp, nsIDBusHandlerApp, nsIHandlerApp)
michael@0 18
michael@0 19 ////////////////////////////////////////////////////////////////////////////////
michael@0 20 //// nsIHandlerApp
michael@0 21
michael@0 22 NS_IMETHODIMP nsDBusHandlerApp::GetName(nsAString& aName)
michael@0 23 {
michael@0 24 aName.Assign(mName);
michael@0 25 return NS_OK;
michael@0 26 }
michael@0 27
michael@0 28 NS_IMETHODIMP nsDBusHandlerApp::SetName(const nsAString & aName)
michael@0 29 {
michael@0 30 mName.Assign(aName);
michael@0 31 return NS_OK;
michael@0 32 }
michael@0 33
michael@0 34 NS_IMETHODIMP nsDBusHandlerApp::SetDetailedDescription(const nsAString & aDescription)
michael@0 35 {
michael@0 36 mDetailedDescription.Assign(aDescription);
michael@0 37
michael@0 38 return NS_OK;
michael@0 39 }
michael@0 40
michael@0 41 NS_IMETHODIMP nsDBusHandlerApp::GetDetailedDescription(nsAString& aDescription)
michael@0 42 {
michael@0 43 aDescription.Assign(mDetailedDescription);
michael@0 44
michael@0 45 return NS_OK;
michael@0 46 }
michael@0 47
michael@0 48 NS_IMETHODIMP
michael@0 49 nsDBusHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
michael@0 50 {
michael@0 51 NS_ENSURE_ARG_POINTER(aHandlerApp);
michael@0 52
michael@0 53 // If the handler app isn't a dbus handler app, then it's not the same app.
michael@0 54 nsCOMPtr<nsIDBusHandlerApp> dbusHandlerApp = do_QueryInterface(aHandlerApp);
michael@0 55 if (!dbusHandlerApp) {
michael@0 56 *_retval = false;
michael@0 57 return NS_OK;
michael@0 58 }
michael@0 59 nsAutoCString service;
michael@0 60 nsAutoCString method;
michael@0 61
michael@0 62 nsresult rv = dbusHandlerApp->GetService(service);
michael@0 63 if (NS_FAILED(rv)) {
michael@0 64 *_retval = false;
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67 rv = dbusHandlerApp->GetMethod(method);
michael@0 68 if (NS_FAILED(rv)) {
michael@0 69 *_retval = false;
michael@0 70 return NS_OK;
michael@0 71 }
michael@0 72
michael@0 73 *_retval = service.Equals(mService) && method.Equals(mMethod);
michael@0 74 return NS_OK;
michael@0 75 }
michael@0 76
michael@0 77 NS_IMETHODIMP
michael@0 78 nsDBusHandlerApp::LaunchWithURI(nsIURI *aURI,
michael@0 79 nsIInterfaceRequestor *aWindowContext)
michael@0 80 {
michael@0 81 nsAutoCString spec;
michael@0 82 nsresult rv = aURI->GetAsciiSpec(spec);
michael@0 83 NS_ENSURE_SUCCESS(rv,rv);
michael@0 84 const char* uri = spec.get();
michael@0 85
michael@0 86 DBusError err;
michael@0 87 dbus_error_init(&err);
michael@0 88
michael@0 89 DBusConnection *connection;
michael@0 90 connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
michael@0 91 if (dbus_error_is_set(&err)) {
michael@0 92 dbus_error_free(&err);
michael@0 93 return NS_ERROR_FAILURE;
michael@0 94 }
michael@0 95 if (nullptr == connection) {
michael@0 96 return NS_ERROR_FAILURE;
michael@0 97 }
michael@0 98 dbus_connection_set_exit_on_disconnect(connection,false);
michael@0 99
michael@0 100 DBusMessage* msg;
michael@0 101 msg = dbus_message_new_method_call(mService.get(),
michael@0 102 mObjpath.get(),
michael@0 103 mInterface.get(),
michael@0 104 mMethod.get());
michael@0 105
michael@0 106 if (!msg) {
michael@0 107 return NS_ERROR_FAILURE;
michael@0 108 }
michael@0 109 dbus_message_set_no_reply(msg, true);
michael@0 110
michael@0 111 DBusMessageIter iter;
michael@0 112 dbus_message_iter_init_append(msg, &iter);
michael@0 113 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uri);
michael@0 114
michael@0 115 if (dbus_connection_send(connection, msg, nullptr)) {
michael@0 116 dbus_connection_flush(connection);
michael@0 117 dbus_message_unref(msg);
michael@0 118 } else {
michael@0 119 dbus_message_unref(msg);
michael@0 120 return NS_ERROR_FAILURE;
michael@0 121 }
michael@0 122 return NS_OK;
michael@0 123
michael@0 124 }
michael@0 125
michael@0 126 ////////////////////////////////////////////////////////////////////////////////
michael@0 127 //// nsIDBusHandlerApp
michael@0 128
michael@0 129 /* attribute AUTF8String service; */
michael@0 130 NS_IMETHODIMP nsDBusHandlerApp::GetService(nsACString & aService)
michael@0 131 {
michael@0 132 aService.Assign(mService);
michael@0 133 return NS_OK;
michael@0 134 }
michael@0 135
michael@0 136 NS_IMETHODIMP nsDBusHandlerApp::SetService(const nsACString & aService)
michael@0 137 {
michael@0 138 mService.Assign(aService);
michael@0 139 return NS_OK;
michael@0 140 }
michael@0 141
michael@0 142 /* attribute AUTF8String method; */
michael@0 143 NS_IMETHODIMP nsDBusHandlerApp::GetMethod(nsACString & aMethod)
michael@0 144 {
michael@0 145 aMethod.Assign(mMethod);
michael@0 146 return NS_OK;
michael@0 147 }
michael@0 148
michael@0 149 NS_IMETHODIMP nsDBusHandlerApp::SetMethod(const nsACString & aMethod)
michael@0 150 {
michael@0 151 mMethod.Assign(aMethod);
michael@0 152 return NS_OK;
michael@0 153 }
michael@0 154
michael@0 155 /* attribute AUTF8String interface; */
michael@0 156 NS_IMETHODIMP nsDBusHandlerApp::GetDBusInterface(nsACString & aInterface)
michael@0 157 {
michael@0 158 aInterface.Assign(mInterface);
michael@0 159 return NS_OK;
michael@0 160 }
michael@0 161
michael@0 162 NS_IMETHODIMP nsDBusHandlerApp::SetDBusInterface(const nsACString & aInterface)
michael@0 163 {
michael@0 164 mInterface.Assign(aInterface);
michael@0 165 return NS_OK;
michael@0 166 }
michael@0 167
michael@0 168 /* attribute AUTF8String objpath; */
michael@0 169 NS_IMETHODIMP nsDBusHandlerApp::GetObjectPath(nsACString & aObjpath)
michael@0 170 {
michael@0 171 aObjpath.Assign(mObjpath);
michael@0 172 return NS_OK;
michael@0 173 }
michael@0 174
michael@0 175 NS_IMETHODIMP nsDBusHandlerApp::SetObjectPath(const nsACString & aObjpath)
michael@0 176 {
michael@0 177 mObjpath.Assign(aObjpath);
michael@0 178 return NS_OK;
michael@0 179 }
michael@0 180
michael@0 181

mercurial