Commit ea8ca3f5 authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

🤝 WIP Allow OriginVerifier to handle multiple requests.

Change-Id: Iaf465c19c4478af20538722def8133c63af13239
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2302759Reviewed-by: default avatarRayan Kanso <rayankans@chromium.org>
Commit-Queue: Peter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789925}
parent 4193c00f
......@@ -44,6 +44,7 @@ public class TwaVerifier implements Verifier, Destroyable {
*/
@Nullable
private Set<Origin> mPendingOrigins;
private boolean mDestroyed;
/**
* All the origins that have been successfully verified.
......@@ -69,8 +70,7 @@ public class TwaVerifier implements Verifier, Destroyable {
@Override
public void destroy() {
// Verification may finish after activity is destroyed.
mOriginVerifier.removeListener();
mDestroyed = true;
}
@Override
......@@ -81,6 +81,8 @@ public class TwaVerifier implements Verifier, Destroyable {
Promise<Boolean> promise = new Promise<>();
if (getPendingOrigins().contains(origin)) {
mOriginVerifier.start((packageName, unused, verified, online) -> {
if (mDestroyed) return;
getPendingOrigins().remove(origin);
if (verified) mVerifiedOrigins.add(origin);
......
......@@ -36,11 +36,10 @@ OriginVerifier::OriginVerifier(JNIEnv* env,
jobject_.Reset(obj);
Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
DCHECK(profile);
asset_link_handler_ =
std::make_unique<digital_asset_links::DigitalAssetLinksHandler>(
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetURLLoaderFactoryForBrowserProcess(),
content::WebContents::FromJavaWebContents(jweb_contents));
url_loader_factory_ =
content::BrowserContext::GetDefaultStoragePartition(profile)
->GetURLLoaderFactoryForBrowserProcess();
web_contents_ = content::WebContents::FromJavaWebContents(jweb_contents);
}
OriginVerifier::~OriginVerifier() = default;
......@@ -59,24 +58,30 @@ bool OriginVerifier::VerifyOrigin(JNIEnv* env,
std::string origin = ConvertJavaStringToUTF8(env, j_origin);
std::string relationship = ConvertJavaStringToUTF8(env, j_relationship);
// Multiple calls here will end up resetting the callback on the handler side
// and cancelling previous requests.
// If during the request this verifier gets killed, the handler and the
// UrlFetcher making the request will also get killed, so we won't get any
// dangling callback reference issues.
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
return asset_link_handler_->CheckDigitalAssetLinkRelationshipForAndroidApp(
auto asset_link_handler =
std::make_unique<digital_asset_links::DigitalAssetLinksHandler>(
url_loader_factory_, web_contents_);
auto* asset_link_handler_ptr = asset_link_handler.get();
return asset_link_handler_ptr->CheckDigitalAssetLinkRelationshipForAndroidApp(
origin, relationship, fingerprint, package_name,
base::BindOnce(&customtabs::OriginVerifier::OnRelationshipCheckComplete,
base::Unretained(this)));
base::Unretained(this), std::move(asset_link_handler),
origin));
}
void OriginVerifier::OnRelationshipCheckComplete(
std::unique_ptr<digital_asset_links::DigitalAssetLinksHandler> handler,
const std::string& origin,
RelationshipCheckResult result) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_OriginVerifier_onOriginVerificationResult(env,
jobject_,
auto j_origin = base::android::ConvertUTF8ToJavaString(env, origin);
Java_OriginVerifier_onOriginVerificationResult(env, jobject_, j_origin,
static_cast<jint>(result));
}
......
......@@ -5,10 +5,18 @@
#ifndef CHROME_BROWSER_ANDROID_CUSTOMTABS_ORIGIN_VERIFIER_H_
#define CHROME_BROWSER_ANDROID_CUSTOMTABS_ORIGIN_VERIFIER_H_
#include <memory>
#include "base/android/scoped_java_ref.h"
#include "base/memory/scoped_refptr.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace content {
class WebContents;
} // namespace content
namespace digital_asset_links {
enum class RelationshipCheckResult;
......@@ -41,10 +49,12 @@ class OriginVerifier {
static int GetClearBrowsingDataCallCountForTesting();
private:
void OnRelationshipCheckComplete(
std::unique_ptr<digital_asset_links::DigitalAssetLinksHandler> handler,
const std::string& origin,
digital_asset_links::RelationshipCheckResult result);
std::unique_ptr<digital_asset_links::DigitalAssetLinksHandler>
asset_link_handler_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
content::WebContents* web_contents_;
base::android::ScopedJavaGlobalRef<jobject> jobject_;
......
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