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