summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/identity/web_auth_flow.h
blob: 399b6bcb62046a46480c44b01d81e760be196b21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_
#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_

#include <string>
#include <vector>

#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "chrome/browser/ui/host_desktop.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_contents_observer.h"
#include "googleurl/src/gurl.h"
#include "ui/gfx/rect.h"

class Profile;
class WebAuthFlowTest;

namespace content {
class NotificationDetails;
class NotificationSource;
class RenderViewHost;
class WebContents;
}

namespace extensions {

// Controller class to perform an auth flow with a provider.
// This is the class to start the auth flow and it takes care of all the
// details. It behaves the following way:
// Given a provider URL, load the URL and perform usual web navigation
// until it results in redirection to a valid extension redirect URL.
// The provider can show any UI to the user if needed before redirecting
// to an appropriate URL.
// TODO(munjal): Add link to the design doc here.
class WebAuthFlow : public content::NotificationObserver,
                    public content::WebContentsObserver {
 public:
  enum Mode {
    INTERACTIVE,  // Show UI to the user if necessary.
    SILENT        // No UI should be shown.
  };

  class Delegate {
   public:
    // Called when the auth flow is completed successfully.
    // |redirect_url| is the full URL the provider redirected to at the end
    // of the flow.
    virtual void OnAuthFlowSuccess(const std::string& redirect_url) = 0;
    // Called when the auth flow fails. This means that the flow did not result
    // in a successful redirect to a valid redirect URL or the user canceled
    // the flow.
    virtual void OnAuthFlowFailure() = 0;

   protected:
    virtual ~Delegate() {}
  };

  // Creates an instance with the given parameters.
  // Caller owns |delegate|.
  WebAuthFlow(Delegate* delegate,
              Profile* profile,
              const std::string& extension_id,
              const GURL& provider_url,
              Mode mode,
              const gfx::Rect& initial_bounds,
              chrome::HostDesktopType host_desktop_type);
  virtual ~WebAuthFlow();

  // Starts the flow.
  // Delegate will be called when the flow is done.
  virtual void Start();

 protected:
  // Overridable for testing.
  virtual content::WebContents* CreateWebContents();
  virtual void ShowAuthFlowPopup();

 private:
  friend class ::WebAuthFlowTest;

  // NotificationObserver implementation.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // WebContentsObserver implementation.
  virtual void ProvisionalChangeToMainFrameUrl(
      const GURL& url,
      content::RenderViewHost* render_view_host) OVERRIDE;
  virtual void DidStopLoading(
      content::RenderViewHost* render_view_host) OVERRIDE;
  virtual void WebContentsDestroyed(
      content::WebContents* web_contents) OVERRIDE;

  bool BeforeUrlLoaded(const GURL& url);
  void AfterUrlLoaded();

  // Reports the results back to the delegate.
  void ReportResult(const GURL& url);
  // Checks if |url| is a valid redirect URL for the extension.
  bool IsValidRedirectUrl(const GURL& url) const;
  // Helper to initialize valid extensions URLs vector.
  void InitValidRedirectUrlPrefixes(const std::string& extension_id);

  Delegate* delegate_;
  Profile* profile_;
  GURL provider_url_;
  Mode mode_;
  gfx::Rect initial_bounds_;
  chrome::HostDesktopType host_desktop_type_;
  bool popup_shown_;
  // List of valid redirect URL prefixes.
  std::vector<std::string> valid_prefixes_;

  content::WebContents* contents_;
  content::NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(WebAuthFlow);
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_WEB_AUTH_FLOW_H_