summaryrefslogtreecommitdiffstats
path: root/content/shell/browser/shell_login_dialog_mac.mm
blob: 3e014e8791ce42a50ba1bd1ee96ac50bd6182ad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2013 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/browser/shell_login_dialog.h"

#import <Cocoa/Cocoa.h>

#include "base/logging.h"
#include "base/mac/bundle_locations.h"
#import "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#import "ui/base/cocoa/nib_loading.h"

namespace {

const int kUsernameFieldTag = 1;
const int kPasswordFieldTag = 2;

}  // namespace

// Helper object that receives the notification that the dialog/sheet is
// going away.
@interface ShellLoginDialogHelper : NSObject<NSAlertDelegate> {
 @private
  base::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 {
  NSView* accessory_view = ui::GetViewFromNib(@"HttpAuth");
  if (!accessory_view)
    return nil;

  usernameField_ = [accessory_view viewWithTag:kUsernameFieldTag];
  passwordField_ = [accessory_view viewWithTag:kPasswordFieldTag];
  return accessory_view;
}

- (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 base::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