File: notice_model.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (198 lines) | stat: -rw-r--r-- 5,084 bytes parent folder | download | duplicates (5)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/privacy_sandbox/notice/notice_model.h"

#include "chrome/browser/privacy_sandbox/notice/notice_storage.h"

namespace privacy_sandbox {

using notice::mojom::PrivacySandboxNoticeEvent;

// NoticeApi class definitions.
NoticeApi::NoticeApi() = default;
NoticeApi::~NoticeApi() = default;

void NoticeApi::SetFulfilledBy(Notice* notice) {
  linked_notices_.emplace_back(notice);
}

NoticeApi* NoticeApi::SetEligibilityCallback(
    base::RepeatingCallback<EligibilityLevel()> callback) {
  eligibility_callback_ = std::move(callback);
  return this;
}

NoticeApi* NoticeApi::SetResultCallback(
    base::OnceCallback<void(bool)> callback) {
  result_callback_ = std::move(callback);
  return this;
}

NoticeApi* NoticeApi::SetFeature(const base::Feature* feature) {
  feature_ = feature;
  return this;
}

bool NoticeApi::IsEnabled() {
  return feature_ && base::FeatureList::IsEnabled(*feature_);
}

EligibilityLevel NoticeApi::GetEligibilityLevel() {
  return !eligibility_callback_.is_null() ? eligibility_callback_.Run()
                                          : EligibilityLevel::kNotEligible;
}

void NoticeApi::UpdateResult(bool enabled) {
  if (!result_callback_.is_null()) {
    std::move(result_callback_).Run(enabled);
  }
}

bool NoticeApi::IsFulfilled() {
  EligibilityLevel eligibility = GetEligibilityLevel();

  for (Notice* notice : linked_notices()) {
    if (eligibility == EligibilityLevel::kEligibleConsent &&
        notice->GetNoticeType() == NoticeType::kNotice) {
      continue;
    }
    if (notice->was_fulfilled()) {
      return true;
    };
  }
  return false;
}

// Notice class definitions.
Notice::Notice(NoticeId notice_id) : notice_id_(notice_id) {}
Notice::~Notice() = default;

Notice* Notice::SetFeature(const base::Feature* feature) {
  feature_ = feature;
  return this;
}

Notice* Notice::SetPreReqApis(const std::vector<NoticeApi*>& apis) {
  std::transform(apis.begin(), apis.end(), std::back_inserter(pre_req_apis_),
                 std::identity());
  return this;
}

Notice* Notice::SetTargetApis(const std::vector<NoticeApi*>& apis) {
  std::transform(apis.begin(), apis.end(), std::back_inserter(target_apis_),
                 std::identity());
  for (NoticeApi* api : apis) {
    api->SetFulfilledBy(this);
  }
  return this;
}

Notice* Notice::SetViewGroup(std::pair<NoticeViewGroup, int> view_group) {
  view_group_ = view_group;
  return this;
}

const char* Notice::GetStorageName() const {
  CHECK(feature());
  return feature()->name;
}

void Notice::RefreshFulfillmentStatus(NoticeStorage& storage) {
  auto data = storage.ReadNoticeData(GetStorageName());
  if (!data) {
    was_fulfilled_ = false;
    return;
  }

  for (const auto& event_pair_ptr : data->notice_events) {
    if (!event_pair_ptr) {
      continue;
    }
    if (EvaluateNoticeEvent(event_pair_ptr->event).has_value()) {
      was_fulfilled_ = true;
      return;
    }
  }
  was_fulfilled_ = false;
}

bool Notice::CanFulfillAllTargetApis() {
  // TODO(crbug.com/417727236) Add caching here: We shouldn't recompute this
  // every time.
  for (NoticeApi* api : target_apis()) {
    auto eligibility = api->GetEligibilityLevel();
    if (eligibility == EligibilityLevel::kEligibleConsent &&
        GetNoticeType() == NoticeType::kConsent) {
      continue;
    }
    if (eligibility == EligibilityLevel::kEligibleNotice &&
        GetNoticeType() == NoticeType::kNotice) {
      continue;
    }
    return false;
  }
  return true;
}

std::optional<bool> Notice::EvaluateNoticeEvent(
    PrivacySandboxNoticeEvent event) {
  switch (event) {
    // Fulfillment : Yes
    case PrivacySandboxNoticeEvent::kAck:
    case PrivacySandboxNoticeEvent::kSettings:
      return true;
    // Not Fulfillment
    case PrivacySandboxNoticeEvent::kShown:
      return std::nullopt;
    // Unexpected.
    default:
      NOTREACHED();
  }
}

std::optional<bool> Consent::EvaluateNoticeEvent(
    PrivacySandboxNoticeEvent event) {
  switch (event) {
    // Fulfillment : Yes
    case PrivacySandboxNoticeEvent::kOptIn:
      return true;
    // Fulfillment : No
    case PrivacySandboxNoticeEvent::kOptOut:
      return false;
    // Not Fulfillment
    case PrivacySandboxNoticeEvent::kShown:
      return std::nullopt;
    // Unexpected.
    default:
      NOTREACHED();
  }
}

void Notice::UpdateTargetApiResults(PrivacySandboxNoticeEvent event) {
  std::optional<bool> result = EvaluateNoticeEvent(event);
  if (!result.has_value()) {
    return;
  }
  for (NoticeApi* api : target_apis()) {
    api->UpdateResult(*result);
  }
}

bool Notice::IsEnabled() const {
  return feature() && base::FeatureList::IsEnabled(*feature());
}

NoticeType Notice::GetNoticeType() {
  return NoticeType::kNotice;
}

// Consent class definitions.
Consent::Consent(NoticeId notice_id) : Notice(notice_id) {}

NoticeType Consent::GetNoticeType() {
  return NoticeType::kConsent;
}

}  // namespace privacy_sandbox