1 // Copyright 2012 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.
5 #import "ui/base/ios/cru_context_menu_holder.h"
7 #import "base/logging.h"
8 #import "base/mac/scoped_nsobject.h"
10 @implementation CRUContextMenuHolder {
11 // Backs the property of the same name.
12 base::scoped_nsobject<NSString> _menuTitle;
13 // Stores the itemTitles of the menu items.
14 base::scoped_nsobject<NSMutableArray> _itemTitles;
15 // Keep a copy of all the actions blocks.
16 base::scoped_nsobject<NSMutableArray> _actions;
17 // A set of indices in |_actions| that should cause the context menu to be
18 // dismissed without animation when tapped.
19 base::scoped_nsobject<NSMutableIndexSet> _dismissImmediatelyActions;
22 - (instancetype)init {
25 _itemTitles.reset([[NSMutableArray alloc] init]);
26 _actions.reset([[NSMutableArray alloc] init]);
27 _dismissImmediatelyActions.reset([[NSMutableIndexSet alloc] init]);
32 - (NSString*)menuTitle {
36 - (void)setMenuTitle:(NSString*)newTitle {
37 _menuTitle.reset([newTitle copy]);
40 - (NSArray*)itemTitles {
41 return [[_itemTitles copy] autorelease];
44 - (NSUInteger)itemCount {
45 return [_itemTitles count];
48 - (void)appendItemWithTitle:(NSString*)title action:(ProceduralBlock)action {
49 [self appendItemWithTitle:title action:action dismissImmediately:NO];
52 - (void)appendItemWithTitle:(NSString*)title
53 action:(ProceduralBlock)action
54 dismissImmediately:(BOOL)dismissImmediately {
55 DCHECK(title && action);
56 [_itemTitles addObject:title];
57 // The action block needs to receive the copy message in order to freeze the
58 // stack data it uses. Only then can it be safely retained by the array.
59 base::scoped_nsprotocol<id> heapAction([action copy]);
60 [_actions addObject:heapAction];
61 if (dismissImmediately) {
62 [_dismissImmediatelyActions addIndex:[_actions count] - 1];
66 - (void)performActionAtIndex:(NSUInteger)index {
67 static_cast<ProceduralBlock>([_actions objectAtIndex:index])();
70 - (BOOL)shouldDismissImmediatelyOnClickedAtIndex:(NSUInteger)index {
71 return [_dismissImmediatelyActions containsIndex:index];