1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "nsGNOMEShellService.h"
7 #include "nsIGIOService.h"
8 #include "nsCOMPtr.h"
9 #include "nsIServiceManager.h"
10 #include "prenv.h"
11 #include "nsIFile.h"
12 #include "nsIStringBundle.h"
13 #include "nsIPromptService.h"
14 #include "nsIPrefService.h"
15 #include "nsIPrefBranch.h"
16 #include "nsDirectoryServiceDefs.h"
17 #include "nsDirectoryServiceUtils.h"
18 #include "nsEmbedCID.h"
19 #include "mozilla/ArrayUtils.h"
20 #include "mozilla/Services.h"
21 
22 #include <glib.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 
26 using mozilla::ArrayLength;
27 
28 static const char* const sMailProtocols[] = {"mailto", "mid"};
29 
30 static const char* const sNewsProtocols[] = {"news", "snews", "nntp"};
31 
32 static const char* const sFeedProtocols[] = {"feed"};
33 
34 static const char* const sCalendarProtocols[] = {"webcal", "webcals"};
35 
36 struct AppTypeAssociation {
37   uint16_t type;
38   const char* const* protocols;
39   unsigned int protocolsLength;
40   const char* mimeType;
41   const char* extensions;
42 };
43 
IsRunningAsASnap()44 static bool IsRunningAsASnap() {
45   // SNAP holds the path to the snap, use SNAP_NAME
46   // which is easier to parse.
47   const char* snap_name = PR_GetEnv("SNAP_NAME");
48 
49   // return early if not set.
50   if (snap_name == nullptr) {
51     return false;
52   }
53 
54   // snap_name as defined on https://snapcraft.io/thunderbird
55   return (strcmp(snap_name, "thunderbird") == 0);
56 }
57 
58 static const AppTypeAssociation sAppTypes[] = {
59     {
60         nsIShellService::MAIL, sMailProtocols, ArrayLength(sMailProtocols),
61         "message/rfc822",
62         nullptr  // don't associate .eml extension, as that breaks printing
63                  // those
64     },
65     {nsIShellService::NEWS, sNewsProtocols, ArrayLength(sNewsProtocols),
66      nullptr, nullptr},
67     {nsIShellService::RSS, sFeedProtocols, ArrayLength(sFeedProtocols),
68      "application/rss+xml", "rss"},
69     {nsIShellService::CALENDAR, sCalendarProtocols,
70      ArrayLength(sCalendarProtocols), "text/calendar", "ics"}};
71 
nsGNOMEShellService()72 nsGNOMEShellService::nsGNOMEShellService()
73     : mCheckedThisSession(false), mAppIsInPath(false) {}
74 
Init()75 nsresult nsGNOMEShellService::Init() {
76   nsresult rv;
77 
78   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
79 
80   if (!giovfs) return NS_ERROR_NOT_AVAILABLE;
81 
82   // Check G_BROKEN_FILENAMES.  If it's set, then filenames in glib use
83   // the locale encoding.  If it's not set, they use UTF-8.
84   mUseLocaleFilenames = PR_GetEnv("G_BROKEN_FILENAMES") != nullptr;
85 
86   if (GetAppPathFromLauncher()) return NS_OK;
87 
88   nsCOMPtr<nsIFile> appPath;
89   rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR,
90                               getter_AddRefs(appPath));
91   NS_ENSURE_SUCCESS(rv, rv);
92 
93   rv = appPath->AppendNative(nsLiteralCString(MOZ_APP_NAME));
94   NS_ENSURE_SUCCESS(rv, rv);
95 
96   rv = appPath->GetNativePath(mAppPath);
97   return rv;
98 }
99 
NS_IMPL_ISUPPORTS(nsGNOMEShellService,nsIShellService,nsIToolkitShellService)100 NS_IMPL_ISUPPORTS(nsGNOMEShellService, nsIShellService, nsIToolkitShellService)
101 
102 bool nsGNOMEShellService::GetAppPathFromLauncher() {
103   gchar* tmp;
104 
105   const char* launcher = PR_GetEnv("MOZ_APP_LAUNCHER");
106   if (!launcher) return false;
107 
108   if (g_path_is_absolute(launcher)) {
109     mAppPath = launcher;
110     tmp = g_path_get_basename(launcher);
111     gchar* fullpath = g_find_program_in_path(tmp);
112     if (fullpath && mAppPath.Equals(fullpath)) {
113       mAppIsInPath = true;
114     }
115     g_free(fullpath);
116   } else {
117     tmp = g_find_program_in_path(launcher);
118     if (!tmp) return false;
119     mAppPath = tmp;
120     mAppIsInPath = true;
121   }
122 
123   g_free(tmp);
124   return true;
125 }
126 
127 NS_IMETHODIMP
IsDefaultClient(bool aStartupCheck,uint16_t aApps,bool * aIsDefaultClient)128 nsGNOMEShellService::IsDefaultClient(bool aStartupCheck, uint16_t aApps,
129                                      bool* aIsDefaultClient) {
130   *aIsDefaultClient = true;
131 
132   for (unsigned int i = 0; i < MOZ_ARRAY_LENGTH(sAppTypes); i++) {
133     if (aApps & sAppTypes[i].type)
134       *aIsDefaultClient &=
135           checkDefault(sAppTypes[i].protocols, sAppTypes[i].protocolsLength);
136   }
137 
138   // If this is the first mail window, maintain internal state that we've
139   // checked this session (so that subsequent window opens don't show the
140   // default client dialog).
141   if (aStartupCheck) mCheckedThisSession = true;
142   return NS_OK;
143 }
144 
145 NS_IMETHODIMP
SetDefaultClient(bool aForAllUsers,uint16_t aApps)146 nsGNOMEShellService::SetDefaultClient(bool aForAllUsers, uint16_t aApps) {
147   nsresult rv = NS_OK;
148   for (unsigned int i = 0; i < MOZ_ARRAY_LENGTH(sAppTypes); i++) {
149     if (aApps & sAppTypes[i].type) {
150       nsresult tmp =
151           MakeDefault(sAppTypes[i].protocols, sAppTypes[i].protocolsLength,
152                       sAppTypes[i].mimeType, sAppTypes[i].extensions);
153       if (NS_FAILED(tmp)) {
154         rv = tmp;
155       }
156     }
157   }
158 
159   return rv;
160 }
161 
162 NS_IMETHODIMP
GetShouldCheckDefaultClient(bool * aResult)163 nsGNOMEShellService::GetShouldCheckDefaultClient(bool* aResult) {
164   if (mCheckedThisSession) {
165     *aResult = false;
166     return NS_OK;
167   }
168 
169   nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
170   return prefs->GetBoolPref("mail.shell.checkDefaultClient", aResult);
171 }
172 
173 NS_IMETHODIMP
SetShouldCheckDefaultClient(bool aShouldCheck)174 nsGNOMEShellService::SetShouldCheckDefaultClient(bool aShouldCheck) {
175   nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
176   return prefs->SetBoolPref("mail.shell.checkDefaultClient", aShouldCheck);
177 }
178 
KeyMatchesAppName(const char * aKeyValue) const179 bool nsGNOMEShellService::KeyMatchesAppName(const char* aKeyValue) const {
180   gchar* commandPath;
181   if (mUseLocaleFilenames) {
182     gchar* nativePath = g_filename_from_utf8(aKeyValue, -1, NULL, NULL, NULL);
183     if (!nativePath) {
184       NS_ERROR("Error converting path to filesystem encoding");
185       return false;
186     }
187 
188     commandPath = g_find_program_in_path(nativePath);
189     g_free(nativePath);
190   } else {
191     commandPath = g_find_program_in_path(aKeyValue);
192   }
193 
194   if (!commandPath) return false;
195 
196   bool matches = mAppPath.Equals(commandPath);
197   g_free(commandPath);
198   return matches;
199 }
200 
CheckHandlerMatchesAppName(const nsACString & handler) const201 bool nsGNOMEShellService::CheckHandlerMatchesAppName(
202     const nsACString& handler) const {
203   gint argc;
204   gchar** argv;
205   nsAutoCString command(handler);
206 
207   if (g_shell_parse_argv(command.get(), &argc, &argv, NULL)) {
208     command.Assign(argv[0]);
209     g_strfreev(argv);
210   } else {
211     return false;
212   }
213 
214   return KeyMatchesAppName(command.get());
215 }
216 
checkDefault(const char * const * aProtocols,unsigned int aLength)217 bool nsGNOMEShellService::checkDefault(const char* const* aProtocols,
218                                        unsigned int aLength) {
219   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
220 
221   nsAutoCString handler;
222   nsresult rv;
223 
224   for (unsigned int i = 0; i < aLength; ++i) {
225     if (IsRunningAsASnap()) {
226       const gchar* argv[] = {"xdg-settings", "get",
227                              "default-url-scheme-handler", aProtocols[i],
228                              nullptr};
229       GSpawnFlags flags = static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
230                                                    G_SPAWN_STDERR_TO_DEV_NULL);
231       gchar* output = nullptr;
232       gint exit_status = 0;
233       if (!g_spawn_sync(nullptr, (gchar**)argv, nullptr, flags, nullptr,
234                         nullptr, &output, nullptr, &exit_status, nullptr)) {
235         return false;
236       }
237       if (exit_status != 0) {
238         g_free(output);
239         return false;
240       }
241       if (strcmp(output, "thunderbird.desktop\n") == 0) {
242         g_free(output);
243         return true;
244       }
245       g_free(output);
246       return false;
247     }
248 
249     if (giovfs) {
250       handler.Truncate();
251       nsCOMPtr<nsIHandlerApp> handlerApp;
252       rv = giovfs->GetAppForURIScheme(nsDependentCString(aProtocols[i]),
253                                       getter_AddRefs(handlerApp));
254       if (NS_FAILED(rv) || !handlerApp) {
255         return false;
256       }
257       nsCOMPtr<nsIGIOMimeApp> app = do_QueryInterface(handlerApp, &rv);
258       if (NS_FAILED(rv) || !app) {
259         return false;
260       }
261       rv = app->GetCommand(handler);
262       if (NS_SUCCEEDED(rv) && !CheckHandlerMatchesAppName(handler)) {
263         return false;
264       }
265     }
266   }
267 
268   return true;
269 }
270 
MakeDefault(const char * const * aProtocols,unsigned int aProtocolsLength,const char * aMimeType,const char * aExtensions)271 nsresult nsGNOMEShellService::MakeDefault(const char* const* aProtocols,
272                                           unsigned int aProtocolsLength,
273                                           const char* aMimeType,
274                                           const char* aExtensions) {
275   nsAutoCString appKeyValue;
276   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
277   if (mAppIsInPath) {
278     // mAppPath is in the users path, so use only the basename as the launcher
279     gchar* tmp = g_path_get_basename(mAppPath.get());
280     appKeyValue = tmp;
281     g_free(tmp);
282   } else {
283     appKeyValue = mAppPath;
284   }
285 
286   appKeyValue.AppendLiteral(" %s");
287 
288   if (IsRunningAsASnap()) {
289     for (unsigned int i = 0; i < aProtocolsLength; ++i) {
290       const gchar* argv[] = {"xdg-settings",
291                              "set",
292                              "default-url-scheme-handler",
293                              aProtocols[i],
294                              "thunderbird.desktop",
295                              nullptr};
296       GSpawnFlags flags = static_cast<GSpawnFlags>(G_SPAWN_SEARCH_PATH |
297                                                    G_SPAWN_STDOUT_TO_DEV_NULL |
298                                                    G_SPAWN_STDERR_TO_DEV_NULL);
299       g_spawn_sync(nullptr, (gchar**)argv, nullptr, flags, nullptr, nullptr,
300                    nullptr, nullptr, nullptr, nullptr);
301     }
302   }
303 
304   nsresult rv;
305   if (giovfs) {
306     nsCOMPtr<nsIStringBundleService> bundleService =
307         mozilla::services::GetStringBundleService();
308     NS_ENSURE_TRUE(bundleService, NS_ERROR_UNEXPECTED);
309 
310     nsCOMPtr<nsIStringBundle> brandBundle;
311     rv = bundleService->CreateBundle(BRAND_PROPERTIES,
312                                      getter_AddRefs(brandBundle));
313     NS_ENSURE_SUCCESS(rv, rv);
314 
315     nsString brandShortName;
316     brandBundle->GetStringFromName("brandShortName", brandShortName);
317 
318     // use brandShortName as the application id.
319     NS_ConvertUTF16toUTF8 id(brandShortName);
320 
321     nsCOMPtr<nsIGIOMimeApp> app;
322     rv = giovfs->CreateAppFromCommand(mAppPath, id, getter_AddRefs(app));
323     NS_ENSURE_SUCCESS(rv, rv);
324 
325     for (unsigned int i = 0; i < aProtocolsLength; ++i) {
326       rv = app->SetAsDefaultForURIScheme(nsDependentCString(aProtocols[i]));
327       NS_ENSURE_SUCCESS(rv, rv);
328       if (aMimeType)
329         rv = app->SetAsDefaultForMimeType(nsDependentCString(aMimeType));
330       NS_ENSURE_SUCCESS(rv, rv);
331       if (aExtensions)
332         rv =
333             app->SetAsDefaultForFileExtensions(nsDependentCString(aExtensions));
334       NS_ENSURE_SUCCESS(rv, rv);
335     }
336   }
337 
338   return NS_OK;
339 }
340