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 "CocoaFileUtils.h"
7 #include "nsDirectoryServiceDefs.h"
8 #include "nsIImageLoadingContent.h"
9 #include "mozilla/dom/Document.h"
10 #include "nsIContent.h"
11 #include "nsIObserverService.h"
12 #include "nsIWebBrowserPersist.h"
13 #include "nsMacShellService.h"
14 #include "nsIProperties.h"
15 #include "nsServiceManagerUtils.h"
16 #include "nsShellService.h"
17 #include "nsString.h"
18 #include "nsIDocShell.h"
19 #include "nsILoadContext.h"
20 #include "mozilla/dom/Element.h"
21 #include "DesktopBackgroundImage.h"
22
23 #include <Carbon/Carbon.h>
24 #include <CoreFoundation/CoreFoundation.h>
25 #include <ApplicationServices/ApplicationServices.h>
26
27 using mozilla::dom::Element;
28 using mozilla::widget::SetDesktopImage;
29
30 #define NETWORK_PREFPANE \
31 NS_LITERAL_CSTRING("/System/Library/PreferencePanes/Network.prefPane")
32 #define DESKTOP_PREFPANE \
33 NS_LITERAL_CSTRING( \
34 "/System/Library/PreferencePanes/DesktopScreenEffectsPref.prefPane")
35
36 #define SAFARI_BUNDLE_IDENTIFIER "com.apple.Safari"
37
NS_IMPL_ISUPPORTS(nsMacShellService,nsIMacShellService,nsIShellService,nsIToolkitShellService,nsIWebProgressListener)38 NS_IMPL_ISUPPORTS(nsMacShellService, nsIMacShellService, nsIShellService,
39 nsIToolkitShellService, nsIWebProgressListener)
40
41 NS_IMETHODIMP
42 nsMacShellService::IsDefaultBrowser(bool aForAllTypes,
43 bool* aIsDefaultBrowser) {
44 *aIsDefaultBrowser = false;
45
46 CFStringRef firefoxID = ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
47 if (!firefoxID) {
48 // CFBundleGetIdentifier is expected to return nullptr only if the specified
49 // bundle doesn't have a bundle identifier in its plist. In this case, that
50 // means a failure, since our bundle does have an identifier.
51 return NS_ERROR_FAILURE;
52 }
53
54 // Get the default http handler's bundle ID (or nullptr if it has not been
55 // explicitly set)
56 CFStringRef defaultBrowserID =
57 ::LSCopyDefaultHandlerForURLScheme(CFSTR("http"));
58 if (defaultBrowserID) {
59 *aIsDefaultBrowser =
60 ::CFStringCompare(firefoxID, defaultBrowserID, 0) == kCFCompareEqualTo;
61 ::CFRelease(defaultBrowserID);
62 }
63
64 return NS_OK;
65 }
66
67 NS_IMETHODIMP
SetDefaultBrowser(bool aClaimAllTypes,bool aForAllUsers)68 nsMacShellService::SetDefaultBrowser(bool aClaimAllTypes, bool aForAllUsers) {
69 // Note: We don't support aForAllUsers on Mac OS X.
70
71 CFStringRef firefoxID = ::CFBundleGetIdentifier(::CFBundleGetMainBundle());
72 if (!firefoxID) {
73 return NS_ERROR_FAILURE;
74 }
75
76 if (::LSSetDefaultHandlerForURLScheme(CFSTR("http"), firefoxID) != noErr) {
77 return NS_ERROR_FAILURE;
78 }
79 if (::LSSetDefaultHandlerForURLScheme(CFSTR("https"), firefoxID) != noErr) {
80 return NS_ERROR_FAILURE;
81 }
82
83 if (aClaimAllTypes) {
84 if (::LSSetDefaultHandlerForURLScheme(CFSTR("ftp"), firefoxID) != noErr) {
85 return NS_ERROR_FAILURE;
86 }
87 if (::LSSetDefaultRoleHandlerForContentType(kUTTypeHTML, kLSRolesAll,
88 firefoxID) != noErr) {
89 return NS_ERROR_FAILURE;
90 }
91 }
92
93 nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
94 if (prefs) {
95 (void)prefs->SetBoolPref(PREF_CHECKDEFAULTBROWSER, true);
96 // Reset the number of times the dialog should be shown
97 // before it is silenced.
98 (void)prefs->SetIntPref(PREF_DEFAULTBROWSERCHECKCOUNT, 0);
99 }
100
101 return NS_OK;
102 }
103
104 NS_IMETHODIMP
SetDesktopBackground(Element * aElement,int32_t aPosition,const nsACString & aImageName)105 nsMacShellService::SetDesktopBackground(Element* aElement, int32_t aPosition,
106 const nsACString& aImageName) {
107 // Note: We don't support aPosition on OS X.
108
109 // Get the image URI:
110 nsresult rv;
111 nsCOMPtr<nsIImageLoadingContent> imageContent =
112 do_QueryInterface(aElement, &rv);
113 NS_ENSURE_SUCCESS(rv, rv);
114 nsCOMPtr<nsIURI> imageURI;
115 rv = imageContent->GetCurrentURI(getter_AddRefs(imageURI));
116 NS_ENSURE_SUCCESS(rv, rv);
117
118 nsIURI* docURI = aElement->OwnerDoc()->GetDocumentURI();
119 if (!docURI) return NS_ERROR_FAILURE;
120
121 nsCOMPtr<nsIProperties> fileLocator(
122 do_GetService("@mozilla.org/file/directory_service;1", &rv));
123 NS_ENSURE_SUCCESS(rv, rv);
124
125 // Get the current user's "Pictures" folder (That's ~/Pictures):
126 fileLocator->Get(NS_OSX_PICTURE_DOCUMENTS_DIR, NS_GET_IID(nsIFile),
127 getter_AddRefs(mBackgroundFile));
128 if (!mBackgroundFile) return NS_ERROR_OUT_OF_MEMORY;
129
130 nsAutoString fileNameUnicode;
131 CopyUTF8toUTF16(aImageName, fileNameUnicode);
132
133 // and add the imgage file name itself:
134 mBackgroundFile->Append(fileNameUnicode);
135
136 // Download the image; the desktop background will be set in OnStateChange()
137 nsCOMPtr<nsIWebBrowserPersist> wbp(do_CreateInstance(
138 "@mozilla.org/embedding/browser/nsWebBrowserPersist;1", &rv));
139 NS_ENSURE_SUCCESS(rv, rv);
140
141 uint32_t flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION |
142 nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES |
143 nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE;
144
145 wbp->SetPersistFlags(flags);
146 wbp->SetProgressListener(this);
147
148 nsCOMPtr<nsILoadContext> loadContext;
149 nsCOMPtr<nsISupports> container = aElement->OwnerDoc()->GetContainer();
150 nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(container);
151 if (docShell) {
152 loadContext = do_QueryInterface(docShell);
153 }
154
155 auto referrerInfo =
156 mozilla::MakeRefPtr<mozilla::dom::ReferrerInfo>(*aElement);
157 return wbp->SaveURI(imageURI, aElement->NodePrincipal(), 0, referrerInfo,
158 nullptr, nullptr, mBackgroundFile,
159 nsIContentPolicy::TYPE_IMAGE, loadContext);
160 }
161
162 NS_IMETHODIMP
OnProgressChange(nsIWebProgress * aWebProgress,nsIRequest * aRequest,int32_t aCurSelfProgress,int32_t aMaxSelfProgress,int32_t aCurTotalProgress,int32_t aMaxTotalProgress)163 nsMacShellService::OnProgressChange(nsIWebProgress* aWebProgress,
164 nsIRequest* aRequest,
165 int32_t aCurSelfProgress,
166 int32_t aMaxSelfProgress,
167 int32_t aCurTotalProgress,
168 int32_t aMaxTotalProgress) {
169 return NS_OK;
170 }
171
172 NS_IMETHODIMP
OnLocationChange(nsIWebProgress * aWebProgress,nsIRequest * aRequest,nsIURI * aLocation,uint32_t aFlags)173 nsMacShellService::OnLocationChange(nsIWebProgress* aWebProgress,
174 nsIRequest* aRequest, nsIURI* aLocation,
175 uint32_t aFlags) {
176 return NS_OK;
177 }
178
179 NS_IMETHODIMP
OnStatusChange(nsIWebProgress * aWebProgress,nsIRequest * aRequest,nsresult aStatus,const char16_t * aMessage)180 nsMacShellService::OnStatusChange(nsIWebProgress* aWebProgress,
181 nsIRequest* aRequest, nsresult aStatus,
182 const char16_t* aMessage) {
183 return NS_OK;
184 }
185
186 NS_IMETHODIMP
OnSecurityChange(nsIWebProgress * aWebProgress,nsIRequest * aRequest,uint32_t aState)187 nsMacShellService::OnSecurityChange(nsIWebProgress* aWebProgress,
188 nsIRequest* aRequest, uint32_t aState) {
189 return NS_OK;
190 }
191
192 NS_IMETHODIMP
OnContentBlockingEvent(nsIWebProgress * aWebProgress,nsIRequest * aRequest,uint32_t aEvent)193 nsMacShellService::OnContentBlockingEvent(nsIWebProgress* aWebProgress,
194 nsIRequest* aRequest,
195 uint32_t aEvent) {
196 return NS_OK;
197 }
198
199 NS_IMETHODIMP
OnStateChange(nsIWebProgress * aWebProgress,nsIRequest * aRequest,uint32_t aStateFlags,nsresult aStatus)200 nsMacShellService::OnStateChange(nsIWebProgress* aWebProgress,
201 nsIRequest* aRequest, uint32_t aStateFlags,
202 nsresult aStatus) {
203 if (NS_SUCCEEDED(aStatus) && (aStateFlags & STATE_STOP) &&
204 (aRequest == nullptr)) {
205 nsCOMPtr<nsIObserverService> os(
206 do_GetService("@mozilla.org/observer-service;1"));
207 if (os)
208 os->NotifyObservers(nullptr, "shell:desktop-background-changed", nullptr);
209
210 bool exists = false;
211 nsresult rv = mBackgroundFile->Exists(&exists);
212 if (NS_FAILED(rv) || !exists) {
213 return NS_OK;
214 }
215
216 SetDesktopImage(mBackgroundFile);
217 }
218
219 return NS_OK;
220 }
221
222 NS_IMETHODIMP
ShowDesktopPreferences()223 nsMacShellService::ShowDesktopPreferences() {
224 nsCOMPtr<nsIFile> lf;
225 nsresult rv =
226 NS_NewNativeLocalFile(DESKTOP_PREFPANE, true, getter_AddRefs(lf));
227 NS_ENSURE_SUCCESS(rv, rv);
228 bool exists;
229 lf->Exists(&exists);
230 if (!exists) return NS_ERROR_FILE_NOT_FOUND;
231 return lf->Launch();
232 }
233
234 NS_IMETHODIMP
GetDesktopBackgroundColor(uint32_t * aColor)235 nsMacShellService::GetDesktopBackgroundColor(uint32_t* aColor) {
236 // This method and |SetDesktopBackgroundColor| has no meaning on Mac OS X.
237 // The mac desktop preferences UI uses pictures for the few solid colors it
238 // supports.
239 return NS_ERROR_NOT_IMPLEMENTED;
240 }
241
242 NS_IMETHODIMP
SetDesktopBackgroundColor(uint32_t aColor)243 nsMacShellService::SetDesktopBackgroundColor(uint32_t aColor) {
244 // This method and |GetDesktopBackgroundColor| has no meaning on Mac OS X.
245 // The mac desktop preferences UI uses pictures for the few solid colors it
246 // supports.
247 return NS_ERROR_NOT_IMPLEMENTED;
248 }
249
250 NS_IMETHODIMP
ShowSecurityPreferences(const nsACString & aPaneID)251 nsMacShellService::ShowSecurityPreferences(const nsACString& aPaneID) {
252 nsresult rv = NS_ERROR_NOT_AVAILABLE;
253
254 CFStringRef paneID = ::CFStringCreateWithBytes(
255 kCFAllocatorDefault, (const UInt8*)PromiseFlatCString(aPaneID).get(),
256 aPaneID.Length(), kCFStringEncodingUTF8, false);
257
258 if (paneID) {
259 CFStringRef format =
260 CFSTR("x-apple.systempreferences:com.apple.preference.security?%@");
261 if (format) {
262 CFStringRef urlStr =
263 CFStringCreateWithFormat(kCFAllocatorDefault, NULL, format, paneID);
264 if (urlStr) {
265 CFURLRef url = ::CFURLCreateWithString(NULL, urlStr, NULL);
266 rv = CocoaFileUtils::OpenURL(url);
267
268 ::CFRelease(urlStr);
269 }
270
271 ::CFRelease(format);
272 }
273
274 ::CFRelease(paneID);
275 }
276 return rv;
277 }
278