1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim:expandtab:shiftwidth=2:tabstop=2:cin:
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * the Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2008
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Brad Lassey <blassey@mozila.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include <dbus/dbus.h>
41 : #include "nsDBusHandlerApp.h"
42 : #include "nsIURI.h"
43 : #include "nsIClassInfoImpl.h"
44 : #include "nsCOMPtr.h"
45 : #include "nsCExternalHandlerService.h"
46 :
47 : #if (MOZ_PLATFORM_MAEMO == 5)
48 : #define APP_LAUNCH_BANNER_SERVICE "com.nokia.hildon-desktop"
49 : #define APP_LAUNCH_BANNER_METHOD_INTERFACE "com.nokia.hildon.hdwm.startupnotification"
50 : #define APP_LAUNCH_BANNER_METHOD_PATH "/com/nokia/hildon/hdwm"
51 : #define APP_LAUNCH_BANNER_METHOD "starting"
52 : #endif
53 :
54 :
55 : // XXX why does nsMIMEInfoImpl have a threadsafe nsISupports? do we need one
56 : // here too?
57 : NS_IMPL_CLASSINFO(nsDBusHandlerApp, NULL, 0, NS_DBUSHANDLERAPP_CID)
58 0 : NS_IMPL_ISUPPORTS2_CI(nsDBusHandlerApp, nsIDBusHandlerApp, nsIHandlerApp)
59 :
60 : ////////////////////////////////////////////////////////////////////////////////
61 : //// nsIHandlerApp
62 :
63 0 : NS_IMETHODIMP nsDBusHandlerApp::GetName(nsAString& aName)
64 : {
65 0 : aName.Assign(mName);
66 0 : return NS_OK;
67 : }
68 :
69 0 : NS_IMETHODIMP nsDBusHandlerApp::SetName(const nsAString & aName)
70 : {
71 0 : mName.Assign(aName);
72 0 : return NS_OK;
73 : }
74 :
75 0 : NS_IMETHODIMP nsDBusHandlerApp::SetDetailedDescription(const nsAString & aDescription)
76 : {
77 0 : mDetailedDescription.Assign(aDescription);
78 :
79 0 : return NS_OK;
80 : }
81 :
82 0 : NS_IMETHODIMP nsDBusHandlerApp::GetDetailedDescription(nsAString& aDescription)
83 : {
84 0 : aDescription.Assign(mDetailedDescription);
85 :
86 0 : return NS_OK;
87 : }
88 :
89 : NS_IMETHODIMP
90 0 : nsDBusHandlerApp::Equals(nsIHandlerApp *aHandlerApp, bool *_retval)
91 : {
92 0 : NS_ENSURE_ARG_POINTER(aHandlerApp);
93 :
94 : // If the handler app isn't a dbus handler app, then it's not the same app.
95 0 : nsCOMPtr<nsIDBusHandlerApp> dbusHandlerApp = do_QueryInterface(aHandlerApp);
96 0 : if (!dbusHandlerApp) {
97 0 : *_retval = false;
98 0 : return NS_OK;
99 : }
100 0 : nsCAutoString service;
101 0 : nsCAutoString method;
102 :
103 0 : nsresult rv = dbusHandlerApp->GetService(service);
104 0 : if (NS_FAILED(rv)) {
105 0 : *_retval = false;
106 0 : return NS_OK;
107 : }
108 0 : rv = dbusHandlerApp->GetMethod(method);
109 0 : if (NS_FAILED(rv)) {
110 0 : *_retval = false;
111 0 : return NS_OK;
112 : }
113 :
114 0 : *_retval = service.Equals(mService) && method.Equals(mMethod);
115 0 : return NS_OK;
116 : }
117 :
118 : NS_IMETHODIMP
119 0 : nsDBusHandlerApp::LaunchWithURI(nsIURI *aURI,
120 : nsIInterfaceRequestor *aWindowContext)
121 : {
122 0 : nsCAutoString spec;
123 0 : nsresult rv = aURI->GetAsciiSpec(spec);
124 0 : NS_ENSURE_SUCCESS(rv,rv);
125 0 : const char* uri = spec.get();
126 :
127 : DBusError err;
128 0 : dbus_error_init(&err);
129 :
130 : DBusConnection *connection;
131 0 : connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
132 0 : if (dbus_error_is_set(&err)) {
133 0 : dbus_error_free(&err);
134 0 : return NS_ERROR_FAILURE;
135 : }
136 0 : if (nsnull == connection) {
137 0 : return NS_ERROR_FAILURE;
138 : }
139 0 : dbus_connection_set_exit_on_disconnect(connection,false);
140 :
141 : DBusMessage* msg;
142 : msg = dbus_message_new_method_call(mService.get(),
143 : mObjpath.get(),
144 : mInterface.get(),
145 0 : mMethod.get());
146 :
147 0 : if (!msg) {
148 0 : return NS_ERROR_FAILURE;
149 : }
150 0 : dbus_message_set_no_reply(msg, true);
151 :
152 : DBusMessageIter iter;
153 0 : dbus_message_iter_init_append(msg, &iter);
154 0 : dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uri);
155 :
156 0 : if (dbus_connection_send(connection, msg, NULL)) {
157 0 : dbus_connection_flush(connection);
158 0 : dbus_message_unref(msg);
159 : #if (MOZ_PLATFORM_MAEMO == 5)
160 : msg = dbus_message_new_method_call (APP_LAUNCH_BANNER_SERVICE,
161 : APP_LAUNCH_BANNER_METHOD_PATH,
162 : APP_LAUNCH_BANNER_METHOD_INTERFACE,
163 : APP_LAUNCH_BANNER_METHOD);
164 :
165 : if (msg) {
166 : const char* service = mService.get();
167 : if (dbus_message_append_args(msg,
168 : DBUS_TYPE_STRING,
169 : &service,
170 : DBUS_TYPE_INVALID)) {
171 : if (dbus_connection_send(connection, msg, NULL)) {
172 : dbus_connection_flush(connection);
173 : }
174 : dbus_message_unref(msg);
175 : }
176 : }
177 : #endif
178 : } else {
179 0 : dbus_message_unref(msg);
180 0 : return NS_ERROR_FAILURE;
181 : }
182 0 : return NS_OK;
183 :
184 : }
185 :
186 : ////////////////////////////////////////////////////////////////////////////////
187 : //// nsIDBusHandlerApp
188 :
189 : /* attribute AUTF8String service; */
190 0 : NS_IMETHODIMP nsDBusHandlerApp::GetService(nsACString & aService)
191 : {
192 0 : aService.Assign(mService);
193 0 : return NS_OK;
194 : }
195 :
196 0 : NS_IMETHODIMP nsDBusHandlerApp::SetService(const nsACString & aService)
197 : {
198 0 : mService.Assign(aService);
199 0 : return NS_OK;
200 : }
201 :
202 : /* attribute AUTF8String method; */
203 0 : NS_IMETHODIMP nsDBusHandlerApp::GetMethod(nsACString & aMethod)
204 : {
205 0 : aMethod.Assign(mMethod);
206 0 : return NS_OK;
207 : }
208 :
209 0 : NS_IMETHODIMP nsDBusHandlerApp::SetMethod(const nsACString & aMethod)
210 : {
211 0 : mMethod.Assign(aMethod);
212 0 : return NS_OK;
213 : }
214 :
215 : /* attribute AUTF8String interface; */
216 0 : NS_IMETHODIMP nsDBusHandlerApp::GetDBusInterface(nsACString & aInterface)
217 : {
218 0 : aInterface.Assign(mInterface);
219 0 : return NS_OK;
220 : }
221 :
222 0 : NS_IMETHODIMP nsDBusHandlerApp::SetDBusInterface(const nsACString & aInterface)
223 : {
224 0 : mInterface.Assign(aInterface);
225 0 : return NS_OK;
226 : }
227 :
228 : /* attribute AUTF8String objpath; */
229 0 : NS_IMETHODIMP nsDBusHandlerApp::GetObjectPath(nsACString & aObjpath)
230 : {
231 0 : aObjpath.Assign(mObjpath);
232 0 : return NS_OK;
233 : }
234 :
235 0 : NS_IMETHODIMP nsDBusHandlerApp::SetObjectPath(const nsACString & aObjpath)
236 : {
237 0 : mObjpath.Assign(aObjpath);
238 0 : return NS_OK;
239 : }
240 :
241 :
|