identity_api.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/app_notify_channel_setup.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;
22
23namespace extensions {
24
25namespace identity_constants {
26extern const char kInvalidClientId[];
27extern const char kInvalidScopes[];
28extern const char kAuthFailure[];
29extern const char kNoGrant[];
30extern const char kUserRejected[];
31extern const char kUserNotSignedIn[];
32extern const char kInvalidRedirect[];
33}
34
35class IdentityGetAuthTokenFunction : public AsyncExtensionFunction,
36                                     public OAuth2MintTokenFlow::Delegate,
37                                     public ExtensionInstallPrompt::Delegate,
38                                     public LoginUIService::Observer {
39 public:
40  DECLARE_EXTENSION_FUNCTION_NAME("experimental.identity.getAuthToken");
41
42  IdentityGetAuthTokenFunction();
43
44 protected:
45  virtual ~IdentityGetAuthTokenFunction();
46
47 private:
48  friend class ::GetAuthTokenFunctionTest;
49  friend class ::MockGetAuthTokenFunction;
50
51  // ExtensionFunction:
52  virtual bool RunImpl() OVERRIDE;
53
54  // OAuth2MintTokenFlow::Delegate implementation:
55  virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE;
56  virtual void OnMintTokenFailure(
57      const GoogleServiceAuthError& error) OVERRIDE;
58  virtual void OnIssueAdviceSuccess(
59      const IssueAdviceInfo& issue_advice) OVERRIDE;
60
61  // LoginUIService::Observer implementation.
62  virtual void OnLoginUIShown(LoginUIService::LoginUI* ui) OVERRIDE {
63    // Do nothing when login ui is shown.
64  }
65  virtual void OnLoginUIClosed(LoginUIService::LoginUI* ui) OVERRIDE;
66
67  // ExtensionInstallPrompt::Delegate implementation:
68  virtual void InstallUIProceed() OVERRIDE;
69  virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
70
71  // Shows the login UI in a browser popup.
72  bool StartLogin();
73  // Starts a MintTokenFlow with the given mode; Returns success.
74  bool StartFlow(OAuth2MintTokenFlow::Mode mode);
75
76  virtual void StartObservingLoginService();
77  virtual void StopObservingLoginService();
78  virtual void ShowLoginPopup();
79  virtual void ShowOAuthApprovalDialog(const IssueAdviceInfo& issue_advice);
80  // Caller owns the returned instance.
81  virtual OAuth2MintTokenFlow* CreateMintTokenFlow(
82      OAuth2MintTokenFlow::Mode mode);
83
84  // Checks if there is a master login token to mint tokens for the extension.
85  virtual bool HasLoginToken() const;
86
87  bool interactive_;
88  scoped_ptr<OAuth2MintTokenFlow> flow_;
89
90  // When launched in interactive mode, and if there is no existing grant,
91  // a permissions prompt will be popped up to the user.
92  scoped_ptr<ExtensionInstallPrompt> install_ui_;
93};
94
95class IdentityLaunchWebAuthFlowFunction : public AsyncExtensionFunction,
96                                          public WebAuthFlow::Delegate {
97 public:
98  DECLARE_EXTENSION_FUNCTION_NAME("experimental.identity.launchWebAuthFlow");
99
100  IdentityLaunchWebAuthFlowFunction();
101
102 private:
103  virtual ~IdentityLaunchWebAuthFlowFunction();
104  virtual bool RunImpl() OVERRIDE;
105
106  // WebAuthFlow::Delegate implementation.
107  virtual void OnAuthFlowSuccess(const std::string& redirect_url) OVERRIDE;
108  virtual void OnAuthFlowFailure() OVERRIDE;
109
110  scoped_ptr<WebAuthFlow> auth_flow_;
111};
112
113}  // namespace extensions
114
115#endif  // CHROME_BROWSER_EXTENSIONS_API_IDENTITY_IDENTITY_API_H_
116