1 // Copyright 2014 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 EXTENSIONS_SHELL_BROWSER_API_IDENTITY_IDENTITY_API_H_
6 #define EXTENSIONS_SHELL_BROWSER_API_IDENTITY_IDENTITY_API_H_
10 #include "base/macros.h"
11 #include "extensions/browser/browser_context_keyed_api_factory.h"
12 #include "extensions/browser/extension_function.h"
13 #include "google_apis/gaia/oauth2_mint_token_flow.h"
14 #include "google_apis/gaia/oauth2_token_service.h"
16 namespace extensions
{
19 // Storage for data used across identity function invocations.
20 class IdentityAPI
: public BrowserContextKeyedAPI
{
22 explicit IdentityAPI(content::BrowserContext
* context
);
23 ~IdentityAPI() override
;
25 static IdentityAPI
* Get(content::BrowserContext
* context
);
27 const std::string
& device_id() const { return device_id_
; }
29 // BrowserContextKeyedAPI:
30 static BrowserContextKeyedAPIFactory
<IdentityAPI
>* GetFactoryInstance();
31 static const char* service_name() { return "IdentityAPI"; }
34 friend class BrowserContextKeyedAPIFactory
<IdentityAPI
>;
36 // A GUID identifying this device.
37 // TODO(jamescook): Make this GUID stable across runs of the app, perhaps by
38 // storing it in a pref.
39 const std::string device_id_
;
42 // Returns an OAuth2 access token for a user. See the IDL file for
44 class IdentityGetAuthTokenFunction
: public UIThreadExtensionFunction
,
45 public OAuth2TokenService::Consumer
,
46 public OAuth2MintTokenFlow::Delegate
{
48 DECLARE_EXTENSION_FUNCTION("identity.getAuthToken", UNKNOWN
);
50 IdentityGetAuthTokenFunction();
53 void SetMintTokenFlowForTesting(OAuth2MintTokenFlow
* flow
);
56 ~IdentityGetAuthTokenFunction() override
;
59 ResponseAction
Run() override
;
61 // OAuth2TokenService::Consumer:
62 void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
63 const std::string
& access_token
,
64 const base::Time
& expiration_time
) override
;
65 void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
66 const GoogleServiceAuthError
& error
) override
;
68 // OAuth2MintTokenFlow::Delegate:
69 void OnMintTokenSuccess(const std::string
& access_token
,
70 int time_to_live
) override
;
71 void OnIssueAdviceSuccess(const IssueAdviceInfo
& issue_advice
) override
;
72 void OnMintTokenFailure(const GoogleServiceAuthError
& error
) override
;
75 // A pending token fetch request to get a login-scoped access token for the
76 // current user for the Chrome project id.
77 scoped_ptr
<OAuth2TokenService::Request
> access_token_request_
;
79 // A request for an access token for the current app and its scopes.
80 scoped_ptr
<OAuth2MintTokenFlow
> mint_token_flow_
;
82 DISALLOW_COPY_AND_ASSIGN(IdentityGetAuthTokenFunction
);
85 // Stub. See the IDL file for documentation.
86 class IdentityRemoveCachedAuthTokenFunction
: public UIThreadExtensionFunction
{
88 DECLARE_EXTENSION_FUNCTION("identity.removeCachedAuthToken", UNKNOWN
)
90 IdentityRemoveCachedAuthTokenFunction();
93 ~IdentityRemoveCachedAuthTokenFunction() override
;
96 ResponseAction
Run() override
;
99 DISALLOW_COPY_AND_ASSIGN(IdentityRemoveCachedAuthTokenFunction
);
103 } // namespace extensions
105 #endif // EXTENSIONS_SHELL_BROWSER_API_IDENTITY_IDENTITY_API_H_