identity_api.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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_IDENTITY_API_H_
6#define CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/weak_ptr.h"
12#include "chrome/browser/extensions/api/identity/web_auth_flow.h"
13#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
14#include "chrome/browser/extensions/extension_function.h"
15#include "chrome/browser/extensions/extension_install_prompt.h"
16#include "chrome/browser/ui/webui/signin/login_ui_service.h"
17#include "google_apis/gaia/oauth2_mint_token_flow.h"
18
19class GetAuthTokenFunctionTest;
20class MockGetAuthTokenFunction;
21class GoogleServiceAuthError;
22class Profile;
23
24namespace extensions {
25
26namespace identity_constants {
27extern const char kInvalidClientId[];
28extern const char kInvalidScopes[];
29extern const char kAuthFailure[];
30extern const char kNoGrant[];
31extern const char kUserRejected[];
32extern const char kUserNotSignedIn[];
33extern const char kInvalidRedirect[];
34}  // namespace identity_constants
35
36class IdentityGetAuthTokenFunction : public AsyncExtensionFunction,
37                                     public OAuth2MintTokenFlow::Delegate,
38                                     public ExtensionInstallPrompt::Delegate,
39                                     public LoginUIService::Observer {
40 public:
41  DECLARE_EXTENSION_FUNCTION("experimental.identity.getAuthToken",
42                             EXPERIMENTAL_IDENTITY_GETAUTHTOKEN)
43
44  IdentityGetAuthTokenFunction();
45
46 protected:
47  virtual ~IdentityGetAuthTokenFunction();
48
49 private:
50  friend class GetAuthTokenFunctionTest;
51  friend class MockGetAuthTokenFunction;
52
53  // ExtensionFunction:
54  virtual bool RunImpl() OVERRIDE;
55
56  // OAuth2MintTokenFlow::Delegate implementation:
57  virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE;
58  virtual void OnMintTokenFailure(
59      const GoogleServiceAuthError& error) OVERRIDE;
60  virtual void OnIssueAdviceSuccess(
61      const IssueAdviceInfo& issue_advice) OVERRIDE;
62
63  // LoginUIService::Observer implementation.
64  virtual void OnLoginUIShown(LoginUIService::LoginUI* ui) OVERRIDE {
65    // Do nothing when login ui is shown.
66  }
67  virtual void OnLoginUIClosed(LoginUIService::LoginUI* ui) OVERRIDE;
68
69  // ExtensionInstallPrompt::Delegate implementation:
70  virtual void InstallUIProceed() OVERRIDE;
71  virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
72
73  // Shows the login UI in a browser popup.
74  bool StartLogin();
75  // Starts a MintTokenFlow with the given mode; Returns success.
76  bool StartFlow(OAuth2MintTokenFlow::Mode mode);
77
78  virtual void StartObservingLoginService();
79  virtual void StopObservingLoginService();
80  virtual void ShowLoginPopup();
81  virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice);
82  // Caller owns the returned instance.
83  virtual OAuth2MintTokenFlow* CreateMintTokenFlow(
84      OAuth2MintTokenFlow::Mode mode);
85
86  // Checks if there is a master login token to mint tokens for the extension.
87  virtual bool HasLoginToken() const;
88
89  bool interactive_;
90  scoped_ptr<OAuth2MintTokenFlow> flow_;
91
92  // When launched in interactive mode, and if there is no existing grant,
93  // a permissions prompt will be popped up to the user.
94  scoped_ptr<ExtensionInstallPrompt> install_ui_;
95};
96
97class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction,
98                                          public WebAuthFlow::Delegate {
99 public:
100  DECLARE_EXTENSION_FUNCTION("experimental.identity.launchWebAuthFlow",
101                             EXPERIMENTAL_IDENTITY_LAUNCHWEBAUTHFLOW)
102
103  IdentityLaunchWebAuthFlowFunction();
104
105 private:
106  virtual ~IdentityLaunchWebAuthFlowFunction();
107  virtual bool RunImpl() OVERRIDE;
108
109  // WebAuthFlow::Delegate implementation.
110  virtual void OnAuthFlowSuccess(const std::string& redirect_url) OVERRIDE;
111  virtual void OnAuthFlowFailure() OVERRIDE;
112
113  scoped_ptr<WebAuthFlow> auth_flow_;
114};
115
116class IdentityAPI : public ProfileKeyedAPI {
117 public:
118  explicit IdentityAPI(Profile* profile);
119  virtual ~IdentityAPI();
120
121  // ProfileKeyedAPI implementation.
122  static ProfileKeyedAPIFactory<IdentityAPI>* GetFactoryInstance();
123
124 private:
125  friend class ProfileKeyedAPIFactory<IdentityAPI>;
126
127  // ProfileKeyedAPI implementation.
128  static const char* service_name() {
129    return "IdentityAPI";
130  }
131  static const bool kServiceIsNULLWhileTesting = true;
132};
133
134}  // namespace extensions
135
136#endif  // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
137