| 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..915f234b0134d11009b0c964434e96554aa0cc51
|
| --- /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"
|
| +#include "components/prefs/pref_change_registrar.h"
|
| +#include "components/prefs/pref_service.h"
|
| +#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
|
| +#import "ios/chrome/browser/device_sharing/handoff_manager.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.
|
| +}
|
| +
|
| +@end
|
| +
|
| +@implementation DeviceSharingManager (TestingOnly)
|
| +
|
| +- (HandoffManager*)handoffManager {
|
| + return _handoffManager.get();
|
| +}
|
| +
|
| +@end
|
|
|