OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "ios/chrome/browser/device_sharing/device_sharing_manager.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #import "components/handoff/handoff_manager.h" |
| 11 #include "components/prefs/pref_change_registrar.h" |
| 12 #include "components/prefs/pref_service.h" |
| 13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 14 #include "ios/chrome/browser/pref_names.h" |
| 15 #include "ios/chrome/browser/prefs/pref_observer_bridge.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 @interface DeviceSharingManager ()<PrefObserverDelegate> { |
| 19 ios::ChromeBrowserState* _browserState; // weak |
| 20 |
| 21 // Bridge to listen to pref changes to the active browser state. |
| 22 std::unique_ptr<PrefObserverBridge> _browserStatePrefObserverBridge; |
| 23 |
| 24 // Registrar for pref change notifications to the active browser state. |
| 25 std::unique_ptr<PrefChangeRegistrar> _browserStatePrefChangeRegistrar; |
| 26 |
| 27 // Responsible for maintaining all state related to the Handoff feature. |
| 28 base::scoped_nsobject<HandoffManager> _handoffManager; |
| 29 } |
| 30 |
| 31 // If handoff is enabled for the active browser state, then this method ensures |
| 32 // that a handoff manager is active. Destroys the handoff manager otherwise. |
| 33 - (void)updateHandoffManager; |
| 34 |
| 35 // Returns an auto-released instance of |HandoffManager|. Factory method |
| 36 // required for unit testing. |
| 37 + (HandoffManager*)createHandoffManager; |
| 38 |
| 39 @end |
| 40 |
| 41 @implementation DeviceSharingManager |
| 42 |
| 43 - (void)updateBrowserState:(ios::ChromeBrowserState*)state { |
| 44 DCHECK(!state || !state->IsOffTheRecord()); |
| 45 if (_browserState == state) { |
| 46 return; |
| 47 } |
| 48 |
| 49 _browserState = state; |
| 50 if (!_browserState) { |
| 51 _browserStatePrefObserverBridge.reset(); |
| 52 _browserStatePrefChangeRegistrar.reset(); |
| 53 } else { |
| 54 // Add a listener to changes to the preferences related to device sharing. |
| 55 _browserStatePrefChangeRegistrar.reset(new PrefChangeRegistrar); |
| 56 _browserStatePrefChangeRegistrar->Init(_browserState->GetPrefs()); |
| 57 _browserStatePrefObserverBridge.reset(new PrefObserverBridge(self)); |
| 58 _browserStatePrefObserverBridge->ObserveChangesForPreference( |
| 59 prefs::kIosHandoffToOtherDevices, |
| 60 _browserStatePrefChangeRegistrar.get()); |
| 61 } |
| 62 [self updateHandoffManager]; |
| 63 [self updateActiveURL:GURL()]; |
| 64 } |
| 65 |
| 66 - (void)updateActiveURL:(const GURL&)activeURL { |
| 67 [_handoffManager updateActiveURL:activeURL]; |
| 68 } |
| 69 |
| 70 - (void)updateHandoffManager { |
| 71 BOOL handoffEnabled = |
| 72 _browserState && |
| 73 _browserState->GetPrefs()->GetBoolean(prefs::kIosHandoffToOtherDevices); |
| 74 if (!handoffEnabled) { |
| 75 _handoffManager.reset(); |
| 76 return; |
| 77 } |
| 78 |
| 79 if (!_handoffManager) |
| 80 _handoffManager.reset([[[self class] createHandoffManager] retain]); |
| 81 } |
| 82 |
| 83 + (HandoffManager*)createHandoffManager { |
| 84 return [[[HandoffManager alloc] init] autorelease]; |
| 85 } |
| 86 |
| 87 #pragma mark - PrefObserverDelegate |
| 88 |
| 89 - (void)onPreferenceChanged:(const std::string&)preferenceName { |
| 90 if (preferenceName == prefs::kIosHandoffToOtherDevices) { |
| 91 [self updateHandoffManager]; |
| 92 } |
| 93 } |
| 94 |
| 95 @end |
| 96 |
| 97 @implementation DeviceSharingManager (TestingOnly) |
| 98 |
| 99 - (HandoffManager*)handoffManager { |
| 100 return _handoffManager.get(); |
| 101 } |
| 102 |
| 103 @end |
OLD | NEW |