1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/webapps/browser/installable/metrics/site_manifest_metrics_task.h"
#include <memory>
#include "base/functional/callback_forward.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "components/webapps/browser/installable/installable_data.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "content/public/browser/web_contents.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
#include "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom-forward.h"
namespace webapps {
SiteManifestMetricsTask::~SiteManifestMetricsTask() = default;
// static
std::unique_ptr<SiteManifestMetricsTask>
SiteManifestMetricsTask::CreateAndStart(
content::WebContents& web_contents,
ResultCallback collect_data_and_self_destruct_callback) {
std::unique_ptr<SiteManifestMetricsTask> result =
base::WrapUnique(new SiteManifestMetricsTask(
web_contents, std::move(collect_data_and_self_destruct_callback)));
result->Start();
return result;
}
SiteManifestMetricsTask::SiteManifestMetricsTask(
content::WebContents& web_contents,
ResultCallback collect_data_and_self_destruct_callback)
: web_contents_(web_contents),
collect_data_and_self_destruct_callback_(
std::move(collect_data_and_self_destruct_callback)),
manager_(InstallableManager::FromWebContents(&web_contents)) {
CHECK(manager_);
}
void SiteManifestMetricsTask::Start() {
InstallableParams params;
params.check_eligibility = true;
manager_->GetData(params,
base::BindOnce(&SiteManifestMetricsTask::OnDidFetchManifest,
weak_factory_.GetWeakPtr()));
}
void SiteManifestMetricsTask::OnDidFetchManifest(const InstallableData& data) {
if (!data.manifest_url->is_empty() &&
!blink::IsEmptyManifest(*data.manifest)) {
manifest_ = data.manifest->Clone();
} else {
// Create an empty manifest and pass it along. Existence of this would mean
// that either the manifest does not exist or it is empty.
manifest_ = blink::mojom::Manifest::New();
}
CHECK(collect_data_and_self_destruct_callback_);
std::move(collect_data_and_self_destruct_callback_).Run(std::move(manifest_));
}
} // namespace webapps
|