1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #import <Cocoa/Cocoa.h>
7 #include "nsMacUserActivityUpdater.h"
9 #include "nsCocoaUtils.h"
10 #include "nsIBaseWindow.h"
12 NS_IMPL_ISUPPORTS(nsMacUserActivityUpdater, nsIMacUserActivityUpdater)
15 nsMacUserActivityUpdater::UpdateLocation(const nsAString& aPageUrl, const nsAString& aPageTitle,
16 nsIBaseWindow* aWindow) {
17 NS_OBJC_BEGIN_TRY_BLOCK_RETURN;
18 if (gfxPlatform::IsHeadless()) {
19 // In headless mode, Handoff will fail since there is no Cocoa window.
23 BaseWindow* cocoaWin = nsMacUserActivityUpdater::GetCocoaWindow(aWindow);
25 return NS_ERROR_FAILURE;
28 NSURL* pageUrl = nsCocoaUtils::ToNSURL(aPageUrl);
30 (![pageUrl.scheme isEqualToString:@"https"] && ![pageUrl.scheme isEqualToString:@"http"])) {
31 [cocoaWin.userActivity invalidate];
35 NSString* pageTitle = nsCocoaUtils::ToNSString(aPageTitle);
37 pageTitle = pageUrl.absoluteString;
40 NSUserActivity* userActivity =
41 [[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
42 userActivity.webpageURL = pageUrl;
43 userActivity.title = pageTitle;
44 cocoaWin.userActivity = userActivity;
45 [userActivity release];
49 NS_OBJC_END_TRY_BLOCK_RETURN(NS_ERROR_FAILURE);
52 BaseWindow* nsMacUserActivityUpdater::GetCocoaWindow(nsIBaseWindow* aWindow) {
53 nsCOMPtr<nsIWidget> widget = nullptr;
54 aWindow->GetMainWidget(getter_AddRefs(widget));
58 BaseWindow* cocoaWin = (BaseWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);