Index: ios/chrome/browser/device_sharing/device_sharing_manager.mm |
diff --git a/ios/chrome/browser/device_sharing/device_sharing_manager.mm b/ios/chrome/browser/device_sharing/device_sharing_manager.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e84f257a444e87a03c2a2f0c1173e3e5097a6886 |
--- /dev/null |
+++ b/ios/chrome/browser/device_sharing/device_sharing_manager.mm |
@@ -0,0 +1,103 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ios/chrome/browser/device_sharing/device_sharing_manager.h" |
+ |
+#include <memory> |
+ |
+#include "base/mac/scoped_nsobject.h" |
+#import "components/handoff/handoff_manager.h" |
+#include "components/prefs/pref_change_registrar.h" |
+#include "components/prefs/pref_service.h" |
+#include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
+#include "ios/chrome/browser/pref_names.h" |
+#include "ios/chrome/browser/prefs/pref_observer_bridge.h" |
+#include "url/gurl.h" |
+ |
+@interface DeviceSharingManager ()<PrefObserverDelegate> { |
+ ios::ChromeBrowserState* _browserState; // weak |
+ |
+ // Bridge to listen to pref changes to the active browser state. |
+ std::unique_ptr<PrefObserverBridge> _browserStatePrefObserverBridge; |
+ |
+ // Registrar for pref change notifications to the active browser state. |
+ std::unique_ptr<PrefChangeRegistrar> _browserStatePrefChangeRegistrar; |
+ |
+ // Responsible for maintaining all state related to the Handoff feature. |
+ base::scoped_nsobject<HandoffManager> _handoffManager; |
+} |
+ |
+// If handoff is enabled for the active browser state, then this method ensures |
+// that a handoff manager is active. Destroys the handoff manager otherwise. |
+- (void)updateHandoffManager; |
+ |
+// Returns an auto-released instance of |HandoffManager|. Factory method |
+// required for unit testing. |
++ (HandoffManager*)createHandoffManager; |
+ |
+@end |
+ |
+@implementation DeviceSharingManager |
+ |
+- (void)updateBrowserState:(ios::ChromeBrowserState*)state { |
+ DCHECK(!state || !state->IsOffTheRecord()); |
+ if (_browserState == state) { |
+ return; |
+ } |
+ |
+ _browserState = state; |
+ if (!_browserState) { |
+ _browserStatePrefObserverBridge.reset(); |
+ _browserStatePrefChangeRegistrar.reset(); |
+ } else { |
+ // Add a listener to changes to the preferences related to device sharing. |
+ _browserStatePrefChangeRegistrar.reset(new PrefChangeRegistrar); |
+ _browserStatePrefChangeRegistrar->Init(_browserState->GetPrefs()); |
+ _browserStatePrefObserverBridge.reset(new PrefObserverBridge(self)); |
+ _browserStatePrefObserverBridge->ObserveChangesForPreference( |
+ prefs::kIosHandoffToOtherDevices, |
+ _browserStatePrefChangeRegistrar.get()); |
+ } |
+ [self updateHandoffManager]; |
+ [self updateActiveURL:GURL()]; |
+} |
+ |
+- (void)updateActiveURL:(const GURL&)activeURL { |
+ [_handoffManager updateActiveURL:activeURL]; |
+} |
+ |
+- (void)updateHandoffManager { |
+ BOOL handoffEnabled = |
+ _browserState && |
+ _browserState->GetPrefs()->GetBoolean(prefs::kIosHandoffToOtherDevices); |
+ if (!handoffEnabled) { |
+ _handoffManager.reset(); |
+ return; |
+ } |
+ |
+ if (!_handoffManager) |
+ _handoffManager.reset([[[self class] createHandoffManager] retain]); |
+} |
+ |
++ (HandoffManager*)createHandoffManager { |
+ return [[[HandoffManager alloc] init] autorelease]; |
+} |
+ |
+#pragma mark - PrefObserverDelegate |
+ |
+- (void)onPreferenceChanged:(const std::string&)preferenceName { |
+ if (preferenceName == prefs::kIosHandoffToOtherDevices) { |
+ [self updateHandoffManager]; |
+ } |
+} |
+ |
+@end |
+ |
+@implementation DeviceSharingManager (TestingOnly) |
+ |
+- (HandoffManager*)handoffManager { |
+ return _handoffManager.get(); |
+} |
+ |
+@end |