Cleanup: Update the path to insets and point headers.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / login_prompt_cocoa.mm
blob39d63941aba0eda1f697029f7d8dfffa6f3cd232
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.
5 #import "chrome/browser/ui/cocoa/login_prompt_cocoa.h"
7 #include "base/mac/bundle_locations.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/tab_contents/tab_util.h"
14 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
15 #include "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
16 #include "chrome/browser/ui/login/login_prompt.h"
17 #include "components/password_manager/core/browser/login_model.h"
18 #include "components/password_manager/core/browser/password_manager.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/web_contents.h"
21 #include "net/url_request/url_request.h"
22 #include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
24 using autofill::PasswordForm;
25 using content::BrowserThread;
26 using content::WebContents;
28 // ----------------------------------------------------------------------------
29 // LoginHandlerMac
31 // This class simply forwards the authentication from the LoginView (on
32 // the UI thread) to the net::URLRequest (on the I/O thread).
33 // This class uses ref counting to ensure that it lives until all InvokeLaters
34 // have been called.
35 class LoginHandlerMac : public LoginHandler,
36                         public ConstrainedWindowMacDelegate {
37  public:
38   LoginHandlerMac(net::AuthChallengeInfo* auth_info, net::URLRequest* request)
39       : LoginHandler(auth_info, request) {
40   }
42   // LoginModelObserver implementation.
43   void OnAutofillDataAvailable(const base::string16& username,
44                                const base::string16& password) override {
45     DCHECK_CURRENTLY_ON(BrowserThread::UI);
47     [sheet_controller_ autofillLogin:base::SysUTF16ToNSString(username)
48                             password:base::SysUTF16ToNSString(password)];
49   }
50   void OnLoginModelDestroying() override {}
52   // LoginHandler:
53   void BuildViewForPasswordManager(password_manager::PasswordManager* manager,
54                                    const base::string16& explanation) override {
55     DCHECK_CURRENTLY_ON(BrowserThread::UI);
57     sheet_controller_.reset(
58         [[LoginHandlerSheet alloc] initWithLoginHandler:this]);
60     SetModel(manager);
62     [sheet_controller_ setExplanation:base::SysUTF16ToNSString(explanation)];
64     // Scary thread safety note: This can potentially be called *after* SetAuth
65     // or CancelAuth (say, if the request was cancelled before the UI thread got
66     // control).  However, that's OK since any UI interaction in those functions
67     // will occur via an InvokeLater on the UI thread, which is guaranteed
68     // to happen after this is called (since this was InvokeLater'd first).
69     WebContents* requesting_contents = GetWebContentsForLogin();
70     DCHECK(requesting_contents);
72     base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
73         [[CustomConstrainedWindowSheet alloc]
74             initWithCustomWindow:[sheet_controller_ window]]);
75     constrained_window_.reset(new ConstrainedWindowMac(
76         this, requesting_contents, sheet));
78     NotifyAuthNeeded();
79   }
81   void CloseDialog() override {
82     // The hosting dialog may have been freed.
83     if (constrained_window_)
84       constrained_window_->CloseWebContentsModalDialog();
85   }
87   // Overridden from ConstrainedWindowMacDelegate:
88   void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override {
89     DCHECK_CURRENTLY_ON(BrowserThread::UI);
90     SetModel(NULL);
91     ReleaseSoon();
93     constrained_window_.reset();
94     sheet_controller_.reset();
95   }
97   void OnLoginPressed(const base::string16& username,
98                       const base::string16& password) {
99     DCHECK_CURRENTLY_ON(BrowserThread::UI);
100     SetAuth(username, password);
101   }
103   void OnCancelPressed() {
104     DCHECK_CURRENTLY_ON(BrowserThread::UI);
105     CancelAuth();
106   }
108  private:
109   friend class LoginPrompt;
111   ~LoginHandlerMac() override {
112     // This class will be deleted on a non UI thread. Ensure that the UI members
113     // have already been deleted.
114     CHECK(!constrained_window_.get());
115     CHECK(!sheet_controller_.get());
116   }
118   // The Cocoa controller of the GUI.
119   base::scoped_nsobject<LoginHandlerSheet> sheet_controller_;
121   scoped_ptr<ConstrainedWindowMac> constrained_window_;
123   DISALLOW_COPY_AND_ASSIGN(LoginHandlerMac);
126 // static
127 LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
128                                    net::URLRequest* request) {
129   return new LoginHandlerMac(auth_info, request);
132 // ----------------------------------------------------------------------------
133 // LoginHandlerSheet
135 @implementation LoginHandlerSheet
137 - (id)initWithLoginHandler:(LoginHandlerMac*)handler {
138   NSString* nibPath =
139       [base::mac::FrameworkBundle() pathForResource:@"HttpAuthLoginSheet"
140                                              ofType:@"nib"];
141   if ((self = [super initWithWindowNibPath:nibPath
142                                      owner:self])) {
143     handler_ = handler;
144     // Force the nib to load so that all outlets are initialized.
145     [self window];
146   }
147   return self;
150 - (void)dealloc {
151   // The buttons could be in a modal loop, so disconnect them so they cannot
152   // call back to us after we're dead.
153   [loginButton_ setTarget:nil];
154   [cancelButton_ setTarget:nil];
155   [super dealloc];
158 - (IBAction)loginPressed:(id)sender {
159   handler_->OnLoginPressed(
160       base::SysNSStringToUTF16([nameField_ stringValue]),
161       base::SysNSStringToUTF16([passwordField_ stringValue]));
164 - (IBAction)cancelPressed:(id)sender {
165   handler_->OnCancelPressed();
168 - (void)autofillLogin:(NSString*)login password:(NSString*)password {
169   if ([[nameField_ stringValue] length] == 0) {
170     [nameField_ setStringValue:login];
171     [passwordField_ setStringValue:password];
172     [nameField_ selectText:self];
173   }
176 - (void)setExplanation:(NSString*)explanation {
177   // Put in the text.
178   [explanationField_ setStringValue:explanation];
180   // Resize the text field.
181   CGFloat windowDelta = [GTMUILocalizerAndLayoutTweaker
182        sizeToFitFixedWidthTextField:explanationField_];
184   NSRect newFrame = [[self window] frame];
185   newFrame.size.height += windowDelta;
186   [[self window] setFrame:newFrame display:NO];
189 @end