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 #ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_
10 #include "apps/shell_window_registry.h"
11 #include "content/public/browser/notification_observer.h"
12 #include "content/public/browser/notification_registrar.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "ui/gfx/rect.h"
18 class WebAuthFlowTest;
21 class NotificationDetails;
22 class NotificationSource;
27 namespace extensions {
29 // Controller class for web based auth flows. The WebAuthFlow creates
30 // a dialog window in the scope approval component app by firing an
31 // event. A webview embedded in the dialog will navigate to the
32 // |provider_url| passed to the WebAuthFlow constructor.
34 // The WebAuthFlow monitors the WebContents of the webview, and
35 // notifies its delegate interface any time the WebContents navigates
36 // to a new URL or changes title. The delegate is expected to delete
37 // the flow when navigation reaches a known target location.
39 // The window is not displayed until the first page load
40 // completes. This allows the flow to complete without flashing a
41 // window on screen if the provider immediately redirects to the
44 // A WebAuthFlow can be started in Mode::SILENT, which never displays
45 // a window. If a window would be required, the flow fails.
46 class WebAuthFlow : public content::NotificationObserver,
47 public content::WebContentsObserver,
48 public apps::ShellWindowRegistry::Observer {
51 INTERACTIVE, // Show UI to the user if necessary.
52 SILENT // No UI should be shown.
56 WINDOW_CLOSED, // Window closed by user.
57 INTERACTION_REQUIRED, // Non-redirect page load in silent mode.
63 // Called when the auth flow fails. This means that the flow did not result
64 // in a successful redirect to a valid redirect URL.
65 virtual void OnAuthFlowFailure(Failure failure) = 0;
66 // Called on redirects and other navigations to see if the URL should stop
68 virtual void OnAuthFlowURLChange(const GURL& redirect_url) = 0;
69 // Called when the title of the current page changes.
70 virtual void OnAuthFlowTitleChange(const std::string& title) = 0;
73 virtual ~Delegate() {}
76 // Creates an instance with the given parameters.
77 // Caller owns |delegate|.
78 WebAuthFlow(Delegate* delegate,
80 const GURL& provider_url,
83 virtual ~WebAuthFlow();
88 // Prevents further calls to the delegate and deletes the flow.
89 void DetachDelegateAndDelete();
92 friend class ::WebAuthFlowTest;
94 // ::ShellWindowRegistry::Observer implementation.
95 virtual void OnShellWindowAdded(apps::ShellWindow* shell_window) OVERRIDE;
96 virtual void OnShellWindowIconChanged(apps::ShellWindow* shell_window)
98 virtual void OnShellWindowRemoved(apps::ShellWindow* shell_window) OVERRIDE;
100 // NotificationObserver implementation.
101 virtual void Observe(int type,
102 const content::NotificationSource& source,
103 const content::NotificationDetails& details) OVERRIDE;
105 // WebContentsObserver implementation.
106 virtual void DidStopLoading(content::RenderViewHost* render_view_host)
108 virtual void DidNavigateMainFrame(
109 const content::LoadCommittedDetails& details,
110 const content::FrameNavigateParams& params) OVERRIDE;
111 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
112 virtual void DidStartProvisionalLoadForFrame(
114 int64 parent_frame_id,
116 const GURL& validated_url,
118 bool is_iframe_srcdoc,
119 content::RenderViewHost* render_view_host) OVERRIDE;
120 virtual void DidFailProvisionalLoad(int64 frame_id,
121 const string16& frame_unique_name,
123 const GURL& validated_url,
125 const string16& error_description,
126 content::RenderViewHost* render_view_host)
129 void BeforeUrlLoaded(const GURL& url);
130 void AfterUrlLoaded();
137 apps::ShellWindow* shell_window_;
138 std::string shell_window_key_;
139 bool embedded_window_created_;
141 content::NotificationRegistrar registrar_;
143 DISALLOW_COPY_AND_ASSIGN(WebAuthFlow);
146 } // namespace extensions
148 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_