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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// 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.
#ifndef COMPONENTS_WEBAPPS_BROWSER_ANDROID_AMBIENT_BADGE_MANAGER_H_
#define COMPONENTS_WEBAPPS_BROWSER_ANDROID_AMBIENT_BADGE_MANAGER_H_
#include <memory>
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "components/segmentation_platform/public/segmentation_platform_service.h"
#include "components/webapps/browser/android/installable/installable_ambient_badge_client.h"
#include "components/webapps/browser/android/installable/installable_ambient_badge_message_controller.h"
#include "components/webapps/browser/installable/installable_data.h"
#include "components/webapps/browser/installable/installable_params.h"
#include "content/public/browser/web_contents.h"
#include "url/gurl.h"
class PrefService;
namespace webapps {
struct AddToHomescreenParams;
struct InstallableData;
class AppBannerManagerAndroid;
// Coordinates the creation of an install ambient badge, from detecting the
// eligibility to promote the associated web/native app and creating the ambient
// badge. Lifecycle: This class is owned by the AppBannerManagerAndroidclass and
// is instantiated when an ambient badge may be shown.
class AmbientBadgeManager : public InstallableAmbientBadgeClient {
public:
explicit AmbientBadgeManager(
content::WebContents* web_contents,
base::WeakPtr<AppBannerManagerAndroid> app_banner_manager,
segmentation_platform::SegmentationPlatformService*
segmentation_platform_service,
PrefService* prefs);
AmbientBadgeManager(const AmbientBadgeManager&) = delete;
AmbientBadgeManager& operator=(const AmbientBadgeManager&) = delete;
~AmbientBadgeManager() override;
// This enum backs a UMA histogram , so it should be treated as append-only.
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.banners
// GENERATED_JAVA_CLASS_NAME_OVERRIDE: AmbientBadgeState
enum class State {
// The ambient badge pipeline has not yet been triggered for this page load.
kInactive = 0,
// The ambient badge pipeline is running.
kActive = 1,
// Ambient badge blocked because of recently dismissed
kBlocked = 2,
// Waiting for service worker install to trigger the banner.
kPendingWorker = 3,
// Waiting for sufficient engagement to trigger the ambient badge.
kPendingEngagement = 4,
// Showing Ambient Badge.
kShowing = 5,
// Ambient badge dismissed.
kDismissed = 6,
// Ambient badge clicked by the user.
kClicked = 7,
// Ambient badge pipeline completed.
kComplete = 8,
// Getting classification result from the segmentation platform.
kSegmentation = 9,
kMaxValue = kSegmentation,
};
State state() const { return state_; }
void MaybeShow(const GURL& validated_url,
const std::u16string& app_name,
const std::string& app_identifier,
std::unique_ptr<AddToHomescreenParams> a2hs_params,
base::OnceClosure show_banner_callback);
// InstallableAmbientBadgeClient overrides.
void AddToHomescreenFromBadge() override;
void BadgeDismissed() override;
void BadgeIgnored() override;
// Hides the ambient badge if it is showing.
void HideAmbientBadge();
// Callback invoked by the InstallableManager once it has finished checking
// service worker for showing ambient badge.
void OnWorkerCheckResult(const InstallableData& data);
protected:
virtual void UpdateState(State state);
content::WebContents* web_contents() const { return web_contents_.get(); }
// Called to show UI that promotes installation of a PWA. This is normally the
// mini-infobar ("banner") but clients can override it by providing a
// specialization of this class.
void ShowAmbientBadge();
private:
// Perform checks and shows the install ambient badge. Uses legacy conditions
// instead of the segmentation APIs.
void MaybeShowAmbientBadgeLegacy();
// Uses the segmentation APIs to decide showing the install ambient badge
void MaybeShowAmbientBadgeSmart();
void OnGotClassificationResult(
const segmentation_platform::ClassificationResult& result);
// Returns true if the prompt should be block.
bool ShouldMessageBeBlockedByGuardrail();
void PerformWorkerCheckForAmbientBadge(InstallableParams params,
InstallableCallback callback);
// Returns true if it's the first visit and the badge should be suprressed.
bool ShouldSuppressAmbientBadgeOnFirstVisit();
// Message controller for the ambient badge.
InstallableAmbientBadgeMessageController message_controller_{this};
base::WeakPtr<content::WebContents> web_contents_;
base::WeakPtr<AppBannerManagerAndroid> app_banner_manager_;
raw_ptr<segmentation_platform::SegmentationPlatformService>
segmentation_platform_service_ = nullptr;
raw_ptr<PrefService> pref_service_ = nullptr;
GURL validated_url_;
std::u16string app_name_;
std::string app_identifier_;
// Contains app parameters such as its type and the install source used.
std::unique_ptr<AddToHomescreenParams> a2hs_params_;
base::OnceClosure show_banner_callback_;
// The current ambient badge status.
State state_ = State::kInactive;
bool passed_worker_check_ = false;
base::WeakPtrFactory<AmbientBadgeManager> weak_factory_{this};
};
} // namespace webapps
#endif // COMPONENTS_WEBAPPS_BROWSER_ANDROID_AMBIENT_BADGE_MANAGER_H_
|