Daniel Murphy | 2e5932d | 2024-09-18 16:23:39 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_WEB_APPLICATIONS_VISITED_MANIFEST_MANAGER_H_ |
| 6 | #define CHROME_BROWSER_WEB_APPLICATIONS_VISITED_MANIFEST_MANAGER_H_ |
| 7 | |
| 8 | #include "base/containers/lru_cache.h" |
| 9 | #include "base/feature_list.h" |
| 10 | #include "base/memory/raw_ptr.h" |
| 11 | #include "base/time/clock.h" |
| 12 | #include "base/time/default_clock.h" |
| 13 | #include "base/time/time.h" |
| 14 | #include "third_party/blink/public/mojom/manifest/manifest.mojom-forward.h" |
| 15 | #include "url/gurl.h" |
| 16 | |
| 17 | namespace web_app { |
| 18 | |
| 19 | BASE_DECLARE_FEATURE(kBlockMlPromotionInNestedPagesNoManifest); |
| 20 | |
| 21 | // This class is used to track recently seen manifests. Currently this is |
| 22 | // limited only to manifest scopes, which are used to detect if a given url |
| 23 | // is in-scope a recently seen manifest entity. This class limits its history |
| 24 | // to `kMaxScopesToSave` items, which are evicted via the least-recently-used |
| 25 | // policy. |
| 26 | // Seeing a manifest scope AND detecting if a scope controls a url both 'update' |
| 27 | // that entry to be at the front of the LRU queue. |
| 28 | class VisitedManifestManager { |
| 29 | public: |
| 30 | static constexpr int kMaxScopesToSave = 100; |
| 31 | |
| 32 | explicit VisitedManifestManager( |
| 33 | base::Clock* clock = base::DefaultClock::GetInstance()); |
| 34 | ~VisitedManifestManager(); |
| 35 | |
| 36 | // If true, then this will also update the scope entry to be at the front of |
| 37 | // the FIFO queue, and record a new time for use with `ClearSeenScopes`. |
| 38 | bool IsUrlControlledBySeenManifest(const GURL& url); |
| 39 | |
| 40 | // Records that the given manifest has been seen now. Only scopes of manifests |
| 41 | // that specify a `start_url` will be saved, and the current time will be |
| 42 | // recorded for use with `ClearSeenScopes`. Called when a manifest scope is |
| 43 | // seen by the AppBannerManager. |
| 44 | void OnManifestSeen(const blink::mojom::Manifest& manifest); |
| 45 | |
| 46 | // Clears scopes that have been recorded in the given time, to be called from |
| 47 | // the clear-browsing-data flow. |
| 48 | void ClearSeenScopes(const base::Time& begin_time, |
| 49 | const base::Time& end_time); |
| 50 | |
| 51 | private: |
| 52 | base::LRUCache<GURL /*scope*/, base::Time /*last_seen_or_used*/> |
| 53 | recent_manifest_scopes_{kMaxScopesToSave}; |
| 54 | |
| 55 | raw_ptr<base::Clock> clock_; |
| 56 | }; |
| 57 | |
| 58 | } // namespace web_app |
| 59 | |
| 60 | #endif // CHROME_BROWSER_WEB_APPLICATIONS_VISITED_MANIFEST_MANAGER_H_ |