Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: ios/shared/chrome/browser/ui/commands/command_dispatcher.mm

Issue 2785153002: Add protocol APIs to CommandDispatcher. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 outCount;
marq (ping after 24h) 2017/03/30 14:03:55 Prefer method_count, here and below.
lpromero 2017/03/30 15:00:39 Would methodCount work?
marq (ping after 24h) 2017/03/31 07:43:01 Yes, sorry; I'm working on similar code inside a C
lpromero 2017/04/04 13:08:52 Done.
31 objc_method_description* requiredInstanceMethods =
32 protocol_copyMethodDescriptionList(protocol, YES /* isRequiredMethod */,
33 YES /* isInstanceMethod */, &outCount);
34 for (unsigned int i = 0; i < outCount; i++) {
35 [self startDispatchingToTarget:target
36 forSelector:requiredInstanceMethods[i].name];
37 }
marq (ping after 24h) 2017/03/30 14:03:55 What about protocols the protocol adheres to? (wit
lpromero 2017/03/30 15:00:39 I started this CL to basically raise all these que
marq (ping after 24h) 2017/03/31 07:43:01 Clearly either of these calls are just convenience
lpromero 2017/04/04 13:08:52 Done.
38 }
39
28 - (void)stopDispatchingForSelector:(SEL)selector { 40 - (void)stopDispatchingForSelector:(SEL)selector {
29 _forwardingTargets.erase(selector); 41 _forwardingTargets.erase(selector);
30 } 42 }
31 43
44 - (void)stopDispatchingForProtocol:(Protocol*)protocol {
45 unsigned int outCount;
46 objc_method_description* requiredInstanceMethods =
47 protocol_copyMethodDescriptionList(protocol, YES /* isRequiredMethod */,
48 YES /* isInstanceMethod */, &outCount);
49 for (unsigned int i = 0; i < outCount; i++) {
50 [self stopDispatchingForSelector:requiredInstanceMethods[i].name];
51 }
52 }
53
32 // |-stopDispatchingToTarget| should be called much less often than 54 // |-stopDispatchingToTarget| should be called much less often than
33 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order 55 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order
34 // to prioritize the speed of lookups. 56 // to prioritize the speed of lookups.
35 - (void)stopDispatchingToTarget:(id)target { 57 - (void)stopDispatchingToTarget:(id)target {
36 std::vector<SEL> selectorsToErase; 58 std::vector<SEL> selectorsToErase;
37 for (auto& kv : _forwardingTargets) { 59 for (auto& kv : _forwardingTargets) {
38 if (kv.second == target) { 60 if (kv.second == target) {
39 selectorsToErase.push_back(kv.first); 61 selectorsToErase.push_back(kv.first);
40 } 62 }
41 } 63 }
42 64
43 for (auto* selector : selectorsToErase) { 65 for (auto* selector : selectorsToErase) {
44 [self stopDispatchingForSelector:selector]; 66 [self stopDispatchingForSelector:selector];
45 } 67 }
46 } 68 }
47 69
48 #pragma mark - NSObject 70 #pragma mark - NSObject
49 71
50 // Overridden to forward messages to registered handlers. 72 // Overridden to forward messages to registered handlers.
51 - (id)forwardingTargetForSelector:(SEL)selector { 73 - (id)forwardingTargetForSelector:(SEL)selector {
52 auto target = _forwardingTargets.find(selector); 74 auto target = _forwardingTargets.find(selector);
53 if (target != _forwardingTargets.end()) { 75 if (target != _forwardingTargets.end()) {
54 return target->second; 76 return target->second;
55 } 77 }
56 return [super forwardingTargetForSelector:selector]; 78 return [super forwardingTargetForSelector:selector];
57 } 79 }
58 80
59 @end 81 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698