File: permissions_aiv1_handler.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 (289 lines) | stat: -rw-r--r-- 10,906 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// 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/permissions/permissions_aiv1_handler.h"

#include "base/check_is_test.h"
#include "base/containers/fixed_flat_set.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/sequence_checker.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/optimization_guide/proto/features/permissions_ai.pb.h"
#include "components/permissions/request_type.h"

namespace permissions {

namespace {
using ::optimization_guide::ModelBasedCapabilityKey;
using ::optimization_guide::SessionConfigParams;
using ::optimization_guide::proto::PermissionsAiRequest;
using ::optimization_guide::proto::PermissionsAiResponse;
using ::optimization_guide::proto::PermissionType;
using EligibilityReason = ::optimization_guide::OnDeviceModelEligibilityReason;

constexpr ModelBasedCapabilityKey kFeatureKey =
    ModelBasedCapabilityKey::kPermissionsAi;

constexpr SessionConfigParams kSessionConfigParams = SessionConfigParams{
    .execution_mode = SessionConfigParams::ExecutionMode::kOnDeviceOnly,
};

// Max delay for permissions AI model execution; inquiries that need more time
// get cancelled.
constexpr base::TimeDelta kMaxModelExecutionDuration = base::Seconds(5);

void LogOnDeviceModelPreviousSessionFinishedInTime(bool success) {
  base::UmaHistogramBoolean("Permissions.AIv1.PreviousSessionFinishedInTime",
                            success);
}

void LogOnDeviceModelSessionCreationSuccess(bool session_created) {
  base::UmaHistogramBoolean("Permissions.AIv1.SessionCreationSuccess",
                            session_created);
}

void LogOnDeviceModelExecutionSuccessAndTime(
    bool success,
    base::TimeTicks session_execution_start_time) {
  base::UmaHistogramBoolean("Permissions.AIv1.ExecutionSuccess", success);
  if (success) {
    base::UmaHistogramMediumTimes(
        "Permissions.AIv1.ExecutionDuration",
        base::TimeTicks::Now() - session_execution_start_time);
  }
}

void LogOnDeviceModelExecutionParse(bool success) {
  base::UmaHistogramBoolean("Permissions.AIv1.ResponseParseSuccess", success);
}

void LogOnDeviceModelAvailabilityAtInquiryTime(bool model_available) {
  base::UmaHistogramBoolean("Permissions.AIv1.AvailableAtInquiryTime",
                            model_available);
}

void LogOnDeviceModelExecutionTimedOut(bool timed_out) {
  base::UmaHistogramBoolean("Permissions.AIv1.ExecutionTimedOut", timed_out);
}

PermissionType GetPermissionType(permissions::RequestType request_type) {
  switch (request_type) {
    case permissions::RequestType::kNotifications:
      return PermissionType::PERMISSION_TYPE_NOTIFICATIONS;
    case permissions::RequestType::kGeolocation:
      return PermissionType::PERMISSION_TYPE_GEOLOCATION;
    default:
      return PermissionType::PERMISSION_TYPE_NOT_SPECIFIED;
  }
}

bool IsOnDeviceModelAvailable(EligibilityReason reason) {
  return reason == EligibilityReason::kSuccess;
}
}  // namespace

// Manages sessions and related data. This will gift us more flexibility, i.e.
// it can allow us to have multiple sessions at some point in the future at the
// same time and also to easily cancel session without fearing asynchronous
// calls to OnModelExecutionComplete meddling with a potentially new permission
// request.
class PermissionsAiv1Handler::EvaluationTask {
 public:
  explicit EvaluationTask(OptimizationGuideKeyedService* optimization_guide) {
    DETACH_FROM_SEQUENCE(sequence_checker_);
    session_ =
        optimization_guide->StartSession(kFeatureKey, kSessionConfigParams);
  }

  ~EvaluationTask() {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    // Explicitly invalidate weak pointers to prevent callbacks that may be
    // triggered by the destructor logic.
    weak_ptr_factory_.InvalidateWeakPtrs();
    if (inquire_on_device_model_callback_) {
      std::move(inquire_on_device_model_callback_).Run(std::nullopt);
    }
  }

  bool IsActive() { return session_ != nullptr; }

  void ExecuteModel(
      std::string rendered_text,
      permissions::RequestType request_type,
      base::OnceCallback<void(std::optional<PermissionsAiResponse>)> callback,
      base::OneShotTimer* execution_timer) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    if (!session_) {
      std::move(callback).Run(std::nullopt);
      return;
    }

    inquire_on_device_model_callback_ = std::move(callback);

    PermissionsAiRequest request;
    request.set_rendered_text(std::move(rendered_text));
    request.set_permission_type(GetPermissionType(request_type));

    session_execution_start_time_ = base::TimeTicks::Now();
    session_->ExecuteModel(
        request,
        base::BindRepeating(
            &PermissionsAiv1Handler::EvaluationTask::OnModelExecutionComplete,
            weak_ptr_factory_.GetWeakPtr(), execution_timer));
  }

 private:
  void OnModelExecutionComplete(
      base::OneShotTimer* execution_timer,
      optimization_guide::OptimizationGuideModelStreamingExecutionResult
          result) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    // This is a non-error response, but since the result is not fully available
    // yet, we defer callback execution until we get a complete response.
    if (result.response.has_value() && !result.response->is_complete) {
      return;
    }

