| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 #include "remoting/host/it2me/it2me_confirmation_dialog.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/callback.h" |
| 10 #include "base/callback_helpers.h" |
| 11 #include "base/i18n/message_formatter.h" |
| 12 #include "base/location.h" |
| 13 #include "base/mac/scoped_nsautorelease_pool.h" |
| 14 #include "base/mac/scoped_nsobject.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/time/time.h" |
| 20 #include "base/timer/timer.h" |
| 21 #include "remoting/base/string_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/l10n/l10n_util_mac.h" |
| 24 |
| 25 @interface It2MeConfirmationDialogMacController : NSObject { |
| 26 @private |
| 27 base::scoped_nsobject<NSAlert> confirmation_alert_; |
| 28 base::string16 username_; |
| 29 remoting::It2MeConfirmationDialog::ResultCallback dialog_action_callback_; |
| 30 } |
| 31 |
| 32 - (id)initWithCallback: |
| 33 (const remoting::It2MeConfirmationDialog::ResultCallback&)callback |
| 34 username:(const std::string&)username; |
| 35 - (void)show; |
| 36 - (void)hide; |
| 37 - (void)onCancel:(id)sender; |
| 38 - (void)onAccept:(id)sender; |
| 39 @end |
| 40 |
| 41 namespace remoting { |
| 42 |
| 43 namespace { |
| 44 // Time to wait before closing the dialog and cancelling the connection. |
| 45 constexpr base::TimeDelta kDialogTimeout = base::TimeDelta::FromMinutes(1); |
| 46 } |
| 47 |
| 48 // Bridge between C++ and ObjC implementations of It2MeConfirmationDialog. |
| 49 class It2MeConfirmationDialogMac : public It2MeConfirmationDialog { |
| 50 public: |
| 51 It2MeConfirmationDialogMac(); |
| 52 ~It2MeConfirmationDialogMac() override; |
| 53 |
| 54 // It2MeConfirmationDialog implementation. |
| 55 void Show(const std::string& remote_user_email, |
| 56 const ResultCallback& callback) override; |
| 57 |
| 58 private: |
| 59 void OnDialogAction(Result result); |
| 60 |
| 61 base::scoped_nsobject<It2MeConfirmationDialogMacController> controller_; |
| 62 |
| 63 ResultCallback result_callback_; |
| 64 |
| 65 base::OneShotTimer dialog_timer_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(It2MeConfirmationDialogMac); |
| 68 }; |
| 69 |
| 70 It2MeConfirmationDialogMac::It2MeConfirmationDialogMac() {} |
| 71 |
| 72 It2MeConfirmationDialogMac::~It2MeConfirmationDialogMac() { |
| 73 dialog_timer_.Stop(); |
| 74 |
| 75 if (controller_) { |
| 76 base::mac::ScopedNSAutoreleasePool pool; |
| 77 [controller_ hide]; |
| 78 } |
| 79 } |
| 80 |
| 81 void It2MeConfirmationDialogMac::Show(const std::string& remote_user_email, |
| 82 const ResultCallback& callback) { |
| 83 result_callback_ = callback; |
| 84 |
| 85 dialog_timer_.Start(FROM_HERE, kDialogTimeout, |
| 86 base::Bind(&It2MeConfirmationDialogMac::OnDialogAction, |
| 87 base::Unretained(this), Result::CANCEL)); |
| 88 |
| 89 ResultCallback dialog_action_callback = base::Bind( |
| 90 &It2MeConfirmationDialogMac::OnDialogAction, base::Unretained(this)); |
| 91 |
| 92 base::mac::ScopedNSAutoreleasePool pool; |
| 93 controller_.reset([[It2MeConfirmationDialogMacController alloc] |
| 94 initWithCallback:dialog_action_callback |
| 95 username:remote_user_email]); |
| 96 [controller_ show]; |
| 97 } |
| 98 |
| 99 void It2MeConfirmationDialogMac::OnDialogAction(Result result) { |
| 100 dialog_timer_.Stop(); |
| 101 |
| 102 if (controller_) { |
| 103 base::mac::ScopedNSAutoreleasePool pool; |
| 104 [controller_ hide]; |
| 105 controller_.reset(); |
| 106 } |
| 107 |
| 108 if (result_callback_) { |
| 109 base::ResetAndReturn(&result_callback_).Run(result); |
| 110 } |
| 111 } |
| 112 |
| 113 std::unique_ptr<It2MeConfirmationDialog> |
| 114 It2MeConfirmationDialogFactory::Create() { |
| 115 return base::MakeUnique<It2MeConfirmationDialogMac>(); |
| 116 } |
| 117 |
| 118 } // namespace remoting |
| 119 |
| 120 @implementation It2MeConfirmationDialogMacController |
| 121 |
| 122 - (id)initWithCallback: |
| 123 (const remoting::It2MeConfirmationDialog::ResultCallback&)callback |
| 124 username:(const std::string&)username { |
| 125 if ((self = [super init])) { |
| 126 username_ = base::UTF8ToUTF16(username); |
| 127 dialog_action_callback_ = callback; |
| 128 } |
| 129 return self; |
| 130 } |
| 131 |
| 132 - (void)show { |
| 133 confirmation_alert_.reset([[NSAlert alloc] init]); |
| 134 |
| 135 base::string16 dialog_text = |
| 136 base::i18n::MessageFormatter::FormatWithNumberedArgs( |
| 137 l10n_util::GetStringUTF16( |
| 138 IDS_SHARE_CONFIRM_DIALOG_MESSAGE_WITH_USERNAME), |
| 139 username_); |
| 140 [confirmation_alert_ setMessageText:base::SysUTF16ToNSString(dialog_text)]; |
| 141 |
| 142 NSButton* cancel_button = [confirmation_alert_ |
| 143 addButtonWithTitle:l10n_util::GetNSString( |
| 144 IDS_SHARE_CONFIRM_DIALOG_DECLINE)]; |
| 145 [cancel_button setAction:@selector(onCancel:)]; |
| 146 [cancel_button setTarget:self]; |
| 147 |
| 148 NSButton* confirm_button = [confirmation_alert_ |
| 149 addButtonWithTitle:l10n_util::GetNSString( |
| 150 IDS_SHARE_CONFIRM_DIALOG_CONFIRM)]; |
| 151 [confirm_button setAction:@selector(onAccept:)]; |
| 152 [confirm_button setTarget:self]; |
| 153 |
| 154 NSBundle* bundle = [NSBundle bundleForClass:[self class]]; |
| 155 NSString* imagePath = [bundle pathForResource:@"chromoting128" ofType:@"png"]; |
| 156 base::scoped_nsobject<NSImage> image( |
| 157 [[NSImage alloc] initByReferencingFile:imagePath]); |
| 158 [confirmation_alert_ setIcon:image]; |
| 159 [confirmation_alert_ layout]; |
| 160 |
| 161 // Force alert to be at the proper level and location. |
| 162 NSWindow* confirmation_window = [confirmation_alert_ window]; |
| 163 [confirmation_window center]; |
| 164 [confirmation_window setTitle:l10n_util::GetNSString(IDS_PRODUCT_NAME)]; |
| 165 [confirmation_window setLevel:NSNormalWindowLevel]; |
| 166 [confirmation_window orderFrontRegardless]; |
| 167 [confirmation_window makeKeyWindow]; |
| 168 } |
| 169 |
| 170 - (void)hide { |
| 171 confirmation_alert_.reset(); |
| 172 } |
| 173 |
| 174 - (void)onCancel:(id)sender { |
| 175 [self hide]; |
| 176 if (dialog_action_callback_) { |
| 177 base::ResetAndReturn(&dialog_action_callback_) |
| 178 .Run(remoting::It2MeConfirmationDialog::Result::CANCEL); |
| 179 } |
| 180 } |
| 181 |
| 182 - (void)onAccept:(id)sender { |
| 183 [self hide]; |
| 184 if (dialog_action_callback_) { |
| 185 base::ResetAndReturn(&dialog_action_callback_) |
| 186 .Run(remoting::It2MeConfirmationDialog::Result::OK); |
| 187 } |
| 188 } |
| 189 |
| 190 @end |
| OLD | NEW |