Home | History | Annotate | Download | only in identity
      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_EXTENSIONS_API_IDENTITY_EXPERIMENTAL_WEB_AUTH_FLOW_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_EXPERIMENTAL_WEB_AUTH_FLOW_H_
      7 
      8 #include "chrome/browser/ui/host_desktop.h"
      9 #include "content/public/browser/notification_observer.h"
     10 #include "content/public/browser/notification_registrar.h"
     11 #include "content/public/browser/web_contents_observer.h"
     12 #include "ui/gfx/rect.h"
     13 #include "url/gurl.h"
     14 
     15 class Profile;
     16 class ExperimentalWebAuthFlowTest;
     17 
     18 namespace content {
     19 class NotificationDetails;
     20 class NotificationSource;
     21 class RenderViewHost;
     22 class WebContents;
     23 }
     24 
     25 namespace extensions {
     26 
     27 // Controller class for web based auth flows. The
     28 // ExperimentalWebAuthFlow starts by navigating a WebContents to a URL
     29 // specificed by the caller. Any time the WebContents navigates to a
     30 // new URL, the flow's delegate is notified. The delegate is expected
     31 // to delete the flow when navigation reaches a known target URL.
     32 //
     33 // The WebContents is not displayed until the first page load
     34 // completes. This allows the flow to complete without flashing a
     35 // window on screen if the provider immediately redirects to the
     36 // target URL.
     37 //
     38 // A ExperimentalWebAuthFlow can be started in Mode::SILENT, which
     39 // never displays a window. If a window would be required, the flow
     40 // fails.
     41 class ExperimentalWebAuthFlow : public content::NotificationObserver,
     42                     public content::WebContentsObserver {
     43  public:
     44   enum Mode {
     45     INTERACTIVE,  // Show UI to the user if necessary.
     46     SILENT        // No UI should be shown.
     47   };
     48 
     49   enum Failure {
     50     WINDOW_CLOSED,  // Window closed by user.
     51     INTERACTION_REQUIRED  // Non-redirect page load in silent mode.
     52   };
     53 
     54   class Delegate {
     55    public:
     56     // Called when the auth flow fails. This means that the flow did not result
     57     // in a successful redirect to a valid redirect URL.
     58     virtual void OnAuthFlowFailure(Failure failure) = 0;
     59     // Called on redirects and other navigations to see if the URL should stop
     60     // the flow.
     61     virtual void OnAuthFlowURLChange(const GURL& redirect_url) = 0;
     62 
     63    protected:
     64     virtual ~Delegate() {}
     65   };
     66 
     67   // Creates an instance with the given parameters.
     68   // Caller owns |delegate|.
     69   ExperimentalWebAuthFlow(Delegate* delegate,
     70               Profile* profile,
     71               const GURL& provider_url,
     72               Mode mode,
     73               const gfx::Rect& initial_bounds,
     74               chrome::HostDesktopType host_desktop_type);
     75   virtual ~ExperimentalWebAuthFlow();
     76 
     77   // Starts the flow.
     78   virtual void Start();
     79 
     80   // Prevents further calls to the delegate and deletes the flow.
     81   void DetachDelegateAndDelete();
     82 
     83  protected:
     84   // Overridable for testing.
     85   virtual content::WebContents* CreateWebContents();
     86   virtual void ShowAuthFlowPopup();
     87 
     88  private:
     89   friend class ::ExperimentalWebAuthFlowTest;
     90 
     91   // NotificationObserver implementation.
     92   virtual void Observe(int type,
     93                        const content::NotificationSource& source,
     94                        const content::NotificationDetails& details) OVERRIDE;
     95 
     96   // WebContentsObserver implementation.
     97   virtual void ProvisionalChangeToMainFrameUrl(
     98       const GURL& url,
     99       content::RenderViewHost* render_view_host) OVERRIDE;
    100   virtual void DidStopLoading(
    101       content::RenderViewHost* render_view_host) OVERRIDE;
    102   virtual void WebContentsDestroyed(
    103       content::WebContents* web_contents) OVERRIDE;
    104 
    105   void BeforeUrlLoaded(const GURL& url);
    106   void AfterUrlLoaded();
    107 
    108   Delegate* delegate_;
    109   Profile* profile_;
    110   GURL provider_url_;
    111   Mode mode_;
    112   gfx::Rect initial_bounds_;
    113   chrome::HostDesktopType host_desktop_type_;
    114   bool popup_shown_;
    115 
    116   content::WebContents* contents_;
    117   content::NotificationRegistrar registrar_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(ExperimentalWebAuthFlow);
    120 };
    121 
    122 }  // namespace extensions
    123 
    124 #endif  // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_EXPERIMENTAL_WEB_AUTH_FLOW_H_
    125