Skip to content

Commit

Permalink
dpwa: Update WebAppOriginAssociationManager to use Scope Extensions
Browse files Browse the repository at this point in the history
Currently the WebAppOriginAssociationManager is setup to use the url
handlers field in the manifest. Url handlers related code is being
removed and replaced with scope extensions.

This CL uses the scope extensions field and it's format to fetch the
origin association file and return back validated associated kWebAppIdentity.

The new format of the WAOriginAssociation file for site example.com associating with web app app.com is:
[ "web_apps": {"web_app_identity": "https://app.com/start"} ]

I2P: https://groups.google.com/a/chromium.org/g/blink-dev/c/-iySPmw8o34/m/oLgrCUQXAQAJ

Design Doc: https://docs.google.com/document/d/1-idhx8heajbPYl3cdXFVCjpIuf96cRa_DrRk6147ELI/edit#heading=h.ut4udjgbyg8e

Bug: 1250011
Change-Id: I6ff2d435eaf0a55d3a5e726f474a282b8993069e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4324125
Reviewed-by: Christian Dullweber <dullweber@chromium.org>
Reviewed-by: Dominick Ng <dominickn@chromium.org>
Reviewed-by: Alan Cutter <alancutter@chromium.org>
Commit-Queue: Hassan Talat <hatalat@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1118773}
  • Loading branch information
htalat authored and Chromium LUCI CQ committed Mar 17, 2023
1 parent 3f7fab9 commit 87f8891
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 641 deletions.
8 changes: 8 additions & 0 deletions chrome/browser/web_applications/scope_extension_info.cc
Expand Up @@ -8,6 +8,9 @@

