forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebLayer] Add support for Gaia-keyed safe browsing realtime URL lookups
This CL adds support for Gaia-keyed safe browsing realtime URL lookups to WebLayer via a //weblayer-specific implementation of SafeBrowsingTokenFetcher that obtains access tokens from the WebLayer embedder. That implementation follows the design of the SafeBrowsingTokenFetcher implementation that Chrome uses, relying on the shared SafeBrowsingTokenFetchTracker helper class to time out access token requests after a given delay. This CL supplies a SafeBrowsingTokenFetcherImpl instance to RealTimeUrlLookupService but does not actually enable access token fetches in the context of WebLayer, as the specific flow for enabling this functionality is still under consideration. This CL also adds tests of the new functionality: - unittests of SafeBrowsingTokenFetcherImpl - browsertests that verify that safe browsing access token fetches occur/don't occur as part of navigations as expected in various configurations of safe browsing. Bug: 1080748 Change-Id: If39c59f44dc469fdd6a22ae4dfa5c2d48e03e158 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2652466 Commit-Queue: Colin Blundell <blundell@chromium.org> Reviewed-by: Clark DuVall <cduvall@chromium.org> Reviewed-by: Xinghui Lu <xinghuilu@chromium.org> Cr-Commit-Position: refs/heads/master@{#848040}
- Loading branch information
1 parent
6a5d3a3
commit 5982efd
Showing
8 changed files
with
590 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
weblayer/browser/safe_browsing/safe_browsing_token_fetcher_impl.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "weblayer/browser/safe_browsing/safe_browsing_token_fetcher_impl.h" | ||
|
||
#include "base/bind.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "weblayer/public/google_account_access_token_fetch_delegate.h" | ||
|
||
namespace weblayer { | ||
|
||
SafeBrowsingTokenFetcherImpl::SafeBrowsingTokenFetcherImpl( | ||
const AccessTokenFetchDelegateGetter& delegate_getter) | ||
: delegate_getter_(delegate_getter) {} | ||
|
||
SafeBrowsingTokenFetcherImpl::~SafeBrowsingTokenFetcherImpl() = default; | ||
|
||
void SafeBrowsingTokenFetcherImpl::Start(Callback callback) { | ||
auto* delegate = delegate_getter_.Run(); | ||
|
||
if (!delegate) { | ||
std::move(callback).Run(""); | ||
return; | ||
} | ||
|
||
// NOTE: base::Unretained() is safe below as this object owns | ||
// |token_fetch_tracker_|, and the callback will not be invoked after | ||
// |token_fetch_tracker_| is destroyed. | ||
const int request_id = token_fetch_tracker_.StartTrackingTokenFetch( | ||
std::move(callback), | ||
base::BindOnce(&SafeBrowsingTokenFetcherImpl::OnTokenTimeout, | ||
base::Unretained(this))); | ||
request_ids_.insert(request_id); | ||
|
||
// In contrast, this object does *not* have a determined lifetime relationship | ||
// with |delegate|. | ||
delegate->FetchAccessToken( | ||
{safe_browsing::kAPIScope}, | ||
base::BindOnce(&SafeBrowsingTokenFetcherImpl::OnTokenFetched, | ||
weak_ptr_factory_.GetWeakPtr(), request_id)); | ||
} | ||
|
||
void SafeBrowsingTokenFetcherImpl::OnTokenFetched( | ||
int request_id, | ||
const std::string& access_token) { | ||
if (!request_ids_.count(request_id)) { | ||
// The request timed out before the delegate responded; nothing to do. | ||
return; | ||
} | ||
|
||
request_ids_.erase(request_id); | ||
|
||
token_fetch_tracker_.OnTokenFetchComplete(request_id, access_token); | ||
} | ||
|
||
void SafeBrowsingTokenFetcherImpl::OnTokenTimeout(int request_id) { | ||
request_ids_.erase(request_id); | ||
} | ||
|
||
} // namespace weblayer |
60 changes: 60 additions & 0 deletions
60
weblayer/browser/safe_browsing/safe_browsing_token_fetcher_impl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2021 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_TOKEN_FETCHER_IMPL_H_ | ||
#define WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_TOKEN_FETCHER_IMPL_H_ | ||
|
||
#include <memory> | ||
|
||
#include "base/containers/flat_map.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "components/safe_browsing/core/browser/safe_browsing_token_fetch_tracker.h" | ||
#include "components/safe_browsing/core/browser/safe_browsing_token_fetcher.h" | ||
|
||
namespace weblayer { | ||
|
||
class GoogleAccountAccessTokenFetchDelegate; | ||
|
||
// This class fetches access tokens for Safe Browsing via a | ||
// GoogleAccountAccessTokenFetcherDelegate. | ||
class SafeBrowsingTokenFetcherImpl | ||
: public safe_browsing::SafeBrowsingTokenFetcher { | ||
public: | ||
using AccessTokenFetchDelegateGetter = | ||
base::RepeatingCallback<GoogleAccountAccessTokenFetchDelegate*()>; | ||
|
||
// Create a SafeBrowsingTokenFetcherImpl that makes access token requests via | ||
// the object returned by |delegate_getter|. |delegate_getter| may return | ||
// null, in which case this object will return the empty string for access | ||
// token requests. This object will not cache the pointer returned by | ||
// |delegate_getter| but will instead invoke it on every access token request, | ||
// as that object might change over time. | ||
// NOTE: In production the getter is | ||
// ProfileImpl::access_token_fetcher_delegate(); this level of indirection is | ||
// present to support unittests. | ||
explicit SafeBrowsingTokenFetcherImpl( | ||
const AccessTokenFetchDelegateGetter& delegate_getter); | ||
~SafeBrowsingTokenFetcherImpl() override; | ||
|
||
// SafeBrowsingTokenFetcher: | ||
void Start(Callback callback) override; | ||
|
||
private: | ||
void OnTokenFetched(int request_id, const std::string& access_token); | ||
void OnTokenTimeout(int request_id); | ||
|
||
AccessTokenFetchDelegateGetter delegate_getter_; | ||
|
||
safe_browsing::SafeBrowsingTokenFetchTracker token_fetch_tracker_; | ||
|
||
// IDs of outstanding client access token requests being tracked by | ||
// |token_fetch_tracker_|. | ||
std::set<int> request_ids_; | ||
|
||
base::WeakPtrFactory<SafeBrowsingTokenFetcherImpl> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace weblayer | ||
|
||
#endif // WEBLAYER_BROWSER_SAFE_BROWSING_SAFE_BROWSING_TOKEN_FETCHER_IMPL_H_ |
Oops, something went wrong.