| 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 <objc/runtime.h> |
| 7 #include <unordered_map> | 8 #include <unordered_map> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 12 | 13 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." | 15 #error "This file requires ARC support." |
| 15 #endif | 16 #endif |
| 16 | 17 |
| 17 @implementation CommandDispatcher { | 18 @implementation CommandDispatcher { |
| 18 // Stores which target to forward to for a given selector. | 19 // Stores which target to forward to for a given selector. |
| 19 std::unordered_map<SEL, __weak id> _forwardingTargets; | 20 std::unordered_map<SEL, __weak id> _forwardingTargets; |
| 20 } | 21 } |
| 21 | 22 |
| 22 - (void)startDispatchingToTarget:(id)target forSelector:(SEL)selector { | 23 - (void)startDispatchingToTarget:(id)target forSelector:(SEL)selector { |
| 23 DCHECK(_forwardingTargets.find(selector) == _forwardingTargets.end()); | 24 DCHECK(_forwardingTargets.find(selector) == _forwardingTargets.end()); |
| 24 | 25 |
| 25 _forwardingTargets[selector] = target; | 26 _forwardingTargets[selector] = target; |
| 26 } | 27 } |
| 27 | 28 |
| 29 - (void)startDispatchingToTarget:(id)target forProtocol:(Protocol*)protocol { |
| 30 unsigned int methodCount; |
| 31 objc_method_description* requiredInstanceMethods = |
| 32 protocol_copyMethodDescriptionList(protocol, YES /* isRequiredMethod */, |
| 33 YES /* isInstanceMethod */, |
| 34 &methodCount); |
| 35 for (unsigned int i = 0; i < methodCount; i++) { |
| 36 [self startDispatchingToTarget:target |
| 37 forSelector:requiredInstanceMethods[i].name]; |
| 38 } |
| 39 } |
| 40 |
| 28 - (void)stopDispatchingForSelector:(SEL)selector { | 41 - (void)stopDispatchingForSelector:(SEL)selector { |
| 29 _forwardingTargets.erase(selector); | 42 _forwardingTargets.erase(selector); |
| 30 } | 43 } |
| 31 | 44 |
| 45 - (void)stopDispatchingForProtocol:(Protocol*)protocol { |
| 46 unsigned int methodCount; |
| 47 objc_method_description* requiredInstanceMethods = |
| 48 protocol_copyMethodDescriptionList(protocol, YES /* isRequiredMethod */, |
| 49 YES /* isInstanceMethod */, |
| 50 &methodCount); |
| 51 for (unsigned int i = 0; i < methodCount; i++) { |
| 52 [self stopDispatchingForSelector:requiredInstanceMethods[i].name]; |
| 53 } |
| 54 } |
| 55 |
| 32 // |-stopDispatchingToTarget| should be called much less often than | 56 // |-stopDispatchingToTarget| should be called much less often than |
| 33 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order | 57 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order |
| 34 // to prioritize the speed of lookups. | 58 // to prioritize the speed of lookups. |
| 35 - (void)stopDispatchingToTarget:(id)target { | 59 - (void)stopDispatchingToTarget:(id)target { |
| 36 std::vector<SEL> selectorsToErase; | 60 std::vector<SEL> selectorsToErase; |
| 37 for (auto& kv : _forwardingTargets) { | 61 for (auto& kv : _forwardingTargets) { |
| 38 if (kv.second == target) { | 62 if (kv.second == target) { |
| 39 selectorsToErase.push_back(kv.first); | 63 selectorsToErase.push_back(kv.first); |
| 40 } | 64 } |
| 41 } | 65 } |
| 42 | 66 |
| 43 for (auto* selector : selectorsToErase) { | 67 for (auto* selector : selectorsToErase) { |
| 44 [self stopDispatchingForSelector:selector]; | 68 [self stopDispatchingForSelector:selector]; |
| 45 } | 69 } |
| 46 } | 70 } |
| 47 | 71 |
| 48 #pragma mark - NSObject | 72 #pragma mark - NSObject |
| 49 | 73 |
| 50 // Overridden to forward messages to registered handlers. | 74 // Overridden to forward messages to registered handlers. |
| 51 - (id)forwardingTargetForSelector:(SEL)selector { | 75 - (id)forwardingTargetForSelector:(SEL)selector { |
| 52 auto target = _forwardingTargets.find(selector); | 76 auto target = _forwardingTargets.find(selector); |
| 53 if (target != _forwardingTargets.end()) { | 77 if (target != _forwardingTargets.end()) { |
| 54 return target->second; | 78 return target->second; |
| 55 } | 79 } |
| 56 return [super forwardingTargetForSelector:selector]; | 80 return [super forwardingTargetForSelector:selector]; |
| 57 } | 81 } |
| 58 | 82 |
| 59 @end | 83 @end |
| OLD | NEW |