Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / login_prompt_views.cc
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 "chrome/browser/ui/login/login_prompt.h"
6
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/views/login_view.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/constrained_window/constrained_window_views.h"
12 #include "components/password_manager/core/browser/password_manager.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "net/url_request/url_request.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/window/dialog_delegate.h"
20
21 // ----------------------------------------------------------------------------
22 // LoginHandlerViews
23
24 // This class simply forwards the authentication from the LoginView (on
25 // the UI thread) to the net::URLRequest (on the I/O thread).
26 // This class uses ref counting to ensure that it lives until all InvokeLaters
27 // have been called.
28 class LoginHandlerViews : public LoginHandler, public views::DialogDelegate {
29  public:
30   LoginHandlerViews(net::AuthChallengeInfo* auth_info, net::URLRequest* request)
31       : LoginHandler(auth_info, request),
32         login_view_(NULL),
33         dialog_(NULL) {
34   }
35
36   // LoginModelObserver:
37   void OnAutofillDataAvailable(const base::string16& username,
38                                const base::string16& password) override {
39     // Nothing to do here since LoginView takes care of autofill for win.
40   }
41   void OnLoginModelDestroying() override {}
42
43   // views::DialogDelegate:
44   base::string16 GetDialogButtonLabel(ui::DialogButton button) const override {
45     if (button == ui::DIALOG_BUTTON_OK)
46       return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL);
47     return DialogDelegate::GetDialogButtonLabel(button);
48   }
49
50   base::string16 GetWindowTitle() const override {
51     return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE);
52   }
53
54   void WindowClosing() override {
55     DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
56     content::WebContents* web_contents = GetWebContentsForLogin();
57     if (web_contents)
58       web_contents->GetRenderViewHost()->SetIgnoreInputEvents(false);
59
60     // Reference is no longer valid.
61     dialog_ = NULL;
62     CancelAuth();
63   }
64
65   void DeleteDelegate() override {
66     DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
67
68     // The widget is going to delete itself; clear our pointer.
69     dialog_ = NULL;
70     SetModel(NULL);
71
72     ReleaseSoon();
73   }
74
75   ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_CHILD; }
76
77   bool Cancel() override {
78     DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
79     CancelAuth();
80     return true;
81   }
82
83   bool Accept() override {
84     DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
85     SetAuth(login_view_->GetUsername(), login_view_->GetPassword());
86     return true;
87   }
88
89   views::View* GetInitiallyFocusedView() override {
90     return login_view_->GetInitiallyFocusedView();
91   }
92
93   views::View* GetContentsView() override { return login_view_; }
94   views::Widget* GetWidget() override { return login_view_->GetWidget(); }
95   const views::Widget* GetWidget() const override {
96     return login_view_->GetWidget();
97   }
98
99   // LoginHandler:
100   void BuildViewForPasswordManager(password_manager::PasswordManager* manager,
101                                    const base::string16& explanation) override {
102     DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
103
104     // Create a new LoginView and set the model for it.  The model (password
105     // manager) is owned by the WebContents, but the view is parented to the
106     // browser window, so the view may be destroyed after the password
107     // manager. The view listens for model destruction and unobserves
108     // accordingly.
109     login_view_ = new LoginView(explanation, manager);
110
111     // Scary thread safety note: This can potentially be called *after* SetAuth
112     // or CancelAuth (say, if the request was cancelled before the UI thread got
113     // control).  However, that's OK since any UI interaction in those functions
114     // will occur via an InvokeLater on the UI thread, which is guaranteed
115     // to happen after this is called (since this was InvokeLater'd first).
116     dialog_ = ShowWebModalDialogViews(this, GetWebContentsForLogin());
117     NotifyAuthNeeded();
118   }
119
120   void CloseDialog() override {
121     // The hosting widget may have been freed.
122     if (dialog_)
123       dialog_->Close();
124   }
125
126  private:
127   friend class base::RefCountedThreadSafe<LoginHandlerViews>;
128   friend class LoginPrompt;
129
130   ~LoginHandlerViews() override {}
131
132   // The LoginView that contains the user's login information.
133   LoginView* login_view_;
134
135   views::Widget* dialog_;
136
137   DISALLOW_COPY_AND_ASSIGN(LoginHandlerViews);
138 };
139
140 // static
141 LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
142                                    net::URLRequest* request) {
143   return new LoginHandlerViews(auth_info, request);
144 }