| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/ui/contextual_search/touch_to_search_permissions_med
iator.h" | 5 #import "ios/chrome/browser/ui/contextual_search/touch_to_search_permissions_med
iator.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #import "base/ios/weak_nsobject.h" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "components/prefs/pref_change_registrar.h" | 11 #include "components/prefs/pref_change_registrar.h" |
| 13 #include "components/prefs/pref_service.h" | 12 #include "components/prefs/pref_service.h" |
| 14 #include "components/search_engines/template_url_service.h" | 13 #include "components/search_engines/template_url_service.h" |
| 15 #include "ios/chrome/app/tests_hook.h" | 14 #include "ios/chrome/app/tests_hook.h" |
| 16 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 17 #include "ios/chrome/browser/chrome_switches.h" | 16 #include "ios/chrome/browser/chrome_switches.h" |
| 18 #include "ios/chrome/browser/pref_names.h" | 17 #include "ios/chrome/browser/pref_names.h" |
| 19 #import "ios/chrome/browser/prefs/pref_observer_bridge.h" | 18 #import "ios/chrome/browser/prefs/pref_observer_bridge.h" |
| 20 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 21 #include "ios/chrome/browser/sync/sync_setup_service.h" | 20 #include "ios/chrome/browser/sync/sync_setup_service.h" |
| 22 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" | 21 #include "ios/chrome/browser/sync/sync_setup_service_factory.h" |
| 23 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" |
| 24 | 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
| 25 namespace { | 28 namespace { |
| 26 // Maps pref string values to state enum. | 29 // Maps pref string values to state enum. |
| 27 const struct { | 30 const struct { |
| 28 const char* value; | 31 const char* value; |
| 29 TouchToSearch::TouchToSearchPreferenceState state; | 32 TouchToSearch::TouchToSearchPreferenceState state; |
| 30 } valueStateMappings[] = { | 33 } valueStateMappings[] = { |
| 31 {"false", TouchToSearch::DISABLED}, | 34 {"false", TouchToSearch::DISABLED}, |
| 32 {"true", TouchToSearch::ENABLED}, | 35 {"true", TouchToSearch::ENABLED}, |
| 33 {"", TouchToSearch::UNDECIDED}, | 36 {"", TouchToSearch::UNDECIDED}, |
| 34 }; | 37 }; |
| 35 } // namespace | 38 } // namespace |
| 36 | 39 |
| 37 @interface TouchToSearchPermissionsMediator ()<PrefObserverDelegate> { | 40 @interface TouchToSearchPermissionsMediator ()<PrefObserverDelegate> { |
| 38 ios::ChromeBrowserState* _browserState; | 41 ios::ChromeBrowserState* _browserState; |
| 39 SyncSetupService* _syncService; | 42 SyncSetupService* _syncService; |
| 40 base::WeakNSProtocol<NSObject<TouchToSearchPermissionsChangeAudience>*> | 43 __weak NSObject<TouchToSearchPermissionsChangeAudience>* _audience; |
| 41 _audience; | |
| 42 // Pref observer to track changes to the touch-to-search and search engine | 44 // Pref observer to track changes to the touch-to-search and search engine |
| 43 // prefs. | 45 // prefs. |
| 44 std::unique_ptr<PrefObserverBridge> _prefObserverBridge; | 46 std::unique_ptr<PrefObserverBridge> _prefObserverBridge; |
| 45 | 47 |
| 46 // Registrar for pref changes notifications. | 48 // Registrar for pref changes notifications. |
| 47 PrefChangeRegistrar _prefChangeRegistrar; | 49 PrefChangeRegistrar _prefChangeRegistrar; |
| 48 } | 50 } |
| 49 | 51 |
| 50 // YES if notifications are being observed. | 52 // YES if notifications are being observed. |
| 51 @property(nonatomic, assign) BOOL observing; | 53 @property(nonatomic, assign) BOOL observing; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 82 return !tests_hook::DisableContextualSearch(); | 84 return !tests_hook::DisableContextualSearch(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 return NO; | 87 return NO; |
| 86 } | 88 } |
| 87 | 89 |
| 88 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { | 90 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 89 if ((self = [super init])) { | 91 if ((self = [super init])) { |
| 90 if (browserState && browserState->IsOffTheRecord()) { | 92 if (browserState && browserState->IsOffTheRecord()) { |
| 91 // Discard the allocated object and return a nil object. | 93 // Discard the allocated object and return a nil object. |
| 92 [self release]; | |
| 93 return nil; | 94 return nil; |
| 94 } | 95 } |
| 95 [self setUpBrowserState:browserState]; | 96 [self setUpBrowserState:browserState]; |
| 96 } | 97 } |
| 97 return self; | 98 return self; |
| 98 } | 99 } |
| 99 | 100 |
| 100 - (instancetype)init { | 101 - (instancetype)init { |
| 101 NOTREACHED(); | 102 NOTREACHED(); |
| 102 return nil; | 103 return nil; |
| 103 } | 104 } |
| 104 | 105 |
| 105 - (void)dealloc { | 106 - (void)dealloc { |
| 106 // Set audience to nil to stop observation. | 107 // Set audience to nil to stop observation. |
| 107 self.audience = nil; | 108 self.audience = nil; |
| 108 [super dealloc]; | |
| 109 } | 109 } |
| 110 | 110 |
| 111 - (void)setUpBrowserState:(ios::ChromeBrowserState*)browserState { | 111 - (void)setUpBrowserState:(ios::ChromeBrowserState*)browserState { |
| 112 DCHECK(browserState); | 112 DCHECK(browserState); |
| 113 _browserState = browserState; | 113 _browserState = browserState; |
| 114 _syncService = SyncSetupServiceFactory::GetForBrowserState(_browserState); | 114 _syncService = SyncSetupServiceFactory::GetForBrowserState(_browserState); |
| 115 } | 115 } |
| 116 | 116 |
| 117 #pragma mark - Property implementation | 117 #pragma mark - Property implementation |
| 118 | 118 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 139 NOTREACHED() << "Unexpected preference state (" << state << ")"; | 139 NOTREACHED() << "Unexpected preference state (" << state << ")"; |
| 140 } | 140 } |
| 141 | 141 |
| 142 - (NSObject<TouchToSearchPermissionsChangeAudience>*)audience { | 142 - (NSObject<TouchToSearchPermissionsChangeAudience>*)audience { |
| 143 return _audience; | 143 return _audience; |
| 144 } | 144 } |
| 145 | 145 |
| 146 - (void)setAudience: | 146 - (void)setAudience: |
| 147 (NSObject<TouchToSearchPermissionsChangeAudience>*)audience { | 147 (NSObject<TouchToSearchPermissionsChangeAudience>*)audience { |
| 148 [self stopObserving]; | 148 [self stopObserving]; |
| 149 _audience.reset(audience); | 149 _audience = audience; |
| 150 [self startObserving]; | 150 [self startObserving]; |
| 151 } | 151 } |
| 152 | 152 |
| 153 #pragma mark - Public methods | 153 #pragma mark - Public methods |
| 154 | 154 |
| 155 - (BOOL)canEnable { | 155 - (BOOL)canEnable { |
| 156 if (![[self class] isTouchToSearchAvailableOnDevice]) { | 156 if (![[self class] isTouchToSearchAvailableOnDevice]) { |
| 157 return NO; | 157 return NO; |
| 158 } else if (self.preferenceState == TouchToSearch::DISABLED) { | 158 } else if (self.preferenceState == TouchToSearch::DISABLED) { |
| 159 return NO; | 159 return NO; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 respondsToSelector:@selector( | 263 respondsToSelector:@selector( |
| 264 touchToSearchDidChangePreferenceState:)]) { | 264 touchToSearchDidChangePreferenceState:)]) { |
| 265 [self.audience touchToSearchDidChangePreferenceState:self.preferenceState]; | 265 [self.audience touchToSearchDidChangePreferenceState:self.preferenceState]; |
| 266 } | 266 } |
| 267 } | 267 } |
| 268 | 268 |
| 269 - (void)maybeNotifyAudienceAsynchronously { | 269 - (void)maybeNotifyAudienceAsynchronously { |
| 270 if (self.audience) { | 270 if (self.audience) { |
| 271 if ([self.audience | 271 if ([self.audience |
| 272 respondsToSelector:@selector(touchToSearchPermissionsUpdated)]) { | 272 respondsToSelector:@selector(touchToSearchPermissionsUpdated)]) { |
| 273 base::WeakNSProtocol<NSObject<TouchToSearchPermissionsChangeAudience>*> | 273 __weak NSObject<TouchToSearchPermissionsChangeAudience>* audience = |
| 274 audience(self.audience); | 274 self.audience; |
| 275 dispatch_async(dispatch_get_main_queue(), ^{ | 275 dispatch_async(dispatch_get_main_queue(), ^{ |
| 276 [audience touchToSearchPermissionsUpdated]; | 276 [audience touchToSearchPermissionsUpdated]; |
| 277 }); | 277 }); |
| 278 } | 278 } |
| 279 } else { | 279 } else { |
| 280 // If audience is now nil, stop observing. | 280 // If audience is now nil, stop observing. |
| 281 [self stopObserving]; | 281 [self stopObserving]; |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 | 284 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 298 | 298 |
| 299 #pragma mark Notification Center handler | 299 #pragma mark Notification Center handler |
| 300 | 300 |
| 301 - (void)statusMayHaveChangedWithNotification:(NSNotification*)notification { | 301 - (void)statusMayHaveChangedWithNotification:(NSNotification*)notification { |
| 302 // VoiceOver may have been enabled or disabled, so (if there is an audience | 302 // VoiceOver may have been enabled or disabled, so (if there is an audience |
| 303 // object), notify it asynchronously. | 303 // object), notify it asynchronously. |
| 304 [self maybeNotifyAudienceAsynchronously]; | 304 [self maybeNotifyAudienceAsynchronously]; |
| 305 } | 305 } |
| 306 | 306 |
| 307 @end | 307 @end |
| OLD | NEW |