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 "nsMacAttribution.h"
7
8 #include <CoreFoundation/CoreFoundation.h>
9 #include <ApplicationServices/ApplicationServices.h>
10
11 #include "../../../xpcom/io/CocoaFileUtils.h"
12 #include "nsCocoaFeatures.h"
13 #include "nsString.h"
14
15 using namespace mozilla;
16
NS_IMPL_ISUPPORTS(nsMacAttributionService,nsIMacAttributionService)17 NS_IMPL_ISUPPORTS(nsMacAttributionService, nsIMacAttributionService)
18
19 NS_IMETHODIMP
20 nsMacAttributionService::GetReferrerUrl(const nsACString& aFilePath,
21 nsAString& aReferrer) {
22 const nsCString& flat = PromiseFlatCString(aFilePath);
23 CFStringRef filePath = ::CFStringCreateWithCString(
24 kCFAllocatorDefault, flat.get(), kCFStringEncodingUTF8);
25
26 CocoaFileUtils::CopyQuarantineReferrerUrl(filePath, aReferrer);
27
28 ::CFRelease(filePath);
29 return NS_OK;
30 }
31
32 NS_IMETHODIMP
SetReferrerUrl(const nsACString & aFilePath,const nsACString & aReferrerUrl,const bool aCreate)33 nsMacAttributionService::SetReferrerUrl(const nsACString& aFilePath,
34 const nsACString& aReferrerUrl,
35 const bool aCreate) {
36 const nsCString& flat = PromiseFlatCString(aFilePath);
37 CFStringRef filePath = ::CFStringCreateWithCString(
38 kCFAllocatorDefault, flat.get(), kCFStringEncodingUTF8);
39
40 if (!filePath) {
41 return NS_ERROR_UNEXPECTED;
42 }
43
44 const nsCString& flatReferrer = PromiseFlatCString(aReferrerUrl);
45 CFStringRef referrer = ::CFStringCreateWithCString(
46 kCFAllocatorDefault, flatReferrer.get(), kCFStringEncodingUTF8);
47 if (!referrer) {
48 ::CFRelease(filePath);
49 return NS_ERROR_UNEXPECTED;
50 }
51 CFURLRef referrerURL =
52 ::CFURLCreateWithString(kCFAllocatorDefault, referrer, nullptr);
53
54 CocoaFileUtils::AddQuarantineMetadataToFile(filePath, NULL, referrerURL, true,
55 aCreate);
56
57 ::CFRelease(filePath);
58 ::CFRelease(referrer);
59 ::CFRelease(referrerURL);
60
61 return NS_OK;
62 }
63