Chromium Code Reviews| 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..d8a4c244008e21548637340bdeb7444913bd464e |
| --- /dev/null |
| +++ b/ios/chrome/browser/device_sharing/device_sharing_manager.mm |
| @@ -0,0 +1,104 @@ |
| +// 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]; |
| + } |
| + // TODO(msarda): Handle nearby sharing pref changes. |
|
erikchen
2016/11/08 18:13:55
msarda is getting rid of this downstream: https://
rohitrao (ping after 24h)
2016/11/08 19:57:35
Done.
|
| +} |
| + |
| +@end |
| + |
| +@implementation DeviceSharingManager (TestingOnly) |
| + |
| +- (HandoffManager*)handoffManager { |
| + return _handoffManager.get(); |
| +} |
| + |
| +@end |