| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "content/shell/shell_login_dialog.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/mac/bundle_locations.h" |
| 11 #import "base/mac/cocoa_protocols.h" |
| 12 #import "base/memory/scoped_nsobject.h" |
| 13 #include "base/sys_string_conversions.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 |
| 16 static const int kUsernameFieldTag = 1; |
| 17 static const int kPasswordFieldTag = 2; |
| 18 |
| 19 // Helper object that receives the notification that the dialog/sheet is |
| 20 // going away. |
| 21 @interface ShellLoginDialogHelper : NSObject<NSAlertDelegate> { |
| 22 @private |
| 23 scoped_nsobject<NSAlert> alert_; |
| 24 NSTextField* usernameField_; // WEAK; owned by alert_ |
| 25 NSSecureTextField* passwordField_; // WEAK; owned by alert_ |
| 26 } |
| 27 |
| 28 - (NSAlert*)alert; |
| 29 - (NSView*)accessoryView; |
| 30 - (void)focus; |
| 31 - (void)alertDidEnd:(NSAlert*)alert |
| 32 returnCode:(int)returnCode |
| 33 contextInfo:(void*)contextInfo; |
| 34 - (void)cancel; |
| 35 |
| 36 @end |
| 37 |
| 38 @implementation ShellLoginDialogHelper |
| 39 |
| 40 - (NSAlert*)alert { |
| 41 alert_.reset([[NSAlert alloc] init]); |
| 42 [alert_ setAccessoryView:[self accessoryView]]; |
| 43 return alert_; |
| 44 } |
| 45 |
| 46 - (NSView*)accessoryView { |
| 47 scoped_nsobject<NSNib> nib( |
| 48 [[NSNib alloc] initWithNibNamed:@"HttpAuth" |
| 49 bundle:base::mac::FrameworkBundle()]); |
| 50 if (!nib) |
| 51 return nil; |
| 52 |
| 53 NSArray* objects; |
| 54 BOOL success = [nib instantiateNibWithOwner:nil |
| 55 topLevelObjects:&objects]; |
| 56 if (!success) |
| 57 return nil; |
| 58 [objects makeObjectsPerformSelector:@selector(release)]; |
| 59 |
| 60 for (NSView* view in objects) { |
| 61 if (![view isKindOfClass:[NSView class]]) |
| 62 continue; |
| 63 |
| 64 usernameField_ = [view viewWithTag:kUsernameFieldTag]; |
| 65 passwordField_ = [view viewWithTag:kPasswordFieldTag]; |
| 66 return view; |
| 67 } |
| 68 |
| 69 return nil; |
| 70 } |
| 71 |
| 72 - (void)focus { |
| 73 [[alert_ window] makeFirstResponder:usernameField_]; |
| 74 } |
| 75 |
| 76 - (void)alertDidEnd:(NSAlert*)alert |
| 77 returnCode:(int)returnCode |
| 78 contextInfo:(void*)contextInfo { |
| 79 if (returnCode == NSRunStoppedResponse) |
| 80 return; |
| 81 |
| 82 content::ShellLoginDialog* this_dialog = |
| 83 reinterpret_cast<content::ShellLoginDialog*>(contextInfo); |
| 84 if (returnCode == NSAlertFirstButtonReturn) { |
| 85 this_dialog->UserAcceptedAuth( |
| 86 base::SysNSStringToUTF16([usernameField_ stringValue]), |
| 87 base::SysNSStringToUTF16([passwordField_ stringValue])); |
| 88 } else { |
| 89 this_dialog->UserCancelledAuth(); |
| 90 } |
| 91 } |
| 92 |
| 93 - (void)cancel { |
| 94 [NSApp endSheet:[alert_ window]]; |
| 95 alert_.reset(); |
| 96 } |
| 97 |
| 98 @end |
| 99 |
| 100 namespace content { |
| 101 |
| 102 void ShellLoginDialog::PlatformCreateDialog(const string16& message) { |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 104 helper_ = [[ShellLoginDialogHelper alloc] init]; |
| 105 |
| 106 // Show the modal dialog. |
| 107 NSAlert* alert = [helper_ alert]; |
| 108 [alert setDelegate:helper_]; |
| 109 [alert setInformativeText:base::SysUTF16ToNSString(message)]; |
| 110 [alert setMessageText:@"Please log in."]; |
| 111 [alert addButtonWithTitle:@"OK"]; |
| 112 NSButton* other = [alert addButtonWithTitle:@"Cancel"]; |
| 113 [other setKeyEquivalent:@"\e"]; |
| 114 [alert |
| 115 beginSheetModalForWindow:nil // nil here makes it app-modal |
| 116 modalDelegate:helper_ |
| 117 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) |
| 118 contextInfo:this]; |
| 119 |
| 120 [helper_ focus]; |
| 121 } |
| 122 |
| 123 void ShellLoginDialog::PlatformCleanUp() { |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 125 [helper_ release]; |
| 126 helper_ = nil; |
| 127 } |
| 128 |
| 129 void ShellLoginDialog::PlatformRequestCancelled() { |
| 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 131 [helper_ cancel]; |
| 132 } |
| 133 |
| 134 } // namespace content |
| OLD | NEW |