Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / identity / web_auth_flow.h
blobdc688c02eb0a983dd3f4c2bdadfdd8984fd65b8d
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_
8 #include <string>
10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "extensions/browser/app_window/app_window_registry.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "url/gurl.h"
17 class Profile;
18 class WebAuthFlowTest;
20 namespace content {
21 class NotificationDetails;
22 class NotificationSource;
23 class RenderViewHost;
24 class WebContents;
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
42 // target URL.
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 AppWindowRegistry::Observer {
49 public:
50 enum Mode {
51 INTERACTIVE, // Show UI to the user if necessary.
52 SILENT // No UI should be shown.
55 enum Failure {
56 WINDOW_CLOSED, // Window closed by user.
57 INTERACTION_REQUIRED, // Non-redirect page load in silent mode.
58 LOAD_FAILED
61 class Delegate {
62 public:
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
67 // the flow.
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;
72 protected:
73 virtual ~Delegate() {}
76 // Creates an instance with the given parameters.
77 // Caller owns |delegate|.
78 WebAuthFlow(Delegate* delegate,
79 Profile* profile,
80 const GURL& provider_url,
81 Mode mode);
83 ~WebAuthFlow() override;
85 // Starts the flow.
86 virtual void Start();
88 // Prevents further calls to the delegate and deletes the flow.
89 void DetachDelegateAndDelete();
91 private:
92 friend class ::WebAuthFlowTest;
94 // ::AppWindowRegistry::Observer implementation.
95 void OnAppWindowAdded(AppWindow* app_window) override;
96 void OnAppWindowRemoved(AppWindow* app_window) override;
98 // NotificationObserver implementation.
99 void Observe(int type,
100 const content::NotificationSource& source,
101 const content::NotificationDetails& details) override;
103 // WebContentsObserver implementation.
104 void DidStopLoading() override;
105 void DidNavigateMainFrame(
106 const content::LoadCommittedDetails& details,
107 const content::FrameNavigateParams& params) override;
108 void RenderProcessGone(base::TerminationStatus status) override;
109 void DidStartProvisionalLoadForFrame(
110 content::RenderFrameHost* render_frame_host,
111 const GURL& validated_url,
112 bool is_error_page,
113 bool is_iframe_srcdoc) override;
114 void DidFailProvisionalLoad(content::RenderFrameHost* render_frame_host,
115 const GURL& validated_url,
116 int error_code,
117 const base::string16& error_description,
118 bool was_ignored_by_handler) override;
119 void DidGetRedirectForResourceRequest(
120 content::RenderFrameHost* render_frame_host,
121 const content::ResourceRedirectDetails& details) override;
122 void TitleWasSet(content::NavigationEntry* entry, bool explicit_set) override;
124 void BeforeUrlLoaded(const GURL& url);
125 void AfterUrlLoaded();
127 Delegate* delegate_;
128 Profile* profile_;
129 GURL provider_url_;
130 Mode mode_;
132 AppWindow* app_window_;
133 std::string app_window_key_;
134 bool embedded_window_created_;
136 content::NotificationRegistrar registrar_;
138 DISALLOW_COPY_AND_ASSIGN(WebAuthFlow);
141 } // namespace extensions
143 #endif // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_