1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/handoff/handoff_manager.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "components/handoff/handoff_utility.h"
10 #include "net/base/mac/url_conversions.h"
13 #include "base/ios/ios_util.h"
16 #if defined(OS_MACOSX) && !defined(OS_IOS)
17 #include "base/mac/mac_util.h"
18 #include "base/mac/sdk_forward_declarations.h"
21 @interface HandoffManager ()
23 // The active user activity.
24 @property(nonatomic, retain) NSUserActivity* userActivity;
26 // Whether the URL of the current tab should be exposed for Handoff.
27 - (BOOL)shouldUseActiveURL;
29 // Updates the active NSUserActivity.
30 - (void)updateUserActivity;
34 @implementation HandoffManager
36 @synthesize userActivity = _userActivity;
38 - (instancetype)init {
41 _propertyReleaser_HandoffManager.Init(self, [HandoffManager class]);
46 - (void)updateActiveURL:(const GURL&)url {
48 // Handoff is only available on iOS 8+.
49 DCHECK(base::ios::IsRunningOnIOS8OrLater());
52 #if defined(OS_MACOSX) && !defined(OS_IOS)
53 // Handoff is only available on OSX 10.10+.
54 DCHECK(base::mac::IsOSYosemiteOrLater());
58 [self updateUserActivity];
61 - (BOOL)shouldUseActiveURL {
62 return _activeURL.SchemeIsHTTPOrHTTPS();
65 - (void)updateUserActivity {
66 // Clear the user activity.
67 if (![self shouldUseActiveURL]) {
68 [self.userActivity invalidate];
69 self.userActivity = nil;
73 // No change to the user activity.
74 const GURL userActivityURL(net::GURLWithNSURL(self.userActivity.webpageURL));
75 if (userActivityURL == _activeURL)
78 // Invalidate the old user activity and make a new one.
79 [self.userActivity invalidate];
81 Class aClass = NSClassFromString(@"NSUserActivity");
82 NSUserActivity* userActivity = [[aClass performSelector:@selector(alloc)]
83 performSelector:@selector(initWithActivityType:)
84 withObject:handoff::kChromeHandoffActivityType];
85 self.userActivity = base::scoped_nsobject<NSUserActivity>(userActivity);
86 self.userActivity.webpageURL = net::NSURLWithGURL(_activeURL);
87 self.userActivity.userInfo = @{ handoff::kOriginKey : handoff::kOriginiOS };
88 [self.userActivity becomeCurrent];