1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/uriloader/exthandler/nsDBusHandlerApp.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim:expandtab:shiftwidth=2:tabstop=2:cin: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include <dbus/dbus.h> 1.11 +#include "nsDBusHandlerApp.h" 1.12 +#include "nsIURI.h" 1.13 +#include "nsIClassInfoImpl.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsCExternalHandlerService.h" 1.16 + 1.17 +// XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one 1.18 +// here too? 1.19 +NS_IMPL_CLASSINFO(nsDBusHandlerApp, nullptr, 0, NS_DBUSHANDLERAPP_CID) 1.20 +NS_IMPL_ISUPPORTS_CI(nsDBusHandlerApp, nsIDBusHandlerApp, nsIHandlerApp) 1.21 + 1.22 +//////////////////////////////////////////////////////////////////////////////// 1.23 +//// nsIHandlerApp 1.24 + 1.25 +NS_IMETHODIMP nsDBusHandlerApp::GetName(nsAString& aName) 1.26 +{ 1.27 + aName.Assign(mName); 1.28 + return NS_OK; 1.29 +} 1.30 + 1.31 +NS_IMETHODIMP nsDBusHandlerApp::SetName(const nsAString & aName) 1.32 +{ 1.33 + mName.Assign(aName); 1.34 + return NS_OK; 1.35 +} 1.36 + 1.37 +NS_IMETHODIMP nsDBusHandlerApp::SetDetailedDescription(const nsAString & aDescription) 1.38 +{ 1.39 + mDetailedDescription.Assign(aDescription); 1.40 + 1.41 + return NS_OK; 1.42 +} 1.43 + 1.44 +NS_IMETHODIMP nsDBusHandlerApp::GetDetailedDescription(nsAString& aDescription) 1.45 +{ 1.46 + aDescription.Assign(mDetailedDescription); 1.47 + 1.48 + return NS_OK; 1.49 +} 1.50 + 1.51 +NS_IMETHODIMP 1.52 +nsDBusHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval) 1.53 +{ 1.54 + NS_ENSURE_ARG_POINTER(aHandlerApp); 1.55 + 1.56 + // If the handler app isn't a dbus handler app, then it's not the same app. 1.57 + nsCOMPtr<nsIDBusHandlerApp> dbusHandlerApp = do_QueryInterface(aHandlerApp); 1.58 + if (!dbusHandlerApp) { 1.59 + *_retval = false; 1.60 + return NS_OK; 1.61 + } 1.62 + nsAutoCString service; 1.63 + nsAutoCString method; 1.64 + 1.65 + nsresult rv = dbusHandlerApp->GetService(service); 1.66 + if (NS_FAILED(rv)) { 1.67 + *_retval = false; 1.68 + return NS_OK; 1.69 + } 1.70 + rv = dbusHandlerApp->GetMethod(method); 1.71 + if (NS_FAILED(rv)) { 1.72 + *_retval = false; 1.73 + return NS_OK; 1.74 + } 1.75 + 1.76 + *_retval = service.Equals(mService) && method.Equals(mMethod); 1.77 + return NS_OK; 1.78 +} 1.79 + 1.80 +NS_IMETHODIMP 1.81 +nsDBusHandlerApp::LaunchWithURI(nsIURI *aURI, 1.82 + nsIInterfaceRequestor *aWindowContext) 1.83 +{ 1.84 + nsAutoCString spec; 1.85 + nsresult rv = aURI->GetAsciiSpec(spec); 1.86 + NS_ENSURE_SUCCESS(rv,rv); 1.87 + const char* uri = spec.get(); 1.88 + 1.89 + DBusError err; 1.90 + dbus_error_init(&err); 1.91 + 1.92 + DBusConnection *connection; 1.93 + connection = dbus_bus_get(DBUS_BUS_SESSION, &err); 1.94 + if (dbus_error_is_set(&err)) { 1.95 + dbus_error_free(&err); 1.96 + return NS_ERROR_FAILURE; 1.97 + } 1.98 + if (nullptr == connection) { 1.99 + return NS_ERROR_FAILURE; 1.100 + } 1.101 + dbus_connection_set_exit_on_disconnect(connection,false); 1.102 + 1.103 + DBusMessage* msg; 1.104 + msg = dbus_message_new_method_call(mService.get(), 1.105 + mObjpath.get(), 1.106 + mInterface.get(), 1.107 + mMethod.get()); 1.108 + 1.109 + if (!msg) { 1.110 + return NS_ERROR_FAILURE; 1.111 + } 1.112 + dbus_message_set_no_reply(msg, true); 1.113 + 1.114 + DBusMessageIter iter; 1.115 + dbus_message_iter_init_append(msg, &iter); 1.116 + dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uri); 1.117 + 1.118 + if (dbus_connection_send(connection, msg, nullptr)) { 1.119 + dbus_connection_flush(connection); 1.120 + dbus_message_unref(msg); 1.121 + } else { 1.122 + dbus_message_unref(msg); 1.123 + return NS_ERROR_FAILURE; 1.124 + } 1.125 + return NS_OK; 1.126 + 1.127 +} 1.128 + 1.129 +//////////////////////////////////////////////////////////////////////////////// 1.130 +//// nsIDBusHandlerApp 1.131 + 1.132 +/* attribute AUTF8String service; */ 1.133 +NS_IMETHODIMP nsDBusHandlerApp::GetService(nsACString & aService) 1.134 +{ 1.135 + aService.Assign(mService); 1.136 + return NS_OK; 1.137 +} 1.138 + 1.139 +NS_IMETHODIMP nsDBusHandlerApp::SetService(const nsACString & aService) 1.140 +{ 1.141 + mService.Assign(aService); 1.142 + return NS_OK; 1.143 +} 1.144 + 1.145 +/* attribute AUTF8String method; */ 1.146 +NS_IMETHODIMP nsDBusHandlerApp::GetMethod(nsACString & aMethod) 1.147 +{ 1.148 + aMethod.Assign(mMethod); 1.149 + return NS_OK; 1.150 +} 1.151 + 1.152 +NS_IMETHODIMP nsDBusHandlerApp::SetMethod(const nsACString & aMethod) 1.153 +{ 1.154 + mMethod.Assign(aMethod); 1.155 + return NS_OK; 1.156 +} 1.157 + 1.158 +/* attribute AUTF8String interface; */ 1.159 +NS_IMETHODIMP nsDBusHandlerApp::GetDBusInterface(nsACString & aInterface) 1.160 +{ 1.161 + aInterface.Assign(mInterface); 1.162 + return NS_OK; 1.163 +} 1.164 + 1.165 +NS_IMETHODIMP nsDBusHandlerApp::SetDBusInterface(const nsACString & aInterface) 1.166 +{ 1.167 + mInterface.Assign(aInterface); 1.168 + return NS_OK; 1.169 +} 1.170 + 1.171 +/* attribute AUTF8String objpath; */ 1.172 +NS_IMETHODIMP nsDBusHandlerApp::GetObjectPath(nsACString & aObjpath) 1.173 +{ 1.174 + aObjpath.Assign(mObjpath); 1.175 + return NS_OK; 1.176 +} 1.177 + 1.178 +NS_IMETHODIMP nsDBusHandlerApp::SetObjectPath(const nsACString & aObjpath) 1.179 +{ 1.180 + mObjpath.Assign(aObjpath); 1.181 + return NS_OK; 1.182 +} 1.183 + 1.184 +