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