| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/shared/chrome/browser/ui/commands/command_dispatcher.h" | 5 #import "ios/shared/chrome/browser/ui/commands/command_dispatcher.h" |
| 6 | 6 |
| 7 #include <unordered_map> | 7 #include <unordered_map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 @implementation CommandDispatcher { | 17 @implementation CommandDispatcher { |
| 18 // Stores which target to forward to for a given selector. | 18 // Stores which target to forward to for a given selector. |
| 19 std::unordered_map<SEL, __weak id> _forwardingTargets; | 19 std::unordered_map<SEL, __weak id> _forwardingTargets; |
| 20 } | 20 } |
| 21 | 21 |
| 22 - (void)startDispatchingToTarget:(id)target forSelector:(SEL)selector { | 22 - (void)startDispatchingToTarget:(id)target forSelector:(SEL)selector { |
| 23 DCHECK(_forwardingTargets.find(selector) == _forwardingTargets.end()); | 23 DCHECK(_forwardingTargets.find(selector) == _forwardingTargets.end()); |
| 24 | 24 |
| 25 _forwardingTargets[selector] = target; | 25 _forwardingTargets[selector] = target; |
| 26 } | 26 } |
| 27 | 27 |
| 28 - (void)stopDispatchingForSelector:(SEL)selector { |
| 29 _forwardingTargets.erase(selector); |
| 30 } |
| 31 |
| 28 // |-stopDispatchingToTarget| should be called much less often than | 32 // |-stopDispatchingToTarget| should be called much less often than |
| 29 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order | 33 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order |
| 30 // to prioritize the speed of lookups. | 34 // to prioritize the speed of lookups. |
| 31 - (void)stopDispatchingToTarget:(id)target { | 35 - (void)stopDispatchingToTarget:(id)target { |
| 32 std::vector<SEL> selectorsToErase; | 36 std::vector<SEL> selectorsToErase; |
| 33 for (auto& kv : _forwardingTargets) { | 37 for (auto& kv : _forwardingTargets) { |
| 34 if (kv.second == target) { | 38 if (kv.second == target) { |
| 35 selectorsToErase.push_back(kv.first); | 39 selectorsToErase.push_back(kv.first); |
| 36 } | 40 } |
| 37 } | 41 } |
| 38 | 42 |
| 39 for (auto* selector : selectorsToErase) { | 43 for (auto* selector : selectorsToErase) { |
| 40 _forwardingTargets.erase(selector); | 44 [self stopDispatchingForSelector:selector]; |
| 41 } | 45 } |
| 42 } | 46 } |
| 43 | 47 |
| 44 #pragma mark - NSObject | 48 #pragma mark - NSObject |
| 45 | 49 |
| 46 // Overridden to forward messages to registered handlers. | 50 // Overridden to forward messages to registered handlers. |
| 47 - (id)forwardingTargetForSelector:(SEL)selector { | 51 - (id)forwardingTargetForSelector:(SEL)selector { |
| 48 auto target = _forwardingTargets.find(selector); | 52 auto target = _forwardingTargets.find(selector); |
| 49 if (target != _forwardingTargets.end()) { | 53 if (target != _forwardingTargets.end()) { |
| 50 return target->second; | 54 return target->second; |
| 51 } | 55 } |
| 52 return [super forwardingTargetForSelector:selector]; | 56 return [super forwardingTargetForSelector:selector]; |
| 53 } | 57 } |
| 54 | 58 |
| 55 @end | 59 @end |
| OLD | NEW |