Hotlist Slow: Remove extra thread pool created by VolumeMountWatcherWin.
[chromium-blink-merge.git] / components / handoff / handoff_manager.mm
blobab1efd693a5f701accbef39d1f56266cfb14f8eb
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"
12 #if defined(OS_IOS)
13 #include "base/ios/ios_util.h"
14 #endif
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
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;
32 @end
34 @implementation HandoffManager
36 @synthesize userActivity = _userActivity;
38 - (instancetype)init {
39   self = [super init];
40   if (self) {
41     _propertyReleaser_HandoffManager.Init(self, [HandoffManager class]);
42   }
43   return self;
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
52 #if defined(OS_MACOSX) && !defined(OS_IOS)
53   // Handoff is only available on OSX 10.10+.
54   DCHECK(base::mac::IsOSYosemiteOrLater());
55 #endif
57   _activeURL = url;
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;
70     return;
71   }
73   // No change to the user activity.
74   const GURL userActivityURL(net::GURLWithNSURL(self.userActivity.webpageURL));
75   if (userActivityURL == _activeURL)
76     return;
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];
91 @end