Index: ios/clean/chrome/browser/ui/commands/command_dispatcher.mm |
diff --git a/ios/clean/chrome/browser/ui/commands/command_dispatcher.mm b/ios/clean/chrome/browser/ui/commands/command_dispatcher.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..36bc5d286b0c6b4fa02bd6624cb38cd5b1b16e8a |
--- /dev/null |
+++ b/ios/clean/chrome/browser/ui/commands/command_dispatcher.mm |
@@ -0,0 +1,44 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ios/clean/chrome/browser/ui/commands/command_dispatcher.h" |
+ |
+#include <unordered_map> |
+#include <vector> |
+ |
+#include "base/logging.h" |
+ |
lpromero
2017/03/07 10:04:17
Missing ARC header.
rohitrao (ping after 24h)
2017/03/09 14:41:11
We should add this to boilerplate.py =)
sdefresne
2017/03/09 15:38:07
This has already been done (https://codereview.chr
lpromero
2017/03/09 16:16:30
Well, Sylvain has struck again! https://codereview
|
+@implementation CommandDispatcher { |
+ std::unordered_map<SEL, __weak id> _forwardingTargets; |
+} |
+ |
+- (void)registerTarget:(id)target forSelector:(SEL)selector { |
+ DCHECK(_forwardingTargets.find(selector) == _forwardingTargets.end()); |
+ |
+ _forwardingTargets[selector] = target; |
+} |
+ |
+- (void)stopDispatchingForTarget:(id)target { |
+ std::vector<SEL> selectorsToErase; |
sdefresne
2017/03/06 22:32:59
I'm wondering whether using C++ algorithms is simp
rohitrao (ping after 24h)
2017/03/09 14:41:11
Kept my original code but changed it to a range-ba
sdefresne
2017/03/09 15:38:07
Looks better, thank you.
|
+ for (auto it = _forwardingTargets.begin(); it != _forwardingTargets.end(); |
+ ++it) { |
+ if (it->second == target) { |
+ selectorsToErase.push_back(it->first); |
+ } |
+ } |
+ |
+ for (auto* selector : selectorsToErase) { |
+ _forwardingTargets.erase(selector); |
+ } |
+} |
+ |
lpromero
2017/03/07 10:04:17
#pragma mark - NSObject
to signify that this is a
rohitrao (ping after 24h)
2017/03/09 14:41:11
Done.
|
+- (id)forwardingTargetForSelector:(SEL)selector { |
+ auto target = _forwardingTargets.find(selector); |
+ if (target != _forwardingTargets.end()) { |
+ return target->second; |
+ } |
+ return nil; |
sdefresne
2017/03/06 22:32:59
I think we should NOTREACHED() here as this will g
rohitrao (ping after 24h)
2017/03/07 18:53:58
I can't put a check here, because this object will
sdefresne
2017/03/07 19:12:35
Ack. We'll have to watch for those uncaught Object
rohitrao (ping after 24h)
2017/03/09 14:41:11
Let's revisit this in a few days. I can try to ad
|
+} |
+ |
+@end |