Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
Robert Sesek
2016/05/23 20:13:53
How about a unit test?
juncai
2016/05/27 21:30:37
Done.
| |
| 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 "chrome/browser/ui/cocoa/extensions/chooser_dialog_cocoa_controller.h" | |
| 6 | |
| 7 #include "base/strings/sys_string_conversions.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #import "chrome/browser/ui/cocoa/chooser_content_view.h" | |
| 10 #import "chrome/browser/ui/cocoa/extensions/chooser_dialog_cocoa.h" | |
| 11 #include "chrome/grit/generated_resources.h" | |
| 12 #include "components/ui/chooser_controller/chooser_controller.h" | |
| 13 #include "components/url_formatter/elide_url.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "extensions/browser/extension_registry.h" | |
| 16 #import "ui/base/l10n/l10n_util_mac.h" | |
| 17 #include "url/gurl.h" | |
| 18 #include "url/origin.h" | |
| 19 | |
| 20 @implementation ChooserDialogCocoaController | |
| 21 | |
| 22 - (instancetype) | |
| 23 initWithChooserDialogCocoa:(ChooserDialogCocoa*)chooserDialogCocoa | |
| 24 initWithChooserController:(ChooserController*)chooserController { | |
| 25 DCHECK(chooserDialogCocoa); | |
| 26 DCHECK(chooserController); | |
| 27 if ((self = [super init])) { | |
| 28 chooserDialogCocoa_ = chooserDialogCocoa; | |
| 29 chooserController_ = chooserController; | |
| 30 } | |
| 31 | |
| 32 return self; | |
| 33 } | |
| 34 | |
| 35 - (void)loadView { | |
| 36 base::string16 chooserTitle; | |
| 37 url::Origin origin = chooserController_->GetOrigin(); | |
| 38 content::WebContents* web_contents = chooserDialogCocoa_->web_contents(); | |
| 39 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
| 40 extensions::ExtensionRegistry* extension_registry = | |
| 41 extensions::ExtensionRegistry::Get(browser_context); | |
| 42 if (extension_registry) { | |
| 43 const extensions::Extension* extension = | |
| 44 extension_registry->enabled_extensions().GetExtensionOrAppByURL( | |
| 45 GURL(origin.Serialize())); | |
| 46 if (extension) | |
| 47 chooserTitle = base::UTF8ToUTF16(extension->name()); | |
| 48 } | |
| 49 | |
| 50 if (chooserTitle.empty()) { | |
| 51 chooserTitle = url_formatter::FormatOriginForSecurityDisplay( | |
| 52 origin, url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC); | |
| 53 } | |
| 54 | |
| 55 chooserContentView_.reset([[ChooserContentView alloc] | |
| 56 initWithChooserTitle:l10n_util::GetNSStringF(IDS_CHOOSER_BUBBLE_PROMPT, | |
| 57 chooserTitle)]); | |
| 58 | |
| 59 tableView_ = [chooserContentView_ tableView]; | |
| 60 connectButton_ = [chooserContentView_ connectButton]; | |
| 61 cancelButton_ = [chooserContentView_ cancelButton]; | |
| 62 helpButton_ = [chooserContentView_ helpButton]; | |
| 63 | |
| 64 [connectButton_ setTarget:self]; | |
| 65 [connectButton_ setAction:@selector(onConnect:)]; | |
| 66 [cancelButton_ setTarget:self]; | |
| 67 [cancelButton_ setAction:@selector(onCancel:)]; | |
| 68 [tableView_ setDelegate:self]; | |
| 69 [tableView_ setDataSource:self]; | |
| 70 [helpButton_ setTarget:self]; | |
| 71 [helpButton_ setAction:@selector(onHelpPressed:)]; | |
| 72 self.view = chooserContentView_; | |
| 73 [self updateTableView]; | |
| 74 } | |
| 75 | |
| 76 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { | |
| 77 // When there are no devices, the table contains a message saying there are | |
| 78 // no devices, so the number of rows is always at least 1. | |
| 79 return std::max(static_cast<NSInteger>(chooserController_->NumOptions()), | |
| 80 static_cast<NSInteger>(1)); | |
|
Robert Sesek
2016/05/23 20:13:53
You shouldn't need to static_cast 1...
juncai
2016/05/27 21:30:37
It seems that the static_cast is necessary, otherw
| |
| 81 } | |
| 82 | |
| 83 - (id)tableView:(NSTableView*)tableView | |
| 84 objectValueForTableColumn:(NSTableColumn*)tableColumn | |
| 85 row:(NSInteger)rowIndex { | |
| 86 if (!chooserController_) | |
| 87 return @""; | |
| 88 NSInteger num_options = | |
| 89 static_cast<NSInteger>(chooserController_->NumOptions()); | |
| 90 if (num_options == 0) { | |
| 91 DCHECK_EQ(0, rowIndex); | |
| 92 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT); | |
| 93 } | |
| 94 | |
| 95 DCHECK_GE(rowIndex, 0); | |
| 96 DCHECK_LT(rowIndex, num_options); | |
| 97 return base::SysUTF16ToNSString( | |
| 98 chooserController_->GetOption(static_cast<size_t>(rowIndex))); | |
| 99 } | |
| 100 | |
| 101 - (BOOL)tableView:(NSTableView*)aTableView | |
| 102 shouldEditTableColumn:(NSTableColumn*)aTableColumn | |
| 103 row:(NSInteger)rowIndex { | |
| 104 return NO; | |
| 105 } | |
| 106 | |
| 107 - (void)onOptionsInitialized { | |
| 108 [self updateTableView]; | |
| 109 } | |
| 110 | |
| 111 - (void)onOptionAdded:(NSInteger)index { | |
| 112 [self updateTableView]; | |
| 113 } | |
| 114 | |
| 115 - (void)onOptionRemoved:(NSInteger)index { | |
| 116 // |tableView_| will automatically selects the next item if the current | |
| 117 // item is removed, so here it tracks if the removed item is the item | |
| 118 // that was previously selected, if so, deselect it. | |
| 119 if ([tableView_ selectedRow] == index) | |
| 120 [tableView_ deselectRow:index]; | |
| 121 | |
| 122 [self updateTableView]; | |
| 123 } | |
| 124 | |
| 125 - (void)updateTableView { | |
| 126 [tableView_ setEnabled:chooserController_->NumOptions() > 0]; | |
| 127 [tableView_ reloadData]; | |
| 128 } | |
| 129 | |
| 130 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification { | |
| 131 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0]; | |
| 132 } | |
| 133 | |
| 134 - (void)onConnect:(id)sender { | |
| 135 NSInteger row = [tableView_ selectedRow]; | |
| 136 chooserController_->Select(row); | |
| 137 chooserController_ = nil; | |
|
Robert Sesek
2016/05/23 20:13:53
nil is only for ObjC objects, use nullptr for C++
Robert Sesek
2016/05/23 20:13:53
If you mash the button hooked up to onConnect, can
juncai
2016/05/27 21:30:37
It seems that after switching the order of:
choose
| |
| 138 chooserDialogCocoa_->Dismissed(); | |
| 139 chooserDialogCocoa_ = nil; | |
| 140 } | |
| 141 | |
| 142 - (void)onCancel:(id)sender { | |
| 143 chooserController_->Cancel(); | |
| 144 chooserController_ = nil; | |
| 145 chooserDialogCocoa_->Dismissed(); | |
| 146 chooserDialogCocoa_ = nil; | |
| 147 } | |
| 148 | |
| 149 - (void)onHelpPressed:(id)sender { | |
| 150 chooserController_->OpenHelpCenterUrl(); | |
| 151 } | |
| 152 | |
| 153 @end | |
| OLD | NEW |