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

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

Issue 2734863002: [ios] Adds a CommandDispatcher to proxy method calls to UI handlers. (Closed)
Patch Set: Review. Created 3 years, 9 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/clean/chrome/browser/ui/commands/command_dispatcher.h"
6
7 #include <unordered_map>
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "base/strings/sys_string_conversions.h"
12
13 #if !defined(__has_feature) || !__has_feature(objc_arc)
14 #error "This file requires ARC support."
15 #endif
16
17 @implementation CommandDispatcher {
18 // Stores which target to forward to for a given selector.
19 std::unordered_map<SEL, __weak id> _forwardingTargets;
20 }
21
22 - (void)registerTarget:(id)target forSelector:(SEL)selector {
23 DCHECK(_forwardingTargets.find(selector) == _forwardingTargets.end());
24
25 _forwardingTargets[selector] = target;
26 }
27
28 // |-stopDispatchingForTarget| should be called much less often than
29 // |-forwardingTargetForSelector|, so removal is intentionally O(n) in order
30 // to prioritize the speed of lookups.
31 - (void)stopDispatchingForTarget:(id)target {
32 std::vector<SEL> selectorsToErase;
33 for (auto& kv : _forwardingTargets) {
34 if (kv.second == target) {
35 selectorsToErase.push_back(kv.first);
36 }
37 }
38
39 for (auto* selector : selectorsToErase) {
40 _forwardingTargets.erase(selector);
41 }
42 }
43
44 #pragma mark - NSObject
45
46 // Overridden to forward messages to registered handlers.
47 - (id)forwardingTargetForSelector:(SEL)selector {
48 auto target = _forwardingTargets.find(selector);
49 if (target != _forwardingTargets.end()) {
50 return target->second;
51 }
52 return [super forwardingTargetForSelector:selector];
53 }
54
55 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698