OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/handoff/handoff_manager.h" |
| 6 |
| 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" |
| 11 |
| 12 #if defined(OS_IOS) |
| 13 #include "base/ios/ios_util.h" |
| 14 #endif |
| 15 |
| 16 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 17 #include "base/mac/mac_util.h" |
| 18 #include "base/mac/sdk_forward_declarations.h" |
| 19 #endif |
| 20 |
| 21 @interface HandoffManager () |
| 22 |
| 23 // The active user activity. |
| 24 @property(nonatomic, retain) NSUserActivity* userActivity; |
| 25 |
| 26 // Whether the URL of the current tab should be exposed for Handoff. |
| 27 - (BOOL)shouldUseActiveURL; |
| 28 |
| 29 // Updates the active NSUserActivity. |
| 30 - (void)updateUserActivity; |
| 31 |
| 32 @end |
| 33 |
| 34 @implementation HandoffManager |
| 35 |
| 36 @synthesize userActivity = _userActivity; |
| 37 |
| 38 - (instancetype)init { |
| 39 self = [super init]; |
| 40 if (self) { |
| 41 _propertyReleaser_HandoffManager.Init(self, [HandoffManager class]); |
| 42 } |
| 43 return self; |
| 44 } |
| 45 |
| 46 - (void)updateActiveURL:(const GURL&)url { |
| 47 #if defined(OS_IOS) |
| 48 // Handoff is only available on iOS 8+. |
| 49 DCHECK(base::ios::IsRunningOnIOS8OrLater()); |
| 50 #endif |
| 51 |
| 52 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 53 // Handoff is only available on OSX 10.10+. |
| 54 DCHECK(base::mac::IsOSYosemiteOrLater()); |
| 55 #endif |
| 56 |
| 57 _activeURL = url; |
| 58 [self updateUserActivity]; |
| 59 } |
| 60 |
| 61 - (BOOL)shouldUseActiveURL { |
| 62 return _activeURL.SchemeIsHTTPOrHTTPS(); |
| 63 } |
| 64 |
| 65 - (void)updateUserActivity { |
| 66 // Clear the user activity. |
| 67 if (![self shouldUseActiveURL]) { |
| 68 [self.userActivity invalidate]; |
| 69 self.userActivity = nil; |
| 70 return; |
| 71 } |
| 72 |
| 73 // No change to the user activity. |
| 74 const GURL userActivityURL(net::GURLWithNSURL(self.userActivity.webpageURL)); |
| 75 if (userActivityURL == _activeURL) |
| 76 return; |
| 77 |
| 78 // Invalidate the old user activity and make a new one. |
| 79 [self.userActivity invalidate]; |
| 80 |
| 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]; |
| 89 } |
| 90 |
| 91 @end |
OLD | NEW |