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#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_WEBUI_LOGIN_VIEW_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_WEBUI_LOGIN_VIEW_H_
7
8#include <map>
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "base/observer_list.h"
13#include "chrome/browser/extensions/scoped_gaia_auth_extension.h"
14#include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
15#include "components/web_modal/web_contents_modal_dialog_host.h"
16#include "content/public/browser/notification_observer.h"
17#include "content/public/browser/notification_registrar.h"
18#include "content/public/browser/web_contents_delegate.h"
19#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
20#include "ui/views/widget/widget.h"
21#include "ui/views/widget/widget_delegate.h"
22
23class GURL;
24
25namespace content {
26class WebUI;
27}
28
29namespace views {
30class View;
31class WebView;
32class Widget;
33}
34
35namespace chromeos {
36
37// View used to render a WebUI supporting Widget. This widget is used for the
38// WebUI based start up and lock screens. It contains a WebView.
39class WebUILoginView : public views::View,
40                       public content::WebContentsDelegate,
41                       public content::NotificationObserver,
42                       public ChromeWebModalDialogManagerDelegate,
43                       public web_modal::WebContentsModalDialogHost {
44 public:
45  // Internal class name.
46  static const char kViewClassName[];
47
48  WebUILoginView();
49  virtual ~WebUILoginView();
50
51  // Initializes the webui login view.
52  virtual void Init(views::Widget* login_window);
53
54  // Overridden from views::Views:
55  virtual bool AcceleratorPressed(
56      const ui::Accelerator& accelerator) OVERRIDE;
57  virtual const char* GetClassName() const OVERRIDE;
58
59  // Overridden from ChromeWebModalDialogManagerDelegate:
60  virtual web_modal::WebContentsModalDialogHost*
61      GetWebContentsModalDialogHost() OVERRIDE;
62
63  // Overridden from web_modal::WebContentsModalDialogHost:
64  virtual gfx::NativeView GetHostView() const OVERRIDE;
65  virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
66  virtual void AddObserver(
67      web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE;
68  virtual void RemoveObserver(
69      web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE;
70
71  // Called when WebUI window is created.
72  virtual void OnWindowCreated();
73
74  // Gets the native window from the view widget.
75  gfx::NativeWindow GetNativeWindow() const;
76
77  // Invokes SetWindowType for the window. This is invoked during startup and
78  // after we've painted.
79  void UpdateWindowType();
80
81  // Loads given page. Should be called after Init() has been called.
82  void LoadURL(const GURL& url);
83
84  // Returns current WebUI.
85  content::WebUI* GetWebUI();
86
87  // Returns current WebContents.
88  content::WebContents* GetWebContents();
89
90  // Opens proxy settings dialog.
91  void OpenProxySettings();
92
93  // Called when WebUI is being shown after being initilized hidden.
94  void OnPostponedShow();
95
96  // Toggles status area visibility.
97  void SetStatusAreaVisible(bool visible);
98
99  // Sets whether UI should be enabled.
100  void SetUIEnabled(bool enabled);
101
102  void set_is_hidden(bool hidden) { is_hidden_ = hidden; }
103
104  // Let suppress emission of this signal.
105  void set_should_emit_login_prompt_visible(bool emit) {
106    should_emit_login_prompt_visible_ = emit;
107  }
108
109 protected:
110  // Overridden from views::View:
111  virtual void Layout() OVERRIDE;
112  virtual void OnLocaleChanged() OVERRIDE;
113  virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
114  virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
115
116  // Overridden from content::NotificationObserver.
117  virtual void Observe(int type,
118                       const content::NotificationSource& source,
119                       const content::NotificationDetails& details) OVERRIDE;
120
121  // WebView for rendering a webpage as a webui login.
122  views::WebView* webui_login_;
123
124 private:
125  // Map type for the accelerator-to-identifier map.
126  typedef std::map<ui::Accelerator, std::string> AccelMap;
127
128  // Overridden from content::WebContentsDelegate.
129  virtual bool HandleContextMenu(
130      const content::ContextMenuParams& params) OVERRIDE;
131  virtual void HandleKeyboardEvent(
132      content::WebContents* source,
133      const content::NativeWebKeyboardEvent& event) OVERRIDE;
134  virtual bool IsPopupOrPanel(
135      const content::WebContents* source) const OVERRIDE;
136  virtual bool TakeFocus(content::WebContents* source, bool reverse) OVERRIDE;
137  virtual void RequestMediaAccessPermission(
138      content::WebContents* web_contents,
139      const content::MediaStreamRequest& request,
140      const content::MediaResponseCallback& callback) OVERRIDE;
141
142  // Performs series of actions when login prompt is considered
143  // to be ready and visible.
144  // 1. Emits LoginPromptVisible signal if needed
145  // 2. Notifies OOBE/sign classes.
146  void OnLoginPromptVisible();
147
148  // Called when focus is returned from status area.
149  // |reverse| is true when focus is traversed backwards (using Shift-Tab).
150  void ReturnFocus(bool reverse);
151
152  content::NotificationRegistrar registrar_;
153
154  // Login window which shows the view.
155  views::Widget* login_window_;
156
157  // Converts keyboard events on the WebContents to accelerators.
158  views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_;
159
160  // Maps installed accelerators to OOBE webui accelerator identifiers.
161  AccelMap accel_map_;
162
163  // Whether the host window is frozen.
164  bool host_window_frozen_;
165
166  // True when WebUI is being initialized hidden.
167  bool is_hidden_;
168
169  // True is login-prompt-visible event has been already handled.
170  bool login_prompt_visible_handled_;
171
172  // Should we emit the login-prompt-visible signal when the login page is
173  // displayed?
174  bool should_emit_login_prompt_visible_;
175
176  // True to forward keyboard event.
177  bool forward_keyboard_event_;
178
179  scoped_ptr<ScopedGaiaAuthExtension> auth_extension_;
180
181  ObserverList<web_modal::WebContentsModalDialogHostObserver> observer_list_;
182
183  DISALLOW_COPY_AND_ASSIGN(WebUILoginView);
184};
185
186}  // namespace chromeos
187
188#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_WEBUI_LOGIN_VIEW_H_
189