Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/web_applications/locks/lock.h" |
| 6 | |
Daniel Murphy | ee53ac5b | 2023-04-14 15:16:39 | [diff] [blame] | 7 | #include <memory> |
Daniel Murphy | a06cd78e | 2023-02-23 18:46:27 | [diff] [blame] | 8 | #include <ostream> |
| 9 | |
Dan Murphy | 84e4003 | 2025-09-03 22:49:08 | [diff] [blame] | 10 | #include "base/time/clock.h" |
Evan Stade | 25bd39a | 2024-08-15 01:54:38 | [diff] [blame] | 11 | #include "chrome/browser/web_applications/locks/partitioned_lock_manager.h" |
Daniel Murphy | 6e438d8 | 2023-06-21 00:24:42 | [diff] [blame] | 12 | #include "chrome/browser/web_applications/locks/web_app_lock_manager.h" |
Daniel Murphy | 2e5932d | 2024-09-18 16:23:39 | [diff] [blame] | 13 | #include "chrome/browser/web_applications/visited_manifest_manager.h" |
msiem | 4f10951 | 2025-06-12 17:52:24 | [diff] [blame] | 14 | #include "chrome/browser/web_applications/web_app_origin_association_manager.h" |
Daniel Murphy | 6e438d8 | 2023-06-21 00:24:42 | [diff] [blame] | 15 | #include "chrome/browser/web_applications/web_app_provider.h" |
Daniel Murphy | 77abe975 | 2024-01-11 20:57:03 | [diff] [blame] | 16 | #include "components/webapps/common/web_app_id.h" |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 17 | |
| 18 | namespace web_app { |
| 19 | |
Daniel Murphy | a06cd78e | 2023-02-23 18:46:27 | [diff] [blame] | 20 | std::string LockTypeToString(LockDescription::Type type) { |
| 21 | switch (type) { |
| 22 | case web_app::LockDescription::Type::kApp: |
| 23 | return "App"; |
| 24 | case web_app::LockDescription::Type::kAppAndWebContents: |
| 25 | return "AppAndWebContents"; |
| 26 | case web_app::LockDescription::Type::kBackgroundWebContents: |
| 27 | return "WebContents"; |
Daniel Murphy | 037d94fd | 2023-04-14 16:17:44 | [diff] [blame] | 28 | case web_app::LockDescription::Type::kAllAppsLock: |
| 29 | return "AllApps"; |
Daniel Murphy | a06cd78e | 2023-02-23 18:46:27 | [diff] [blame] | 30 | case web_app::LockDescription::Type::kNoOp: |
| 31 | return "NoOp"; |
| 32 | } |
| 33 | } |
| 34 | |
Glenn Hartmann | 5f992ed | 2023-09-25 18:05:36 | [diff] [blame] | 35 | LockDescription::LockDescription(base::flat_set<webapps::AppId> app_ids, |
Phillis Tang | 295a333 | 2022-10-24 22:50:35 | [diff] [blame] | 36 | LockDescription::Type type) |
Daniel Murphy | 77abe975 | 2024-01-11 20:57:03 | [diff] [blame] | 37 | : app_ids_(std::move(app_ids)), type_(type) { |
| 38 | for (const webapps::AppId& app_id : app_ids_) { |
| 39 | CHECK(!app_id.empty()) << "Cannot have an empty app_id"; |
| 40 | } |
| 41 | } |
| 42 | LockDescription::LockDescription(LockDescription&&) = default; |
| 43 | |
Phillis Tang | 295a333 | 2022-10-24 22:50:35 | [diff] [blame] | 44 | LockDescription::~LockDescription() = default; |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 45 | |
Phillis Tang | 295a333 | 2022-10-24 22:50:35 | [diff] [blame] | 46 | bool LockDescription::IncludesSharedWebContents() const { |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 47 | switch (type_) { |
| 48 | case Type::kNoOp: |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 49 | case Type::kApp: |
Daniel Murphy | 037d94fd | 2023-04-14 16:17:44 | [diff] [blame] | 50 | case Type::kAllAppsLock: |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 51 | return false; |
| 52 | case Type::kBackgroundWebContents: |
| 53 | case Type::kAppAndWebContents: |
| 54 | return true; |
| 55 | } |
| 56 | } |
Daniel Murphy | a06cd78e | 2023-02-23 18:46:27 | [diff] [blame] | 57 | base::Value LockDescription::AsDebugValue() const { |
| 58 | base::Value::Dict result; |
| 59 | base::Value::List ids; |
| 60 | ids.reserve(app_ids_.size()); |
| 61 | for (const auto& id : app_ids_) { |
| 62 | ids.Append(id); |
| 63 | } |
| 64 | result.Set("type", LockTypeToString(type())); |
| 65 | result.Set("app_ids", std::move(ids)); |
| 66 | return base::Value(std::move(result)); |
| 67 | } |
| 68 | |
| 69 | std::ostream& operator<<(std::ostream& out, |
| 70 | const LockDescription& lock_description) { |
| 71 | return out << lock_description.AsDebugValue(); |
| 72 | } |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 73 | |
Daniel Murphy | 6e438d8 | 2023-06-21 00:24:42 | [diff] [blame] | 74 | WebContentsManager& Lock::web_contents_manager() { |
| 75 | CHECK(lock_manager_); |
| 76 | return lock_manager_->provider().web_contents_manager(); |
| 77 | } |
| 78 | |
Daniel Murphy | 2e5932d | 2024-09-18 16:23:39 | [diff] [blame] | 79 | VisitedManifestManager& Lock::visited_manifest_manager() { |
| 80 | CHECK(lock_manager_); |
| 81 | return lock_manager_->provider().visited_manifest_manager(); |
| 82 | } |
| 83 | |
msiem | 4f10951 | 2025-06-12 17:52:24 | [diff] [blame] | 84 | WebAppOriginAssociationManager& Lock::origin_association_manager() { |
| 85 | CHECK(lock_manager_); |
| 86 | return lock_manager_->provider().origin_association_manager(); |
| 87 | } |
| 88 | |
Dan Murphy | 84e4003 | 2025-09-03 22:49:08 | [diff] [blame] | 89 | base::Clock& Lock::clock() { |
| 90 | CHECK(lock_manager_); |
| 91 | return lock_manager_->provider().clock(); |
| 92 | } |
| 93 | |
Daniel Murphy | 51461d5 | 2024-10-04 19:57:26 | [diff] [blame] | 94 | Lock::Lock() : holder_(std::make_unique<PartitionedLockHolder>()) {} |
Phillis Tang | 87b2a771 | 2022-11-19 00:27:31 | [diff] [blame] | 95 | Lock::~Lock() = default; |
| 96 | |
Daniel Murphy | 51461d5 | 2024-10-04 19:57:26 | [diff] [blame] | 97 | bool Lock::IsGranted() const { |
| 98 | return !!lock_manager_; |
| 99 | } |
| 100 | |
| 101 | void Lock::GrantLockResources(WebAppLockManager& lock_manager) { |
| 102 | CHECK(!lock_manager_); |
| 103 | lock_manager_ = lock_manager.GetWeakPtr(); |
| 104 | } |
| 105 | |
Daniel Murphy | 17625885 | 2022-08-12 01:29:44 | [diff] [blame] | 106 | } // namespace web_app |