Chromium Code Reviews| Index: content/shell/shell_login_dialog_mac.mm |
| diff --git a/content/shell/shell_login_dialog_mac.mm b/content/shell/shell_login_dialog_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2839f075447e13feeeca5f16eace0c612ac68089 |
| --- /dev/null |
| +++ b/content/shell/shell_login_dialog_mac.mm |
| @@ -0,0 +1,134 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/shell/shell_login_dialog.h" |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "base/logging.h" |
| +#include "base/mac/bundle_locations.h" |
| +#import "base/mac/cocoa_protocols.h" |
| +#import "base/memory/scoped_nsobject.h" |
| +#include "base/sys_string_conversions.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +static const int kUsernameFieldTag = 1; |
| +static const int kPasswordFieldTag = 2; |
|
jochen (gone - plz use gerrit)
2012/04/04 15:46:56
anonymous name space instead of static?
Avi (use Gerrit)
2012/04/04 16:18:03
Done.
|
| + |
| +// Helper object that receives the notification that the dialog/sheet is |
| +// going away. |
| +@interface ShellLoginDialogHelper : NSObject<NSAlertDelegate> { |
| + @private |
| + scoped_nsobject<NSAlert> alert_; |
| + NSTextField* usernameField_; // WEAK; owned by alert_ |
| + NSSecureTextField* passwordField_; // WEAK; owned by alert_ |
| +} |
| + |
| +- (NSAlert*)alert; |
| +- (NSView*)accessoryView; |
| +- (void)focus; |
| +- (void)alertDidEnd:(NSAlert*)alert |
| + returnCode:(int)returnCode |
| + contextInfo:(void*)contextInfo; |
| +- (void)cancel; |
| + |
| +@end |
| + |
| +@implementation ShellLoginDialogHelper |
| + |
| +- (NSAlert*)alert { |
| + alert_.reset([[NSAlert alloc] init]); |
| + [alert_ setAccessoryView:[self accessoryView]]; |
| + return alert_; |
| +} |
| + |
| +- (NSView*)accessoryView { |
| + scoped_nsobject<NSNib> nib( |
| + [[NSNib alloc] initWithNibNamed:@"HttpAuth" |
| + bundle:base::mac::FrameworkBundle()]); |
| + if (!nib) |
| + return nil; |
| + |
| + NSArray* objects; |
| + BOOL success = [nib instantiateNibWithOwner:nil |
| + topLevelObjects:&objects]; |
| + if (!success) |
| + return nil; |
| + [objects makeObjectsPerformSelector:@selector(release)]; |
| + |
| + for (NSView* view in objects) { |
| + if (![view isKindOfClass:[NSView class]]) |
| + continue; |
| + |
| + usernameField_ = [view viewWithTag:kUsernameFieldTag]; |
| + passwordField_ = [view viewWithTag:kPasswordFieldTag]; |
| + return view; |
| + } |
| + |
| + return nil; |
| +} |
| + |
| +- (void)focus { |
| + [[alert_ window] makeFirstResponder:usernameField_]; |
| +} |
| + |
| +- (void)alertDidEnd:(NSAlert*)alert |
| + returnCode:(int)returnCode |
| + contextInfo:(void*)contextInfo { |
| + if (returnCode == NSRunStoppedResponse) |
| + return; |
| + |
| + content::ShellLoginDialog* this_dialog = |
| + reinterpret_cast<content::ShellLoginDialog*>(contextInfo); |
| + if (returnCode == NSAlertFirstButtonReturn) { |
| + this_dialog->UserAcceptedAuth( |
| + base::SysNSStringToUTF16([usernameField_ stringValue]), |
| + base::SysNSStringToUTF16([passwordField_ stringValue])); |
| + } else { |
| + this_dialog->UserCancelledAuth(); |
| + } |
| +} |
| + |
| +- (void)cancel { |
| + [NSApp endSheet:[alert_ window]]; |
| + alert_.reset(); |
| +} |
| + |
| +@end |
| + |
| +namespace content { |
| + |
| +void ShellLoginDialog::PlatformCreateDialog(const string16& message) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + helper_ = [[ShellLoginDialogHelper alloc] init]; |
| + |
| + // Show the modal dialog. |
| + NSAlert* alert = [helper_ alert]; |
| + [alert setDelegate:helper_]; |
| + [alert setInformativeText:base::SysUTF16ToNSString(message)]; |
| + [alert setMessageText:@"Please log in."]; |
| + [alert addButtonWithTitle:@"OK"]; |
| + NSButton* other = [alert addButtonWithTitle:@"Cancel"]; |
| + [other setKeyEquivalent:@"\e"]; |
| + [alert |
| + beginSheetModalForWindow:nil // nil here makes it app-modal |
| + modalDelegate:helper_ |
| + didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) |
| + contextInfo:this]; |
| + |
| + [helper_ focus]; |
| +} |
| + |
| +void ShellLoginDialog::PlatformCleanUp() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + [helper_ release]; |
| + helper_ = nil; |
| +} |
| + |
| +void ShellLoginDialog::PlatformRequestCancelled() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + [helper_ cancel]; |
| +} |
| + |
| +} // namespace content |