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 "net/base/mac/url_conversions.h"
12 #include "base/ios/ios_util.h"
15 #if defined(OS_MACOSX) && !defined(OS_IOS)
16 #include "base/mac/mac_util.h"
17 #include "base/mac/sdk_forward_declarations.h"
20 @interface HandoffManager ()
22 // The active user activity.
23 @property(nonatomic, retain) NSUserActivity* userActivity;
25 // Whether the URL of the current tab should be exposed for Handoff.
26 - (BOOL)shouldUseActiveURL;
28 // Updates the active NSUserActivity.
29 - (void)updateUserActivity;
33 @implementation HandoffManager
35 @synthesize userActivity = _userActivity;
37 - (instancetype)init {
40 _propertyReleaser_HandoffManager.Init(self, [HandoffManager class]);
41 #if defined(OS_MACOSX) && !defined(OS_IOS)
42 _origin = handoff::ORIGIN_MAC;
44 _origin = handoff::ORIGIN_IOS;
52 - (void)updateActiveURL:(const GURL&)url {
54 // Handoff is only available on iOS 8+.
55 DCHECK(base::ios::IsRunningOnIOS8OrLater());
58 #if defined(OS_MACOSX) && !defined(OS_IOS)
59 // Handoff is only available on OSX 10.10+.
60 DCHECK(base::mac::IsOSYosemiteOrLater());
64 [self updateUserActivity];
67 - (BOOL)shouldUseActiveURL {
68 return _activeURL.SchemeIsHTTPOrHTTPS();
71 - (void)updateUserActivity {
72 // Clear the user activity.
73 if (![self shouldUseActiveURL]) {
74 [self.userActivity invalidate];
75 self.userActivity = nil;
79 // No change to the user activity.
80 const GURL userActivityURL(net::GURLWithNSURL(self.userActivity.webpageURL));
81 if (userActivityURL == _activeURL)
84 // Invalidate the old user activity and make a new one.
85 [self.userActivity invalidate];
87 Class aClass = NSClassFromString(@"NSUserActivity");
88 NSUserActivity* userActivity = [[aClass performSelector:@selector(alloc)]
89 performSelector:@selector(initWithActivityType:)
90 withObject:handoff::kChromeHandoffActivityType];
91 self.userActivity = base::scoped_nsobject<NSUserActivity>(userActivity);
92 self.userActivity.webpageURL = net::NSURLWithGURL(_activeURL);
93 NSString* origin = handoff::StringFromOrigin(_origin);
95 self.userActivity.userInfo = @{ handoff::kOriginKey : origin };
96 [self.userActivity becomeCurrent];