blob: bd3a41628d1d2c682225f396a2669c7bcba4c1c0 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2022 The Chromium Authors
Daniel Murphy176258852022-08-12 01:29:442// 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 Murphyee53ac5b2023-04-14 15:16:397#include <memory>
Daniel Murphya06cd78e2023-02-23 18:46:278#include <ostream>
9
Dan Murphy84e40032025-09-03 22:49:0810#include "base/time/clock.h"
Evan Stade25bd39a2024-08-15 01:54:3811#include "chrome/browser/web_applications/locks/partitioned_lock_manager.h"
Daniel Murphy6e438d82023-06-21 00:24:4212#include "chrome/browser/web_applications/locks/web_app_lock_manager.h"
Daniel Murphy2e5932d2024-09-18 16:23:3913#include "chrome/browser/web_applications/visited_manifest_manager.h"
msiem4f109512025-06-12 17:52:2414#include "chrome/browser/web_applications/web_app_origin_association_manager.h"
Daniel Murphy6e438d82023-06-21 00:24:4215#include "chrome/browser/web_applications/web_app_provider.h"
Daniel Murphy77abe9752024-01-11 20:57:0316#include "components/webapps/common/web_app_id.h"
Daniel Murphy176258852022-08-12 01:29:4417
18namespace web_app {
19
Daniel Murphya06cd78e2023-02-23 18:46:2720std::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 Murphy037d94fd2023-04-14 16:17:4428 case web_app::LockDescription::Type::kAllAppsLock:
29 return "AllApps";
Daniel Murphya06cd78e2023-02-23 18:46:2730 case web_app::LockDescription::Type::kNoOp:
31 return "NoOp";
32 }
33}
34
Glenn Hartmann5f992ed2023-09-25 18:05:3635LockDescription::LockDescription(base::flat_set<webapps::AppId> app_ids,
Phillis Tang295a3332022-10-24 22:50:3536 LockDescription::Type type)
Daniel Murphy77abe9752024-01-11 20:57:0337 : 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}
42LockDescription::LockDescription(LockDescription&&) = default;
43
Phillis Tang295a3332022-10-24 22:50:3544LockDescription::~LockDescription() = default;
Daniel Murphy176258852022-08-12 01:29:4445
Phillis Tang295a3332022-10-24 22:50:3546bool LockDescription::IncludesSharedWebContents() const {
Daniel Murphy176258852022-08-12 01:29:4447 switch (type_) {
48 case Type::kNoOp:
Daniel Murphy176258852022-08-12 01:29:4449 case Type::kApp:
Daniel Murphy037d94fd2023-04-14 16:17:4450 case Type::kAllAppsLock:
Daniel Murphy176258852022-08-12 01:29:4451 return false;
52 case Type::kBackgroundWebContents:
53 case Type::kAppAndWebContents:
54 return true;
55 }
56}
Daniel Murphya06cd78e2023-02-23 18:46:2757base::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
69std::ostream& operator<<(std::ostream& out,
70 const LockDescription& lock_description) {
71 return out << lock_description.AsDebugValue();
72}
Daniel Murphy176258852022-08-12 01:29:4473
Daniel Murphy6e438d82023-06-21 00:24:4274WebContentsManager& Lock::web_contents_manager() {
75 CHECK(lock_manager_);
76 return lock_manager_->provider().web_contents_manager();
77}
78
Daniel Murphy2e5932d2024-09-18 16:23:3979VisitedManifestManager& Lock::visited_manifest_manager() {
80 CHECK(lock_manager_);
81 return lock_manager_->provider().visited_manifest_manager();
82}
83
msiem4f109512025-06-12 17:52:2484WebAppOriginAssociationManager& Lock::origin_association_manager() {
85 CHECK(lock_manager_);
86 return lock_manager_->provider().origin_association_manager();
87}
88
Dan Murphy84e40032025-09-03 22:49:0889base::Clock& Lock::clock() {
90 CHECK(lock_manager_);
91 return lock_manager_->provider().clock();
92}
93
Daniel Murphy51461d52024-10-04 19:57:2694Lock::Lock() : holder_(std::make_unique<PartitionedLockHolder>()) {}
Phillis Tang87b2a7712022-11-19 00:27:3195Lock::~Lock() = default;
96
Daniel Murphy51461d52024-10-04 19:57:2697bool Lock::IsGranted() const {
98 return !!lock_manager_;
99}
100
101void Lock::GrantLockResources(WebAppLockManager& lock_manager) {
102 CHECK(!lock_manager_);
103 lock_manager_ = lock_manager.GetWeakPtr();
104}
105
Daniel Murphy176258852022-08-12 01:29:44106} // namespace web_app