    if (execution_timer) {
      execution_timer->Stop();
      LogOnDeviceModelExecutionTimedOut(/*timed_out=*/false);
    }
    // Since we do not want to reuse a once used session, lets make sure that we
    // destroy it when the model execution has finished.
    session_.reset();

    // If there is no callback available anymore, we do not need to do any work
    // here.
    if (!inquire_on_device_model_callback_) {
      return;
    }

    LogOnDeviceModelExecutionSuccessAndTime(/*success=*/
                                            result.response.has_value(),
                                            session_execution_start_time_);
    if (!result.response.has_value()) {
      VLOG(1)
          << "[PermissionsAIv1] OnModelExecutionComplete failed with error: "
          << static_cast<int>(result.response.error().error());
      std::move(inquire_on_device_model_callback_).Run(std::nullopt);
      return;
    }

    std::optional<PermissionsAiResponse> permissions_ai_response =
        optimization_guide::ParsedAnyMetadata<PermissionsAiResponse>(
            result.response->response);
    LogOnDeviceModelExecutionParse(
        /*success=*/permissions_ai_response.has_value());

    if (!permissions_ai_response.has_value()) {
      VLOG(1) << "[PermissionsAIv1] OnModelExecutionComplete failed while "
                 "parsing the response proto.";
      std::move(inquire_on_device_model_callback_).Run(std::nullopt);
      return;
    }

    std::move(inquire_on_device_model_callback_).Run(permissions_ai_response);
  }

  SEQUENCE_CHECKER(sequence_checker_);

  std::unique_ptr<optimization_guide::OptimizationGuideModelExecutor::Session>
      session_;
  base::TimeTicks session_execution_start_time_;
  base::OnceCallback<void(
      std::optional<optimization_guide::proto::PermissionsAiResponse>)>
      inquire_on_device_model_callback_;
  base::WeakPtrFactory<PermissionsAiv1Handler::EvaluationTask>
      weak_ptr_factory_{this};
};

PermissionsAiv1Handler::PermissionsAiv1Handler(
    OptimizationGuideKeyedService* optimization_guide)
    : optimization_guide_(optimization_guide),
      execution_timer_(std::make_unique<base::OneShotTimer>()) {}

PermissionsAiv1Handler::~PermissionsAiv1Handler() {
  execution_timer_->Stop();
}

bool PermissionsAiv1Handler::IsModelExecutionInProgress() {
  return evaluation_task_ && evaluation_task_->IsActive();
}

void PermissionsAiv1Handler::InquireAiOnDeviceModel(
    std::string rendered_text,
    permissions::RequestType request_type,
    base::OnceCallback<void(std::optional<PermissionsAiResponse>)> callback) {
  if (!optimization_guide_) {
    // If optimization_guide_ is a nullptr then we cannot do anything here at
    // all.
    std::move(callback).Run(std::nullopt);
    return;
  }

  EligibilityReason model_state =
      optimization_guide_->GetOnDeviceModelEligibility(kFeatureKey);
  bool model_available_at_inquiry_time = IsOnDeviceModelAvailable(model_state);
  LogOnDeviceModelAvailabilityAtInquiryTime(model_available_at_inquiry_time);

  if (model_available_at_inquiry_time) {
    if (IsModelExecutionInProgress()) {
      LogOnDeviceModelPreviousSessionFinishedInTime(/*success=*/false);
      // TODO(crbug.com/382447738): It can happen that a new inquiry comes
      // before the previous finishes its execution. To avoid unexpected
      // behavior return `std::nullopt` which means another type of CPSS logic
      // will be executed.
      std::move(callback).Run(std::nullopt);
      return;
    }
    LogOnDeviceModelPreviousSessionFinishedInTime(/*success=*/true);
  }

  // We make sure by recreating the session on every inquiry that no
  // callback executions or timing data linked to the previous request gets
  // accidentally mixed up with the new request when we cancel lengthy model
  // executions.
  evaluation_task_ = std::make_unique<EvaluationTask>(optimization_guide_);

  LogOnDeviceModelSessionCreationSuccess(
      /*session_created=*/evaluation_task_->IsActive());

  if (evaluation_task_->IsActive()) {
    evaluation_task_->ExecuteModel(std::move(rendered_text), request_type,
                                   std::move(callback), execution_timer_.get());

    // We check again to make sure the callback did not immediately return.
    if (evaluation_task_->IsActive()) {
      execution_timer_->Start(
          FROM_HERE, kMaxModelExecutionDuration,
          base::BindOnce(&PermissionsAiv1Handler::CancelModelExecution,
                         weak_ptr_factory_.GetWeakPtr()));
    }
    return;
  }

  // We end up here if the model download has not started/ended yet or
  // there is a transient error. If we did not download the model yet, trying
  // to create the session will have started the download already so we do
  // nothing here and just return. We also return for the other two cases,
  // hoping the next inquiry will have better luck.
  std::move(callback).Run(std::nullopt);
}

void PermissionsAiv1Handler::CancelModelExecution() {
  evaluation_task_.reset();
  execution_timer_->Stop();
  LogOnDeviceModelExecutionTimedOut(/*timed_out=*/true);
}

void PermissionsAiv1Handler::set_execution_timer_for_testing(
    std::unique_ptr<base::OneShotTimer> execution_timer) {
  CHECK_IS_TEST();
  execution_timer_ = std::move(execution_timer);
}

}  // namespace permissions