File: dependency_parser_model_loader.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 (152 lines) | stat: -rw-r--r-- 5,187 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 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/accessibility/phrase_segmentation/dependency_parser_model_loader.h"

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_macros.h"
#include "components/optimization_guide/core/delivery/optimization_guide_model_provider.h"

namespace {

// Load the model file at the provided file path.
base::File LoadModelFile(const base::FilePath& model_file_path) {
  if (!base::PathExists(model_file_path)) {
    return base::File();
  }

  return base::File(model_file_path,
                    base::File::FLAG_OPEN | base::File::FLAG_READ);
}

// Close the provided model file.
void CloseModelFile(base::File model_file) {
  if (!model_file.IsValid()) {
    return;
  }
  model_file.Close();
}

// Util class for recording the result of loading the dependency parser model.
// The result is recorded when it goes out of scope and its destructor is
// called.
class ScopedModelLoadingResultRecorder {
 public:
  ScopedModelLoadingResultRecorder() = default;
  ~ScopedModelLoadingResultRecorder() {
    UMA_HISTOGRAM_BOOLEAN(
        "Accessibility.DependencyParserModelLoader.DependencyParserModel."
        "WasLoaded",
        was_loaded_);
  }

  void SetLoaded() { was_loaded_ = true; }

 private:
  bool was_loaded_ = false;
};

// The maximum number of pending model requests allowed to be kept
// by the DependencyParserModelLoader.
constexpr int kMaxPendingRequestsAllowed = 100;

}  // namespace

DependencyParserModelLoader::DependencyParserModelLoader(
    optimization_guide::OptimizationGuideModelProvider* opt_guide,
    const scoped_refptr<base::SequencedTaskRunner>& background_task_runner)
    : opt_guide_(opt_guide), background_task_runner_(background_task_runner) {
  opt_guide_->AddObserverForOptimizationTargetModel(
      optimization_guide::proto::OPTIMIZATION_TARGET_PHRASE_SEGMENTATION,
      /*model_metadata=*/std::nullopt, this);
}

DependencyParserModelLoader::~DependencyParserModelLoader() {
  opt_guide_->RemoveObserverForOptimizationTargetModel(
      optimization_guide::proto::OPTIMIZATION_TARGET_PHRASE_SEGMENTATION, this);
  // Clear any pending requests, no model file is acceptable as shutdown is
  // happening.
  NotifyModelUpdatesAndClear(false);
}

void DependencyParserModelLoader::Shutdown() {
  // This and the optimization guide are keyed services, currently optimization
  // guide is a BrowserContextKeyedService, it will be cleaned first so removing
  // the observer should not be performed.
  UnloadModelFile();
  // Clear any pending requests, no model file is acceptable as shutdown is
  // happening.
  NotifyModelUpdatesAndClear(false);
}

void DependencyParserModelLoader::UnloadModelFile() {
  if (dependency_parser_model_file_) {
    // If the model file is already loaded, it should be closed on a
    // background thread.
    background_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&CloseModelFile,
                                  std::move(*dependency_parser_model_file_)));
  }
}

void DependencyParserModelLoader::NotifyModelUpdatesAndClear(
    bool is_model_available) {
  for (auto& pending_request : pending_model_requests_) {
    std::move(pending_request).Run(is_model_available);
  }
  pending_model_requests_.clear();
}

void DependencyParserModelLoader::OnModelUpdated(
    optimization_guide::proto::OptimizationTarget optimization_target,
    base::optional_ref<const optimization_guide::ModelInfo> model_info) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (optimization_target !=
      optimization_guide::proto::OPTIMIZATION_TARGET_PHRASE_SEGMENTATION) {
    return;
  }
  if (!model_info.has_value()) {
    UnloadModelFile();
    NotifyModelUpdatesAndClear(false);
    return;
  }
  background_task_runner_->PostTaskAndReplyWithResult(
      FROM_HERE, base::BindOnce(&LoadModelFile, model_info->GetModelFilePath()),
      base::BindOnce(&DependencyParserModelLoader::OnModelFileLoaded,
                     weak_ptr_factory_.GetWeakPtr()));
}

void DependencyParserModelLoader::OnModelFileLoaded(base::File model_file) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  ScopedModelLoadingResultRecorder result_recorder;
  if (!model_file.IsValid()) {
    return;
  }

  UnloadModelFile();
  dependency_parser_model_file_ = std::move(model_file);
  result_recorder.SetLoaded();
  NotifyModelUpdatesAndClear(true);
}

base::File DependencyParserModelLoader::GetDependencyParserModelFile() {
  DCHECK(IsModelAvailable());
  if (!dependency_parser_model_file_) {
    return base::File();
  }
  // The model must be valid at this point.
  DCHECK(dependency_parser_model_file_->IsValid());
  return dependency_parser_model_file_->Duplicate();
}

void DependencyParserModelLoader::NotifyOnModelFileAvailable(
    NotifyModelAvailableCallback callback) {
  DCHECK(!IsModelAvailable());
  if (pending_model_requests_.size() < kMaxPendingRequestsAllowed) {
    pending_model_requests_.emplace_back(std::move(callback));
    return;
  }
  std::move(callback).Run(false);
}