Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / handoff / handoff_manager.mm
blob0f889d161f8df5eeab8f23e934fd3953c99468c1
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"
11 #if defined(OS_IOS)
12 #include "base/ios/ios_util.h"
13 #endif
15 #if defined(OS_MACOSX) && !defined(OS_IOS)
16 #include "base/mac/mac_util.h"
17 #include "base/mac/sdk_forward_declarations.h"
18 #endif
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;
31 @end
33 @implementation HandoffManager
35 @synthesize userActivity = _userActivity;
37 - (instancetype)init {
38   self = [super init];
39   if (self) {
40     _propertyReleaser_HandoffManager.Init(self, [HandoffManager class]);
41 #if defined(OS_MACOSX) && !defined(OS_IOS)
42     _origin = handoff::ORIGIN_MAC;
43 #elif defined(OS_IOS)
44     _origin = handoff::ORIGIN_IOS;
45 #else
46     NOTREACHED();
47 #endif
48   }
49   return self;
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
58 #if defined(OS_MACOSX) && !defined(OS_IOS)
59   // Handoff is only available on OSX 10.10+.
60   DCHECK(base::mac::IsOSYosemiteOrLater());
61 #endif
63   _activeURL = url;
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;
76     return;
77   }
79   // No change to the user activity.
80   const GURL userActivityURL(net::GURLWithNSURL(self.userActivity.webpageURL));
81   if (userActivityURL == _activeURL)
82     return;
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);
94   DCHECK(origin);
95   self.userActivity.userInfo = @{ handoff::kOriginKey : origin };
96   [self.userActivity becomeCurrent];
99 @end