Home | History | Annotate | Download | only in ui
      1 // Copyright 2014 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_UI_WEBUI_LOGIN_VIEW_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_UI_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/signin/scoped_gaia_auth_extension.h"
     14 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
     15 #include "components/web_modal/popup_manager.h"
     16 #include "components/web_modal/web_contents_modal_dialog_host.h"
     17 #include "content/public/browser/notification_observer.h"
     18 #include "content/public/browser/notification_registrar.h"
     19 #include "content/public/browser/web_contents_delegate.h"
     20 #include "content/public/browser/web_contents_observer.h"
     21 #include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
     22 #include "ui/views/widget/widget.h"
     23 #include "ui/views/widget/widget_delegate.h"
     24 
     25 class GURL;
     26 
     27 namespace content {
     28 class WebUI;
     29 }
     30 
     31 namespace views {
     32 class View;
     33 class WebView;
     34 class Widget;
     35 }
     36 
     37 namespace chromeos {
     38 
     39 // View used to render a WebUI supporting Widget. This widget is used for the
     40 // WebUI based start up and lock screens. It contains a WebView.
     41 class WebUILoginView : public views::View,
     42                        public content::WebContentsDelegate,
     43                        public content::WebContentsObserver,
     44                        public content::NotificationObserver,
     45                        public ChromeWebModalDialogManagerDelegate,
     46                        public web_modal::WebContentsModalDialogHost {
     47  public:
     48   class FrameObserver {
     49    public:
     50     // Called when a frame failed to load.
     51     virtual void OnFrameError(const std::string& frame_unique_name) = 0;
     52   };
     53 
     54   // Internal class name.
     55   static const char kViewClassName[];
     56 
     57   WebUILoginView();
     58   virtual ~WebUILoginView();
     59 
     60   // Initializes the webui login view.
     61   virtual void Init();
     62 
     63   // Overridden from views::View:
     64   virtual bool AcceleratorPressed(
     65       const ui::Accelerator& accelerator) OVERRIDE;
     66   virtual const char* GetClassName() const OVERRIDE;
     67   virtual void RequestFocus() OVERRIDE;
     68 
     69   // Overridden from ChromeWebModalDialogManagerDelegate:
     70   virtual web_modal::WebContentsModalDialogHost*
     71       GetWebContentsModalDialogHost() OVERRIDE;
     72 
     73   // Overridden from web_modal::WebContentsModalDialogHost:
     74   virtual gfx::NativeView GetHostView() const OVERRIDE;
     75   virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
     76   virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
     77   virtual void AddObserver(
     78       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
     79   virtual void RemoveObserver(
     80       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
     81 
     82   // Gets the native window from the view widget.
     83   gfx::NativeWindow GetNativeWindow() const;
     84 
     85   // Loads given page. Should be called after Init() has been called.
     86   void LoadURL(const GURL& url);
     87 
     88   // Returns current WebUI.
     89   content::WebUI* GetWebUI();
     90 
     91   // Returns current WebContents.
     92   content::WebContents* GetWebContents();
     93 
     94   // Opens proxy settings dialog.
     95   void OpenProxySettings();
     96 
     97   // Called when WebUI is being shown after being initilized hidden.
     98   void OnPostponedShow();
     99 
    100   // Toggles status area visibility.
    101   void SetStatusAreaVisible(bool visible);
    102 
    103   // Sets whether UI should be enabled.
    104   void SetUIEnabled(bool enabled);
    105 
    106   void set_is_hidden(bool hidden) { is_hidden_ = hidden; }
    107 
    108   bool webui_visible() const { return webui_visible_; }
    109 
    110   // Let suppress emission of this signal.
    111   void set_should_emit_login_prompt_visible(bool emit) {
    112     should_emit_login_prompt_visible_ = emit;
    113   }
    114 
    115   void AddFrameObserver(FrameObserver* frame_observer);
    116   void RemoveFrameObserver(FrameObserver* frame_observer);
    117 
    118  protected:
    119   // Overridden from views::View:
    120   virtual void Layout() OVERRIDE;
    121   virtual void OnLocaleChanged() OVERRIDE;
    122   virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
    123   virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE;
    124 
    125   // Overridden from content::NotificationObserver.
    126   virtual void Observe(int type,
    127                        const content::NotificationSource& source,
    128                        const content::NotificationDetails& details) OVERRIDE;
    129 
    130   // WebView for rendering a webpage as a webui login.
    131   views::WebView* webui_login_;
    132 
    133  private:
    134   // Map type for the accelerator-to-identifier map.
    135   typedef std::map<ui::Accelerator, std::string> AccelMap;
    136 
    137   // Overridden from content::WebContentsDelegate.
    138   virtual bool HandleContextMenu(
    139       const content::ContextMenuParams& params) OVERRIDE;
    140   virtual void HandleKeyboardEvent(
    141       content::WebContents* source,
    142       const content::NativeWebKeyboardEvent& event) OVERRIDE;
    143   virtual bool IsPopupOrPanel(
    144       const content::WebContents* source) const OVERRIDE;
    145   virtual bool TakeFocus(content::WebContents* source, bool reverse) OVERRIDE;
    146   virtual void RequestMediaAccessPermission(
    147       content::WebContents* web_contents,
    148       const content::MediaStreamRequest& request,
    149       const content::MediaResponseCallback& callback) OVERRIDE;
    150   virtual bool CheckMediaAccessPermission(
    151       content::WebContents* web_contents,
    152       const GURL& security_origin,
    153       content::MediaStreamType type) OVERRIDE;
    154   virtual bool PreHandleGestureEvent(
    155       content::WebContents* source,
    156       const blink::WebGestureEvent& event) OVERRIDE;
    157 
    158   // Overridden from content::WebContentsObserver.
    159   virtual void DidFailProvisionalLoad(
    160       content::RenderFrameHost* render_frame_host,
    161       const GURL& validated_url,
    162       int error_code,
    163       const base::string16& error_description) OVERRIDE;
    164 
    165   // Performs series of actions when login prompt is considered
    166   // to be ready and visible.
    167   // 1. Emits LoginPromptVisible signal if needed
    168   // 2. Notifies OOBE/sign classes.
    169   void OnLoginPromptVisible();
    170 
    171   // Called when focus is returned from status area.
    172   // |reverse| is true when focus is traversed backwards (using Shift-Tab).
    173   void ReturnFocus(bool reverse);
    174 
    175   content::NotificationRegistrar registrar_;
    176 
    177   // Converts keyboard events on the WebContents to accelerators.
    178   views::UnhandledKeyboardEventHandler unhandled_keyboard_event_handler_;
    179 
    180   // Maps installed accelerators to OOBE webui accelerator identifiers.
    181   AccelMap accel_map_;
    182 
    183   // True when WebUI is being initialized hidden.
    184   bool is_hidden_;
    185 
    186   // True when the WebUI has finished initializing and is visible.
    187   bool webui_visible_;
    188 
    189   // Should we emit the login-prompt-visible signal when the login page is
    190   // displayed?
    191   bool should_emit_login_prompt_visible_;
    192 
    193   // True to forward keyboard event.
    194   bool forward_keyboard_event_;
    195 
    196   scoped_ptr<ScopedGaiaAuthExtension> auth_extension_;
    197 
    198   ObserverList<web_modal::ModalDialogHostObserver> observer_list_;
    199   ObserverList<FrameObserver> frame_observer_list_;
    200 
    201   // Manage popups appearing over the login window.
    202   // TODO(gbillock): See if we can get rid of this. Perhaps in favor of
    203   // in-content styled popups or something? There oughtta be a way...
    204   scoped_ptr<web_modal::PopupManager> popup_manager_;
    205 
    206   DISALLOW_COPY_AND_ASSIGN(WebUILoginView);
    207 };
    208 
    209 }  // namespace chromeos
    210 
    211 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_UI_WEBUI_LOGIN_VIEW_H_
    212