namespace web_app {

ScopeExtensionInfo::ScopeExtensionInfo(const url::Origin& origin)
: origin(origin) {}

ScopeExtensionInfo::ScopeExtensionInfo(const url::Origin& origin,
bool has_origin_wildcard)
: origin(origin), has_origin_wildcard(has_origin_wildcard) {}
Expand All @@ -19,6 +22,11 @@ base::Value ScopeExtensionInfo::AsDebugValue() const {
return root;
}

void ScopeExtensionInfo::Reset() {
origin = url::Origin();
has_origin_wildcard = false;
}

bool operator==(const ScopeExtensionInfo& scope_extension1,
const ScopeExtensionInfo& scope_extension2) {
return scope_extension1.origin == scope_extension2.origin &&
Expand Down
11 changes: 11 additions & 0 deletions chrome/browser/web_applications/scope_extension_info.h
Expand Up @@ -5,6 +5,10 @@
#ifndef CHROME_BROWSER_WEB_APPLICATIONS_SCOPE_EXTENSION_INFO_H_
#define CHROME_BROWSER_WEB_APPLICATIONS_SCOPE_EXTENSION_INFO_H_

#include <string>
#include <unordered_map>
#include <vector>

#include "base/values.h"
#include "url/origin.h"

Expand All @@ -14,6 +18,7 @@ namespace web_app {
// from its web app manifest.
struct ScopeExtensionInfo {
ScopeExtensionInfo() = default;
explicit ScopeExtensionInfo(const url::Origin& origin);
ScopeExtensionInfo(const url::Origin& origin, bool has_origin_wildcard);

// Copyable to support web_app::WebApp being copyable as it has a
Expand All @@ -27,6 +32,9 @@ struct ScopeExtensionInfo {

~ScopeExtensionInfo() = default;

// Reset the scope extension to its default state.
REINITIALIZES_AFTER_MOVE void Reset();

base::Value AsDebugValue() const;

url::Origin origin;
Expand All @@ -45,6 +53,9 @@ bool operator!=(const ScopeExtensionInfo& scope_extension1,
bool operator<(const ScopeExtensionInfo& scope_extension1,
const ScopeExtensionInfo& scope_extension2);

using ScopeExtensions = std::vector<ScopeExtensionInfo>;
using ScopeExtensionMap = std::unordered_map<std::string, ScopeExtensionInfo>;

} // namespace web_app

#endif // CHROME_BROWSER_WEB_APPLICATIONS_SCOPE_EXTENSION_INFO_H_
Expand Up @@ -7,7 +7,6 @@
#include <utility>

#include "base/task/sequenced_task_runner.h"
#include "url/gurl.h"

namespace web_app {

Expand All @@ -18,16 +17,16 @@ FakeWebAppOriginAssociationManager::~FakeWebAppOriginAssociationManager() =
default;

void FakeWebAppOriginAssociationManager::GetWebAppOriginAssociations(
const GURL& manifest_url,
apps::UrlHandlers url_handlers,
const GURL& web_app_identity,
ScopeExtensions scope_extensions,
OnDidGetWebAppOriginAssociations callback) {
apps::UrlHandlers result;
ScopeExtensions result;

if (pass_through_) {
result = url_handlers;
result = scope_extensions;
} else {
for (const auto& url_handler : url_handlers) {
auto it = data_.find(url_handler);
for (const auto& scope_extension : scope_extensions) {
auto it = data_.find(scope_extension);
if (it != data_.end())
result.push_back(it->second);
}
Expand All @@ -37,7 +36,7 @@ void FakeWebAppOriginAssociationManager::GetWebAppOriginAssociations(
}

void FakeWebAppOriginAssociationManager::SetData(
std::map<apps::UrlHandlerInfo, apps::UrlHandlerInfo> data) {
std::map<ScopeExtensionInfo, ScopeExtensionInfo> data) {
data_ = std::move(data);
}

Expand Down
Expand Up @@ -19,20 +19,20 @@ class FakeWebAppOriginAssociationManager
~FakeWebAppOriginAssociationManager() override;

// Sends back preset data.
// Sends back |url_handlers| as is if pass_through_ is set.
// Sends back |scope_extensions| as is if pass_through_ is set.
void GetWebAppOriginAssociations(
const GURL& manifest_url,
apps::UrlHandlers url_handlers,
const GURL& web_app_identity,
ScopeExtensions scope_extensions,
OnDidGetWebAppOriginAssociations callback) override;

void SetData(std::map<apps::UrlHandlerInfo, apps::UrlHandlerInfo> data);
void SetData(std::map<ScopeExtensionInfo, ScopeExtensionInfo> data);

void set_pass_through(bool value) { pass_through_ = value; }

private:
// Maps a url handler to the corresponding result to send back in the
// callback.
std::map<apps::UrlHandlerInfo, apps::UrlHandlerInfo> data_;
std::map<ScopeExtensionInfo, ScopeExtensionInfo> data_;
bool pass_through_ = false;
};

Expand Down
Expand Up @@ -10,7 +10,6 @@
#include "chrome/browser/web_applications/web_app_origin_association_task.h"
#include "components/webapps/services/web_app_origin_association/web_app_origin_association_fetcher.h"
#include "components/webapps/services/web_app_origin_association/web_app_origin_association_parser_service.h"
#include "url/gurl.h"

namespace web_app {

Expand All @@ -20,16 +19,17 @@ WebAppOriginAssociationManager::WebAppOriginAssociationManager()
WebAppOriginAssociationManager::~WebAppOriginAssociationManager() = default;

void WebAppOriginAssociationManager::GetWebAppOriginAssociations(
const GURL& manifest_url,
apps::UrlHandlers url_handlers,
const GURL& web_app_identity,
ScopeExtensions scope_extensions,
OnDidGetWebAppOriginAssociations callback) {
if (url_handlers.empty()) {
std::move(callback).Run(apps::UrlHandlers());
if (scope_extensions.empty()) {
std::move(callback).Run(ScopeExtensions());
return;
}

auto task = std::make_unique<Task>(manifest_url, std::move(url_handlers),
*this, std::move(callback));
auto task =
std::make_unique<Task>(web_app_identity, std::move(scope_extensions),
*this, std::move(callback));
pending_tasks_.push_back(std::move(task));
MaybeStartNextTask();
}
Expand Down
Expand Up @@ -10,24 +10,22 @@

#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "components/services/app_service/public/cpp/url_handler_info.h"
#include "chrome/browser/web_applications/scope_extension_info.h"
#include "components/webapps/services/web_app_origin_association/public/mojom/web_app_origin_association_parser.mojom.h"
#include "components/webapps/services/web_app_origin_association/web_app_origin_association_fetcher.h"
#include "mojo/public/cpp/bindings/remote.h"

class GURL;

namespace web_app {

// Callback type that sends back the valid |url_handlers|.
// Callback type that sends back the valid |scope_extensions|.
using OnDidGetWebAppOriginAssociations =
base::OnceCallback<void(apps::UrlHandlers url_handlers)>;
base::OnceCallback<void(ScopeExtensions scope_extensions)>;

// Fetch, parse, and validate web app origin association files.
class WebAppOriginAssociationManager {
public:
// Does the fetching, parsing, and validation work for a batch of url
// handlers.
// Does the fetching, parsing, and validation work for a batch of scope
// extensions.
class Task;

WebAppOriginAssociationManager();
Expand All @@ -38,9 +36,10 @@ class WebAppOriginAssociationManager {
virtual ~WebAppOriginAssociationManager();

virtual void GetWebAppOriginAssociations(
const GURL& manifest_url,
apps::UrlHandlers url_handlers,
const GURL& web_app_identity,
ScopeExtensions scope_extensions,
OnDidGetWebAppOriginAssociations callback);

void SetFetcherForTest(
std::unique_ptr<webapps::WebAppOriginAssociationFetcher> fetcher);

Expand Down

0 comments on commit 87f8891

Please sign in to comment.