Commit 5b0a7bb9 authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

[Extensions Functions] Migrate identity API to ExtensionFunction

Bug: 634140
Change-Id: I959c0e21765c968910f3d64faa4102fc86d032ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2066491Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarAlex Ilin <alexilin@chromium.org>
Commit-Queue: Anand Mistry <amistry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745252}
parent a6903f53
......@@ -28,7 +28,6 @@
#include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
#include "chrome/browser/extensions/api/identity/identity_remove_cached_auth_token_function.h"
#include "chrome/browser/extensions/api/identity/web_auth_flow.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "components/signin/public/base/signin_buildflags.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
......
......@@ -89,13 +89,12 @@ IdentityGetAuthTokenFunction::~IdentityGetAuthTokenFunction() {
this);
}
bool IdentityGetAuthTokenFunction::RunAsync() {
ExtensionFunction::ResponseAction IdentityGetAuthTokenFunction::Run() {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1("identity", "IdentityGetAuthTokenFunction",
this, "extension", extension()->id());
if (GetProfile()->IsOffTheRecord()) {
error_ = identity_constants::kOffTheRecord;
return false;
return RespondNow(Error(identity_constants::kOffTheRecord));
}
std::unique_ptr<api::identity::GetAuthToken::Params> params(
......@@ -114,8 +113,7 @@ bool IdentityGetAuthTokenFunction::RunAsync() {
// Check that the necessary information is present in the manifest.
oauth2_client_id_ = GetOAuth2ClientId();
if (oauth2_client_id_.empty()) {
error_ = identity_constants::kInvalidClientId;
return false;
return RespondNow(Error(identity_constants::kInvalidClientId));
}
std::set<std::string> scopes(oauth2_info.scopes.begin(),
......@@ -133,8 +131,7 @@ bool IdentityGetAuthTokenFunction::RunAsync() {
}
if (scopes.empty()) {
error_ = identity_constants::kInvalidScopes;
return false;
return RespondNow(Error(identity_constants::kInvalidScopes));
}
token_key_.scopes = scopes;
......@@ -159,7 +156,7 @@ bool IdentityGetAuthTokenFunction::RunAsync() {
weak_ptr_factory_.GetWeakPtr(), gaia_id));
}
return true;
return RespondLater();
}
void IdentityGetAuthTokenFunction::GetAuthTokenForPrimaryAccount(
......@@ -282,25 +279,23 @@ void IdentityGetAuthTokenFunction::StartAsyncRun() {
&IdentityGetAuthTokenFunction::OnIdentityAPIShutdown, this));
}
void IdentityGetAuthTokenFunction::CompleteAsyncRun(bool success) {
void IdentityGetAuthTokenFunction::CompleteAsyncRun(ResponseValue response) {
identity_api_shutdown_subscription_.reset();
SendResponse(success);
Respond(std::move(response));
Release(); // Balanced in StartAsyncRun
}
void IdentityGetAuthTokenFunction::CompleteFunctionWithResult(
const std::string& access_token) {
SetResult(std::make_unique<base::Value>(access_token));
CompleteAsyncRun(true);
CompleteAsyncRun(OneArgument(std::make_unique<base::Value>(access_token)));
}
void IdentityGetAuthTokenFunction::CompleteFunctionWithError(
const std::string& error) {
TRACE_EVENT_NESTABLE_ASYNC_INSTANT1("identity", "CompleteFunctionWithError",
this, "error", error);
error_ = error;
CompleteAsyncRun(false);
CompleteAsyncRun(Error(error));
}
bool IdentityGetAuthTokenFunction::ShouldStartSigninFlow() {
......@@ -940,4 +935,8 @@ bool IdentityGetAuthTokenFunction::IsPrimaryAccountOnly() const {
->AreExtensionsRestrictedToPrimaryAccount();
}
Profile* IdentityGetAuthTokenFunction::GetProfile() const {
return Profile::FromBrowserContext(browser_context());
}
} // namespace extensions
......@@ -15,8 +15,8 @@
#include "chrome/browser/extensions/api/identity/gaia_remote_consent_flow.h"
#include "chrome/browser/extensions/api/identity/gaia_web_auth_flow.h"
#include "chrome/browser/extensions/api/identity/identity_mint_queue.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_function_histogram_value.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_manager.h"
......@@ -46,7 +46,7 @@ namespace extensions {
// profile will be signed in already, but if it turns out we need a
// new login token, there is a sign-in flow. If that flow completes
// successfully, getAuthToken proceeds to the non-interactive flow.
class IdentityGetAuthTokenFunction : public ChromeAsyncExtensionFunction,
class IdentityGetAuthTokenFunction : public ExtensionFunction,
public GaiaWebAuthFlow::Delegate,
public GaiaRemoteConsentFlow::Delegate,
public IdentityMintRequestQueue::Request,
......@@ -114,6 +114,8 @@ class IdentityGetAuthTokenFunction : public ChromeAsyncExtensionFunction,
// Exposed for testing.
virtual std::unique_ptr<OAuth2MintTokenFlow> CreateMintTokenFlow();
Profile* GetProfile() const;
// Pending request for an access token from the device account (via
// DeviceOAuth2TokenService).
std::unique_ptr<OAuth2AccessTokenManager::Request>
......@@ -154,11 +156,11 @@ class IdentityGetAuthTokenFunction : public ChromeAsyncExtensionFunction,
const CoreAccountInfo& primary_account_info) override;
// ExtensionFunction:
bool RunAsync() override;
ResponseAction Run() override;
// Helpers to report async function results to the caller.
void StartAsyncRun();
void CompleteAsyncRun(bool success);
void CompleteAsyncRun(ResponseValue response);
void CompleteFunctionWithResult(const std::string& access_token);
void CompleteFunctionWithError(const std::string& error);
......
......@@ -27,10 +27,10 @@ IdentityLaunchWebAuthFlowFunction::~IdentityLaunchWebAuthFlowFunction() {
auth_flow_.release()->DetachDelegateAndDelete();
}
bool IdentityLaunchWebAuthFlowFunction::RunAsync() {
if (GetProfile()->IsOffTheRecord()) {
error_ = identity_constants::kOffTheRecord;
return false;
ExtensionFunction::ResponseAction IdentityLaunchWebAuthFlowFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
if (profile->IsOffTheRecord()) {
return RespondNow(Error(identity_constants::kOffTheRecord));
}
std::unique_ptr<api::identity::LaunchWebAuthFlow::Params> params(
......@@ -48,9 +48,9 @@ bool IdentityLaunchWebAuthFlowFunction::RunAsync() {
AddRef(); // Balanced in OnAuthFlowSuccess/Failure.
auth_flow_.reset(new WebAuthFlow(this, GetProfile(), auth_url, mode));
auth_flow_.reset(new WebAuthFlow(this, profile, auth_url, mode));
auth_flow_->Start();
return true;
return RespondLater();
}
void IdentityLaunchWebAuthFlowFunction::InitFinalRedirectURLPrefixForTest(
......@@ -68,32 +68,32 @@ void IdentityLaunchWebAuthFlowFunction::InitFinalRedirectURLPrefix(
void IdentityLaunchWebAuthFlowFunction::OnAuthFlowFailure(
WebAuthFlow::Failure failure) {
std::string error;
switch (failure) {
case WebAuthFlow::WINDOW_CLOSED:
error_ = identity_constants::kUserRejected;
error = identity_constants::kUserRejected;
break;
case WebAuthFlow::INTERACTION_REQUIRED:
error_ = identity_constants::kInteractionRequired;
error = identity_constants::kInteractionRequired;
break;
case WebAuthFlow::LOAD_FAILED:
error_ = identity_constants::kPageLoadFailure;
error = identity_constants::kPageLoadFailure;
break;
default:
NOTREACHED() << "Unexpected error from web auth flow: " << failure;
error_ = identity_constants::kInvalidRedirect;
error = identity_constants::kInvalidRedirect;
break;
}
SendResponse(false);
Respond(Error(error));
if (auth_flow_)
auth_flow_.release()->DetachDelegateAndDelete();
Release(); // Balanced in RunAsync.
Release(); // Balanced in Run.
}
void IdentityLaunchWebAuthFlowFunction::OnAuthFlowURLChange(
const GURL& redirect_url) {
if (redirect_url.GetWithEmptyPath() == final_url_prefix_) {
SetResult(std::make_unique<base::Value>(redirect_url.spec()));
SendResponse(true);
Respond(OneArgument(std::make_unique<base::Value>(redirect_url.spec())));
if (auth_flow_)
auth_flow_.release()->DetachDelegateAndDelete();
Release(); // Balanced in RunAsync.
......
......@@ -8,12 +8,12 @@
#include <string>
#include "chrome/browser/extensions/api/identity/web_auth_flow.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_function_histogram_value.h"
namespace extensions {
class IdentityLaunchWebAuthFlowFunction : public ChromeAsyncExtensionFunction,
class IdentityLaunchWebAuthFlowFunction : public ExtensionFunction,
public WebAuthFlow::Delegate {
public:
DECLARE_EXTENSION_FUNCTION("identity.launchWebAuthFlow",
......@@ -26,7 +26,7 @@ class IdentityLaunchWebAuthFlowFunction : public ChromeAsyncExtensionFunction,
private:
~IdentityLaunchWebAuthFlowFunction() override;
bool RunAsync() override;
ResponseAction Run() override;
// WebAuthFlow::Delegate implementation.
void OnAuthFlowFailure(WebAuthFlow::Failure failure) override;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment