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